Beispiel #1
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);
        }
Beispiel #2
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));
        }
Beispiel #3
0
        public object Get(Token name)
        {
            // Fields shadow properties
            if (fields.TryGetValue(name.lexeme, out object value))
            {
                return(value);
            }

            // Property
            LoxFunction method = loxClass.FindMethod(name.lexeme);

            if (method != null)
            {
                return(method.Bind(this));
            }

            throw new RuntimeError(name, "Undefined property '" + name.data + "'.");
        }