public MyVoid VisitClassStmt(Stmt.Class stmt)
        {
            ClassType enclosingClass = currentClass;

            currentClass = ClassType.Class;

            declare(stmt.name);
            define(stmt.name);

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

            foreach (Stmt.Function method in stmt.methods)
            {
                FunctionType declaration = FunctionType.Method;
                if (method.name.lexeme == "init")
                {
                    declaration = FunctionType.Initializer;
                }

                resolveFunction(method, declaration);
            }

            endScope();

            currentClass = enclosingClass;

            return(null);
        }
Exemple #2
0
        public MyVoid VisitClassStmt(Stmt.Class stmt)
        {
            environment.Define(stmt.name.lexeme, null);

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

            foreach (Stmt.Function method in stmt.methods)
            {
                LoxFunction function = new LoxFunction(method, environment, isInitializer: method.name.lexeme == "init");
                methods[method.name.lexeme] = function;
            }

            LoxClass klass = new LoxClass(stmt.name.lexeme, methods);

            environment.Assign(stmt.name, new LoxClass(stmt.name.lexeme, methods));
            return(null);
        }