public LoxFunction Bind(LoxInstance instance) { Environment environment = new Environment(closure); environment.Define("this", instance); return(new LoxFunction(declaration, environment, isInitializer)); }
public object Call(Interpreter interpreter, List <object> arguments) { LoxInstance instance = new LoxInstance(this); if (methods.TryGetValue("init", out LoxFunction initializer)) { initializer.Bind(instance).Call(interpreter, arguments); } return(instance); }
public LoxFunction FindMethod(LoxInstance instance, string name) { if (methods.ContainsKey(name)) { return(methods[name].Bind(instance)); } if (superclass != null) { return(superclass.FindMethod(instance, name)); } return(null); }
public object VisitSuperExpr(Expr.Super expr) { int distance = locals[expr]; LoxClass superclass = (LoxClass)environment.GetAt(distance, "super"); // "this" is always one level nearer than "super"'s environment. LoxInstance objekt = (LoxInstance)environment.GetAt(distance - 1, "this"); LoxFunction method = superclass.FindMethod(objekt, expr.Method.Lexeme); if (method == null) { throw new RuntimeError(expr.Method, $"Undefined property '{expr.Method.Lexeme}'."); } return(method); }