Diff
Not logged in

Differences From Artifact [4f24d3bd11889ec1]:

To Artifact [84ee94a6f293fac0]:


123 return (cast(FunValue)ctx.get(lay, "(system)", e 123 return (cast(FunValue)ctx.get(lay, "(system)", e 124 [ctx.get(e.var, "@v", e.pos)] 124 [ctx.get(e.var, "@v", e.pos)] 125 ); 125 ); 126 } 126 } 127 }, 127 }, 128 (LayeredExpression e) 128 (LayeredExpression e) 129 { 129 { > 130 if( e.lay == "@macro" ) > 131 return macroEval(e.expr, ctx, false); > 132 else 130 return eval(e.expr, ctx, false, e.lay); | 133 return eval(e.expr, ctx, false, e.lay); 131 }, 134 }, 132 (LetExpression e) 135 (LetExpression e) 133 { 136 { 134 // for letrec, we need this, but should avoid overwritin 137 // for letrec, we need this, but should avoid overwritin 135 // ctx.set(e.var, "@v", new UndefinedValue, e.pos); 138 // ctx.set(e.var, "@v", new UndefinedValue, e.pos); 136 Value v = eval(e.init, ctx, true, lay); 139 Value v = eval(e.init, ctx, true, lay); 137 if(splitCtx) 140 if(splitCtx) ................................................................................................................................................................................ 149 return f.call(e.pos, lay, args); 152 return f.call(e.pos, lay, args); 150 } 153 } 151 throw genex!RuntimeException(e.pos, "Non-funcion is appl 154 throw genex!RuntimeException(e.pos, "Non-funcion is appl 152 }, 155 }, 153 (FunLiteral e) 156 (FunLiteral e) 154 { 157 { 155 Value[Value[]][Layer] memo; 158 Value[Value[]][Layer] memo; > 159 AST macroMemo = null; // cache 156 160 157 // funvalue need not be rised 161 // funvalue need not be rised 158 // no, need to be rised !! suppose @t(fib)("int") 162 // no, need to be rised !! suppose @t(fib)("int") 159 return new FunValue(delegate Value(immutable LexPosition 163 return new FunValue(delegate Value(immutable LexPosition 160 // TODO: only auto raised ones need memo? no? 164 // TODO: only auto raised ones need memo? no? 161 // auto memoization 165 // auto memoization 162 if( lay != "@v" ) | 166 if( lay != "@v" && lay != "@macro" ) 163 { 167 { 164 if( auto memolay = lay in memo ) 168 if( auto memolay = lay in memo ) 165 if( auto pv = args in *memolay ) 169 if( auto pv = args in *memolay ) 166 return *pv; 170 return *pv; 167 memo[lay][args] = (cast(FunValue)ctx.get 171 memo[lay][args] = (cast(FunValue)ctx.get 168 [new UndValue] 172 [new UndValue] 169 ); 173 ); ................................................................................................................................................................................ 171 175 172 if( e.params.length != args.length ) 176 if( e.params.length != args.length ) 173 throw genex!RuntimeException(e.pos, spri 177 throw genex!RuntimeException(e.pos, spri 174 (e.params.length, args.length)); 178 (e.params.length, args.length)); 175 Table ctxNeo = new Table(ctx, Table.Kind.NotProp 179 Table ctxNeo = new Table(ctx, Table.Kind.NotProp 176 foreach(i,p; e.params) 180 foreach(i,p; e.params) 177 ctxNeo.set(p.name, lay, args[i]); 181 ctxNeo.set(p.name, lay, args[i]); > 182 > 183 // @macro run!!! > 184 if( lay == "@macro" ) > 185 return macroEval(e.funbody, ctxNeo, fals > 186 if( macroMemo is null ) > 187 macroMemo = tableToAST("@v",macroEval(e. > 188 auto v = eval(macroMemo, ctxNeo, true, lay); > 189 178 auto v = eval(e.funbody, ctxNeo, true, lay); | 190 //auto v = eval(e.funbody, ctxNeo, true, lay); 179 // auto memoization 191 // auto memoization 180 if( lay != "@v" ) | 192 if( lay != "@v" && lay != "@macro" ) 181 memo[lay][args] = v; 193 memo[lay][args] = v; 182 return v; 194 return v; 183 }); 195 }); 184 }, 196 }, 185 delegate Value (AST e) 197 delegate Value (AST e) 186 { 198 { 187 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kin 199 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kin ................................................................................................................................................................................ 213 t.set("pos", theLayer, pos); 225 t.set("pos", theLayer, pos); 214 t.set("is", theLayer, new StrValue("int")); 226 t.set("is", theLayer, new StrValue("int")); 215 t.set("data", theLayer, new IntValue(e.data)); 227 t.set("data", theLayer, new IntValue(e.data)); 216 return t; 228 return t; 217 }, 229 }, 218 (VarExpression e) 230 (VarExpression e) 219 { 231 { > 232 try { > 233 return ctx.get(e.var, "@macro", e.pos); > 234 } catch( Throwable ) {// [TODO] more precies... 220 Table t = new Table; | 235 Table t = new Table; 221 t.set("pos", theLayer, pos); | 236 t.set("pos", theLayer, pos); 222 t.set("is", theLayer, new StrValue("var")); | 237 t.set("is", theLayer, new StrValue("var")); 223 t.set("name", theLayer, new StrValue(e.var)); | 238 t.set("name", theLayer, new StrValue(e.var)); 224 return t; | 239 return cast(Value)t; > 240 } 225 }, 241 }, 226 (LayeredExpression e) 242 (LayeredExpression e) 227 { 243 { 228 if( AlwaysMacro ) 244 if( AlwaysMacro ) 229 { 245 { 230 Table t = new Table; 246 Table t = new Table; 231 t.set("pos", theLayer, pos); 247 t.set("pos", theLayer, pos); 232 t.set("is", theLayer, new StrValue("lay")); 248 t.set("is", theLayer, new StrValue("lay")); > 249 t.set("layer", theLayer, new StrValue(e.lay)); 233 t.set("expr", theLayer, macroEval(e.expr,ctx,Alw 250 t.set("expr", theLayer, macroEval(e.expr,ctx,Alw 234 return cast(Value)t; 251 return cast(Value)t; 235 } 252 } 236 else 253 else 237 { 254 { 238 return eval(e.expr, ctx, true, e.lay); 255 return eval(e.expr, ctx, true, e.lay); 239 } 256 } ................................................................................................................................................................................ 246 t.set("name", theLayer, new StrValue(e.var)); 263 t.set("name", theLayer, new StrValue(e.var)); 247 t.set("init", theLayer, macroEval(e.init,ctx,AlwaysMacro 264 t.set("init", theLayer, macroEval(e.init,ctx,AlwaysMacro 248 t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro 265 t.set("expr", theLayer, macroEval(e.expr,ctx,AlwaysMacro 249 return t; 266 return t; 250 }, 267 }, 251 (FuncallExpression e) 268 (FuncallExpression e) 252 { 269 { 253 // [TODO] @macro invokation!!!! | 270 Value _f = macroEval(e.fun,ctx,AlwaysMacro); 254 // if e.fun is varname and its ctx[@macro] is set, sw < > 271 > 272 // copy & pase from normal eval > 273 // [TODO] sync with @layerd parameters. > 274 if( auto f = cast(FunValue)_f ) { > 275 Value[] args; > 276 foreach(a; e.args) > 277 args ~= macroEval(a, ctx, AlwaysMacro); > 278 return f.call(e.pos, "@macro", args); // explici > 279 } > 280 255 Table t = new Table; 281 Table t = new Table; 256 t.set("pos", theLayer, pos); 282 t.set("pos", theLayer, pos); 257 t.set("is", theLayer, new StrValue("app")); 283 t.set("is", theLayer, new StrValue("app")); 258 t.set("fun", theLayer, macroEval(e.fun,ctx,AlwaysMacro)) | 284 t.set("fun", theLayer, _f); 259 Table args = new Table; 285 Table args = new Table; 260 foreach_reverse(a; e.args) { 286 foreach_reverse(a; e.args) { 261 Table cons = new Table; 287 Table cons = new Table; 262 cons.set("car",theLayer,macroEval(a,ctx,AlwaysMa 288 cons.set("car",theLayer,macroEval(a,ctx,AlwaysMa 263 cons.set("cdr",theLayer,args); 289 cons.set("cdr",theLayer,args); 264 args = cons; 290 args = cons; 265 } 291 } 266 t.set("arg", theLayer, args); 292 t.set("arg", theLayer, args); 267 return t; | 293 return cast(Value)t; 268 }, 294 }, 269 (FunLiteral e) 295 (FunLiteral e) 270 { 296 { 271 Table t = new Table; 297 Table t = new Table; 272 t.set("pos", theLayer, pos); 298 t.set("pos", theLayer, pos); 273 t.set("is", theLayer, new StrValue("fun")); 299 t.set("is", theLayer, new StrValue("fun")); 274 t.set("body", theLayer, macroEval(e.funbody,ctx,AlwaysM 300 t.set("body", theLayer, macroEval(e.funbody,ctx,AlwaysM ................................................................................................................................................................................ 339 fac(10);`).val, new IntValue(BigInt(10*9*8*5040))); 365 fac(10);`).val, new IntValue(BigInt(10*9*8*5040))); 340 assert_eq( evalString(`var fib = fun(x){ 366 assert_eq( evalString(`var fib = fun(x){ 341 if(x<2) 367 if(x<2) 342 { 1; } 368 { 1; } 343 else 369 else 344 { fib(x-1) + fib(x-2); }; 370 { fib(x-1) + fib(x-2); }; 345 }; 371 }; 346 fib(10);`).val, new IntValue(BigInt(89))); | 372 fib(5);`).val, new IntValue(BigInt(8))); 347 } 373 } 348 374 349 unittest 375 unittest 350 { 376 { 351 assert_throw!Throwable( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};@s(1 377 assert_throw!Throwable( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};@s(1 352 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};1+2`).val, new In 378 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};1+2`).val, new In 353 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`) 379 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`)