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

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

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

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

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

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

73 73 if( auto fe = cast(FunValue)args[2] ) 74 74 return (x.data == 0 ? fe : ft).call(pos,lay,[]); 75 75 throw genex!RuntimeException(pos, "type mismatch in if"); 76 76 })); 77 77 ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cast(IntValue)v is null ? 0 : 1));} )); 78 78 ctx.set("_isstr", "@v", native( (Value v){return new IntValue(BigInt(cast(StrValue)v is null ? 0 : 1));} )); 79 79 ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cast(FunValue)v is null ? 0 : 1));} )); 80 + ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigInt(cast(UndValue)v is null ? 0 : 1));} )); 80 81 return ctx; 81 82 } 82 83 83 84 /// Entry point of this module 84 85 85 86 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 86 87 { ................................................................................ 161 162 args ~= eval(a, ctx, true, lay); 162 163 return f.call(e.pos, lay, args); 163 164 } 164 165 throw genex!RuntimeException(e.pos, "Non-funcion is applied"); 165 166 }, 166 167 (FunLiteral e) 167 168 { 169 + Value[Value[]][Layer] memo; 170 + 168 171 // funvalue need not be rised 172 + // no, need to be rised !! suppose @t(fib)("int") 169 173 return new FunValue(delegate Value(immutable LexPosition pos, string lay, Value[] args){ 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(lay, "(system)", e.pos)).call(e.pos, "@v", 182 + [new UndValue] 183 + ); 184 + } 185 + 170 186 if( e.params.length != args.length ) 171 187 throw genex!RuntimeException(e.pos, sprintf!"Argument Number Mismatch (%d required but %d given)" 172 188 (e.params.length, args.length)); 173 189 Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet); 174 190 foreach(i,p; e.params) 175 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 199 delegate Value (AST e) 180 200 { 181 201 throw genex!RuntimeException(e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(e))); 182 202 } 183 203 );

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

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