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.ContainsKey("init")) { methods["init"].Bind(instance).Call(interpreter, arguments); } return(instance); }
public LoxFunction FindMethod(LoxInstance instance, string name) { LoxFunction ret = null; if (methods.ContainsKey(name)) { ret = methods[name].Bind(instance); } else if (superclass != null) { ret = superclass.FindMethod(instance, name); } return(ret); }
public object Visit(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 obj = (LoxInstance)environment.GetAt(distance - 1, "this"); LoxFunction method = superclass.FindMethod(obj, expr.method.lexeme); if (method == null) { throw new RuntimeError(expr.method, "Undefined property '" + expr.method.lexeme + "."); } return(method); }