private Stmt ParseForStatement() { ConsumeToken(TokenType.LEFT_PARENTHESIS, "Expect '(' after for keyword"); Stmt initializer; if (MatchTokens(TokenType.SEMICOLON)) { initializer = null; } else if (MatchTokens(TokenType.VAR)) { initializer = ParseVarDeclaration(); } else { initializer = ParseExpressionStatement(); } Expr condition = null; if (!CheckToken(TokenType.SEMICOLON)) { condition = ParseExpression(); } ConsumeToken(TokenType.SEMICOLON, "Expect ';' after for loop condition"); Expr increment = null; if (!CheckToken(TokenType.RIGHT_PARENTHESIS)) { increment = ParseExpression(); } ConsumeToken(TokenType.RIGHT_PARENTHESIS, "Expect ')' after for clause"); Stmt body = ParseStatement(); if (increment != null) { body = new Stmt.Block(new List <Stmt>() { body, new Stmt.Expression(increment) }); } if (condition is null) { condition = new Expr.Literal(true); } body = new Stmt.While(condition, body); if (initializer != null) { body = new Stmt.Block(new List <Stmt>() { initializer, body }); } return(body); }
public object VisitLiteralExpr(Expr.Literal expr) => expr.Value;