Diff
Not logged in

Differences From Artifact [fe0b0bcfc1d8a230]:

To Artifact [31eeea68e341add3]:


51 51 this(Lexer lex) 52 52 { 53 53 this.lex = lex; 54 54 } 55 55 56 56 Program parseProgram() 57 57 { 58 + Program p = parseStatements(); 59 + if( !lex.empty ) { 60 + auto e = ParserException.create(lex, "cannot reach eof"); 61 + throw e; 62 + } 63 + return p; 64 + } 65 + 66 + Program parseStatements() 67 + { 58 68 Program p; 59 - while( !lex.empty ) 69 + while( !lex.empty && (lex.front.kind!=Token.Kind.identifier || lex.front.str!="}") ) 60 70 p ~= parseStatement(); 61 71 return p; 62 72 } 63 73 64 74 Statement parseStatement() 65 75 { 66 76 auto saved = lex.save; ................................................................................ 183 193 } 184 194 if( tryEat("(") ) 185 195 { 186 196 auto e = parseE(); 187 197 eat(")", "after parenthesized expression"); 188 198 return e; 189 199 } 200 + if( tryEat("if") ) 201 + { 202 + eat("(", "after if"); 203 + auto cond = parseE(); 204 + eat(")", "after if condition"); 205 + auto thenPos = lex.front.pos; 206 + eat("{", "after if condition"); 207 + Statement[] th = parseStatements(); 208 + eat("}", "after if-then body"); 209 + Statement[] el; 210 + auto elsePos = lex.front.pos; 211 + if( tryEat("else") ) { 212 + eat("{", "after else"); 213 + el = parseStatements(); 214 + eat("}", "after else body"); 215 + } 216 + return new FuncallExpression(pos, 217 + new VarExpression(pos, "if"), 218 + cond, 219 + new FunLiteralExpression(thenPos, [], th), 220 + new FunLiteralExpression(elsePos, [], el) 221 + ); 222 + } 190 223 191 224 if( tryEat("fun") ) 192 225 { 193 226 eat("(", "after fun"); 194 227 string[] params; 195 228 while(!tryEat(")")) 196 229 { ................................................................................ 311 344 )))); 312 345 } 313 346 unittest 314 347 { 315 348 auto p = parserFromString(`var x = 1; var f = fun(){x=x+1;}; f(); f(); x;`); 316 349 Program prog = p.parseProgram(); 317 350 } 351 + 352 +unittest 353 +{ 354 + auto p = parserFromString(`if(x<2){1;}else{x;};`); 355 + Program prog = p.parseProgram(); 356 + assert( prog[0] == new ExprStatement(null, new FuncallExpression(null, 357 + new VarExpression(null, "if"), 358 + new FuncallExpression(null, new VarExpression(null,"<"), new VarExpression(null,"x"), 359 + new IntLiteralExpression(null, BigInt(2))), 360 + new FunLiteralExpression(null, [], [new ExprStatement(null, new IntLiteralExpression(null, BigInt(1)))]), 361 + new FunLiteralExpression(null, [], [new ExprStatement(null, new VarExpression(null, "x"))]) 362 + ))); 363 +}