Example #1
0
        public LoxFunction Bind(LoxInstance instance)
        {
            Environment closureWithThis = new Environment(enclosing: closure);

            closureWithThis.Define(instance);
            return(new LoxFunction(function, closure: closureWithThis, isInitializer));
        }
Example #2
0
        public object Call(Interpreter interpreter, List <object> args)
        {
            LoxInstance instance = new LoxInstance(this);

            if (initializer != null)
            {
                initializer.Bind(instance).Call(interpreter, args);
            }
            return(instance);
        }
Example #3
0
        public object visitSuperExpr(Expr.Super expr)
        {
            var      exprLocationData = locals[expr];
            LoxClass superclass       = (LoxClass)environment.GetAt(exprLocationData.depth, exprLocationData.index);
            // We know "this" is one environment removed from "super" because of the way we set the environments up
            // in visitClassStmt. Hacky fix but it works. We also know its index is 0 because "this" is the only variable
            // in its environment.
            LoxInstance instance = (LoxInstance)environment.GetAt(exprLocationData.depth - 1, 0);
            LoxFunction method   = superclass.FindMethod(expr.method.lexeme);

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