public object VisitFunctionStmt(Function stmt) { var function = new LoxFunction(stmt, _environment, false); _environment.Define(stmt.Name.Lexeme, function); return(null); }
public object VisitClassStmt(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); } var methods = new Dictionary <string, LoxFunction>(); foreach (var method in stmt.Methods) { var isInitializer = method.Name.Lexeme == "init"; var function = new LoxFunction(method, _environment, isInitializer); methods.Add(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); }