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 Call(Interpreter interpreter, List <object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initaliser = FindMethod("init"); if (initaliser != null) { initaliser.Bind(instance) // runtime binding .Call(interpreter, arguments); // Create instnace. } return(instance); }
public object VisitSuperExpr(Expr.Super expr) { int distance = Locals[expr]; LoxClass superclass = (LoxClass)Environment.GetAt(distance, "super"); LoxInstance instance = (LoxInstance)Environment.GetAt( distance - 1, "this"); LoxFunction method = superclass.FindMethod(expr.Method.Lexeme); if (method == null) { throw new RuntimeException(expr.Method, $"Undefined property '{expr.Method.Lexeme}'."); } return(method.Bind(instance)); }
public object Get(Token name) { if (fields.ContainsKey(name.Lexeme)) { return(fields[name.Lexeme]); } /// When we are returning a method /// Before it's actually called '()' /// Ensure this class instance environment as a closure around the method, /// Which means 'this' is correct no matter where the method is called eg callback. LoxFunction method = loxClass.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeError(name, $"Undefined property '{name.Lexeme}'"); }
public object Get(Token name) { object value; if (Fields.TryGetValue(name.Lexeme, out value)) { return(value); } LoxFunction method = Klass.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeException(name, $"Undefined property '{name.Lexeme}'."); }