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); LoxFunction initializer = findMethod("init"); if (initializer != null) { initializer.bind(instance).call(interpreter, arguments); } return(instance); }
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 object_ = (LoxInstance)environment.getAt(distance - 1, "this"); LoxFunction method = superclass.findMethod(expr.method.lexeme()); if (method == null) { throw new RuntimeError(expr.method, "Undefined property '" + expr.method.lexeme() + "'."); } return(method.bind(object_)); }