Differences From Artifact [f2bd29fa05629861]:
- File
polemy/eval.d
- 2010-11-07 15:03:38 - part of checkin [820e7198cc] on branch trunk - Made helloworld work. (user: kinaba) [annotate]
To Artifact [70846dfa5638c8e7]:
- File
polemy/eval.d
- 2010-11-07 16:31:52 - part of checkin [633e700889] on branch trunk - If-expression implemented. Factorial now works. (user: kinaba) [annotate]
50 })); 50 }));
51 ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos, 51 ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos,
52 foreach(a; args) 52 foreach(a; args)
53 write(a); 53 write(a);
54 writeln(""); 54 writeln("");
55 return new UndefinedValue; 55 return new UndefinedValue;
56 })); 56 }));
> 57 ctx.add("if", new FunValue(delegate Value(immutable LexPosition pos, Val
> 58 if( args.length != 3 )
> 59 throw new PolemyRuntimeException("if takes three argumen
> 60 if( auto x = cast(IntValue)args[0] )
> 61 if( auto ft = cast(FunValue)args[1] )
> 62 if( auto fe = cast(FunValue)args[2] )
> 63 return (x.data == 0 ? fe : ft).call(pos,[]);
> 64 throw new PolemyRuntimeException("type mismatch in if ["~to!stri
> 65 }));
57 return ctx; 66 return ctx;
58 } 67 }
59 68
60 Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params) 69 Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params)
61 { 70 {
62 return eval( parserFromString(params).parseProgram() ); 71 return eval( parserFromString(params).parseProgram() );
63 } 72 }
................................................................................................................................................................................
179 assert( r.val == new IntValue(BigInt(4)) ); 188 assert( r.val == new IntValue(BigInt(4)) );
180 } 189 }
181 unittest 190 unittest
182 { 191 {
183 evalString(`print("Hello, world!");`); 192 evalString(`print("Hello, world!");`);
184 evalString(`print(fun(){});`); 193 evalString(`print(fun(){});`);
185 } 194 }
> 195 unittest
> 196 {
> 197 evalString(`var fac = fun(x){
> 198 1;
> 199 };
> 200 print(fac(3));`);
> 201 evalString(`var fac = fun(x){
> 202 if(x)
> 203 { x*fac(x-1); }
> 204 else
> 205 { 1; };
> 206 };
> 207 print(fac(10));`);
> 208 }