Check-in [8e3db9ef20]
Not logged in
Overview
SHA1 Hash:8e3db9ef20d81211722691f1f65d705a8adcd5d4
Date: 2010-11-20 23:04:44
User: kinaba
Comment:macro worked!
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified .poseidon from [706faeb501e0d2d1] to [c60d98a3acceffca].

7 7 <filter>*.d</filter> 8 8 <showemptyfolder>0</showemptyfolder> 9 9 <buildSpec> 10 10 <buildType>0</buildType> 11 11 <mainFile>main.d</mainFile> 12 12 <Args /> 13 13 <options> 14 - <dmd> -cov -g -unittest </dmd> 14 + <dmd> -g -unittest </dmd> 15 15 <tool /> 16 16 <lib /> 17 17 <implib /> 18 18 <extra /> 19 19 <toolextra /> 20 20 <merge>0</merge> 21 21 <nonfiles>0</nonfiles>

Modified d2stacktrace/stacktrace.d from [9e66870b7d120c42] to [e1d39624d6c41174].

244 244 245 245 StackTrace trace = new StackTrace(); 246 246 return trace.GetCallstack(); 247 247 } 248 248 249 249 public: 250 250 static this(){ 251 - Runtime.traceHandler(&TraceHandler); 252 - SetUnhandledExceptionFilter(&UnhandeledExceptionFilterHandler); 251 +// Runtime.traceHandler(&TraceHandler); 252 +// SetUnhandledExceptionFilter(&UnhandeledExceptionFilterHandler); 253 253 } 254 254 255 255 this(){ 256 256 if(isInit) 257 257 return; 258 258 HANDLE hProcess = GetCurrentProcess(); 259 259 DWORD pid = GetCurrentProcessId();

Modified polemy/eval.d from [4f24d3bd11889ec1] to [84ee94a6f293fac0].

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

Modified polemy/value.d from [10fa9090c50cdaf9] to [ff42bce4fb7dc674].

145 145 else 146 146 { 147 147 if(auto next = this.access!Table(lay,path)) 148 148 return next.access!T(lay,rest); 149 149 } 150 150 return null; 151 151 } 152 + 153 + string toStringWithoutParen() const 154 + { 155 + string result; 156 + bool first = true; 157 + foreach(k, l2d; data) 158 + foreach(l,d; l2d) 159 + { 160 + if(first) first=false; else result~=", "; 161 + result ~= k; 162 + result ~= l; 163 + result ~= ":"; 164 + result ~= text(cast(Value)d); 165 + } 166 + if( prototype !is null ) 167 + { 168 + result ~= " / "; 169 + result ~= prototype.toStringWithoutParen(); 170 + } 171 + return result; 172 + } 173 + 174 + string toString() const 175 + { 176 + return "{" ~ toStringWithoutParen() ~ "}"; 177 + } 152 178 153 179 private: 154 180 Table prototype; 155 181 Kind kind; 156 182 Value[Layer][string] data; 157 183 158 184 bool setIfExist(string i, Layer lay, Value v) ................................................................................ 242 268 if(auto t = cast(Table)v) 243 269 result ~= tableToAST(theLayer,t); 244 270 else 245 271 throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST (non-table in cons-list)"); 246 272 return result; 247 273 } 248 274 249 -AST tableToAST( Layer theLayer, Table t ) 275 +AST tableToAST( Layer theLayer, Value vvvv ) 250 276 { 277 + Table t = cast(Table)vvvv; 278 + if( t is null ) 279 + throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST (not a table)"); 280 + 251 281 auto nodeType = t.access!StrValue(theLayer, "is"); 252 282 if( nodeType is null ) 253 283 throw genex!RuntimeException(cast(LexPosition)null, "Invalid AST {is:(not string)}"); 254 284 auto pos = extractPos(t); 255 285 switch(nodeType.data) 256 286 { 257 287 case "int": ................................................................................ 304 334 if(auto ll = tt.access!Table(theLayer, "layer")) 305 335 { 306 336 Layer[] ls; 307 337 foreach(lll; tableAsConsList(theLayer, ll)) 308 338 if(auto l = cast(StrValue)lll) 309 339 ls ~= l.data; 310 340 else 311 - throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {bad fun params}`); 341 + throw genex!RuntimeException(cast(LexPosition)null, sprintf!`Invalid AST {bad fun params %s}`(lll)); 312 342 ps ~= new Parameter(ss.data, ls); 313 343 continue; 314 344 } 315 345 else 316 346 { 317 347 Layer[] emp; 318 348 ps ~= new Parameter(ss.data, emp); 349 + continue; 319 350 } 320 - throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {bad fun params}`); 351 + throw genex!RuntimeException(cast(LexPosition)null, sprintf!`Invalid AST {bad fun params %s}`(v)); 321 352 } 322 353 auto bb = tableToAST(theLayer, b); 323 354 return new FunLiteral(pos,ps,bb); 324 355 } 325 356 throw genex!RuntimeException(cast(LexPosition)null, `Invalid AST {is:"fun", param:???, body:???}`); 326 357 default: 327 358 throw genex!RuntimeException(cast(LexPosition)null, sprintf!`Invalid AST {is: "%s"} unknown`(nodeType.data)); 328 359 } 329 360 }

Modified sample/plusminus.pmy from [5278c04aa82ef1ff] to [2f68238d12815ac3].

1 +@@s(x){x}; 1 2 @s "+" = fun(x, y) {@v( 2 3 @s(x) - @s(y) 3 4 )}; 4 5 5 6 print( 1 + 2 ); 6 7 print( @s(1 + 2) );