Check-in [153a14cec0]
Not logged in
Overview
SHA1 Hash:153a14cec0b8a0678e9bebcd4832aa2bb08c8854
Date: 2010-11-24 20:20:42
User: kinaba
Comment:if-then-else without {}s. some cosmetic changes
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 [e0978d70034e5fac] to [4eba423d1304ad73].

68 68 assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not defined")); 69 69 } 70 70 71 71 Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 72 72 { 73 73 if( isASTLayer(lay) ) 74 74 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 75 - if( lay==ValueLayer ) 76 - return new StrValue(e.data); 77 - return lift(new StrValue(e.data), lay, ctx, e.pos); 75 + if( isUserDefinedLayer(lay) ) 76 + return lift(new StrValue(e.data), lay, ctx, e.pos); 77 + return new StrValue(e.data); 78 78 } 79 79 80 80 Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 81 81 { 82 82 if( isASTLayer(lay) ) 83 83 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 84 - if( lay==ValueLayer ) 85 - return new IntValue(e.data); 86 - return lift(new IntValue(e.data), lay, ctx, e.pos); 84 + if( isUserDefinedLayer(lay) ) 85 + return lift(new IntValue(e.data), lay, ctx, e.pos); 86 + return new IntValue(e.data); 87 87 } 88 88 89 89 Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 90 90 { 91 91 if( isASTLayer(lay) ) 92 92 if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) ) 93 93 return ctx.get(e.name, MacroLayer, e.pos); 94 94 else 95 95 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 96 - if( lay==ValueLayer || ctx.has(e.name, lay) ) 97 - return ctx.get(e.name, lay, e.pos); 98 - return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx, e.pos); 96 + if( isUserDefinedLayer(lay) && !ctx.has(e.name, lay) ) 97 + return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx, e.pos); 98 + return ctx.get(e.name, lay, e.pos); 99 99 } 100 100 101 101 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 102 102 { 103 103 Value f = eval( e.fun, lay, ctx ); 104 104 if( isASTLayer(lay) ) { 105 105 auto ff = cast(FunValue)f; ................................................................................ 134 134 } 135 135 136 136 Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 137 137 { 138 138 Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.NotPropagateSet); 139 139 if( isASTLayer(lay) ) 140 140 return ast2table(e, (AST ee){ 141 - if(ee is e.expr) // need this for correct scoping (outer scope macro variables must be hidden!) 141 + // need this for correct scoping (outer scope macro variables must be hidden!) 142 + if(ee is e.expr) 142 143 newCtx.set(e.name, ValueLayer, new UndefinedValue); 143 144 return eval(ee,lay,newCtx); 144 145 }); 145 146 else 146 147 { 147 148 Value ri = eval(e.init, lay, newCtx); 148 149 newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri); ................................................................................ 251 252 { 252 253 Fun ast; 253 254 Table defCtx; 254 255 override const(Parameter[]) params() { return ast.params; } 255 256 override Table definitionContext() { return defCtx; } 256 257 257 258 this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx=defCtx; } 258 - override string toString() const { return sprintf!"(function:%x:%x)"(cast(void*)ast, cast(void*)defCtx); } 259 + override string toString() const 260 + { return sprintf!"(function:%x:%x)"(cast(void*)ast, cast(void*)defCtx); } 259 261 override int opCmp(Object rhs) { 260 262 if(auto r = cast(UserDefinedFunValue)rhs) { 261 - if(auto i = this.ast.opCmp(r.ast)) 262 - return i; 263 + auto a = cast(void*)this.ast; 264 + auto b = cast(void*)r.ast; 265 + if(a<b) return -1; 266 + if(a>b) return +1; // [TODO] avoid using pointer value... 263 267 return this.defCtx.opCmp(r.defCtx); 264 268 } 265 - if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r)); 269 + if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r)); 266 270 throw genex!RuntimeException("comparison with value and something other"); 267 271 } 268 272 mixin SimpleToHash; 269 273 270 274 AST afterMacroAST; 271 275 override Value invoke(Layer lay, Table ctx, LexPosition pos) 272 276 { ................................................................................ 294 298 override const(Parameter[]) params() { return params_data; } 295 299 override Table definitionContext() { return theContext; } 296 300 297 301 override string toString() { return sprintf!"(native:%x)"(dg.funcptr); } 298 302 override int opCmp(Object rhs) { 299 303 if(auto r = cast(NativeFunValue)rhs) return typeid(typeof(dg)).compare(&dg,&r.dg); 300 304 if(auto r = cast(Value)rhs) return typeid(this).opCmp(typeid(r)); 301 - throw genex!RuntimeException(LexPosition.dummy, "comparison with value and something other"); 305 + throw genex!RuntimeException("comparison with value and something other"); 302 306 } 303 307 mixin SimpleToHash; 304 308 305 309 R delegate(T) dg; 306 310 Parameter[] params_data; 307 311 308 312 this(R delegate(T) dg) ................................................................................ 311 315 foreach(i, Ti; T) 312 316 params_data ~= new Parameter(text(i), []); 313 317 } 314 318 315 319 override Value invoke(Layer lay, Table ctx, LexPosition pos) 316 320 { 317 321 if( lay != defLay ) 318 - throw genex!RuntimeException(pos, text("only ", defLay, " layer can call native function: ", name)); 322 + throw genex!RuntimeException(pos, 323 + text("only ", defLay, " layer can call native function: ", name)); 319 324 T typed_args; 320 325 foreach(i, Ti; T) { 321 326 typed_args[i] = cast(Ti) ctx.get(text(i), ValueLayer, pos); 322 327 if( typed_args[i] is null ) 323 - throw genex!RuntimeException(pos, sprintf!"type mismatch on the argument %d of native function: %s"(i+1,name)); 328 + throw genex!RuntimeException(pos, 329 + sprintf!"type mismatch on the argument %d of native function: %s"(i+1,name)); 324 330 } 325 331 try { 326 332 return dg(typed_args); 327 333 } catch( RuntimeException e ) { 328 334 throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e; 329 335 } 330 336 }

