Пример #1
0
        public LoxFunction Bind(LoxInstance instance)
        {
            Environment environment = new Environment(closure);

            environment.Define("this", instance);
            return(new LoxFunction(declaration, environment, isInitializer));
        }
Пример #2
0
        internal LoxFunction FindMethod(LoxInstance instance, string name)
        {
            if (_methods.ContainsKey(name))
            {
                return(_methods[name].Bind(instance));
            }

            return(null);
        }
Пример #3
0
        public object Call(Interpreter interpreter, object[] arguments)
        {
            var instance = new LoxInstance(this);

            FindMethod(instance, "init")
            ?.Call(interpreter, arguments);

            return(instance);
        }
Пример #4
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            var instance = new LoxInstance(this);

            var initializer = _methods.ContainsKey("init") ? _methods["init"] : null;

            initializer?.Bind(instance).Call(interpreter, arguments);

            return(instance);
        }
Пример #5
0
        public override object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance    = new LoxInstance(this);
            LoxFunction initializer = methods["init"];

            if (initializer != null)
            {
                initializer.Bind(instance).Call(interpreter, arguments);
            }
            return(instance);
        }
Пример #6
0
        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);
        }
Пример #7
0
        object Expr.IVisitor <object> .VisitSuperExpr(Expr.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(obj, expr.method.lexeme);

            if (method == null)
            {
                throw new RuntimeError(expr.method, "Undefined property '" + expr.method.lexeme + "'.");
            }

            return(method);
        }
Пример #8
0
 public LoxFunction FindMethod(LoxInstance instance, string name)
 {
     return(methods.ContainsKey(name)
         ? methods[name].Bind(instance)
         : superclass?.FindMethod(instance, name));
 }