Differences From Artifact [e0978d70034e5fac]:
- File
polemy/eval.d
- 2010-11-24 03:30:56 - part of checkin [20be503cae] on branch trunk - set up referece manual (user: kinaba) [annotate]
To Artifact [4eba423d1304ad73]:
- File
polemy/eval.d
- 2010-11-24 11:20:42 - part of checkin [153a14cec0] on branch trunk - if-then-else without {}s. some cosmetic changes (user: kinaba) [annotate]
68 68 assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not defined"));
69 69 }
70 70
71 71 Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
72 72 {
73 73 if( isASTLayer(lay) )
74 74 return ast2table(e, (AST e){return eval(e,lay,ctx);});
75 - if( lay==ValueLayer )
76 - return new StrValue(e.data);
77 - return lift(new StrValue(e.data), lay, ctx, e.pos);
75 + if( isUserDefinedLayer(lay) )
76 + return lift(new StrValue(e.data), lay, ctx, e.pos);
77 + return new StrValue(e.data);
78 78 }
79 79
80 80 Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
81 81 {
82 82 if( isASTLayer(lay) )
83 83 return ast2table(e, (AST e){return eval(e,lay,ctx);});
84 - if( lay==ValueLayer )
85 - return new IntValue(e.data);
86 - return lift(new IntValue(e.data), lay, ctx, e.pos);
84 + if( isUserDefinedLayer(lay) )
85 + return lift(new IntValue(e.data), lay, ctx, e.pos);
86 + return new IntValue(e.data);
87 87 }
88 88
89 89 Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
90 90 {
91 91 if( isASTLayer(lay) )
92 92 if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) )
93 93 return ctx.get(e.name, MacroLayer, e.pos);
94 94 else
95 95 return ast2table(e, (AST e){return eval(e,lay,ctx);});
96 - if( lay==ValueLayer || ctx.has(e.name, lay) )
97 - return ctx.get(e.name, lay, e.pos);
98 - return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx, e.pos);
96 + if( isUserDefinedLayer(lay) && !ctx.has(e.name, lay) )
97 + return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx, e.pos);
98 + return ctx.get(e.name, lay, e.pos);
99 99 }
100 100
101 101 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
102 102 {
103 103 Value f = eval( e.fun, lay, ctx );
104 104 if( isASTLayer(lay) ) {
105 105 auto ff = cast(FunValue)f;
................................................................................
134 134 }
135 135
136 136 Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
137 137 {
138 138 Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.NotPropagateSet);
139 139 if( isASTLayer(lay) )
140 140 return ast2table(e, (AST ee){
141 - if(ee is e.expr) // need this for correct scoping (outer scope macro variables must be hidden!)
141 + // need this for correct scoping (outer scope macro variables must be hidden!)
142 + if(ee is e.expr)
142 143 newCtx.set(e.name, ValueLayer, new UndefinedValue);
143 144 return eval(ee,lay,newCtx);
144 145 });
145 146 else
146 147 {
147 148 Value ri = eval(e.init, lay, newCtx);
148 149 newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri);
................................................................................
251 252 {
252 253 Fun ast;
253 254 Table defCtx;
254 255 override const(Parameter[]) params() { return ast.params; }
255 256 override Table definitionContext() { return defCtx; }
256 257
257 258 this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx=defCtx; }
258 - override string toString() const { return sprintf!"(function:%x:%x)"(cast(void*)ast, cast(void*)defCtx); }
259 + override string toString() const
260 + { return sprintf!"(function:%x:%x)"(cast(void*)ast, cast(void*)defCtx); }
259 261 override int opCmp(Object rhs) {
260 262 if(auto r = cast(UserDefinedFunValue)rhs) {
261 - if(auto i = this.ast.opCmp(r.ast))
262 - return i;
263 + auto a = cast(void*)this.ast;
264 + auto b = cast(void*)r.ast;
265 + if(a<b) return -1;
266 + if(a>b) return +1; // [TODO] avoid using pointer value...
263 267 return this.defCtx.opCmp(r.defCtx);
264 268 }
265 - if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r));
269 + if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r));
266 270 throw genex!RuntimeException("comparison with value and something other");
267 271 }
268 272 mixin SimpleToHash;
269 273
270 274 AST afterMacroAST;
271 275 override Value invoke(Layer lay, Table ctx, LexPosition pos)
272 276 {
................................................................................
294 298 override const(Parameter[]) params() { return params_data; }
295 299 override Table definitionContext() { return theContext; }
296 300
297 301 override string toString() { return sprintf!"(native:%x)"(dg.funcptr); }
298 302 override int opCmp(Object rhs) {
299 303 if(auto r = cast(NativeFunValue)rhs) return typeid(typeof(dg)).compare(&dg,&r.dg);
300 304 if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r));
301 - throw genex!RuntimeException(LexPosition.dummy, "comparison with value and something other");
305 + throw genex!RuntimeException("comparison with value and something other");
302 306 }
303 307 mixin SimpleToHash;
304 308
305 309 R delegate(T) dg;
306 310 Parameter[] params_data;
307 311
308 312 this(R delegate(T) dg)
................................................................................
311 315 foreach(i, Ti; T)
312 316 params_data ~= new Parameter(text(i), []);
313 317 }
314 318
315 319 override Value invoke(Layer lay, Table ctx, LexPosition pos)
316 320 {
317 321 if( lay != defLay )
318 - throw genex!RuntimeException(pos, text("only ", defLay, " layer can call native function: ", name));
322 + throw genex!RuntimeException(pos,
323 + text("only ", defLay, " layer can call native function: ", name));
319 324 T typed_args;
320 325 foreach(i, Ti; T) {
321 326 typed_args[i] = cast(Ti) ctx.get(text(i), ValueLayer, pos);
322 327 if( typed_args[i] is null )
323 - throw genex!RuntimeException(pos, sprintf!"type mismatch on the argument %d of native function: %s"(i+1,name));
328 + throw genex!RuntimeException(pos,
329 + sprintf!"type mismatch on the argument %d of native function: %s"(i+1,name));
324 330 }
325 331 try {
326 332 return dg(typed_args);
327 333 } catch( RuntimeException e ) {
328 334 throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e;
329 335 }
330 336 }