public object VisitSuperExpr(Expr.Super expr) { int distance; if (locals.TryGetValue(expr, out distance)) { LoxClass superclass = (LoxClass)environment.GetAt(distance, "super"); LoxInstance obj = (LoxInstance)environment.GetAt(distance - 1, "this"); LoxFunction method = null; if (expr.Method != null) { method = superclass.FindMethod(expr.Method.Lexeme); } else { method = superclass.FindMethod(superclass.Name); } if (method == null) { throw new InterpretingException(expr.Method, $"Undefined property '{expr.Method.Lexeme}'."); } return(method.Bind(obj)); } return(null); }
public LoxFunction FindMethod(string name) { if (methods.ContainsKey(name)) { return(methods[name]); } if (superclass != null) { return(superclass.FindMethod(name)); } return(null); }
public object Get(Token name) { if (fields.ContainsKey(name.Lexeme)) { return(fields[name.Lexeme]); } var method = loxClass.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new InterpretingException(name, $"Undefined property '{name.Lexeme}'."); }