public object Call(Interpreter interpreter, List <object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initializer = FindMethod("init"); initializer?.Bind(instance).Call(interpreter, arguments); return(instance); }
public Object Call(Interpreter interpreter, List <Object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initializer = FindMethod("init"); if (initializer != null) { initializer.Bind(instance).Call(interpreter, arguments); } return(instance); }
public object VisitSuperExpr(Expr.Super expr) { int? distance = _locals[expr]; LoxClass superclass = (LoxClass)_environment.GetAt((int)distance, "super"); LoxInstance @object = (LoxInstance)_environment.GetAt((int)distance - 1, "this"); LoxFunction method = superclass.FindMethod(expr.Method.Lexeme); if (method == null) { throw new RuntimeError(expr.Method, $"Undefined method '{expr.Method.Lexeme}'."); } return(method.Bind(@object)); }
public object Get(Token name) { if (fields.TryGetValue(name.Lexeme, out object value)) { return(value); } LoxFunction method = @class.FindMethod(name.Lexeme); if (method != null) { return(method.Bind(this)); } throw new RuntimeException(name, $"Undefined property '{name.Lexeme}'."); }
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 RuntimeError(name, $"Undefined property '{name.Lexeme}'."); }
public object VisitSuperExpr(Super expr) { int distance = locals[expr]; 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(expr.Method.Lexeme); if (method == null) { throw new RuntimeException(expr.Method, $"Undefined property '{expr.Method.Lexeme}'."); } return(method.Bind(@object)); }
public Object Visit_SuperExpr(SuperExpr expr) { int distance = _locals[expr]; LoxClass superclass = (LoxClass)_environment.GetAt(distance, "super"); // "this" is always one level nearer than "super"'s environment. LoxInstance obj = (LoxInstance)_environment.GetAt(distance - 1, "this"); string name = (string)expr.method.value; LoxFunction method = superclass.FindMethod(name); if (method == null) { throw new RuntimeException(expr.method.line, expr.method.column, "Undefined property '" + name + "'."); } return(method.Bind(obj)); }
public Object Get(Token nameToken) { var name = (string)nameToken.value; var hasKey = _fields.TryGetValue(name, out Object value); if (hasKey) { return(value); } LoxFunction method = _klass.FindMethod(name); if (method != null) { return(method.Bind(this)); } throw new RuntimeException(nameToken.line, nameToken.column, "Undefined property '" + name + "'."); }