Differences From Artifact [8212de2433d6e818]:
- File
polemy/parse.d
- 2010-11-12 04:40:33 - part of checkin [a7b5d1d95a] on branch trunk - refactored the parser, and added layerd params fun(x @t){...} (user: kinaba) [annotate]
To Artifact [7ed0053850ffa032]:
- File
polemy/parse.d
- 2010-11-13 03:55:17 - part of checkin [c368edbcb1] on branch trunk - @@lay(x) { ... } declaration and value rising. (user: kinaba) [annotate]
72 } 72 }
73 73
74 AST Declaration() // returns null if it is not a declaration 74 AST Declaration() // returns null if it is not a declaration
75 { 75 {
76 /// Declaration ::= 76 /// Declaration ::=
77 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"i 77 /// ["@" Layer|"let"|"var"|"def"] Var "=" Expression ([";"|"i
78 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" B 78 /// | ["@" Layer|"let"|"var"|"def"] Var "(" Param%"," ")" "{" B
> 79 /// | ["@" "@" Layer "=" Expression ([";"|"in"] Body?)?
> 80 /// | ["@" "@" Layer "(" Param%"," ")" "{" Body "}" ([";"|"in"]
79 81
80 auto pos = currentPosition(); 82 auto pos = currentPosition();
81 string layer = ""; 83 string layer = "";
> 84 bool layerRiseDecl = false;
82 85
83 if( tryEat("@") ) 86 if( tryEat("@") )
84 { 87 {
85 layer = "@" ~ eatId("after @", AllowQuoted); 88 layer = "@" ~ eatId("after @", AllowQuoted);
> 89 if( layer == "@@" )
> 90 {
> 91 layer = "@" ~ eatId("after @@", AllowQuoted);
> 92 layerRiseDecl = true;
> 93 }
> 94 else
> 95 {
86 if( tryEat("(") ) | 96 if( tryEat("(") )
87 return null; // @lay(...) expression, not a decl | 97 return null; // @lay(...) expression, no
88 } | 98 }
89 | 99 }
90 string kwd = layer; <
91 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="var") && ! <
92 return null; // none of {@lay, let, var, def} occurred, <
93 | 100
94 auto varpos = currentPosition(); <
95 string var = eatId("after "~kwd, AllowQuoted); // name of the de <
> 101 // [TODO] Refactor
> 102 if( layerRiseDecl )
96 | 103 {
> 104 string kwd = "@" ~ layer;
> 105 string var = layer;
> 106
97 auto e = tryEat("(") | 107 auto e = tryEat("(")
98 ? parseLambdaAfterOpenParen(varpos) // let var ( ... | 108 ? parseLambdaAfterOpenParen(pos) // let var ( .
99 : (eat("=", "after "~kwd), E(0)); // let var = ... | 109 : (eat("=", "after "~kwd), E(0)); // let var = .
100 <
101 if( moreDeclarationExists() ) | 110 if( moreDeclarationExists() )
102 return new LetExpression(pos, var, layer, e, Body()); | 111 return new LetExpression(pos, var, "(system)", e
> 112 else
> 113 return new LetExpression(pos, var, "(system)", e
> 114 }
103 else 115 else
> 116 {
> 117 string kwd = layer;
> 118 if( layer.empty && !tryEat(kwd="let") && !tryEat(kwd="va
> 119 return null; // none of {@lay, let, var, def} oc
> 120
> 121 auto varpos = currentPosition();
> 122 string var = eatId("after "~kwd, AllowQuoted); // name o
> 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, Bod
> 129 else
104 return new LetExpression(pos, var, layer, e, new VarExpr | 130 return new LetExpression(pos, var, layer, e, new
> 131 }
105 } 132 }
106 133
107 AST TopLevelExpression() 134 AST TopLevelExpression()
108 { 135 {
109 /// TopLevelExpression ::= Expression ([";"|"in"] Body?)? 136 /// TopLevelExpression ::= Expression ([";"|"in"] Body?)?
110 137
111 auto pos = currentPosition(); 138 auto pos = currentPosition();
................................................................................................................................................................................
404 { 431 {
405 mixin EasyAST; 432 mixin EasyAST;
406 assert_eq(parseString(`def foo(x) { x+1 }; foo`), 433 assert_eq(parseString(`def foo(x) { x+1 }; foo`),
407 let("foo", "", 434 let("foo", "",
408 fun(["x"], call(var("+"), var("x"), intl(1))), 435 fun(["x"], call(var("+"), var("x"), intl(1))),
409 var("foo")) 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 }