예제 #1
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            Declare(stmt.Name);
            Define(stmt.Name);

            var enclosingClass = currentClass;

            currentClass = ClassType.CLASS;

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

            stmt.Methods.ForEach(m =>
            {
                var declaration = FunctionType.METHOD;

                if (m.name.Lexeme == "init")
                {
                    declaration = FunctionType.INITIALIZER;
                }
                ResolveFunction(m, declaration);
            });

            currentClass = enclosingClass;

            EndScope();
            return(null);
        }
예제 #2
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            environment.Define(stmt.Name.Lexeme, null);

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

            stmt.Methods.ForEach(m =>
            {
                var function = new LoxFunction(m, environment, m.name.Equals("init"));
                methods.Add(m.name.Lexeme, function);
            });

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

            environment.Assign(stmt.Name, klass);
            return(null);
        }
예제 #3
0
        object Stmt.IVisitor <object> .VisitClassStmt(Stmt.Class stmt)
        {
            ClassType enclosingClass = currentClass;

            currentClass = ClassType.CLASS;

            Declare(stmt.name);

            if (stmt.superclass != null)
            {
                currentClass = ClassType.SUBCLASS;
                BeginScope();
                scopes.Peek()["super"] = true;
                Resolve(stmt.superclass);
            }

            Define(stmt.name);

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

            foreach (Stmt.Function 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);
        }
예제 #4
0
        object Stmt.IVisitor <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 (Stmt.Function 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);
        }
예제 #5
0
파일: Resolver.cs 프로젝트: leddt/cslox
        public Void Visit(Stmt.Class stmt)
        {
            Declare(stmt.Name);
            Define(stmt.Name);

            var enclosingClass = currentClass;

            currentClass = ClassType.Class;

            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 decl = method.Name.Lexeme.Equals("init")
                    ? FunctionType.Initializer
                    : FunctionType.Method;

                ResolveFunction(method, decl);
            }

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

            currentClass = enclosingClass;

            return(Void.Instance);
        }
예제 #6
0
파일: Interpreter.cs 프로젝트: leddt/cslox
        public Void Visit(Stmt.Class stmt)
        {
            environment.Define(stmt.Name.Lexeme, null);

            object superclass = null;

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

                environment = new Environment(environment);
                environment.Define("super", superclass);
            }

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

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

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

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

            environment.Assign(stmt.Name, klass);

            return(null);
        }