Check-in [5afe8e3f26]
Not logged in
Overview
SHA1 Hash:5afe8e3f267745202e31caf1823d363619fc3ef8
Date: 2010-11-13 21:16:47
User: kinaba
Comment:Memoization on non "@v" layer. Now simplest metalevel computation works!! Also, added -l option.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified d2stacktrace/stacktrace.d from [1456272a3238466c] to [2d470e3c95d4cfbd].

29 import std.string; 29 import std.string; 30 import dbghelp; 30 import dbghelp; 31 import core.runtime; 31 import core.runtime; 32 import std.stdio; 32 import std.stdio; 33 import std.c.stdlib; 33 import std.c.stdlib; 34 import std.demangle; 34 import std.demangle; 35 import std.conv; 35 import std.conv; > 36 import std.path; 36 37 37 extern(Windows){ 38 extern(Windows){ 38 DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR pBuffer, DWORD nSize) 39 DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR pBuffer, DWORD nSize) 39 void RtlCaptureContext(CONTEXT* ContextRecord); 40 void RtlCaptureContext(CONTEXT* ContextRecord); 40 typedef LONG function(void*) UnhandeledExceptionFilterFunc; 41 typedef LONG function(void*) UnhandeledExceptionFilterFunc; 41 void* SetUnhandledExceptionFilter(void* handler); 42 void* SetUnhandledExceptionFilter(void* handler); 42 } 43 } ................................................................................................................................................................................ 333 break; 334 break; 334 } 335 } 335 336 336 if(stackframe.AddrPC.Offset != 0){ 337 if(stackframe.AddrPC.Offset != 0){ 337 string lineStr = ""; 338 string lineStr = ""; 338 Dbghelp.DWORD64 offsetFromSymbol = cast(Dbghelp. 339 Dbghelp.DWORD64 offsetFromSymbol = cast(Dbghelp. 339 if( Dbghelp.SymGetSymFromAddr64(hProcess,stackfr 340 if( Dbghelp.SymGetSymFromAddr64(hProcess,stackfr 340 char[] symName = new char[strlen(cast(co | 341 char[] symName = new char[strlen(cast(co 341 memcpy(symName.ptr,Symbol.Name.ptr,symNa 342 memcpy(symName.ptr,Symbol.Name.ptr,symNa 342 string symString = ""; 343 string symString = ""; 343 if(symName[0] == 'D') 344 if(symName[0] == 'D') 344 symString = "_"; 345 symString = "_"; 345 symString ~= symName; 346 symString ~= symName; 346 347 347 string demangeledName = demangle(symStri | 348 string demangledName = demangle(symStrin > 349 bool isOK = true; > 350 for(int i=0; i<demangledName.length; ++i > 351 if( demangledName[i] >= 0x80 ) > 352 isOK = false; > 353 if(isOK) 348 lineStr ~= demangeledName; | 354 lineStr ~= demangledName; 349 355 350 DWORD zeichen = 0; 356 DWORD zeichen = 0; 351 if(Dbghelp.SymGetLineFromAddr64(hProcess 357 if(Dbghelp.SymGetLineFromAddr64(hProcess 352 char[] fileName = new char[strle 358 char[] fileName = new char[strle 353 fileName[] = Line.FileName[0..fi | 359 fileName = std.path.basename( Li 354 lineStr = to!string(fileName ~ " 360 lineStr = to!string(fileName ~ " 355 } 361 } 356 } 362 } 357 else { 363 else { 358 lineStr = to!string(cast(ulong)stackfram 364 lineStr = to!string(cast(ulong)stackfram 359 } 365 } 360 lineStr = to!string(frameNum-2) ~ " " ~ lineStr; 366 lineStr = to!string(frameNum-2) ~ " " ~ lineStr;

Modified main.d from [981f3ba98be4374d] to [b6f89f38f0a1626d].

18 { 18 { 19 Table ctx; 19 Table ctx; 20 string buf; 20 string buf; 21 Value lastVal; 21 Value lastVal; 22 int lineno = 1; 22 int lineno = 1; 23 int nextlineno = 1; 23 int nextlineno = 1; 24 this() { ctx = createGlobalContext(); } 24 this() { ctx = createGlobalContext(); } > 25 this(string filename) { > 26 ctx = createGlobalContext(); > 27 eval(parseFile(filename), ctx, false, "@v"); > 28 } 25 29 26 bool tryRun( string s ) 30 bool tryRun( string s ) 27 { 31 { 28 scope(failure) 32 scope(failure) 29 { buf = ""; lineno = nextlineno; } 33 { buf = ""; lineno = nextlineno; } 30 34 31 buf ~= s; 35 buf ~= s; ................................................................................................................................................................................ 52 writeln(e); 56 writeln(e); 53 } 57 } 54 return true; 58 return true; 55 } 59 } 56 } 60 } 57 61 58 /// Entry point. If args.length==1, invoke REPL. 62 /// Entry point. If args.length==1, invoke REPL. > 63 /// If args.length==3 && args[1]=="-l" read args[2] and invoke REPL. 59 /// Otherwise interpret the argument as a filename. 64 /// Otherwise interpret the argument as a filename. 60 void main( string[] args ) 65 void main( string[] args ) 61 { 66 { 62 if( args.length <= 1 ) 67 if( args.length <= 1 ) 63 { 68 { 64 writeln("Welcome to Polemy 0.1.0"); 69 writeln("Welcome to Polemy 0.1.0"); 65 for(auto r = new REPL; r.singleInteraction();) {} 70 for(auto r = new REPL; r.singleInteraction();) {} 66 } 71 } > 72 else if( args.length>=3 && args[1]=="-l" ) > 73 { > 74 writeln("Welcome to Polemy 0.1.0"); > 75 for(auto r = new REPL(args[2]); r.singleInteraction();) {} > 76 } 67 else 77 else 68 { 78 { 69 evalFile(args[1]); 79 evalFile(args[1]); 70 } 80 } 71 } 81 }

Modified polemy/_common.d from [0d687b04437e837f] to [7abcb8181e6c0ff0].

9 public import std.range; 9 public import std.range; 10 public import std.algorithm; 10 public import std.algorithm; 11 public import std.conv : to; 11 public import std.conv : to; 12 public import std.bigint; 12 public import std.bigint; 13 public import std.exception; 13 public import std.exception; 14 public import tricks.tricks; 14 public import tricks.tricks; 15 public import tricks.test; 15 public import tricks.test; > 16 public import std.stdio : writeln; // for debugging...

Modified polemy/eval.d from [2bd159f2e71889e8] to [5c2f449580dd9555].

73 if( auto fe = cast(FunValue)args[2] ) 73 if( auto fe = cast(FunValue)args[2] ) 74 return (x.data == 0 ? fe : ft).call(pos,lay,[]); 74 return (x.data == 0 ? fe : ft).call(pos,lay,[]); 75 throw genex!RuntimeException(pos, "type mismatch in if"); 75 throw genex!RuntimeException(pos, "type mismatch in if"); 76 })); 76 })); 77 ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cas 77 ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cas 78 ctx.set("_isstr", "@v", native( (Value v){return new IntValue(BigInt(cas 78 ctx.set("_isstr", "@v", native( (Value v){return new IntValue(BigInt(cas 79 ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cas 79 ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cas > 80 ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigI 80 return ctx; 81 return ctx; 81 } 82 } 82 83 83 /// Entry point of this module 84 /// Entry point of this module 84 85 85 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 86 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 86 { 87 { ................................................................................................................................................................................ 161 args ~= eval(a, ctx, true, lay); 162 args ~= eval(a, ctx, true, lay); 162 return f.call(e.pos, lay, args); 163 return f.call(e.pos, lay, args); 163 } 164 } 164 throw genex!RuntimeException(e.pos, "Non-funcion is appl 165 throw genex!RuntimeException(e.pos, "Non-funcion is appl 165 }, 166 }, 166 (FunLiteral e) 167 (FunLiteral e) 167 { 168 { > 169 Value[Value[]][Layer] memo; > 170 168 // funvalue need not be rised 171 // funvalue need not be rised > 172 // no, need to be rised !! suppose @t(fib)("int") 169 return new FunValue(delegate Value(immutable LexPosition 173 return new FunValue(delegate Value(immutable LexPosition > 174 // TODO: only auto raised ones need memo? no? > 175 // auto memoization > 176 if( lay != "@v" ) > 177 { > 178 if( auto memolay = lay in memo ) > 179 if( auto pv = args in *memolay ) > 180 return *pv; > 181 memo[lay][args] = (cast(FunValue)ctx.get > 182 [new UndValue] > 183 ); > 184 } > 185 170 if( e.params.length != args.length ) 186 if( e.params.length != args.length ) 171 throw genex!RuntimeException(e.pos, spri 187 throw genex!RuntimeException(e.pos, spri 172 (e.params.length, args.length)); 188 (e.params.length, args.length)); 173 Table ctxNeo = new Table(ctx, Table.Kind.NotProp 189 Table ctxNeo = new Table(ctx, Table.Kind.NotProp 174 foreach(i,p; e.params) 190 foreach(i,p; e.params) 175 ctxNeo.set(p.name, lay, args[i]); 191 ctxNeo.set(p.name, lay, args[i]); 176 return eval(e.funbody, ctxNeo, true, lay); | 192 auto v = eval(e.funbody, ctxNeo, true, lay); > 193 // auto memoization > 194 if( lay != "@v" ) > 195 memo[lay][args] = v; > 196 return v; 177 }); 197 }); 178 }, 198 }, 179 delegate Value (AST e) 199 delegate Value (AST e) 180 { 200 { 181 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kin 201 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kin 182 } 202 } 183 ); 203 );

Modified polemy/value.d from [01c119756ed23ca5] to [fe3f0c39a200a0bc].

41 { 41 { 42 Value delegate(immutable LexPosition pos, string lay, Value[]) data; 42 Value delegate(immutable LexPosition pos, string lay, Value[]) data; 43 43 44 mixin SimpleConstructor; 44 mixin SimpleConstructor; 45 alias data call; 45 alias data call; 46 override string toString() const { return sprintf!"(function:%s:%s)"(dat 46 override string toString() const { return sprintf!"(function:%s:%s)"(dat 47 } 47 } > 48 > 49 class UndValue : Value > 50 { > 51 mixin SimpleClass; > 52 override string toString() const { return "<undefined>"; } > 53 } 48 54 49 /// Layer ID 55 /// Layer ID 50 56 51 alias string Layer; 57 alias string Layer; 52 58 53 /// Context (variable environment) 59 /// Context (variable environment) 54 /// Simlar to prototype chain of ECMAScript etc. 60 /// Simlar to prototype chain of ECMAScript etc. ................................................................................................................................................................................ 66 if( setIfExist(i, lay, v) ) 72 if( setIfExist(i, lay, v) ) 67 return; 73 return; 68 data[i][lay] = v; 74 data[i][lay] = v; 69 } 75 } 70 76 71 Value get(string i, Layer lay, in LexPosition pos=null) 77 Value get(string i, Layer lay, in LexPosition pos=null) 72 { 78 { 73 if( i in data ) | 79 if( i in data ) { > 80 if( lay !in data[i] ) > 81 throw genex!RuntimeException(pos, sprintf!"varia 74 return data[i][lay]; 82 return data[i][lay]; > 83 } 75 if( prototype is null ) 84 if( prototype is null ) 76 throw new RuntimeException(pos, sprintf!"variable %s not 85 throw new RuntimeException(pos, sprintf!"variable %s not 77 return prototype.get(i, lay); | 86 return prototype.get(i, lay, pos); 78 } 87 } 79 88 80 private: 89 private: 81 Table prototype; 90 Table prototype; 82 Kind kind; 91 Kind kind; 83 Value[Layer][string] data; 92 Value[Layer][string] data; 84 93

Added sample/type.pmy version [c9a55d8e7b3293ae]

> 1 @@type = fun(x){ > 2 if( _isint(x) ) { "int" } > 3 else { if( _isstr(x) ) { "str" } > 4 else { if( _isfun(x) ) { x } > 5 else { if( _isundefined(x) ) { "undefined" } > 6 else { "any" }}}} > 7 }; > 8 > 9 def binop(a,b,c) { > 10 fun(x,y){@v( > 11 if( @type(x)=="undefined" || @type(y)=="undefined" ) { "undefined" } else { > 12 if( @type(x)==a && @type(y)==b ) { c } else { "error" } > 13 } > 14 )} > 15 }; > 16 > 17 @type "+" = binop("int", "int", "int"); > 18 @type "-" = binop("int", "int", "int"); > 19 @type "<" = binop("int", "int", "int"); > 20 @type ">" = binop("int", "int", "int"); > 21 > 22 def mergeType(a,b) { > 23 if( a == "undefined" ) { if(b=="undefined"){"error"}else{b} } else { a } > 24 }; > 25 > 26 @type "if" = fun(c,t,e) {@v( > 27 if(@type(c)=="int" ) { mergeType(@type(t()), @type(e())) } else { "error" } > 28 )};