Diff
Not logged in

Differences From Artifact [f2bd29fa05629861]:

To Artifact [70846dfa5638c8e7]:


50 50 })); 51 51 ctx.add("print", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 52 52 foreach(a; args) 53 53 write(a); 54 54 writeln(""); 55 55 return new UndefinedValue; 56 56 })); 57 + ctx.add("if", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){ 58 + if( args.length != 3 ) 59 + throw new PolemyRuntimeException("if takes three arguments!! ["~to!string(pos)~"]"); 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!string(pos)~"]"); 65 + })); 57 66 return ctx; 58 67 } 59 68 60 69 Tuple!(Value,"val",Context,"ctx") evalString(T...)(T params) 61 70 { 62 71 return eval( parserFromString(params).parseProgram() ); 63 72 } ................................................................................ 179 188 assert( r.val == new IntValue(BigInt(4)) ); 180 189 } 181 190 unittest 182 191 { 183 192 evalString(`print("Hello, world!");`); 184 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 +}