public LoxFunction FindMethod(string name) { if (_methods.TryGetValue(name, out var method)) { return(method); } if (_superclass != null) { return(_superclass.FindMethod(name)); } return(null); }
private object EvaluateSuperExpression(SuperExpression expr) { int distance = _locals[expr]; LoxClass superclass = (LoxClass)_environment.GetAt(distance, new Token(TokenType.Super, "super", null, 0)); LoxInstance obj = (LoxInstance)_environment.GetAt(distance - 1, new Token(TokenType.This, "this", null, 0)); var method = superclass.FindMethod(expr.Method.Lexeme); if (method == null) { throw new RuntimeError(expr.Method, $"Undefine property {expr.Method.Lexeme}"); } return(method.Bind(obj)); }
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]); } LoxFunction method = klass.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeError(name, "Undefined property '" + name.Lexeme + "'."); }
public object VisitSuperExpr(Super expr) { int distance = locals[expr]; LoxClass superclass = (LoxClass)environment.GetAt(distance, "super"); LoxInstance obj = (LoxInstance)environment.GetAt(distance - 1, "this"); LoxFunction method = superclass.FindMethod(expr.Method.Lexeme); if (method == null) { throw new RuntimeError(expr.Method, "Undefined property '" + expr.Method.Lexeme + "'."); } return(method.Bind(obj)); }
public object Get(Token name) { if (_fields.ContainsKey(name.Lexeme)) { return(_fields[name.Lexeme]); } var method = _class.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeError(name, $"Undefined property {name.Lexeme}."); }
public object Get(Token name) { if (_fields.TryGetValue(name.Lexeme, out var value)) { return(value); } var method = _class.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeError(name, $"Undefined property '{name.Lexeme}'."); }
object Expr.IVisitor <object> .Visit(Expr.Super _super) { int distance = _locals[_super]; LoxClass superclass = (LoxClass)_environment.GetAt(distance, "super"); // "this" is always one level nearer than "super"'s environment. LoxInstance _object = (LoxInstance)_environment.GetAt(distance - 1, "this"); LoxFunction method = superclass.FindMethod(_super.method.lexeme); if (method == null) { throw new RuntimeError(_super.method, "Undefined property '" + _super.method.lexeme + "'."); } return(method.Bind(_object)); }