public LoxFunction Bind(LoxInstance instance) { Environment environment = new Environment(closure); environment.Define("this", instance); return(new LoxFunction(declaration, environment, isInitializer)); }
internal LoxFunction FindMethod(LoxInstance instance, string name) { if (_methods.ContainsKey(name)) { return(_methods[name].Bind(instance)); } return(null); }
public object Call(Interpreter interpreter, object[] arguments) { var instance = new LoxInstance(this); FindMethod(instance, "init") ?.Call(interpreter, arguments); return(instance); }
public object Call(Interpreter interpreter, List <object> arguments) { var instance = new LoxInstance(this); var initializer = _methods.ContainsKey("init") ? _methods["init"] : null; initializer?.Bind(instance).Call(interpreter, arguments); return(instance); }
public override object Call(Interpreter interpreter, List <object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initializer = methods["init"]; if (initializer != null) { 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); }
object Expr.IVisitor <object> .VisitSuperExpr(Expr.Super expr) { int distance = locals[expr]; LoxClass superclass = (LoxClass)environment.GetAt(distance, "super"); 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); }
public LoxFunction FindMethod(LoxInstance instance, string name) { return(methods.ContainsKey(name) ? methods[name].Bind(instance) : superclass?.FindMethod(instance, name)); }