public LoxFunction findMethod(LoxInstance instance, string name) { if (methods.ContainsKey(name)) { return(methods[name].bind(instance)); } if (superclass != null) { return(superclass.findMethod(instance, name)); } return(null); }
public object get(Token name) { if (fields.ContainsKey(name.lexeme)) { return(fields[name.lexeme]); } LoxFunction method = _class.findMethod(name.lexeme); if (method != null) { return(method.bind(this)); } throw new Exceptions.RuntimeError(name, "Undefined property '" + name.lexeme + "'."); }
public object get(Token name) { if (fields.ContainsKey(name.lexeme)) { return(fields[name.lexeme]); } LoxFunction method = klass.findMethod(this, name.lexeme); if (method != null) { return(method); } throw new RuntimeError(name, $"Undefined properrty '{name.lexeme}'."); }
public object visit_Super_Expr(GExpr.Super expr) { int distance = locals[expr]; LoxClass superClass = (LoxClass)environment.getAt(distance, "super"); // "this" is always 1 level nearer than "super's" environment LoxInstance obj = (LoxInstance)environment.getAt(distance - 1, "this"); LoxFunction method = superClass.findMethod(obj, expr.method.lexeme); if (method == null) { throw new RuntimeError(expr.method, $"Undefined property '{expr.method.lexeme}'"); } return(method); }