Beispiel #1
0
        public object VisitSuperExpr(Expr.Super expr)
        {
            int distance;

            if (locals.TryGetValue(expr, out distance))
            {
                LoxClass    superclass = (LoxClass)environment.GetAt(distance, "super");
                LoxInstance obj        = (LoxInstance)environment.GetAt(distance - 1, "this");

                LoxFunction method = null;
                if (expr.Method != null)
                {
                    method = superclass.FindMethod(expr.Method.Lexeme);
                }
                else
                {
                    method = superclass.FindMethod(superclass.Name);
                }

                if (method == null)
                {
                    throw new InterpretingException(expr.Method,
                                                    $"Undefined property '{expr.Method.Lexeme}'.");
                }

                return(method.Bind(obj));
            }

            return(null);
        }
Beispiel #2
0
        public LoxFunction Bind(LoxInstance instance)
        {
            var environment = new Environment(closure);

            environment.Define("this", instance);
            return(new LoxFunction(declaration, environment, isInitializer));
        }
Beispiel #3
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            var instance    = new LoxInstance(this);
            var initializer = FindMethod(this.name);

            if (initializer != null)
            {
                initializer.Bind(instance).Call(interpreter, arguments);
            }

            return(instance);
        }