Differences From Artifact [ca0ab118700475a8]:
- File
polemy/eval.d
- 2010-11-23 10:09:03 - part of checkin [36c517dfc4] on branch trunk - refactored d-value and polemy-value conversion (user: kinaba) [annotate]
To Artifact [a7c677ad83621b47]:
- File
polemy/eval.d
- 2010-11-23 13:55:15 - part of checkin [2134cd44cc] on branch trunk - further clean-up for polemy2d (user: kinaba) [annotate]
98 } 98 }
99 99
100 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 100 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
101 { 101 {
102 Value f = eval( e.fun, lay, ctx ); 102 Value f = eval( e.fun, lay, ctx );
103 if( isMacroishLayer(lay) ) 103 if( isMacroishLayer(lay) )
104 if( auto ff = cast(FunValue)f ) 104 if( auto ff = cast(FunValue)f )
105 return invokeFunction(ff, e.args, MacroLayer, ct | 105 return invokeFunction(ff, e.args, MacroLayer, ct
106 else 106 else
107 return ast2table(e, (AST e){return eval(e,lay,ct 107 return ast2table(e, (AST e){return eval(e,lay,ct
108 return invokeFunction(f, e.args, lay, ctx, e.pos); | 108 return invokeFunction(f, e.args, lay, ctx, e.pos, getNameIfPossi
109 } 109 }
110 110
111 Value eval( Fun e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 111 Value eval( Fun e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx )
112 { 112 {
113 if( isMacroishLayer(lay) ) 113 if( isMacroishLayer(lay) )
114 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 114 return ast2table(e, (AST e){return eval(e,lay,ctx);});
115 else 115 else
................................................................................................................................................................................
137 string theLayer = e.layer.empty ? lay : e.layer; // neut 137 string theLayer = e.layer.empty ? lay : e.layer; // neut
138 ctx.set(e.name, theLayer, ri); 138 ctx.set(e.name, theLayer, ri);
139 return eval(e.expr, lay, ctx, OverwriteCtx); 139 return eval(e.expr, lay, ctx, OverwriteCtx);
140 } 140 }
141 } 141 }
142 142
143 private: 143 private:
> 144 string getNameIfPossible(AST e)
> 145 {
> 146 if(auto v = cast(Var)e)
> 147 return v.name;
> 148 return "";
> 149 }
> 150
144 Value invokeFunction(Value _f, AST[] args, Layer lay, Table ctx, LexPosi | 151 Value invokeFunction(Value _f, AST[] args, Layer lay, Table ctx, LexPosi
145 { 152 {
146 if(auto f = cast(FunValue)_f) 153 if(auto f = cast(FunValue)_f)
147 { 154 {
148 Table newCtx = new Table(f.definitionContext(), Table.Ki 155 Table newCtx = new Table(f.definitionContext(), Table.Ki
149 foreach(i,p; f.params()) 156 foreach(i,p; f.params())
150 if( p.layers.empty ) 157 if( p.layers.empty )
151 newCtx.set(p.name, (lay==RawMacroLayer ? 158 newCtx.set(p.name, (lay==RawMacroLayer ?
152 else 159 else
153 foreach(argLay; p.layers) 160 foreach(argLay; p.layers)
154 newCtx.set(p.name, argLay, eval( 161 newCtx.set(p.name, argLay, eval(
> 162 scope _ = new PushCallStack(pos, callstackmsg);
155 return f.invoke(lay==RawMacroLayer ? MacroLayer : lay, n 163 return f.invoke(lay==RawMacroLayer ? MacroLayer : lay, n
156 } 164 }
157 throw genex!RuntimeException(pos, text("tried to call non-functi 165 throw genex!RuntimeException(pos, text("tried to call non-functi
158 } 166 }
159 167
160 Value lift(Value v, Layer lay, Table ctx, LexPosition pos=null) | 168 Value lift(Value v, Layer lay, Table ctx, LexPosition pos)
161 { 169 {
162 assert( !isMacroishLayer(lay), "lift to the @macro layer should | 170 assert( !isMacroishLayer(lay), "lift to the @macro layer should
163 171
164 // functions are automatically lifterd 172 // functions are automatically lifterd
165 if( cast(FunValue) v ) 173 if( cast(FunValue) v )
166 return v; 174 return v;
167 175
> 176 if( !ctx.has(lay, SystemLayer) )
> 177 throw genex!RuntimeException(pos, "lift function for "~l
> 178
168 // similar to invoke Function, but with only one argument bound | 179 // similar to invokeFunction, but with only one argument bound t
169 if(auto f = cast(FunValue)ctx.get(lay, SystemLayer, pos)) | 180 auto _f = ctx.get(lay, SystemLayer, pos);
> 181 if(auto f = cast(FunValue)_f)
170 { 182 {
171 Table newCtx = new Table(f.definitionContext(), Table.Ki 183 Table newCtx = new Table(f.definitionContext(), Table.Ki
172 auto ps = f.params(); 184 auto ps = f.params();
173 if( ps.length != 1 ) 185 if( ps.length != 1 )
174 throw genex!RuntimeException(pos, "lift function | 186 throw genex!RuntimeException(pos,
> 187 text("lift function for", lay, " must ta
175 if( ps[0].layers.length==0 || ps[0].layers.length==1 && 188 if( ps[0].layers.length==0 || ps[0].layers.length==1 &&
176 { 189 {
177 newCtx.set(ps[0].name, ValueLayer, v); 190 newCtx.set(ps[0].name, ValueLayer, v);
> 191 scope _ = new PushCallStack(pos, lay);
178 return f.invoke(ValueLayer, newCtx, pos); 192 return f.invoke(ValueLayer, newCtx, pos);
179 } 193 }
180 else 194 else
181 throw genex!RuntimeException(pos, "lift function | 195 throw genex!RuntimeException(pos,
> 196 text("lift function for", lay, " must ta
182 } 197 }
183 throw genex!RuntimeException(pos, "tried to call non-function"); | 198 throw genex!RuntimeException(pos,
> 199 text("non-function ", _f, " is registered as the lift fu
184 } 200 }
185 201
186 Value createNewFunction(Fun e, Table ctx) 202 Value createNewFunction(Fun e, Table ctx)
187 { 203 {
188 class UserDefinedFunValue : FunValue 204 class UserDefinedFunValue : FunValue
189 { 205 {
190 Fun ast; 206 Fun ast;
................................................................................................................................................................................
215 assert(false, sprintf!"Cannot compare %s with %s 231 assert(false, sprintf!"Cannot compare %s with %s
216 } 232 }
217 233
218 override Value invoke(Layer lay, Table ctx, LexPosition 234 override Value invoke(Layer lay, Table ctx, LexPosition
219 { 235 {
220 if( lay == MacroLayer ) 236 if( lay == MacroLayer )
221 return eval(ast.funbody, lay, ctx); 237 return eval(ast.funbody, lay, ctx);
> 238 try {
222 if( afterMacroAST is null ) | 239 if( afterMacroAST is null )
223 afterMacroAST = tableToAST(ValueLayer, e | 240 afterMacroAST = polemy2d!(AST)(e
224 return eval(afterMacroAST, lay, ctx); | 241 return eval(afterMacroAST, lay, ctx);
> 242 } catch( RuntimeException e ) {
> 243 throw e.pos is null ? new RuntimeExcepti
> 244 }
225 } 245 }
226 246
227 AST afterMacroAST; 247 AST afterMacroAST;
228 } 248 }
229 return new UserDefinedFunValue(e,ctx); 249 return new UserDefinedFunValue(e,ctx);
230 } 250 }
231 251