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); }
private object EvaluateSuperExpression(SuperExpression expr) { int distance = _locals[expr]; LoxClass superclass = _environment.GetAt(distance, new Token(SyntaxKind.Super, "super", null, 0)) as LoxClass; LoxInstance obj = _environment.GetAt(distance - 1, new Token(SyntaxKind.This, "this", null, 0)) as LoxInstance; LoxFunction method = superclass.FindMethod(expr.Method.Lexeme); if (method is null) { throw new RuntimeError(expr.Method, $"Undefine property {expr.Method.Lexeme}"); } return(method.Bind(obj)); }
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)); }
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)); }