Check-in [c368edbcb1]
Not logged in
Overview
SHA1 Hash:c368edbcb1c2e20563329d605819ba9506d8ddf4
Date: 2010-11-13 12:55:17
User: kinaba
Comment:@@lay(x) { ... } declaration and value rising.
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified polemy/eval.d from [a16d8d322739557e] to [d315cdb0d9e5cf48].

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 }

Modified polemy/parse.d from [8212de2433d6e818] to [7ed0053850ffa032].

72 } 72 } 73 73 74 AST Declaration() // returns null if it is not a declaration 74 AST Declaration() // returns null if it is not a declaration 75 { 75 { 76 /// Declaration ::= 76 /// Declaration ::= 77 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"i 77 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"i 78 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" B 78 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" B > 79 /// | ["@" "@" Layer "=" Expression ([";"|"in"] Body?)? > 80 /// | ["@" "@" Layer "(" Param%"," ")" "{" Body "}" ([";"|"in"] 79 81 80 auto pos = currentPosition(); 82 auto pos = currentPosition(); 81 string layer = ""; 83 string layer = ""; > 84 bool layerRiseDecl = false; 82 85 83 if( tryEat("@") ) 86 if( tryEat("@") ) 84 { 87 { 85 layer = "@" ~ eatId("after @", AllowQuoted); 88 layer = "@" ~ eatId("after @", AllowQuoted); > 89 if( layer == "@@" ) > 90 { > 91 layer = "@" ~ eatId("after @@", AllowQuoted); > 92 layerRiseDecl = true; > 93 } > 94 else > 95 { 86 if( tryEat("(") ) | 96 if( tryEat("(") ) 87 return null; // @lay(...) expression, not a decl | 97 return null; // @lay(...) expression, no 88 } | 98 } 89 | 99 } 90 string kwd = layer; < 91 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="var") && ! < 92 return null; // none of {@lay, let, var, def} occurred, < 93 | 100 94 auto varpos = currentPosition(); < 95 string var = eatId("after "~kwd, AllowQuoted); // name of the de < > 101 // [TODO] Refactor > 102 if( layerRiseDecl ) 96 | 103 { > 104 string kwd = "@" ~ layer; > 105 string var = layer; > 106 97 auto e = tryEat("(") | 107 auto e = tryEat("(") 98 ? parseLambdaAfterOpenParen(varpos) // let var ( ... | 108 ? parseLambdaAfterOpenParen(pos) // let var ( . 99 : (eat("=", "after "~kwd), E(0)); // let var = ... | 109 : (eat("=", "after "~kwd), E(0)); // let var = . 100 < 101 if( moreDeclarationExists() ) | 110 if( moreDeclarationExists() ) 102 return new LetExpression(pos, var, layer, e, Body()); | 111 return new LetExpression(pos, var, "(system)", e > 112 else > 113 return new LetExpression(pos, var, "(system)", e > 114 } 103 else 115 else > 116 { > 117 string kwd = layer; > 118 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="va > 119 return null; // none of {@lay, let, var, def} oc > 120 > 121 auto varpos = currentPosition(); > 122 string var = eatId("after "~kwd, AllowQuoted); // name o > 123 > 124 auto e = tryEat("(") > 125 ? parseLambdaAfterOpenParen(varpos) // let var > 126 : (eat("=", "after "~kwd), E(0)); // let var > 127 if( moreDeclarationExists() ) > 128 return new LetExpression(pos, var, layer, e, Bod > 129 else 104 return new LetExpression(pos, var, layer, e, new VarExpr | 130 return new LetExpression(pos, var, layer, e, new > 131 } 105 } 132 } 106 133 107 AST TopLevelExpression() 134 AST TopLevelExpression() 108 { 135 { 109 /// TopLevelExpression ::= Expression ([";"|"in"] Body?)? 136 /// TopLevelExpression ::= Expression ([";"|"in"] Body?)? 110 137 111 auto pos = currentPosition(); 138 auto pos = currentPosition(); ................................................................................................................................................................................ 404 { 431 { 405 mixin EasyAST; 432 mixin EasyAST; 406 assert_eq(parseString(`def foo(x) { x+1 }; foo`), 433 assert_eq(parseString(`def foo(x) { x+1 }; foo`), 407 let("foo", "", 434 let("foo", "", 408 fun(["x"], call(var("+"), var("x"), intl(1))), 435 fun(["x"], call(var("+"), var("x"), intl(1))), 409 var("foo")) 436 var("foo")) 410 ); 437 ); > 438 > 439 assert_eq(parseString(`@@type ( x ) { x }`), > 440 let("@type", "(system)", fun(["x"], var("x")), var("@type")) ); 411 } 441 }