public CoxFunction Bind(CoxInstance self) { Environment environment = new Environment(Closure); environment.Define("this", self); return(new CoxFunction(Declaration, environment, IsInitializer)); }
public Object Call(Interpreter interpreter, List <Object> arguments) { CoxInstance instance = new CoxInstance(this); CoxFunction initializer = Methods["init"]; if (initializer != null) { initializer.Bind(instance).Call(interpreter, arguments); } return(instance); }
public CoxFunction FindMethod(CoxInstance instance, String name) { CoxClass klass = this; while (klass != null) { if (klass.Methods.ContainsKey(name)) { return(klass.Methods[name].Bind(instance)); } klass = klass.Superclass; } return(null); }
public object VisitSuperExpr(Super expr) { int distance = Locals[expr]; CoxClass superclass = (CoxClass)Environment.GetAt(distance, "super"); // "this" is always one level nearer than "super"'s environment. CoxInstance receiver = (CoxInstance)Environment.GetAt(distance - 1, "this"); CoxFunction method = superclass.FindMethod(receiver, expr.Method.Lexeme); if (method == null) { throw new RuntimeError(expr.Method, $"Undefined property '{expr.Method.Lexeme}'."); } return(method); }