Check-in [060f267779]
Not logged in
Overview
SHA1 Hash:060f267779a02b1c101fec939083fac0feffaabf
Date: 2010-11-20 23:29:49
User: kinaba
Comment:fixed the bug that the first declaration inside a function cannt be recursive: def foo() { def bar() { bar() }; bar() }; foo() # bar cannot be found
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 [84ee94a6f293fac0] to [360da420d41dfbbf].

126 126 } 127 127 }, 128 128 (LayeredExpression e) 129 129 { 130 130 if( e.lay == "@macro" ) 131 131 return macroEval(e.expr, ctx, false); 132 132 else 133 - return eval(e.expr, ctx, false, e.lay); 133 + return eval(e.expr, ctx, true, e.lay); 134 134 }, 135 135 (LetExpression e) 136 136 { 137 137 // for letrec, we need this, but should avoid overwriting???? 138 138 // ctx.set(e.var, "@v", new UndefinedValue, e.pos); 139 - Value v = eval(e.init, ctx, true, lay); 140 139 if(splitCtx) 141 140 ctx = new Table(ctx, Table.Kind.NotPropagateSet); 141 + Value v = eval(e.init, ctx, true, lay); 142 142 ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.pos); 143 143 return eval(e.expr, ctx, false, lay); 144 144 }, 145 145 (FuncallExpression e) 146 146 { 147 147 Value _f = eval(e.fun, ctx, true, lay); 148 148 if( auto f = cast(FunValue)_f ) { ................................................................................ 379 379 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new IntValue(BigInt(3)) ); 380 380 assert_eq( evalString(`@@s(x){x}; @s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, new IntValue(BigInt(-1)) ); 381 381 } 382 382 383 383 unittest 384 384 { 385 385 assert_eq( evalString(`@@t = fun(x){x+1}; @t(123)`).val, new IntValue(BigInt(124)) ); 386 + // there was a bug that declaration in the first line of function definition 387 + // cannot be recursive 388 + assert_nothrow( evalString(`def foo() { 389 + def bar(y) { if(y<1) {0} else {bar(0)} }; 390 + bar(1) 391 +}; foo()`) ); 386 392 }

Added sample/macro.pmy version [827dbc8186458d43]

1 +@macro twice(x) { x; x }; 2 +@macro max(x,y) { 3 + var _x = x; # no hygenic macro btw.... 4 + var _y = y; # no hygenic macro btw.... 5 + if(_x<_y){_y}else{_x} 6 +}; 7 +def maxNormal(x,y) { 8 + if(x<y){y}else{x} 9 +}; 10 +@macro maxBad(x,y) { 11 + if(x<y){y}else{x} 12 +}; 13 + 14 +@macro LetItBe(x, y) { 15 + let it = x in y 16 +}; 17 + 18 +@macro pow10(x) { 19 + @v( 20 + def pow(x, n) { 21 + if( n == 1 ) { x } 22 + else { 23 + @macro( @v(x) * @v(pow(x,n-1)) ) 24 + } 25 + } 26 + in 27 + pow(@macro(x),10) 28 + ) 29 +}; 30 + 31 +def printAndReturn(x) 32 +{ 33 + print(x); 34 + x 35 +}; 36 + 37 + 38 + 39 + 40 + 41 +def main() 42 +{ 43 + twice( print("foo") ); 44 + print("--------------"); 45 + print(max(printAndReturn(100),printAndReturn(200))); 46 + print("--------------"); 47 + print(maxNormal(printAndReturn(100),printAndReturn(200))); 48 + print("--------------"); 49 + print(maxBad(printAndReturn(100),printAndReturn(200))); 50 + print("--------------"); 51 + print( LetItBe( 1+2+3, it*it ) ); 52 + print("--------------"); 53 + print(pow10(2)); 54 + 55 +}; 56 + 57 +main()