Exemplo n.º 1
0
        public LoxFunction Bind(LoxInstance instance)
        {
            Environment environment = new Environment(closure);

            environment.Define("this", instance);
            return(new LoxFunction(declaration, environment, isInitializer));
        }
Exemplo n.º 2
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance = new LoxInstance(this);

            if (methods.ContainsKey("init"))
            {
                methods["init"].Bind(instance).Call(interpreter, arguments);
            }

            return(instance);
        }
Exemplo n.º 3
0
        public LoxFunction FindMethod(LoxInstance instance, string name)
        {
            LoxFunction ret = null;

            if (methods.ContainsKey(name))
            {
                ret = methods[name].Bind(instance);
            }
            else if (superclass != null)
            {
                ret = superclass.FindMethod(instance, name);
            }

            return(ret);
        }
Exemplo n.º 4
0
        public object Visit(Expr.Super 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");

            LoxFunction method = superclass.FindMethod(obj, expr.method.lexeme);

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

            return(method);
        }