Differences From Artifact [a16d8d322739557e]:
- File
polemy/eval.d
- 2010-11-13 02:48:58 - part of checkin [1c01f44f52] on branch trunk - simplepatternmatch (user: kinaba) [annotate]
To Artifact [d315cdb0d9e5cf48]:
- File
polemy/eval.d
- 2010-11-13 03:55:17 - part of checkin [c368edbcb1] on branch trunk - @@lay(x) { ... } declaration and value rising. (user: kinaba) [annotate]
111 111 /// lay is the layer ID for evaluation (standard value semantics uses "@v").
112 112 import std.typetuple;
113 113 Value eval(AST e, Table ctx, bool splitCtx, Layer lay)
114 114 {
115 115 return e.match(
116 116 (StrLiteral e)
117 117 {
118 - return new StrValue(e.data);
118 + Value v = new StrValue(e.data);
119 + if( lay == "@v" )
120 + return v;
121 + else
122 + return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v", [v]);
119 123 },
120 124 (IntLiteral e)
121 125 {
122 - return new IntValue(e.data);
126 + Value v = new IntValue(e.data);
127 + if( lay == "@v" )
128 + return v;
129 + else // are these "@v"s appropriate???
130 + return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v", [v]);
123 131 },
124 132 (VarExpression e)
125 133 {
126 - return ctx.get(e.var, lay, e.pos);
134 + try {
135 + return ctx.get(e.var, lay, e.pos);
136 + } catch( RuntimeException ) {
137 + // rise
138 + return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v",
139 + [ctx.get(e.var, "@v", e.pos)]
140 + );
141 + }
127 142 },
128 143 (LayeredExpression e)
129 144 {
130 145 return eval(e.expr, ctx, false, e.lay);
131 146 },
132 147 (LetExpression e)
133 148 {
................................................................................
214 229 { fib(x-1) + fib(x-2); };
215 230 };
216 231 fib(10);`).val, new IntValue(BigInt(89)));
217 232 }
218 233
219 234 unittest
220 235 {
221 - assert_throw!Throwable( evalString(`@s "+"=fun(x,y){x-y};@s(1+2)`) );
222 - assert_eq( evalString(`@s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigInt(3)) );
223 - assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) );
224 - assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, new IntValue(BigInt(-1)) );
236 + assert_throw!Throwable( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};@s(1+2)`) );
237 + assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigInt(3)) );
238 + assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) );
239 + assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, new IntValue(BigInt(-1)) );
240 +}
241 +
242 +unittest
243 +{
244 + assert_eq( evalString(`@@t = fun(x){x+1}; @t(123)`).val, new IntValue(BigInt(124)) );
225 245 }