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
- branch=trunk inherited from [f65680e1d2]
- sym-trunk inherited from [f65680e1d2]
Changes
Modified polemy/eval.d from [e0978d70034e5fac] to [4eba423d1304ad73].
68 assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not 68 assert(false, text("eval() for ",typeid(e)," [",e.pos,"] is not 69 } 69 } 70 70 71 Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 71 Value eval( Str e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 72 { 72 { 73 if( isASTLayer(lay) ) 73 if( isASTLayer(lay) ) 74 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 74 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 75 if( lay==ValueLayer ) | 75 if( isUserDefinedLayer(lay) ) 76 return new StrValue(e.data); | 76 return lift(new StrValue(e.data), lay, ctx, e.pos); 77 return lift(new StrValue(e.data), lay, ctx, e.pos); | 77 return new StrValue(e.data); 78 } 78 } 79 79 80 Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 80 Value eval( Int e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 81 { 81 { 82 if( isASTLayer(lay) ) 82 if( isASTLayer(lay) ) 83 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 83 return ast2table(e, (AST e){return eval(e,lay,ctx);}); 84 if( lay==ValueLayer ) | 84 if( isUserDefinedLayer(lay) ) 85 return new IntValue(e.data); | 85 return lift(new IntValue(e.data), lay, ctx, e.pos); 86 return lift(new IntValue(e.data), lay, ctx, e.pos); | 86 return new IntValue(e.data); 87 } 87 } 88 88 89 Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 89 Value eval( Var e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 90 { 90 { 91 if( isASTLayer(lay) ) 91 if( isASTLayer(lay) ) 92 if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) ) 92 if( isMacroLayer(lay) && ctx.has(e.name,MacroLayer) ) 93 return ctx.get(e.name, MacroLayer, e.pos); 93 return ctx.get(e.name, MacroLayer, e.pos); 94 else 94 else 95 return ast2table(e, (AST e){return eval(e,lay,ct 95 return ast2table(e, (AST e){return eval(e,lay,ct 96 if( lay==ValueLayer || ctx.has(e.name, lay) ) | 96 if( isUserDefinedLayer(lay) && !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) | 97 return lift(ctx.get(e.name, ValueLayer, e.pos), lay, ctx > 98 return ctx.get(e.name, lay, e.pos); 99 } 99 } 100 100 101 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 101 Value eval( App e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 102 { 102 { 103 Value f = eval( e.fun, lay, ctx ); 103 Value f = eval( e.fun, lay, ctx ); 104 if( isASTLayer(lay) ) { 104 if( isASTLayer(lay) ) { 105 auto ff = cast(FunValue)f; 105 auto ff = cast(FunValue)f; ................................................................................................................................................................................ 134 } 134 } 135 135 136 Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 136 Value eval( Let e, Layer lay, Table ctx, bool overwriteCtx=CascadeCtx ) 137 { 137 { 138 Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.No 138 Table newCtx = overwriteCtx ? ctx : new Table(ctx, Table.Kind.No 139 if( isASTLayer(lay) ) 139 if( isASTLayer(lay) ) 140 return ast2table(e, (AST ee){ 140 return ast2table(e, (AST ee){ 141 if(ee is e.expr) // need this for correct scopin | 141 // need this for correct scoping (outer scope ma > 142 if(ee is e.expr) 142 newCtx.set(e.name, ValueLayer, new Undef 143 newCtx.set(e.name, ValueLayer, new Undef 143 return eval(ee,lay,newCtx); 144 return eval(ee,lay,newCtx); 144 }); 145 }); 145 else 146 else 146 { 147 { 147 Value ri = eval(e.init, lay, newCtx); 148 Value ri = eval(e.init, lay, newCtx); 148 newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri); 149 newCtx.set(e.name, e.layer.empty ? lay : e.layer, ri); ................................................................................................................................................................................ 251 { 252 { 252 Fun ast; 253 Fun ast; 253 Table defCtx; 254 Table defCtx; 254 override const(Parameter[]) params() { return ast.params 255 override const(Parameter[]) params() { return ast.params 255 override Table definitionContext() { return defCtx; } 256 override Table definitionContext() { return defCtx; } 256 257 257 this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx= 258 this(Fun ast, Table defCtx) { this.ast=ast; this.defCtx= > 259 override string toString() const 258 override string toString() const { return sprintf!"(func | 260 { return sprintf!"(function:%x:%x)"(cast(void*)a 259 override int opCmp(Object rhs) { 261 override int opCmp(Object rhs) { 260 if(auto r = cast(UserDefinedFunValue)rhs) { 262 if(auto r = cast(UserDefinedFunValue)rhs) { 261 if(auto i = this.ast.opCmp(r.ast)) | 263 auto a = cast(void*)this.ast; > 264 auto b = cast(void*)r.ast; 262 return i; | 265 if(a<b) return -1; > 266 if(a>b) return +1; // [TODO] avoid using 263 return this.defCtx.opCmp(r.defCtx); 267 return this.defCtx.opCmp(r.defCtx); 264 } 268 } 265 if(auto r = cast(Value)rhs) return typeid(thi | 269 if(auto r = cast(Value)rhs) return typeid(this). 266 throw genex!RuntimeException("comparison with va 270 throw genex!RuntimeException("comparison with va 267 } 271 } 268 mixin SimpleToHash; 272 mixin SimpleToHash; 269 273 270 AST afterMacroAST; 274 AST afterMacroAST; 271 override Value invoke(Layer lay, Table ctx, LexPosition 275 override Value invoke(Layer lay, Table ctx, LexPosition 272 { 276 { ................................................................................................................................................................................ 294 override const(Parameter[]) params() { return params_dat 298 override const(Parameter[]) params() { return params_dat 295 override Table definitionContext() { return theContext 299 override Table definitionContext() { return theContext 296 300 297 override string toString() { return sprintf!"(native:%x) 301 override string toString() { return sprintf!"(native:%x) 298 override int opCmp(Object rhs) { 302 override int opCmp(Object rhs) { 299 if(auto r = cast(NativeFunValue)rhs) return type 303 if(auto r = cast(NativeFunValue)rhs) return type 300 if(auto r = cast(Value)rhs) return type 304 if(auto r = cast(Value)rhs) return type 301 throw genex!RuntimeException(LexPosition.dummy, | 305 throw genex!RuntimeException("comparison with va 302 } 306 } 303 mixin SimpleToHash; 307 mixin SimpleToHash; 304 308 305 R delegate(T) dg; 309 R delegate(T) dg; 306 Parameter[] params_data; 310 Parameter[] params_data; 307 311 308 this(R delegate(T) dg) 312 this(R delegate(T) dg) ................................................................................................................................................................................ 311 foreach(i, Ti; T) 315 foreach(i, Ti; T) 312 params_data ~= new Parameter(text(i), [] 316 params_data ~= new Parameter(text(i), [] 313 } 317 } 314 318 315 override Value invoke(Layer lay, Table ctx, LexPosition 319 override Value invoke(Layer lay, Table ctx, LexPosition 316 { 320 { 317 if( lay != defLay ) 321 if( lay != defLay ) > 322 throw genex!RuntimeException(pos, 318 throw genex!RuntimeException(pos, text(" | 323 text("only ", defLay, " layer ca 319 T typed_args; 324 T typed_args; 320 foreach(i, Ti; T) { 325 foreach(i, Ti; T) { 321 typed_args[i] = cast(Ti) ctx.get(text(i) 326 typed_args[i] = cast(Ti) ctx.get(text(i) 322 if( typed_args[i] is null ) 327 if( typed_args[i] is null ) > 328 throw genex!RuntimeException(pos 323 throw genex!RuntimeException(pos | 329 sprintf!"type mismatch o 324 } 330 } 325 try { 331 try { 326 return dg(typed_args); 332 return dg(typed_args); 327 } catch( RuntimeException e ) { 333 } catch( RuntimeException e ) { 328 throw e.pos is null ? new RuntimeExcepti 334 throw e.pos is null ? new RuntimeExcepti 329 } 335 } 330 } 336 }
Modified polemy/layer.d from [ec0e00bb40d202e7] to [70a85e9a66eddacd].
13 13 14 enum : Layer 14 enum : Layer 15 { 15 { 16 SystemLayer = "(system)", /// Predefined layer for internal data 16 SystemLayer = "(system)", /// Predefined layer for internal data 17 ValueLayer = "@value", /// Predefined layer for normal run 17 ValueLayer = "@value", /// Predefined layer for normal run 18 MacroLayer = "@macro", /// Predefined layer for macro run (@lay() 18 MacroLayer = "@macro", /// Predefined layer for macro run (@lay() 19 RawMacroLayer = "(rawmacro)", /// Predefined layer for macro run (@lay() 19 RawMacroLayer = "(rawmacro)", /// Predefined layer for macro run (@lay() 20 AstLayer = "(ast)", /// Predefined layer for macro run (never < > 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!=Ra > 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 /// True if it is macro-like layer that basically generates syntax tree 38 /// True if it is macro-like layer that basically generates syntax tree 24 39 25 bool isASTLayer( Layer lay ) 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 unittest 45 unittest 31 { 46 { 32 assert( !isASTLayer(SystemLayer) ); 47 assert( !isASTLayer(SystemLayer) ); 33 assert( !isASTLayer(ValueLayer) ); 48 assert( !isASTLayer(ValueLayer) ); 34 assert( isASTLayer(MacroLayer) ); 49 assert( isASTLayer(MacroLayer) ); 35 assert( isASTLayer(RawMacroLayer) ); 50 assert( isASTLayer(RawMacroLayer) ); 36 assert( isASTLayer(AstLayer) ); < 37 } 51 } 38 52 39 /// True if in the specified layer @lay(...) has no effect and merely produces a 53 /// True if in the specified layer @lay(...) has no effect and merely produces a 40 54 41 bool isNoLayerChangeLayer( Layer lay ) 55 bool isNoLayerChangeLayer( Layer lay ) 42 { 56 { 43 return lay==RawMacroLayer || lay==AstLayer; | 57 return lay==RawMacroLayer; 44 } 58 } 45 59 46 unittest 60 unittest 47 { 61 { 48 assert( !isNoLayerChangeLayer(SystemLayer) ); 62 assert( !isNoLayerChangeLayer(SystemLayer) ); 49 assert( !isNoLayerChangeLayer(ValueLayer) ); 63 assert( !isNoLayerChangeLayer(ValueLayer) ); 50 assert( !isNoLayerChangeLayer(MacroLayer) ); 64 assert( !isNoLayerChangeLayer(MacroLayer) ); 51 assert( isNoLayerChangeLayer(RawMacroLayer) ); 65 assert( isNoLayerChangeLayer(RawMacroLayer) ); 52 assert( isNoLayerChangeLayer(AstLayer) ); < 53 } 66 } 54 67 55 /// True if do macro expanstion 68 /// True if do macro expanstion 56 69 57 bool isMacroLayer( Layer lay ) 70 bool isMacroLayer( Layer lay ) 58 { 71 { 59 return lay==MacroLayer || lay==RawMacroLayer; 72 return lay==MacroLayer || lay==RawMacroLayer; ................................................................................................................................................................................ 61 74 62 unittest 75 unittest 63 { 76 { 64 assert( !isMacroLayer(SystemLayer) ); 77 assert( !isMacroLayer(SystemLayer) ); 65 assert( !isMacroLayer(ValueLayer) ); 78 assert( !isMacroLayer(ValueLayer) ); 66 assert( isMacroLayer(MacroLayer) ); 79 assert( isMacroLayer(MacroLayer) ); 67 assert( isMacroLayer(RawMacroLayer) ); 80 assert( isMacroLayer(RawMacroLayer) ); 68 assert( !isMacroLayer(AstLayer) ); < 69 } 81 }
Modified polemy/parse.d from [64474993b2d6bb44] to [6c61d557752efef5].
280 if( tryEat("{") ) 280 if( tryEat("{") ) 281 { 281 { 282 AST e = new App(pos, new Var(pos,"{}")); 282 AST e = new App(pos, new Var(pos,"{}")); 283 return parseTableSetAfterBrace(e); 283 return parseTableSetAfterBrace(e); 284 } 284 } 285 if( tryEat("if") ) 285 if( tryEat("if") ) 286 { 286 { 287 eat("(", "after if"); | 287 return parseIfAfterIf(pos); 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.fron < 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 ); < 307 } 288 } 308 if( tryEat("case") ) 289 if( tryEat("case") ) 309 { 290 { 310 return parsePatternMatch(pos); 291 return parsePatternMatch(pos); 311 } 292 } 312 if( tryEat("fun") || tryEat("\u03BB") ) // lambda!! 293 if( tryEat("fun") || tryEat("\u03BB") ) // lambda!! 313 { 294 { 314 eat("(", "after fun"); 295 eat("(", "after fun"); 315 return parseLambdaAfterOpenParen(pos); 296 return parseLambdaAfterOpenParen(pos); 316 } 297 } 317 scope(exit) lex.popFront; 298 scope(exit) lex.popFront; 318 return new Var(pos, lex.front.str); 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,[], > 325 } 320 326 321 AST parsePatternMatch(LexPosition pos) 327 AST parsePatternMatch(LexPosition pos) 322 { 328 { 323 // case "(" pmExpr ")" CASES 329 // case "(" pmExpr ")" CASES 324 //==> 330 //==> 325 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir 331 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir 326 eat("(", "after case"); 332 eat("(", "after case"); ................................................................................................................................................................................ 665 `)); 671 `)); 666 assert_nothrow(parseString(` 672 assert_nothrow(parseString(` 667 case( 1 ) 673 case( 1 ) 668 when({aaaa:@value(x)}){1} 674 when({aaaa:@value(x)}){1} 669 when({aaaa:{bbb:_}, ccc:123}){1} 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 LexPosition pos = extractPos(t); 65 LexPosition pos = extractPos(t); 66 66 67 StrValue typ = cast(StrValue) t.access!StrValue(ValueLay 67 StrValue typ = cast(StrValue) t.access!StrValue(ValueLay 68 if( typ is null ) 68 if( typ is null ) 69 throw genex!RuntimeException(callpos, text(`Inva 69 throw genex!RuntimeException(callpos, text(`Inva 70 70 71 foreach(AT; ListOfASTTypes) 71 foreach(AT; ListOfASTTypes) 72 if(typ.data == typeid(AT).name.split(".")[$-1].t | 72 if(typ.data == typeid(AT).name.split(".")[$-1]) 73 { 73 { 74 typeof(AT.tupleof) mems; 74 typeof(AT.tupleof) mems; 75 foreach(i,m; mems) 75 foreach(i,m; mems) 76 { 76 { 77 string name = AT.tupleof[i].stri 77 string name = AT.tupleof[i].stri 78 Value vm = t.access!Value(ValueL 78 Value vm = t.access!Value(ValueL 79 if( vm is null ) 79 if( vm is null ) ................................................................................................................................................................................ 142 } 142 } 143 else 143 else 144 static if(is(T : AST)) 144 static if(is(T : AST)) 145 { 145 { 146 assert( typeid(e) == typeid(T), text("abstracted: ", typeid(e), 146 assert( typeid(e) == typeid(T), text("abstracted: ", typeid(e), 147 auto t = new Table; 147 auto t = new Table; 148 t.set("pos", ValueLayer, ast2table(e.pos,rec)); 148 t.set("pos", ValueLayer, ast2table(e.pos,rec)); 149 t.set("is" , ValueLayer, new StrValue(typeid(e).name.split(".")[ | 149 t.set("is" , ValueLayer, new StrValue(typeid(e).name.split(".")[ 150 foreach(i,m; e.tupleof) 150 foreach(i,m; e.tupleof) 151 static if(is(typeof(m) : AST)) 151 static if(is(typeof(m) : AST)) 152 t.set(e.tupleof[i].stringof.split(".")[$-1], Val 152 t.set(e.tupleof[i].stringof.split(".")[$-1], Val 153 else 153 else 154 t.set(e.tupleof[i].stringof.split(".")[$-1], Val 154 t.set(e.tupleof[i].stringof.split(".")[$-1], Val 155 return t; 155 return t; 156 } 156 }
Modified sample/ast.pmy from [94485181e809cc7b] to [157799e01f30b7fa].
4 when( {car:a, cdr: d} ) { reverse(d, {car:a, cdr:acc}) } 4 when( {car:a, cdr: d} ) { reverse(d, {car:a, cdr:acc}) } 5 when( {} ) { acc } 5 when( {} ) { acc } 6 }; 6 }; 7 7 8 @macro reverseArgs(e) {@value( 8 @macro reverseArgs(e) {@value( 9 var ev = @macro(e); 9 var ev = @macro(e); 10 case(ev) 10 case(ev) 11 when( {is:"app", fun:f, args:a} ) | 11 when( {is:"App", fun:f, args:a} ) 12 { 12 { 13 ev {args: reverse(a, {})} 13 ev {args: reverse(a, {})} 14 } 14 } 15 when( _ ) 15 when( _ ) 16 { 16 { 17 ev 17 ev 18 } 18 }