Example #1
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            ClassType enclosingClass = currentClass;

            currentClass = ClassType.CLASS;

            Declare(stmt.Name);
            Define(stmt.Name);

            if (stmt.Superclass != null && stmt.Name.Lexeme.Equals(stmt.Superclass.Name.Lexeme))
            {
                Program.Error(stmt.Superclass.Name, "A class can't inherit from itself.");
            }

            if (stmt.Superclass != null)
            {
                currentClass = ClassType.SUBCLASS;
                Resolve(stmt.Superclass);
            }

            if (stmt.Superclass != null)
            {
                BeginScope();
                scopes.Peek()["super"] = true;
            }

            BeginScope();
            scopes.Peek()["this"] = true;

            foreach (var method in stmt.Methods)
            {
                FunctionType declaration = FunctionType.METHOD;
                if (method.Name.Lexeme.Equals("init"))
                {
                    declaration = FunctionType.INITIALIZER;
                }
                ResolveFunction(method, declaration);
            }

            EndScope();

            if (stmt.Superclass != null)
            {
                EndScope();
            }

            currentClass = enclosingClass;

            return(null);
        }
Example #2
0
        public object Visit(Stmt.Class _class)
        {
            ClassType enclosingClass = _currentClass;

            _currentClass = ClassType.CLASS;

            Declare(_class.name);
            Define(_class.name);

            if ((_class.superclass != null) &&
                _class.name.lexeme.Equals(_class.superclass.name.lexeme))
            {
                Lox.Error(_class.superclass.name, "A class cannot inherit from itself.");
            }

            if (_class.superclass != null)
            {
                _currentClass = ClassType.SUBCLASS;
                Resolve(_class.superclass);
            }

            if (_class.superclass != null)
            {
                BeginScope();
                _scopes.Peek().Add("super", true);
            }

            BeginScope();
            _scopes.Peek().Add("this", true);

            foreach (Stmt.Function method in _class.methods)
            {
                FunctionType declaration = (method.name.lexeme.Equals("init")) ? FunctionType.INITIALIZER : FunctionType.METHOD;
                ResolveFunction(method, declaration);
            }

            EndScope();

            if (_class.superclass != null)
            {
                EndScope();
            }

            _currentClass = enclosingClass;
            return(null);
        }
Example #3
0
        public Void VisitClassStmt(Stmt.Class stmt)
        {
            var enclosingClass = _currentClass;

            _currentClass = ClassType.CLASS;
            Declare(stmt.Name);
            Define(stmt.Name);

            if (stmt.Superclass != null &&
                stmt.Name.Lexeme.Equals(stmt.Superclass.Name.Lexeme, StringComparison.Ordinal))
            {
                Lox.Error(stmt.Superclass.Name, "A class can't inherit from itself.");
            }

            if (stmt.Superclass != null)
            {
                _currentClass = ClassType.SUBCLASS;
                Resolve(stmt.Superclass);
                BeginScope();
                _scopes.Peek()["super"] = true;
            }

            BeginScope();
            _scopes.Peek()["this"] = true;
            foreach (var method in stmt.Methods)
            {
                var declaration = FunctionType.METHOD;
                if (method.Name.Lexeme.Equals("init", StringComparison.Ordinal))
                {
                    declaration = FunctionType.INITIALIZER;
                }

                ResolveFunction(method, declaration);
            }
            EndScope();

            if (stmt.Superclass != null)
            {
                EndScope();
            }

            _currentClass = enclosingClass;
            return(null);
        }
