Differences From Artifact [62b3543f769fc9c9]:
- File
polemy/eval.d
- 2010-11-09 14:24:09 - part of checkin [2459e9a821] on branch trunk - refactored eof-driven REPL (user: kinaba) [annotate]
To Artifact [050eab1743a45f8b]:
- File
polemy/eval.d
- 2010-11-09 14:59:36 - part of checkin [7465fcdd7f] on branch trunk - layered function invocation (user: kinaba) [annotate]
13 13 import std.typecons;
14 14 import std.stdio;
15 15
16 16 Table createGlobalContext()
17 17 {
18 18 auto ctx = new Table;
19 19 // [TODO] autogenerate these typechecks
20 - ctx.set("+", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
20 + ctx.set("+", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
21 21 if( args.length != 2 )
22 - throw new RuntimeException(pos, "+ takes two arguments!!");
22 + throw genex!RuntimeException(pos, "+ takes two arguments!!");
23 23 if( auto x = cast(IntValue)args[0] )
24 24 if( auto y = cast(IntValue)args[1] )
25 25 return new IntValue(x.data+y.data);
26 - throw new RuntimeException(pos, "cannot add non-integers");
26 + throw genex!RuntimeException(pos, "cannot add non-integers");
27 27 }));
28 - ctx.set("-", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
28 + ctx.set("-", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
29 29 if( args.length != 2 )
30 - throw new RuntimeException(pos, "- takes two arguments!!");
30 + throw genex!RuntimeException(pos, "- takes two arguments!!");
31 31 if( auto x = cast(IntValue)args[0] )
32 32 if( auto y = cast(IntValue)args[1] )
33 33 return new IntValue(x.data-y.data);
34 - throw new RuntimeException(pos, "cannot subtract non-integers");
34 + throw genex!RuntimeException(pos, "cannot subtract non-integers");
35 35 }));
36 - ctx.set("*", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
36 + ctx.set("*", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
37 37 if( args.length != 2 )
38 - throw new RuntimeException(pos, "* takes two arguments!!");
38 + throw genex!RuntimeException(pos, "* takes two arguments!!");
39 39 if( auto x = cast(IntValue)args[0] )
40 40 if( auto y = cast(IntValue)args[1] )
41 41 return new IntValue(x.data*y.data);
42 - throw new RuntimeException(pos, "cannot multiply non-integers");
42 + throw genex!RuntimeException(pos, "cannot multiply non-integers");
43 43 }));
44 - ctx.set("/", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
44 + ctx.set("/", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
45 45 if( args.length != 2 )
46 - throw new RuntimeException(pos, "/ takes two arguments!!");
46 + throw genex!RuntimeException(pos, "/ takes two arguments!!");
47 47 if( auto x = cast(IntValue)args[0] )
48 48 if( auto y = cast(IntValue)args[1] )
49 49 return new IntValue(x.data/y.data);
50 - throw new RuntimeException(pos, "cannot divide non-integers");
50 + throw genex!RuntimeException(pos, "cannot divide non-integers");
51 51 }));
52 - ctx.set("<", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
52 + ctx.set("<", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
53 53 if( args.length != 2 )
54 - throw new RuntimeException(pos, "< takes two arguments!!");
54 + throw genex!RuntimeException(pos, "< takes two arguments!!");
55 55 if( auto x = cast(IntValue)args[0] )
56 56 if( auto y = cast(IntValue)args[1] )
57 57 return new IntValue(BigInt(to!int(x.data < y.data)));
58 - throw new RuntimeException(pos, "cannot compare non-integers");
58 + throw genex!RuntimeException(pos, "cannot compare non-integers");
59 59 }));
60 - ctx.set(">", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
60 + ctx.set(">", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
61 61 if( args.length != 2 )
62 - throw new RuntimeException(pos, "> takes two arguments!!");
62 + throw genex!RuntimeException(pos, "> takes two arguments!!");
63 63 if( auto x = cast(IntValue)args[0] )
64 64 if( auto y = cast(IntValue)args[1] )
65 65 return new IntValue(BigInt(to!int(x.data>y.data)));
66 - throw new RuntimeException(pos, "cannot compare non-integers");
66 + throw genex!RuntimeException(pos, "cannot compare non-integers");
67 67 }));
68 - ctx.set("print", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
68 + ctx.set("print", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
69 69 foreach(a; args)
70 70 write(a);
71 71 writeln("");
72 72 return new IntValue(BigInt(178));
73 73 }));
74 - ctx.set("if", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
74 + ctx.set("if", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
75 75 if( args.length != 3 )
76 - throw new RuntimeException(pos, "if takes three arguments!!");
76 + throw genex!RuntimeException(pos, "if takes three arguments!!");
77 77 if( auto x = cast(IntValue)args[0] )
78 78 if( auto ft = cast(FunValue)args[1] )
79 79 if( auto fe = cast(FunValue)args[2] )
80 - return (x.data == 0 ? fe : ft).call(pos,[]);
81 - throw new RuntimeException(pos, "type mismatch in if");
80 + return (x.data == 0 ? fe : ft).call(pos,lay,[]);
81 + throw genex!RuntimeException(pos, "type mismatch in if");
82 82 }));
83 83 return ctx;
84 84 }
85 85
86 86 /// Entry point of this module
87 87
88 88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn)
................................................................................
94 94 {
95 95 return eval( polemy.parse.parseFile(filename, ln_cn) );
96 96 }
97 97
98 98 Tuple!(Value,"val",Table,"ctx") eval(AST e)
99 99 {
100 100 Table ctx = createGlobalContext();
101 - return typeof(return)(eval(e, ctx), ctx);
101 + return typeof(return)(eval(e, ctx, false, "@v"), ctx);
102 102 }
103 103
104 -Value eval(AST _e, Table ctx, bool splitCtx = false, Layer lay="@val")
104 +Value eval(AST _e, Table ctx, bool splitCtx, Layer lay)
105 105 {
106 106 if( auto e = cast(StrLiteral)_e )
107 107 {
108 108 return new StrValue(e.data);
109 109 }
110 110 else
111 111 if( auto e = cast(IntLiteral)_e )
................................................................................
122 122 {
123 123 return eval(e.expr, ctx, false, e.lay);
124 124 }
125 125 else
126 126 if( auto e = cast(LetExpression)_e )
127 127 {
128 128 // for letrec, we need this, but should avoid overwriting????
129 - // ctx.set(e.var, "@val", new UndefinedValue, e.pos);
130 - Value v = eval(e.init, ctx, true);
129 + // ctx.set(e.var, "@v", new UndefinedValue, e.pos);
130 + Value v = eval(e.init, ctx, true, lay);
131 131 if(splitCtx)
132 132 ctx = new Table(ctx, Table.Kind.NotPropagateSet);
133 133 ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.pos);
134 - return eval(e.expr, ctx);
134 + return eval(e.expr, ctx, false, lay);
135 135 }
136 136 else
137 137 if( auto e = cast(FuncallExpression)_e )
138 138 {
139 - Value _f = eval(e.fun, ctx);
139 + Value _f = eval(e.fun, ctx, true, lay);
140 140 if( auto f = cast(FunValue)_f ) {
141 141 Value[] args;
142 142 foreach(a; e.args)
143 - args ~= eval(a, ctx);
144 - return f.call(e.pos, args);
143 + args ~= eval(a, ctx, true, lay);
144 + return f.call(e.pos, lay, args);
145 145 } else
146 - throw new RuntimeException(e.pos, "Non-funcion is applied");
146 + throw genex!RuntimeException(e.pos, "Non-funcion is applied");
147 147 }
148 148 else
149 149 if( auto e = cast(FunLiteral)_e )
150 150 {
151 - return new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
151 + return new FunValue(delegate Value(immutable LexPosition pos, string lay, Value[] args){
152 152 if( e.params.length != args.length )
153 - throw new RuntimeException(e.pos, sprintf!"Argument Number Mismatch (%d required but %d given)"
153 + throw genex!RuntimeException(e.pos, sprintf!"Argument Number Mismatch (%d required but %d given)"
154 154 (e.params.length, args.length));
155 155 Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet);
156 156 foreach(i,p; e.params)
157 - ctxNeo.set(p, "@val", args[i]);
158 - return eval(e.funbody, ctxNeo);
157 + ctxNeo.set(p, lay, args[i]);
158 + return eval(e.funbody, ctxNeo, true, lay);
159 159 });
160 160 }
161 - throw new RuntimeException(_e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(_e)));
161 + throw genex!RuntimeException(_e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(_e)));
162 162 }
163 163
164 164 unittest
165 165 {
166 166 auto r = assert_nothrow( evalString(`var x = 21; x + x*x;`) );
167 167 assert_eq( r.val, new IntValue(BigInt(21+21*21)) );
168 - assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21)) );
169 - assert_nothrow( r.ctx.get("x","@val") );
170 - assert_throw!RuntimeException( r.ctx.get("y","@val") );
168 + assert_eq( r.ctx.get("x","@v"), new IntValue(BigInt(21)) );
169 + assert_nothrow( r.ctx.get("x","@v") );
170 + assert_throw!RuntimeException( r.ctx.get("y","@v") );
171 171 }
172 172 unittest
173 173 {
174 174 auto r = assert_nothrow( evalString(`var x = 21; var x = x + x*x;`) );
175 175 assert_eq( r.val, new IntValue(BigInt(21+21*21)) );
176 - assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21+21*21)) );
177 - assert_nothrow( r.ctx.get("x","@val") );
178 - assert_throw!RuntimeException( r.ctx.get("y","@val") );
176 + assert_eq( r.ctx.get("x","@v"), new IntValue(BigInt(21+21*21)) );
177 + assert_nothrow( r.ctx.get("x","@v") );
178 + assert_throw!RuntimeException( r.ctx.get("y","@v") );
179 179 }
180 180 unittest
181 181 {
182 182 assert_eq( evalString(`let x=1; let y=(let x=2); x`).val, new IntValue(BigInt(1)) );
183 183 assert_eq( evalString(`let x=1; let y=(let x=2;fun(){x}); y()`).val, new IntValue(BigInt(2)) );
184 184 }
185 185 unittest
................................................................................
203 203 if(x<2)
204 204 { 1; }
205 205 else
206 206 { fib(x-1) + fib(x-2); };
207 207 };
208 208 fib(10);`).val, new IntValue(BigInt(89)));
209 209 }
210 +
211 +unittest
212 +{
213 + assert_throw!Throwable( evalString(`@s "+"=fun(x,y){x-y};@s(1+2)`) );
214 + assert_eq( evalString(`@s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigInt(3)) );
215 + assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) );
216 + assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, new IntValue(BigInt(-1)) );
217 +}