Diff
Not logged in

Differences From Artifact [0e6e0e9d77fcbedc]:

To Artifact [c85d31e67d8ceb4b]:


29 ctx.set(">", ValueLayer, native( (Value lhs, Value rhs){return new IntVa 29 ctx.set(">", ValueLayer, native( (Value lhs, Value rhs){return new IntVa 30 ctx.set("<=", ValueLayer, native( (Value lhs, Value rhs){return new IntV 30 ctx.set("<=", ValueLayer, native( (Value lhs, Value rhs){return new IntV 31 ctx.set(">=", ValueLayer, native( (Value lhs, Value rhs){return new IntV 31 ctx.set(">=", ValueLayer, native( (Value lhs, Value rhs){return new IntV 32 ctx.set("==", ValueLayer, native( (Value lhs, Value rhs){return new IntV 32 ctx.set("==", ValueLayer, native( (Value lhs, Value rhs){return new IntV 33 ctx.set("!=", ValueLayer, native( (Value lhs, Value rhs){return new IntV 33 ctx.set("!=", ValueLayer, native( (Value lhs, Value rhs){return new IntV 34 ctx.set("print", ValueLayer, native( (Value a){ 34 ctx.set("print", ValueLayer, native( (Value a){ 35 writeln(a); 35 writeln(a); 36 return new IntValue(BigInt(178)); | 36 return new IntValue(BigInt(0)); 37 })); 37 })); 38 ctx.set("if", ValueLayer, native( (IntValue x, FunValue ft, FunValue fe) 38 ctx.set("if", ValueLayer, native( (IntValue x, FunValue ft, FunValue fe) 39 auto toRun = (x.data==0 ? fe : ft); 39 auto toRun = (x.data==0 ? fe : ft); > 40 // [TODO] fill positional information 40 return toRun.invoke(null, ValueLayer, toRun.definitionContext()) 41 return toRun.invoke(null, ValueLayer, toRun.definitionContext()) 41 // return toRun.invoke(pos, lay, toRun.definitionContext()); 42 // return toRun.invoke(pos, lay, toRun.definitionContext()); 42 })); 43 })); 43 ctx.set("_isint", ValueLayer, native( (Value v){return new IntValue(BigI 44 ctx.set("_isint", ValueLayer, native( (Value v){return new IntValue(BigI 44 ctx.set("_isstr", ValueLayer, native( (Value v){return new IntValue(BigI 45 ctx.set("_isstr", ValueLayer, native( (Value v){return new IntValue(BigI 45 ctx.set("_isfun", ValueLayer, native( (Value v){return new IntValue(BigI 46 ctx.set("_isfun", ValueLayer, native( (Value v){return new IntValue(BigI 46 ctx.set("_isundefined", ValueLayer, native( (Value v){return new IntValu 47 ctx.set("_isundefined", ValueLayer, native( (Value v){return new IntValu ................................................................................................................................................................................ 149 return v; 150 return v; 150 else // rise 151 else // rise 151 return lift(e.pos,v,lay,ctx); 152 return lift(e.pos,v,lay,ctx); 152 }, 153 }, 153 (VarExpression e) 154 (VarExpression e) 154 { 155 { 155 if( lay == ValueLayer ) 156 if( lay == ValueLayer ) 156 return ctx.get(e.var, lay, e.pos); | 157 return ctx.get(e.name, lay, e.pos); 157 try { 158 try { 158 return ctx.get(e.var, lay, e.pos); | 159 return ctx.get(e.name, lay, e.pos); 159 } catch( Throwable ) { // [TODO] more precise... 160 } catch( Throwable ) { // [TODO] more precise... 160 return lift(e.pos, ctx.get(e.var, ValueLayer, e. | 161 return lift(e.pos, ctx.get(e.name, ValueLayer, e 161 } 162 } 162 }, 163 }, 163 (LayeredExpression e) | 164 (LayExpression e) 164 { 165 { 165 if( e.lay == MacroLayer ) | 166 if( e.layer == MacroLayer ) 166 return macroEval(e.expr, ctx, false); 167 return macroEval(e.expr, ctx, false); 167 else 168 else 168 return eval(e.expr, ctx, true, e.lay); | 169 return eval(e.expr, ctx, true, e.layer); 169 }, 170 }, 170 (LetExpression e) 171 (LetExpression e) 171 { 172 { 172 // for letrec, we need this, but should avoid overwritin 173 // for letrec, we need this, but should avoid overwritin 173 // ctx.set(e.var, ValueLayer, new UndefinedValue, e.pos) 174 // ctx.set(e.var, ValueLayer, new UndefinedValue, e.pos) 174 if(splitCtx) 175 if(splitCtx) 175 ctx = new Table(ctx, Table.Kind.NotPropagateSet) 176 ctx = new Table(ctx, Table.Kind.NotPropagateSet) 176 Value v = eval(e.init, ctx, true, lay); 177 Value v = eval(e.init, ctx, true, lay); 177 ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.po | 178 ctx.set(e.name, (e.layer.length ? e.layer : lay), v, e.p 178 return eval(e.expr, ctx, false, lay); 179 return eval(e.expr, ctx, false, lay); 179 }, 180 }, 180 (FuncallExpression e) 181 (FuncallExpression e) 181 { 182 { 182 return invokeFunction(e.pos, eval(e.fun, ctx, true, lay) 183 return invokeFunction(e.pos, eval(e.fun, ctx, true, lay) 183 }, 184 }, 184 (FunLiteral e) 185 (FunLiteral e) ................................................................................................................................................................................ 197 ); 198 ); 198 } 199 } 199 200 200 // [TODO] Optimization 201 // [TODO] Optimization 201 Value macroEval(AST e, Table ctx, bool AlwaysMacro) 202 Value macroEval(AST e, Table ctx, bool AlwaysMacro) 202 { 203 { 203 Layer theLayer = ValueLayer; 204 Layer theLayer = ValueLayer; > 205 > 206 Table makeCons(Value a, Value d) > 207 { > 208 Table t = new Table; > 209 t.set("car", theLayer, a); > 210 t.set("cdr", theLayer, d); > 211 return t; > 212 } 204 213 205 Table pos = new Table; 214 Table pos = new Table; > 215 if( e.pos !is null ) { 206 pos.set("filename", theLayer, new StrValue(e.pos.filename)); | 216 pos.set("filename", theLayer, new StrValue(e.pos.filename)); 207 pos.set("lineno", theLayer, new IntValue(BigInt(e.pos.lineno))); | 217 pos.set("lineno", theLayer, new IntValue(BigInt(e.pos.lineno)) 208 pos.set("column", theLayer, new IntValue(BigInt(e.pos.column))); | 218 pos.set("column", theLayer, new IntValue(BigInt(e.pos.column)) > 219 } else { > 220 pos.set("filename", theLayer, new StrValue("nullpos")); > 221 pos.set("lineno", theLayer, new IntValue(BigInt(0))); > 222 pos.set("column", theLayer, new IntValue(BigInt(0))); > 223 } > 224 209 return e.match( 225 return e.match( 210 (StrLiteral e) 226 (StrLiteral e) 211 { 227 { 212 Table t = new Table; 228 Table t = new Table; 213 t.set("pos", theLayer, pos); 229 t.set("pos", theLayer, pos); 214 t.set("is", theLayer, new StrValue("str")); 230 t.set("is", theLayer, new StrValue("str")); 215 t.set("data", theLayer, new StrValue(e.data)); 231 t.set("data", theLayer, new StrValue(e.data)); ................................................................................................................................................................................ 222 t.set("is", theLayer, new StrValue("int")); 238 t.set("is", theLayer, new StrValue("int")); 223 t.set("data", theLayer, new IntValue(e.data)); 239 t.set("data", theLayer, new IntValue(e.data)); 224 return t; 240 return t; 225 }, 241 }, 226 (VarExpression e) 242 (VarExpression e) 227 { 243 { 228 try { 244 try { 229 return ctx.get(e.var, MacroLayer, e.pos); | 245 return ctx.get(e.name, MacroLayer, e.pos); 230 } catch( Throwable ) {// [TODO] more precies... 246 } catch( Throwable ) {// [TODO] more precies... 231 Table t = new Table; 247 Table t = new Table; 232 t.set("pos", theLayer, pos); 248 t.set("pos", theLayer, pos); 233 t.set("is", theLayer, new StrValue("var")); 249 t.set("is", theLayer, new StrValue("var")); 234 t.set("name", theLayer, new StrValue(e.var)); | 250 t.set("name", theLayer, new StrValue(e.name)); 235 return cast(Value)t; 251 return cast(Value)t; 236 } 252 } 237 }, 253 }, 238 (LayeredExpression e) | 254 (LayExpression e) 239 { 255 { 240 if( AlwaysMacro ) 256 if( AlwaysMacro ) 241 { 257 { 242 Table t = new Table; 258 Table t = new Table; 243 t.set("pos", theLayer, pos); | 259 t.set("pos", theLayer, pos); 244 t.set("is", theLayer, new StrValue("lay")); | 260 t.set("is", theLayer, new StrValue("lay")); 245 t.set("layer", theLayer, new StrValue(e.lay)); | 261 t.set("layer", theLayer, new StrValue(e.layer)); 246 t.set("expr", theLayer, macroEval(e.expr,ctx,Alw | 262 t.set("expr", theLayer, macroEval(e.expr,ctx,Al 247 return cast(Value)t; 263 return cast(Value)t; 248 } 264 } 249 else 265 else 250 { 266 { 251 if( e.lay == MacroLayer ) | 267 if( e.layer == MacroLayer ) 252 return macroEval(e.expr, ctx, false); 268 return macroEval(e.expr, ctx, false); 253 else 269 else 254 return eval(e.expr, ctx, true, e.lay); | 270 return eval(e.expr, ctx, true, e.layer); 255 } 271 } 256 }, 272 }, 257 (LetExpression e) 273 (LetExpression e) 258 { 274 { 259 Table t = new Table; 275 Table t = new Table; 260 t.set("pos", theLayer, pos); 276 t.set("pos", theLayer, pos); 261 t.set("is", theLayer, new StrValue("let")); 277 t.set("is", theLayer, new StrValue("let")); 262 t.set("name", theLayer, new StrValue(e.var)); | 278 t.set("name", theLayer, new StrValue(e.name)); 263 t.set("init", theLayer, macroEval(e.init,ctx,AlwaysMacro 279 t.set("init", theLayer, macroEval(e.init,ctx,AlwaysMacro 264 t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro 280 t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro 265 return t; 281 return t; 266 }, 282 }, 267 (FuncallExpression e) 283 (FuncallExpression e) 268 { 284 { 269 Value _f = macroEval(e.fun,ctx,AlwaysMacro); 285 Value _f = macroEval(e.fun,ctx,AlwaysMacro); ................................................................................................................................................................................ 278 Table args = new Table; 294 Table args = new Table; 279 foreach_reverse(a; e.args) { 295 foreach_reverse(a; e.args) { 280 Table cons = new Table; 296 Table cons = new Table; 281 cons.set("car",theLayer,macroEval(a,ctx,AlwaysMa 297 cons.set("car",theLayer,macroEval(a,ctx,AlwaysMa 282 cons.set("cdr",theLayer,args); 298 cons.set("cdr",theLayer,args); 283 args = cons; 299 args = cons; 284 } 300 } 285 t.set("arg", theLayer, args); | 301 t.set("args", theLayer, args); 286 return cast(Value)t; 302 return cast(Value)t; 287 }, 303 }, 288 (FunLiteral e) 304 (FunLiteral e) 289 { 305 { 290 Table t = new Table; 306 Table t = new Table; 291 t.set("pos", theLayer, pos); 307 t.set("pos", theLayer, pos); 292 t.set("is", theLayer, new StrValue("fun")); 308 t.set("is", theLayer, new StrValue("fun")); 293 t.set("body", theLayer, macroEval(e.funbody,ctx,AlwaysM | 309 t.set("funbody", theLayer, macroEval(e.funbody,ctx,Alwa 294 Table param = new Table; | 310 Table params = new Table; 295 foreach_reverse(p; e.params) 311 foreach_reverse(p; e.params) 296 { 312 { 297 Table cons = new Table; | 313 Table lays = new Table; > 314 foreach_reverse(lay; p.layers) > 315 lays = makeCons(new StrValue(lay), lays) 298 Table kv = new Table; 316 Table kv = new Table; 299 kv.set("name", theLayer, new StrValue(p.name)); 317 kv.set("name", theLayer, new StrValue(p.name)); 300 foreach_reverse(lay; p.layers) | 318 kv.set("layers", theLayer, lays); 301 { < 302 Table cons2 = new Table; | 319 Table cons = new Table; 303 cons2.set("car", theLayer, new StrValue( < 304 cons2.set("cdr", theLayer, kv); < 305 kv = cons2; < 306 } < 307 cons.set("car", theLayer, kv); < 308 cons.set("cdr", theLayer, param); < 309 param = cons; < > 320 params = makeCons(kv, params); 310 } 321 } 311 t.set("param", theLayer, param); | 322 t.set("params", theLayer, params); 312 return t; 323 return t; 313 }, 324 }, 314 delegate Value (AST e) 325 delegate Value (AST e) 315 { 326 { 316 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kin 327 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kin 317 } 328 } 318 ); 329 ); ................................................................................................................................................................................ 342 unittest 353 unittest 343 { 354 { 344 assert_eq( evalString(`@a x=1; @b x=2; @a(x)`).val, new IntValue(BigInt( 355 assert_eq( evalString(`@a x=1; @b x=2; @a(x)`).val, new IntValue(BigInt( 345 assert_eq( evalString(`@a x=1; @b x=2; @b(x)`).val, new IntValue(BigInt( 356 assert_eq( evalString(`@a x=1; @b x=2; @b(x)`).val, new IntValue(BigInt( 346 assert_eq( evalString(`let x=1; let _ = (@a x=2;2); x`).val, new IntValu 357 assert_eq( evalString(`let x=1; let _ = (@a x=2;2); x`).val, new IntValu 347 assert_throw!Throwable( evalString(`let x=1; let _ = (@a x=2;2); @a(x)`) 358 assert_throw!Throwable( evalString(`let x=1; let _ = (@a x=2;2); @a(x)`) 348 } 359 } 349 < > 360 /* 350 unittest 361 unittest 351 { 362 { 352 assert_eq( evalString(`var fac = fun(x){ 363 assert_eq( evalString(`var fac = fun(x){ 353 if(x) 364 if(x) 354 { x*fac(x-1); } 365 { x*fac(x-1); } 355 else 366 else 356 { 1; }; 367 { 1; }; ................................................................................................................................................................................ 379 // there was a bug that declaration in the first line of function defini 390 // there was a bug that declaration in the first line of function defini 380 // cannot be recursive 391 // cannot be recursive 381 assert_nothrow( evalString(`def foo() { 392 assert_nothrow( evalString(`def foo() { 382 def bar(y) { if(y<1) {0} else {bar(0)} }; 393 def bar(y) { if(y<1) {0} else {bar(0)} }; 383 bar(1) 394 bar(1) 384 }; foo()`) ); 395 }; foo()`) ); 385 } 396 } > 397 */