Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 public object Visit(Expr.Grouping expr)
 {
     return(Evaluate(expr.expression));
 }
Esempio n. 3
0
 public string Visit(Expr.Grouping grouping)
 {
     return(Parentherize("group", grouping.expression));
 }
Esempio n. 4
0
        public object Visit(Expr.Grouping expr)
        {
            Resolve(expr.expression);

            return(null);
        }