private Stmt ForStatement() { Consume(TokenType.LEFT_PAREN, "Expect '(' after 'for'."); Stmt initializer; if (Match(TokenType.SEMICOLON)) { initializer = null; } else if (Match(TokenType.VAR)) { initializer = VarDeclaration(); } else { initializer = ExpressionStatement(); } Expr condition = null; if (!Check(TokenType.SEMICOLON)) { condition = Expression(); } Consume(TokenType.SEMICOLON, "Expect ';' after loop condition."); Expr increment = null; if (!Check(TokenType.RIGHT_PAREN)) { increment = Expression(); } Consume(TokenType.RIGHT_PAREN, "Expect ')' after for clauses"); Stmt body = Statement(); if (increment != null) { body = new Stmt.Block(new List <Stmt>(new[] { body, new Stmt.Expression(increment) })); } if (condition == null) { condition = new Expr.Literal(true); } body = new Stmt.While(condition, body); if (initializer != null) { body = new Stmt.Block(new List <Stmt>(new[] { initializer, body })); } return(body); }
public string VisitLiteral(Expr.Literal expr) { if (expr.Value == null) { return("nil"); } if (expr.Value is string) { return(string.Format("\"{0}\"", expr.Value)); } return(expr.Value.ToString()); }
// varDecl :: "var" IDENTIFIER ( "=" expression )? ";" ; private Stmt VarDeclaration() { var name = Consume(TokenType.Identifier, "Expect variable name"); Expr init = new Expr.Literal(null); if (MatchAny(TokenType.Equal)) { init = Expression(); } Consume(TokenType.Semicolon, "Expect ';' after variable declaration"); return(new Stmt.Var(name, init)); }
public object VisitLiteralExpr(Expr.Literal expr) { return(expr.Value); }
// forStmt :: "for" "(" ( varDecl | exprStmt | "; ") // expression? ";" // expression? ")" statement ; private Stmt ForStatement() { Consume(TokenType.LeftParen, "Expect '(' after 'for'"); // Get the initializer. Stmt init; if (MatchAny(TokenType.Semicolon)) { init = null; } else if (MatchAny(TokenType.Var)) { init = VarDeclaration(); } else { init = ExpressionStatement(); } // Get the condition. Expr condition = null; if (!Check(TokenType.Semicolon)) { condition = Expression(); } Consume(TokenType.Semicolon, "Expect ';' after loop condition"); // Get the increment. Expr increment = null; if (!Check(TokenType.RightParen)) { increment = Expression(); } Consume(TokenType.RightParen, "Expect ')' after for clauses"); var body = Statement(); // Desugar by creating a while loop instead. if (increment != null) { body = new Stmt.Block( new List <Stmt> { body, new Stmt.Expression(increment) }); } if (condition == null) { condition = new Expr.Literal(true); } body = new Stmt.While(condition, body); if (init != null) { body = new Stmt.Block( new List <Stmt> { init, body }); } return(body); }
object Expr.IVisitor <object> .VisitLiteralExpr(Expr.Literal expr) { return(expr.value); }
public object VisitLiteralExpr(Expr.Literal expr) { return(null); }
public Unit VisitLiteral(Expr.Literal expr) { return(Unit.Default); }
public Void Visit(Expr.Literal expr) { return(Void.Instance); }
object Expr.IVisitor <object> .VisitLiteralExpr(Expr.Literal expr) { return(null); }