Modified polemy/layer.d from [ec0e00bb40d202e7] to [70a85e9a66eddacd].

13 13 14 14 enum : Layer 15 15 { 16 16 SystemLayer = "(system)", /// Predefined layer for internal data 17 17 ValueLayer = "@value", /// Predefined layer for normal run 18 18 MacroLayer = "@macro", /// Predefined layer for macro run (@lay() changes layer) 19 19 RawMacroLayer = "(rawmacro)", /// Predefined layer for macro run (@lay() becomes AST) 20 - AstLayer = "(ast)", /// Predefined layer for macro run (never invoke macro) 20 +} 21 + 22 +/// True if it is a user-defined layer 23 + 24 +bool isUserDefinedLayer( Layer lay ) 25 +{ 26 + return lay!=SystemLayer && lay!=ValueLayer && lay!=MacroLayer && lay!=RawMacroLayer; 27 +} 28 + 29 +unittest 30 +{ 31 + assert( !isUserDefinedLayer(SystemLayer) ); 32 + assert( !isUserDefinedLayer(ValueLayer) ); 33 + assert( !isUserDefinedLayer(MacroLayer) ); 34 + assert( !isUserDefinedLayer(RawMacroLayer) ); 35 + assert( isUserDefinedLayer("@foo") ); 21 36 } 22 37 23 38 /// True if it is macro-like layer that basically generates syntax tree 24 39 25 40 bool isASTLayer( Layer lay ) 26 41 { 27 - return lay==MacroLayer || lay==RawMacroLayer || lay==AstLayer; 42 + return lay==MacroLayer || lay==RawMacroLayer; 28 43 } 29 44 30 45 unittest 31 46 { 32 47 assert( !isASTLayer(SystemLayer) ); 33 48 assert( !isASTLayer(ValueLayer) ); 34 49 assert( isASTLayer(MacroLayer) ); 35 50 assert( isASTLayer(RawMacroLayer) ); 36 - assert( isASTLayer(AstLayer) ); 37 51 } 38 52 39 53 /// True if in the specified layer @lay(...) has no effect and merely produces a syntax tree 40 54 41 55 bool isNoLayerChangeLayer( Layer lay ) 42 56 { 43 - return lay==RawMacroLayer || lay==AstLayer; 57 + return lay==RawMacroLayer; 44 58 } 45 59 46 60 unittest 47 61 { 48 62 assert( !isNoLayerChangeLayer(SystemLayer) ); 49 63 assert( !isNoLayerChangeLayer(ValueLayer) ); 50 64 assert( !isNoLayerChangeLayer(MacroLayer) ); 51 65 assert( isNoLayerChangeLayer(RawMacroLayer) ); 52 - assert( isNoLayerChangeLayer(AstLayer) ); 53 66 } 54 67 55 68 /// True if do macro expanstion 56 69 57 70 bool isMacroLayer( Layer lay ) 58 71 { 59 72 return lay==MacroLayer || lay==RawMacroLayer; ................................................................................ 61 74 62 75 unittest 63 76 { 64 77 assert( !isMacroLayer(SystemLayer) ); 65 78 assert( !isMacroLayer(ValueLayer) ); 66 79 assert( isMacroLayer(MacroLayer) ); 67 80 assert( isMacroLayer(RawMacroLayer) ); 68 - assert( !isMacroLayer(AstLayer) ); 69 81 }

Modified polemy/parse.d from [64474993b2d6bb44] to [6c61d557752efef5].

280 280 if( tryEat("{") ) 281 281 { 282 282 AST e = new App(pos, new Var(pos,"{}")); 283 283 return parseTableSetAfterBrace(e); 284 284 } 285 285 if( tryEat("if") ) 286 286 { 287 - eat("(", "after if"); 288 - auto cond = E(0); 289 - eat(")", "after if condition"); 290 - auto thenPos = lex.front.pos; 291 - eat("{", "after if condition"); 292 - auto th = Body(); 293 - eat("}", "after if-then body"); 294 - auto el = doNothingExpression(); 295 - auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos); 296 - if( tryEat("else") ) { 297 - eat("{", "after else"); 298 - el = Body(); 299 - eat("}", "after else body"); 300 - } 301 - return new App(pos, 302 - new Var(pos, "if"), 303 - cond, 304 - new Fun(thenPos, [], th), 305 - new Fun(elsePos, [], el) 306 - ); 287 + return parseIfAfterIf(pos); 307 288 } 308 289 if( tryEat("case") ) 309 290 { 310 291 return parsePatternMatch(pos); 311 292 } 312 293 if( tryEat("fun") || tryEat("\u03BB") ) // lambda!! 313 294 { 314 295 eat("(", "after fun"); 315 296 return parseLambdaAfterOpenParen(pos); 316 297 } 317 298 scope(exit) lex.popFront; 318 299 return new Var(pos, lex.front.str); 319 300 } 301 + 302 + AST parseIfAfterIf(LexPosition pos) 303 + { 304 + eat("(", "after if"); 305 + auto cond = E(0); 306 + eat(")", "after if condition"); 307 + auto thenPos = lex.front.pos; 308 + AST th; 309 + if( tryEat("{") ) { 310 + th = Body(); 311 + eat("}", "after if-then body"); 312 + } else { 313 + th = E(0); 314 + } 315 + auto el = doNothingExpression(); 316 + auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos); 317 + if( tryEat("else") ) 318 + if( tryEat("{") ) { 319 + el = Body(); 320 + eat("}", "after else body"); 321 + } else { 322 + el = E(0); 323 + } 324 + return new App(pos, new Var(pos,"if"), cond, new Fun(thenPos,[],th), new Fun(elsePos,[],el)); 325 + } 320 326 321 327 AST parsePatternMatch(LexPosition pos) 322 328 { 323 329 // case "(" pmExpr ")" CASES 324 330 //==> 325 331 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFirst()) 326 332 eat("(", "after case"); ................................................................................ 665 671 `)); 666 672 assert_nothrow(parseString(` 667 673 case( 1 ) 668 674 when({aaaa:@value(x)}){1} 669 675 when({aaaa:{bbb:_}, ccc:123}){1} 670 676 `)); 671 677 } 678 + 679 +unittest 680 +{ 681 + // test for omitting { .. } 682 + assert_nothrow(parseString(` 683 + if(1) 2 else 3 684 + `)); 685 + assert_nothrow(parseString(` 686 + if(1) x{y:z} else 3 687 + `)); 688 +}

