Beispiel #1
0
        public Void Visit_ClassStmt(ClassStmt stmt)
        {
            ClassType enclosingClass = _currentClass;

            _currentClass = ClassType.Class;

            Declare(stmt.name);
            Define(stmt.name);

            if (stmt.superclass != null && (string)stmt.name.value == (string)stmt.superclass.name.value)
            {
                _errors.Add(new Error(stmt.name, ErrorType.InvalidSuperclass, "A class cannot inherit from itself."));
            }

            if (stmt.superclass != null)
            {
                _currentClass = ClassType.Subclass;
                Resolve(stmt.superclass);
                BeginScope();
                _scopes.Peek().Add("super", true);
            }

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

            foreach (var method in stmt.methods)
            {
                var ft = FunctionType.Method;
                if ((string)method.name.value == "init")
                {
                    ft = FunctionType.Initializer;
                }
                ResolveFunction(method, ft);
            }
            EndScope();

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

            _currentClass = enclosingClass;
            return(null);
        }
Beispiel #2
0
        public Void Visit_ClassStmt(ClassStmt stmt)
        {
            Object superclass = null;

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

            var name = (string)stmt.name.value;

            _environment.Define(name, 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, name == "init");
                methods.Add((string)method.name.value, function);
            }

            LoxClass klass = new LoxClass((string)stmt.name.value, (LoxClass)superclass, methods);

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

            _environment.Assign(stmt.name, klass);
            return(null);
        }
Beispiel #3
0
        public object VisitClassStmt(ClassStmt stmt)
        {
            Declare(stmt.Name);
            Define(stmt.Name);

            var enclosingClassType = _currentClassType;

            _currentClassType = ClassType.CLASS;

            if (stmt.SuperClass != null)
            {
                _currentClassType = ClassType.SUBCLASS;
                Resolve(stmt.SuperClass);
                BeginScope();
                _scopes.Peek().Add("super", true);
            }

            BeginScope();

            _scopes.Peek().Add("this", true);
            foreach (var method in stmt.Methods)
            {
                var declaration = method.Name.Lexeme.Equals("init")
                    ? FunctionType.INITIALIZER
                    : FunctionType.METHOD;

                ResolveFunction(method, declaration);
            }

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

            _currentClassType = enclosingClassType;

            return(null);
        }
Beispiel #4
0
        public object VisitClassStmt(ClassStmt stmt)
        {
            _scope.Define(stmt.Name.Lexeme, null);
            var superClass = default(LoxClass);

            if (stmt.SuperClass != null)
            {
                superClass = Evaluate(stmt.SuperClass) as LoxClass;
                if (superClass == null)
                {
                    throw new LoxRuntimeException(stmt.SuperClass.Name, "Base class must be a class.");
                }

                _scope = new Scope(_scope);
                _scope.Define("super", superClass);
            }

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

            foreach (var method in stmt.Methods)
            {
                var func = new LoxFunction(method, _scope, method.Name.Lexeme.Equals("init"));
                methods.Add(method.Name.Lexeme, func);
            }

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

            if (superClass != null)
            {
                _scope = _scope.Parent;
            }

            _scope.Assign(stmt.Name, klass);

            return(null);
        }
Beispiel #5
0
        public string Visit_ClassStmt(ClassStmt stmt)
        {
            var name = (string)stmt.name.value;

            return(Parenthesize("ClassDecl " + name, stmt.methods.ToArray()));
        }