Differences From 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]
To Artifact [6db0ce492da06d00]:
- File
polemy/eval.d
- 2010-11-20 09:20:03 - part of checkin [515502e8d1] on branch trunk - table get, init, ask expressions addded (user: kinaba) [annotate]
9 import polemy.lex : LexPosition; 9 import polemy.lex : LexPosition;
10 import polemy.ast; 10 import polemy.ast;
11 import polemy.parse; 11 import polemy.parse;
12 import polemy.value; 12 import polemy.value;
13 import std.typecons; 13 import std.typecons;
14 import std.stdio; 14 import std.stdio;
15 15
16 // [todo] move to value.d <
17 <
18 FunValue nativef(Value delegate(immutable LexPosition pos, Layer lay, Value[] ar <
19 { <
20 return new FunValue(dg); <
21 } <
22 <
23 FunValue native(R,T...)(R delegate (T) dg) <
24 { <
25 return nativef( delegate Value(immutable LexPosition pos, Layer lay, Val <
26 if( lay != "@v" ) <
27 throw genex!RuntimeException(pos, "only @v layer can cal <
28 if( T.length != args.length ) <
29 throw genex!RuntimeException(pos, "argument number misma <
30 T typed_args; <
31 foreach(i, Ti; T) <
32 { <
33 typed_args[i] = cast(Ti) args[i]; <
34 if( typed_args[i] is null ) <
35 throw genex!RuntimeException(pos, sprintf!"type <
36 } <
37 try { <
38 return dg(typed_args); <
39 } catch( RuntimeException e ) { <
40 throw e.pos is null ? new RuntimeException(pos, e.msg, e <
41 } <
42 }); <
43 } <
44 <
45 /// 16 ///
46 Table createGlobalContext() 17 Table createGlobalContext()
47 { 18 {
48 auto ctx = new Table; 19 auto ctx = new Table;
49 ctx.set("+", "@v", native( (IntValue lhs, IntValue rhs){return new IntVa 20 ctx.set("+", "@v", native( (IntValue lhs, IntValue rhs){return new IntVa
50 ctx.set("-", "@v", native( (IntValue lhs, IntValue rhs){return new IntVa 21 ctx.set("-", "@v", native( (IntValue lhs, IntValue rhs){return new IntVa
51 ctx.set("*", "@v", native( (IntValue lhs, IntValue rhs){return new IntVa 22 ctx.set("*", "@v", native( (IntValue lhs, IntValue rhs){return new IntVa
................................................................................................................................................................................
74 return (x.data == 0 ? fe : ft).call(pos,lay,[]); 45 return (x.data == 0 ? fe : ft).call(pos,lay,[]);
75 throw genex!RuntimeException(pos, "type mismatch in if"); 46 throw genex!RuntimeException(pos, "type mismatch in if");
76 })); 47 }));
77 ctx.set("_isint", "@v", native( (Value v){return new IntValue(BigInt(cas 48 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 49 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 50 ctx.set("_isfun", "@v", native( (Value v){return new IntValue(BigInt(cas
80 ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigI 51 ctx.set("_isundefined", "@v", native( (Value v){return new IntValue(BigI
> 52 ctx.set("_istable", "@v", native( (Value v){return new IntValue(BigInt(c
> 53 ctx.set(".", "@v", native( (Table t, StrValue s){
> 54 return (t.has(s.data, "@v") ? t.get(s.data, "@v") : new UndValue
> 55 }) );
> 56 ctx.set(".?", "@v", native( (Table t, StrValue s){
> 57 return new IntValue(BigInt(t.has(s.data, "@v") ? 1 : 0));
> 58 }) );
> 59 ctx.set(".=", "@v", native( (Table t, StrValue s, Value v){
> 60 auto t2 = new Table(t, Table.Kind.NotPropagateSet);
> 61 t2.set(s.data, "@v", v);
> 62 return t2;
> 63 }) );
> 64 ctx.set("{}", "@v", native( (){
> 65 return new Table;
> 66 }) );
81 return ctx; 67 return ctx;
82 } 68 }
83 69
84 /// Entry point of this module 70 /// Entry point of this module
85 71
86 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 72 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn)
87 { 73 {