Beispiel #1
0
        private Stmt ClassDeclaration()
        {
            Token name = Consume(TokenType.IDENTIFIER, "Expect class name.");

            Expr.Variable superclass = null;
            if (Match(TokenType.LESS))
            {
                Consume(TokenType.IDENTIFIER, "Expect superclass name.");
                superclass = new Expr.Variable(Previous());
            }

            Consume(TokenType.LEFT_BRACE, "Expect '{' before class body.");

            List <Stmt.Function> methods = new List <Stmt.Function>();

            while (!Check(TokenType.RIGHT_BRACE) && !IsAtEnd())
            {
                methods.Add(Function("method"));
            }


            Consume(TokenType.RIGHT_BRACE, "Expect '}' after class body.");

            return(new Stmt.Class(name, superclass, methods));
        }
Beispiel #2
0
        public object VisitVariableExpr(Expr.Variable expr)
        {
            if (scopes.Any() && scopes.Peek()[expr.Name.Lexeme] == false)
            {
                Lox.Error(expr.Name, "Cannot read local variable in its own initializer.");
            }

            ResolveLocal(expr, expr.Name);
            return(null);
        }
Beispiel #3
0
        object Expr.IVisitor <object> .VisitVariableExpr(Expr.Variable expr)
        {
            if (scopes.Count > 0 && scopes.Peek().TryGetValue(expr.name.lexeme, out bool value))
            {
                if (!value)
                {
                    Lox.Error(expr.name, "Cannot read local variable in its own initializer.");
                }
            }

            ResolveLocal(expr, expr.name);
            return(null);
        }
Beispiel #4
0
        public Unit VisitVariable(Expr.Variable expr)
        {
            // Because we recurse into the initializer expression (while the scope has the variable)
            // in 'NotReady'), we can use this to check that the initializer expression doesn't refer
            // to this variable.
            Resolution res;

            if (_scopes.Count != 0 && _scopes.Peek().TryGetValue(expr.Name.Lexeme, out res) && res == Resolution.NotReady)
            {
                _errors.AddResolverError(expr.Name, "Cannot read local variable in its own initializer");
            }

            ResolveLocal(expr, expr.Name);
            return(Unit.Default);
        }
Beispiel #5
0
        public Void Visit(Expr.Variable expr)
        {
            if (scopes.Any())
            {
                var scope = scopes.Peek();
                var name  = expr.Name.Lexeme;

                if (scope.ContainsKey(name) && scope[name] == false)
                {
                    Lox.Error(expr.Name, "Cannot read local variable in its own initializer.");
                }
            }

            ResolveLocal(expr, expr.Name);

            return(Void.Instance);
        }
Beispiel #6
0
 public object VisitVariableExpr(Expr.Variable expr)
 {
     return(LookUpVariable(expr.Name, expr));
 }
Beispiel #7
0
 public string VisitVariable(Expr.Variable expr)
 {
     return(expr.Name.Lexeme);
 }
Beispiel #8
0
 object Expr.IVisitor <object> .VisitVariableExpr(Expr.Variable expr)
 {
     return(LookUpVariable(expr.name, expr));
 }
Beispiel #9
0
 public Class(Token name, Expr.Variable superclass, List <Stmt.Function> methods)
 {
     this.name       = name;
     this.superclass = superclass;
     this.methods    = methods;
 }
Beispiel #10
0
 public string VisitVariableExpr(Expr.Variable expr)
 {
     return(parenthesize("VAR", expr));
 }