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 assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not 68 assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not
69 } 69 }
70 70
71 Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 71 Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
72 { 72 {
73 if( isASTLayer(lay) ) 73 if( isASTLayer(lay) )
74 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 74 return ast2table(e, (AST e){return eval(e,lay,ctx);});
75 if( lay==ValueLayer ) | 75 if( isUserDefinedLayer(lay) )
76 return new StrValue(e.data); | 76 return lift(new StrValue(e.data), lay, ctx, e.pos);
77 return lift(new StrValue(e.data), lay, ctx, e.pos); | 77 return new StrValue(e.data);
78 } 78 }
79 79
80 Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 80 Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
81 { 81 {
82 if( isASTLayer(lay) ) 82 if( isASTLayer(lay) )
83 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 83 return ast2table(e, (AST e){return eval(e,lay,ctx);});
84 if( lay==ValueLayer ) | 84 if( isUserDefinedLayer(lay) )
85 return new IntValue(e.data); | 85 return lift(new IntValue(e.data), lay, ctx, e.pos);
86 return lift(new IntValue(e.data), lay, ctx, e.pos); | 86 return new IntValue(e.data);
87 } 87 }
88 88
89 Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 89 Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
90 { 90 {
91 if( isASTLayer(lay) ) 91 if( isASTLayer(lay) )
92 if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) ) 92 if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) )
93 return ctx.get(e.name, MacroLayer, e.pos); 93 return ctx.get(e.name, MacroLayer, e.pos);
94 else 94 else
95 return ast2table(e, (AST e){return eval(e,lay,ct 95 return ast2table(e, (AST e){return eval(e,lay,ct
96 if( lay==ValueLayer || ctx.has(e.name, lay) ) | 96 if( isUserDefinedLayer(lay) && !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) | 97 return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx
> 98 return ctx.get(e.name, lay, e.pos);
99 } 99 }
100 100
101 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 101 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
102 { 102 {
103 Value f = eval( e.fun, lay, ctx ); 103 Value f = eval( e.fun, lay, ctx );
104 if( isASTLayer(lay) ) { 104 if( isASTLayer(lay) ) {
105 auto ff = cast(FunValue)f; 105 auto ff = cast(FunValue)f;
................................................................................................................................................................................
134 } 134 }
135 135
136 Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 136 Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
137 { 137 {
138 Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.No 138 Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.No
139 if( isASTLayer(lay) ) 139 if( isASTLayer(lay) )
140 return ast2table(e, (AST ee){ 140 return ast2table(e, (AST ee){
141 if(ee is e.expr) // need this for correct scopin | 141 // need this for correct scoping (outer scope ma
> 142 if(ee is e.expr)
142 newCtx.set(e.name, ValueLayer, new Undef 143 newCtx.set(e.name, ValueLayer, new Undef
143 return eval(ee,lay,newCtx); 144 return eval(ee,lay,newCtx);
144 }); 145 });
145 else 146 else
146 { 147 {
147 Value ri = eval(e.init, lay, newCtx); 148 Value ri = eval(e.init, lay, newCtx);
148 newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri); 149 newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri);
................................................................................................................................................................................
251 { 252 {
252 Fun ast; 253 Fun ast;
253 Table defCtx; 254 Table defCtx;
254 override const(Parameter[]) params() { return ast.params 255 override const(Parameter[]) params() { return ast.params
255 override Table definitionContext() { return defCtx; } 256 override Table definitionContext() { return defCtx; }
256 257
257 this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx= 258 this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx=
> 259 override string toString() const
258 override string toString() const { return sprintf!"(func | 260 { return sprintf!"(function:%x:%x)"(cast(void*)a
259 override int opCmp(Object rhs) { 261 override int opCmp(Object rhs) {
260 if(auto r = cast(UserDefinedFunValue)rhs) { 262 if(auto r = cast(UserDefinedFunValue)rhs) {
261 if(auto i = this.ast.opCmp(r.ast)) | 263 auto a = cast(void*)this.ast;
> 264 auto b = cast(void*)r.ast;
262 return i; | 265 if(a<b) return -1;
> 266 if(a>b) return +1; // [TODO] avoid using
263 return this.defCtx.opCmp(r.defCtx); 267 return this.defCtx.opCmp(r.defCtx);
264 } 268 }
265 if(auto r = cast(Value)rhs) return typeid(thi | 269 if(auto r = cast(Value)rhs) return typeid(this).
266 throw genex!RuntimeException("comparison with va 270 throw genex!RuntimeException("comparison with va
267 } 271 }
268 mixin SimpleToHash; 272 mixin SimpleToHash;
269 273
270 AST afterMacroAST; 274 AST afterMacroAST;
271 override Value invoke(Layer lay, Table ctx, LexPosition 275 override Value invoke(Layer lay, Table ctx, LexPosition
272 { 276 {
................................................................................................................................................................................
294 override const(Parameter[]) params() { return params_dat 298 override const(Parameter[]) params() { return params_dat
295 override Table definitionContext() { return theContext 299 override Table definitionContext() { return theContext
296 300
297 override string toString() { return sprintf!"(native:%x) 301 override string toString() { return sprintf!"(native:%x)
298 override int opCmp(Object rhs) { 302 override int opCmp(Object rhs) {
299 if(auto r = cast(NativeFunValue)rhs) return type 303 if(auto r = cast(NativeFunValue)rhs) return type
300 if(auto r = cast(Value)rhs) return type 304 if(auto r = cast(Value)rhs) return type
301 throw genex!RuntimeException(LexPosition.dummy, | 305 throw genex!RuntimeException("comparison with va
302 } 306 }
303 mixin SimpleToHash; 307 mixin SimpleToHash;
304 308
305 R delegate(T) dg; 309 R delegate(T) dg;
306 Parameter[] params_data; 310 Parameter[] params_data;
307 311
308 this(R delegate(T) dg) 312 this(R delegate(T) dg)
................................................................................................................................................................................
311 foreach(i, Ti; T) 315 foreach(i, Ti; T)
312 params_data ~= new Parameter(text(i), [] 316 params_data ~= new Parameter(text(i), []
313 } 317 }
314 318
315 override Value invoke(Layer lay, Table ctx, LexPosition 319 override Value invoke(Layer lay, Table ctx, LexPosition
316 { 320 {
317 if( lay != defLay ) 321 if( lay != defLay )
> 322 throw genex!RuntimeException(pos,
318 throw genex!RuntimeException(pos, text(" | 323 text("only ", defLay, " layer ca
319 T typed_args; 324 T typed_args;
320 foreach(i, Ti; T) { 325 foreach(i, Ti; T) {
321 typed_args[i] = cast(Ti) ctx.get(text(i) 326 typed_args[i] = cast(Ti) ctx.get(text(i)
322 if( typed_args[i] is null ) 327 if( typed_args[i] is null )
> 328 throw genex!RuntimeException(pos
323 throw genex!RuntimeException(pos | 329 sprintf!"type mismatch o
324 } 330 }
325 try { 331 try {
326 return dg(typed_args); 332 return dg(typed_args);
327 } catch( RuntimeException e ) { 333 } catch( RuntimeException e ) {
328 throw e.pos is null ? new RuntimeExcepti 334 throw e.pos is null ? new RuntimeExcepti
329 } 335 }
330 } 336 }