Beispiel #1
0
        public object VisitFunctionStmt(Stmt.Function stmt)
        {
            var function = new LoxFunction(stmt,
                                           environment,
                                           false);

            environment.Define(stmt.Name.Lexeme, function);
            return(null);
        }
Beispiel #2
0
        public object VisitClassStmt(Stmt.Class stmt)
        {
            object superclass = null;

            if (stmt.Superclass != null)
            {
                superclass = Evaluate(stmt.Superclass);
                if (!(superclass is LoxClass))
                {
                    throw new InterpretingException(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)
            {
                var function = new LoxFunction(method,
                                               environment,
                                               method.Name.Lexeme == stmt.Name.Lexeme);
                methods[method.Name.Lexeme] = function;
            }

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

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

            environment.Assign(stmt.Name, lclass);
            return(null);
        }