/// <summary> /// Bind 'this' /// </summary> /// <param name="instance">The instance</param> /// <returns></returns> public LoxFunction Bind(LoxInstance instance) { LoxEnvironment environment = new LoxEnvironment(_closure); environment.Define("this", instance); return(new LoxFunction(_declaration, environment, _is_initializer)); }
public object Call(Interpreter interpreter, IList <object> arguments) { LoxInstance instance = new LoxInstance(this); // Constructor LoxFunction initializer = _methods.Get("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.Get(name).Bind(instance)); } // Check the superclass if we are inherited if (this.Superclass != null) { return(this.Superclass.FindMethod(instance, name)); } return(null); }
public object Visit(Expr.Super expr) { // Look up the superclass int? distance = _locals.Get(expr); LoxClass superclass = (LoxClass)_environment.GetAt(distance.Value, "super"); // "this" is always one level nearer than "super" LoxInstance obj = (LoxInstance)_environment.GetAt(distance.Value - 1, "this"); // Lookup the method LoxFunction method = superclass.FindMethod(obj, expr.Method.Lexeme); if (method == null) { throw new RuntimeErrorException(expr.Method, $"Undefined property '{expr.Method.Lexeme}'."); } return(method); }