public object Call(Interpreter interpreter, List <object> arguments) { var instance = new LoxInstance(this); FindMethod("init")?.Bind(instance).Call(interpreter, arguments); return(instance); }
public LoxFunction Bind(LoxInstance instance) { var environment = new Environment(Closure); environment.Define("this", instance); return(new LoxFunction(Declaration, environment, IsInitializer)); }
public LoxFunction bind(LoxInstance instance) { Environment environment = new Environment(closure); environment.define("this", instance); return(new LoxFunction(declaration, environment, isInitializer, isGet)); }
public object visitSuperExpr(Expr.Super super) { int distance = -1; locals.TryGetValue(super, out distance); List <LoxClass> superClasses = (List <LoxClass>)environment.getAt(distance, "super"); LoxInstance _object = (LoxInstance)environment.getAt(distance - 1, "this"); LoxFunction method = null; LoxClass foundInClass = null; foreach (LoxClass superClass in superClasses) { LoxFunction methodFind = superClass.findMethod(super.method.lexeme); if (methodFind != null) { if (method != null) { throw new Exceptions.RuntimeError(super.method, "Error: Found '" + super.method.lexeme + "' in " + foundInClass.name + " and " + superClass.name + "."); } method = methodFind; foundInClass = superClass; } } if (method == null) { throw new Exceptions.RuntimeError(super.method, "Undefined property '" + super.method.lexeme + "'."); } return(method.bind(_object)); }
public LoxFunction Bind(LoxInstance instance) { var env = new Environment(_closure); env.Define("this", instance); return(new LoxFunction(_declaration, env, _isInitializer)); }
public LoxFunction Bind(LoxInstance instance, IClass superClass) { var environment = new Environment(closure); environment.Define("this", instance); environment.Define("super", superClass); return(new LoxFunction(declaration, environment, isInitializer)); }
public object Call(Interpreter interpreter, List <object> arguments) { var instance = new LoxInstance(this); var initializer = FindMethod("init"); if (initializer != null) { initializer.Bind(instance).Call(interpreter, arguments); } return(instance); }
public object call(Interpreter interpreter, List <object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initializer = methods.ContainsKey("init") ? methods["init"] : null; if (initializer != null) { initializer.bind(instance).call(interpreter, arguments); } return(instance); }
/// <summary> /// When a class is called with (), we construct a new instance (aka an object) of it. /// </summary> public object Call(Interpreter interpreter, List <object> arguments) { var instance = new LoxInstance(this); if (methods.TryGetValue("init", out LoxFunction initializer)) { initializer.Bind(instance, superclass).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 Call(Evaluator evaluator, List <object> arguments) { LoxInstance instance = new LoxInstance(this); LoxFunction initializer = FindMethod("init"); if (initializer != null) { initializer.Bind(instance).Call(evaluator, arguments); } return(instance); }
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); }
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(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); }
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); }
private object EvaluateSetExpression(SetExpression expr) { object obj = Evaluate(expr.Object); LoxInstance instance = obj as LoxInstance; if (instance is null) { throw new RuntimeError(expr.Name, "Only instances have fields"); } object value = Evaluate(expr.Value); instance.Set(expr.Name, value); return(value); }
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)); }
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)); }
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); }