public string Visit(Expr.Literal literal) { string ret = "nil"; if (literal.value != null) { ret = literal.value.ToString(); } return(ret); }
private Expr Primary() { Expr expr = null; if (Match(TokenType.False)) { expr = new Expr.Literal(false); } else if (Match(TokenType.True)) { expr = new Expr.Literal(true); } else if (Match(TokenType.Nil)) { expr = new Expr.Literal(null); } else if (Match(TokenType.Number, TokenType.String)) { expr = new Expr.Literal(Previous().literal); } else if (Match(TokenType.Super)) { Token keyword = Previous(); Consume(TokenType.Dot, "Expect '.' after 'super'."); Token method = Consume(TokenType.Identifier, "Expect superclass method name."); expr = new Expr.Super(keyword, method); } else if (Match(TokenType.This)) { expr = new Expr.This(Previous()); } else if (Match(TokenType.Identifier)) { expr = new Expr.Variable(Previous()); } else if (Match(TokenType.LeftParen)) { expr = Expression(); Consume(TokenType.RightParen, "Expect ')' after expression."); expr = new Expr.Grouping(expr); } else { throw Error(Peek(), "Expect expression."); } return(expr); }
public object Visit(Expr.Literal expr) { return(expr.value); }
private Stmt ForStatement() { Consume(TokenType.LeftParen, "Expect '(' after 'for'"); // initializer Stmt initializer = null; if (Match(TokenType.Semicolon)) { initializer = null; } else if (Match(TokenType.Var)) { initializer = VarDeclaration(); } else { initializer = ExpressionStatement(); } // condition Expr condition = null; if (!Check(TokenType.Semicolon)) { condition = Expression(); } Consume(TokenType.Semicolon, "Expect ';' after loop condition."); // increment Expr increment = null; if (!Check(TokenType.RightParen)) { increment = Expression(); } Consume(TokenType.RightParen, "Expect ')' after for clauses."); // body Stmt body = Statement(); // desugar if (increment != null) { List <Stmt> statements = new List <Stmt>(); statements.Add(body); statements.Add(new Stmt.Expression(increment)); body = new Stmt.Block(statements); } if (condition == null) { condition = new Expr.Literal(true); } body = new Stmt.While(condition, body); if (initializer != null) { List <Stmt> statements = new List <Stmt>(); statements.Add(initializer); statements.Add(body); body = new Stmt.Block(statements); } return(body); }
public object Visit(Expr.Literal expr) { return(null); // do nothing }