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
- branch=trunk inherited from [f65680e1d2]
- sym-trunk inherited from [f65680e1d2]
Changes
Modified polemy/eval.d from [a16d8d322739557e] to [d315cdb0d9e5cf48].
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 }
Modified polemy/parse.d from [8212de2433d6e818] to [7ed0053850ffa032].
72 72 } 73 73 74 74 AST Declaration() // returns null if it is not a declaration 75 75 { 76 76 /// Declaration ::= 77 77 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"in"] Body?)? 78 78 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" Body "}" ([";"|"in"] Body?)? 79 + /// | ["@" "@" Layer "=" Expression ([";"|"in"] Body?)? 80 + /// | ["@" "@" Layer "(" Param%"," ")" "{" Body "}" ([";"|"in"] Body?)? 79 81 80 82 auto pos = currentPosition(); 81 83 string layer = ""; 84 + bool layerRiseDecl = false; 82 85 83 86 if( tryEat("@") ) 84 87 { 85 88 layer = "@" ~ eatId("after @", AllowQuoted); 86 - if( tryEat("(") ) 87 - return null; // @lay(...) expression, not a declaration 88 - } 89 - 90 - string kwd = layer; 91 - if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="var") && !tryEat(kwd="def") ) 92 - return null; // none of {@lay, let, var, def} occurred, it's not a declaration 93 - 94 - auto varpos = currentPosition(); 95 - string var = eatId("after "~kwd, AllowQuoted); // name of the declared variable 96 - 97 - auto e = tryEat("(") 98 - ? parseLambdaAfterOpenParen(varpos) // let var ( ... 99 - : (eat("=", "after "~kwd), E(0)); // let var = ... 100 - 101 - if( moreDeclarationExists() ) 102 - return new LetExpression(pos, var, layer, e, Body()); 89 + if( layer == "@@" ) 90 + { 91 + layer = "@" ~ eatId("after @@", AllowQuoted); 92 + layerRiseDecl = true; 93 + } 94 + else 95 + { 96 + if( tryEat("(") ) 97 + return null; // @lay(...) expression, not a declaration 98 + } 99 + } 100 + 101 + // [TODO] Refactor 102 + if( layerRiseDecl ) 103 + { 104 + string kwd = "@" ~ layer; 105 + string var = layer; 106 + 107 + auto e = tryEat("(") 108 + ? parseLambdaAfterOpenParen(pos) // let var ( ... 109 + : (eat("=", "after "~kwd), E(0)); // let var = ... 110 + if( moreDeclarationExists() ) 111 + return new LetExpression(pos, var, "(system)", e, Body()); 112 + else 113 + return new LetExpression(pos, var, "(system)", e, new VarExpression(pos, var)); 114 + } 103 115 else 104 - return new LetExpression(pos, var, layer, e, new VarExpression(varpos, var)); 116 + { 117 + string kwd = layer; 118 + if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="var") && !tryEat(kwd="def") ) 119 + return null; // none of {@lay, let, var, def} occurred, it's not a declaration 120 + 121 + auto varpos = currentPosition(); 122 + string var = eatId("after "~kwd, AllowQuoted); // name of the declared variable 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, Body()); 129 + else 130 + return new LetExpression(pos, var, layer, e, new VarExpression(varpos, var)); 131 + } 105 132 } 106 133 107 134 AST TopLevelExpression() 108 135 { 109 136 /// TopLevelExpression ::= Expression ([";"|"in"] Body?)? 110 137 111 138 auto pos = currentPosition(); ................................................................................ 404 431 { 405 432 mixin EasyAST; 406 433 assert_eq(parseString(`def foo(x) { x+1 }; foo`), 407 434 let("foo", "", 408 435 fun(["x"], call(var("+"), var("x"), intl(1))), 409 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 }