Example #4
0
        public Void VisitClassStmt(Stmt.Class stmt)
        {
            object superclass = null;

            if (stmt.Superclass != null)
            {
                superclass = Evaluate(stmt.Superclass);

                if (!(superclass is LoxClass))
                {
                    throw new RuntimeError(stmt.Superclass.Name, "Superclass must be a class");
                }
            }
            _environment.Define(stmt.Name.Lexeme, null);

            if (stmt.Superclass != null)
            {
                _environment = new LoxEnvironment(_environment);
                _environment.Define("super", superclass);
            }

            var methods = new Dictionary <string, LoxFunction>();

            foreach (var method in stmt.Methods)
            {
                var isInitializer = method.Name.Lexeme.Equals("init", StringComparison.Ordinal);
                var function      = new LoxFunction(method, _environment, isInitializer);
                methods[method.Name.Lexeme] = function;
            }

            var lclass = new LoxClass(stmt.Name.Lexeme, superclass as LoxClass, methods);

            if (superclass != null)
            {
                _environment = _environment.Enclosing;
            }

            _environment.Assign(stmt.Name, lclass);
            return(null);
        }
Example #5
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            object superclass = null;

            if (stmt.Superclass != null)
            {
                superclass = Evaluate(stmt.Superclass);
                if (!(superclass is LoxClass))
                {
                    throw new RuntimeError(stmt.Superclass.Name, "Superclass must be a class.");
                }
            }

            environment.Define(stmt.Name.Lexeme, null);

            if (stmt.Superclass != null)
            {
                environment = new Environment(environment);
                environment.Define("super", superclass);
            }

            Dictionary <string, LoxFunction> methods = new Dictionary <string, LoxFunction>();

            foreach (var method in stmt.Methods)
            {
                LoxFunction function = new LoxFunction(method, environment, method.Name.Lexeme.Equals("init"));
                methods[method.Name.Lexeme] = function;
            }

            LoxClass klass = new LoxClass(stmt.Name.Lexeme, (LoxClass)superclass, methods);

            if (superclass != null)
            {
                environment = environment.Enclosing;
            }

            environment.Assign(stmt.Name, klass);
            return(null);
        }
Example #6
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            Declare(stmt.name);
            Define(stmt.name);

            ClassType enclosingClass = currentClass;

            currentClass = ClassType.CLASS;

            BeginScope();
            if (stmt.superclass != null)
            {
                currentClass = ClassType.SUBCLASS;
                Resolve(stmt.superclass);
                CurrentScope.TryAdd("super", true);
            }
            CurrentScope.TryAdd("this", true);
            stmt.methods.ForEach(m => ResolveFunction(m, (m.name.lexeme == "init" ? FunctionType.INITIALIZER : FunctionType.METHOD)));
            EndScope();

            currentClass = enclosingClass;
            return(null);
        }
        public object VisitClassStmt(Stmt.Class stmt)
        {
            environment.Define(stmt.name.lexeme, null);

            // Try to inherit if superclass is defined.
            object superclass = null;

            if (stmt.superclass != null)
            {
                superclass = Evaluate(stmt.superclass);
                if (!(superclass is LoxClass))
                {
                    throw new RuntimeError(stmt.name, "Superclass must be a class");
                }
            }

            // Static methods are attached to the metaclass.
            var staticMethods = stmt.methods.Where(m => m.isStatic)
                                .ToDictionary(m => m.name.lexeme, m => new LoxFunction(m, environment, m.name.lexeme == "this"));

            // Metaclass inherits the metaclass of the superclass, if it exists.
            var metaClass = new MetaClass($"{stmt.name.lexeme}Meta", ((LoxInstance)superclass)?.InstanceOf, staticMethods);

            // Normal methods go to the class.
            var methods = stmt.methods.Where(m => !m.isStatic)
                          .ToDictionary(m => m.name.lexeme, m => new LoxFunction(m, environment, m.name.lexeme == "this"));

            var klass = new LoxClass(stmt.name.lexeme, (IClass)superclass, metaClass, methods);

            var staticInitilizer = metaClass.FindMethod(klass, "init");

            staticInitilizer?.Call(this, null);

            environment.Assign(stmt.name, klass);
            return(null);
        }
Example #8
0
 public string VisitClassStmt(Stmt.Class stmt)
 {
     throw new NotImplementedException();
 }
Example #9
0
 public string VisitClassStmt(Stmt.Class stmt) => throw new System.NotImplementedException();