Example #1
0
        public object VisitVariableExpr(Expr.Variable expr)
        {
            if (scopes.Any() && scopes.Peek().TryGetValue(expr.Name.Lexeme, out var variable) && variable == false)
            {
                Lox.Error(expr.Name, "Can't read local variable in its own initializer.");
            }

            ResolveLocal(expr, expr.Name);
            return(null);
        }
Example #2
0
        private Stmt ClassDeclaration()
        {
            var name = Consume(IDENTIFIER, "Expect class name.");

            Expr.Variable superclass = null;
            if (Match(LESS))
            {
                Consume(IDENTIFIER, "Expect superclass name.");
                superclass = new Expr.Variable(Previous());
            }
            Consume(LEFT_BRACE, "Expect '{' before class body.");

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

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

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

            return(new Stmt.Class(name, superclass, methods.ToArray()));
        }