Diff
Not logged in

Differences From Artifact [0e6e0e9d77fcbedc]:

To Artifact [c85d31e67d8ceb4b]:


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