Differences From Artifact [2bd159f2e71889e8]:
- File
polemy/eval.d
- 2010-11-13 05:38:21 - part of checkin [078444a806] on branch trunk - additional primitives for doing typechecking (user: kinaba) [annotate]
To Artifact [5c2f449580dd9555]:
- File
polemy/eval.d
- 2010-11-13 12:16:47 - part of checkin [5afe8e3f26] on branch trunk - Memoization on non "@v" layer. Now simplest metalevel computation works!! Also, added -l option. (user: kinaba) [annotate]
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 );