Пример #1
0
        public object Visit(Expr.This expr)
        {
            if (currentClass == ClassType.None)
            {
                Lox.Error(expr.keyword, "Cannot use 'this' outside of a class.");
            }
            else
            {
                ResolveLocal(expr, expr.keyword);
            }

            return(null);
        }
Пример #2
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);
        }
Пример #3
0
 public object Visit(Expr.This expr)
 {
     return(LookUpVariable(expr.keyword, expr));
 }