Overview
SHA1 Hash: | 0f02103885bdb17f3e5c15aa69c4c308e37f8157 |
---|---|
Date: | 2010-11-09 16:27:21 |
User: | kinaba |
Comment: | let, var, def became layer-neutral definition (not @val). scope splitting (let x=1;let x=2;let y=(let x=3);x is 1) is correctly implemented now. |
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 [c3d65954914654a5] to [73d121b46bcd4718].
97 97 98 98 Tuple!(Value,"val",Table,"ctx") eval(AST e) 99 99 { 100 100 Table ctx = createGlobalContext(); 101 101 return typeof(return)(eval(e, ctx), ctx); 102 102 } 103 103 104 -Value eval(AST _e, Table ctx, bool splitCtx = true) 104 +Value eval(AST _e, Table ctx, bool splitCtx = false) 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 ) ................................................................................ 119 119 } 120 120 else 121 121 if( auto e = cast(LetExpression)_e ) 122 122 { 123 123 // for letrec, we need this, but should avoid overwriting???? 124 124 // ctx.set(e.var, "@val", new UndefinedValue, e.pos); 125 125 Value v = eval(e.init, ctx, true); 126 + if(splitCtx) 127 + ctx = new Table(ctx, Table.Kind.NotPropagateSet); 126 128 ctx.set(e.var, "@val", v, e.pos); 127 129 return eval(e.expr, ctx); 128 130 } 129 131 else 130 132 if( auto e = cast(FuncallExpression)_e ) 131 133 { 132 134 Value _f = eval(e.fun, ctx); ................................................................................ 171 173 assert_throw!RuntimeException( r.ctx.get("y","@val") ); 172 174 } 173 175 unittest 174 176 { 175 177 assert_nothrow( evalString(`print("Hello, world!");`) ); 176 178 assert_nothrow( evalString(`print(fun(){});`) ); 177 179 } 180 +unittest 181 +{ 182 + assert_eq( evalString(`let x=1; let y=(let x=2); x`).val, new IntValue(BigInt(1)) ); 183 + assert_eq( evalString(`let x=1; let y=(let x=2;fun(){x}); y()`).val, new IntValue(BigInt(2)) ); 184 +} 178 185 unittest 179 186 { 180 187 assert_nothrow( evalString(`var fac = fun(x){ 181 188 1; 182 189 }; 183 190 print(fac(3));`)); 184 191 assert_nothrow( evalString(`var fac = fun(x){
Modified polemy/parse.d from [5f90478aa23ae726] to [e14df84da140adf3].
64 64 if( tryEat("let") || tryEat("var") || tryEat("def") || tryEat("@") ) 65 65 { 66 66 if( kwd == "@" ) 67 67 kwd ~= eatId("after @"); 68 68 immutable LexPosition varpos = (lex.empty ? null : lex.front.pos); 69 69 string var = eatId("after "~kwd); 70 70 eat("=", "after "~kwd); 71 - kwd = (kwd[0]=='@' ? kwd : "@val"); 71 + kwd = (kwd[0]=='@' ? kwd : ""); // "let, var, def ==> neutral layer" 72 72 auto e = E(0); 73 73 if( tryEat(";") && !lex.empty && (lex.front.quoted || (lex.front.str!="}" && lex.front.str!=")")) ) 74 74 return new LetExpression(pos, var, kwd, e, Body()); 75 75 else 76 76 return new LetExpression(pos, var, kwd, e, new VarExpression(varpos, var)); 77 77 } 78 78 else ................................................................................ 259 259 assert_eq(parseString(`"foo"`), strl("foo")); 260 260 assert_eq(parseString(`fun(){1}`), fun([],intl(1))); 261 261 assert_eq(parseString(`fun(x){1}`), fun(["x"],intl(1))); 262 262 assert_eq(parseString("\u03BB(){1}"), fun([],intl(1))); 263 263 assert_eq(parseString("\u03BB(x){1}"), fun(["x"],intl(1))); 264 264 assert_eq(parseString(`1;2`), let("_","@val",intl(1),intl(2))); 265 265 assert_eq(parseString(`1;2;`), let("_","@val",intl(1),intl(2))); 266 - assert_eq(parseString(`let x=1;2`), let("x","@val",intl(1),intl(2))); 267 - assert_eq(parseString(`var x=1;2;`), let("x","@val",intl(1),intl(2))); 268 - assert_eq(parseString(`def x=1`), let("x","@val",intl(1),var("x"))); 266 + assert_eq(parseString(`let x=1;2`), let("x","",intl(1),intl(2))); 267 + assert_eq(parseString(`var x=1;2;`), let("x","",intl(1),intl(2))); 268 + assert_eq(parseString(`def x=1`), let("x","",intl(1),var("x"))); 269 269 assert_eq(parseString(`@val x=1;`), let("x","@val",intl(1),var("x"))); 270 270 assert_eq(parseString(`@typ x="#int";`), let("x","@typ",strl("#int"),var("x"))); 271 271 assert_eq(parseString(`f(1,2)`), call(var("f"),intl(1),intl(2))); 272 272 assert_eq(parseString(`if(1){2}`), call(var("if"),intl(1),fun([],intl(2)),fun([],intl(178)))); 273 273 assert_eq(parseString(`if(1){2}else{3}`), call(var("if"),intl(1),fun([],intl(2)),fun([],intl(3)))); 274 274 assert_eq(parseString(`if(1){}else{3}()()`), 275 275 call(call(call(var("if"),intl(1),fun([],intl(178)),fun([],intl(3)))))); ................................................................................ 279 279 assert_eq(parseString(`1*2+3`), call(var("+"),call(var("*"),intl(1),intl(2)),intl(3))); 280 280 281 281 assert_eq(parseString(` 282 282 let x = 100; #comment 283 283 let y = 200; #comment!!!!! 284 284 x+y 285 285 `), 286 - let("x", "@val", intl(100), let("y", "@val", intl(200), call(var("+"), var("x"), var("y")))) 286 + let("x", "", intl(100), let("y", "", intl(200), call(var("+"), var("x"), var("y")))) 287 287 ); 288 288 289 289 assert_eq(parseString(` 290 290 var fac = fun(x){ if(x <= 1) {1} else {x*fac(x-1)} }; 291 291 fac(10) 292 292 `), 293 - let("fac", "@val", fun(["x"], 293 + let("fac", "", fun(["x"], 294 294 call(var("if"), 295 295 call(var("<="), var("x"), intl(1)), 296 296 fun([], intl(1)), 297 297 fun([], call(var("*"), var("x"), call(var("fac"),call(var("-"),var("x"),intl(1))))) 298 298 )), 299 299 call(var("fac"),intl(10)) 300 300 )
Modified tricks/test.d from [de7560f1ecccee67] to [314aad0104b85557].
54 54 { 55 55 void assertOp(A, B, string fn=__FILE__, size_t ln=__LINE__)(A a, B b, string msg="") 56 56 { 57 57 try 58 58 { if( mixin("a"~op~"b") ) return; } 59 59 catch(Throwable e) 60 60 { onAssertErrorMsg(fn, ln, msg.length ? msg : "exception ["~e.toString()~"]"); } 61 - onAssertErrorMsg(fn, ln, msg.length ? msg : to!string(a)~" !"~op~to!string(b)); 61 + onAssertErrorMsg(fn, ln, msg.length ? msg : to!string(a)~" !"~op~" "~to!string(b)); 62 62 } 63 63 } 64 64 65 65 alias assertOp!(`==`) assert_eq; 66 66 alias assertOp!(`!=`) assert_ne; 67 67 alias assertOp!(`<`) assert_lt; 68 68 alias assertOp!(`<=`) assert_le;