Modified polemy/valueconv.d from [94ac916389440918] to [bca45e855c0f4664].

65 65 LexPosition pos = extractPos(t); 66 66 67 67 StrValue typ = cast(StrValue) t.access!StrValue(ValueLayer, "is"); 68 68 if( typ is null ) 69 69 throw genex!RuntimeException(callpos, text(`Invalid AST (no "is" field): `, _v)); 70 70 71 71 foreach(AT; ListOfASTTypes) 72 - if(typ.data == typeid(AT).name.split(".")[$-1].tolower()) 72 + if(typ.data == typeid(AT).name.split(".")[$-1]) 73 73 { 74 74 typeof(AT.tupleof) mems; 75 75 foreach(i,m; mems) 76 76 { 77 77 string name = AT.tupleof[i].stringof.split(".")[$-1]; 78 78 Value vm = t.access!Value(ValueLayer, name); 79 79 if( vm is null ) ................................................................................ 142 142 } 143 143 else 144 144 static if(is(T : AST)) 145 145 { 146 146 assert( typeid(e) == typeid(T), text("abstracted: ", typeid(e), " vs ", typeid(T)) ); 147 147 auto t = new Table; 148 148 t.set("pos", ValueLayer, ast2table(e.pos,rec)); 149 - t.set("is" , ValueLayer, new StrValue(typeid(e).name.split(".")[$-1].tolower())); 149 + t.set("is" , ValueLayer, new StrValue(typeid(e).name.split(".")[$-1])); 150 150 foreach(i,m; e.tupleof) 151 151 static if(is(typeof(m) : AST)) 152 152 t.set(e.tupleof[i].stringof.split(".")[$-1], ValueLayer, rec(m)); 153 153 else 154 154 t.set(e.tupleof[i].stringof.split(".")[$-1], ValueLayer, ast2table(m,rec)); 155 155 return t; 156 156 }

Modified sample/ast.pmy from [94485181e809cc7b] to [157799e01f30b7fa].

4 4 when( {car:a, cdr: d} ) { reverse(d, {car:a, cdr:acc}) } 5 5 when( {} ) { acc } 6 6 }; 7 7 8 8 @macro reverseArgs(e) {@value( 9 9 var ev = @macro(e); 10 10 case(ev) 11 - when( {is:"app", fun:f, args:a} ) 11 + when( {is:"App", fun:f, args:a} ) 12 12 { 13 13 ev {args: reverse(a, {})} 14 14 } 15 15 when( _ ) 16 16 { 17 17 ev 18 18 }