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 /// lay is the layer ID for evaluation (standard value semantics uses "@v"). 111 /// lay is the layer ID for evaluation (standard value semantics uses "@v").
112 import std.typetuple; 112 import std.typetuple;
113 Value eval(AST e, Table ctx, bool splitCtx, Layer lay) 113 Value eval(AST e, Table ctx, bool splitCtx, Layer lay)
114 { 114 {
115 return e.match( 115 return e.match(
116 (StrLiteral e) 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
119 }, 123 },
120 (IntLiteral e) 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
123 }, 131 },
124 (VarExpression e) 132 (VarExpression e)
125 { 133 {
> 134 try {
126 return ctx.get(e.var, lay, e.pos); | 135 return ctx.get(e.var, lay, e.pos);
> 136 } catch( RuntimeException ) {
> 137 // rise
> 138 return (cast(FunValue)ctx.get(lay, "(system)", e
> 139 [ctx.get(e.var, "@v", e.pos)]
> 140 );
> 141 }
127 }, 142 },
128 (LayeredExpression e) 143 (LayeredExpression e)
129 { 144 {
130 return eval(e.expr, ctx, false, e.lay); 145 return eval(e.expr, ctx, false, e.lay);
131 }, 146 },
132 (LetExpression e) 147 (LetExpression e)
133 { 148 {
................................................................................................................................................................................
214 { fib(x-1) + fib(x-2); }; 229 { fib(x-1) + fib(x-2); };
215 }; 230 };
216 fib(10);`).val, new IntValue(BigInt(89))); 231 fib(10);`).val, new IntValue(BigInt(89)));
217 } 232 }
218 233
219 unittest 234 unittest
220 { 235 {
221 assert_throw!Throwable( evalString(`@s "+"=fun(x,y){x-y};@s(1+2)`) ); | 236 assert_throw!Throwable( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};@s(1
222 assert_eq( evalString(`@s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigI | 237 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){x-y};1+2`).val, new In
223 assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new I | 238 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`)
224 assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, n | 239 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+
> 240 }
> 241
> 242 unittest
> 243 {
> 244 assert_eq( evalString(`@@t = fun(x){x+1}; @t(123)`).val, new IntValue(Bi
225 } 245 }