public object VisitSuperExpr(Expr.Super expr) { int distance = locals[expr]; // Find reference to the superclass and current instance ("this"). IClass superclass = (IClass)environment.GetAt(distance, "super"); LoxInstance thisInstance = (LoxInstance)environment.GetAt(distance, "this"); // Find method from superclass. LoxFunction method = superclass.FindMethod(thisInstance, expr.method.lexeme); return(method); }
public LoxFunction FindMethod(LoxInstance instance, string name) { if (methods.TryGetValue(name, out LoxFunction fun)) { // Methods "this" is set to a given instance (from which instance it is accessed from). return(fun.Bind(instance, superclass)); } if (superclass != null) { return(superclass.FindMethod(instance, name)); } return(null); }
// Expose properties via an indexer. public object this[Token name] { get { if (fields.TryGetValue(name.lexeme, out object val)) { return(val); } var method = instanceOf.FindMethod(this, name.lexeme); if (method != null) { return(method); } throw new RuntimeError(name, $"Undefined property '{name.lexeme}'"); } set { fields.AddOrUpdate(name.lexeme, value); } }