示例#1
0
        public object visitSuperExpr(Expr.Super super)
        {
            int distance = -1;

            locals.TryGetValue(super, out distance);
            List <LoxClass> superClasses = (List <LoxClass>)environment.getAt(distance, "super");
            LoxInstance     _object      = (LoxInstance)environment.getAt(distance - 1, "this");
            LoxFunction     method       = null;
            LoxClass        foundInClass = null;

            foreach (LoxClass superClass in superClasses)
            {
                LoxFunction methodFind = superClass.findMethod(super.method.lexeme);
                if (methodFind != null)
                {
                    if (method != null)
                    {
                        throw new Exceptions.RuntimeError(super.method, "Error: Found '" + super.method.lexeme + "' in " + foundInClass.name + " and " + superClass.name + ".");
                    }
                    method       = methodFind;
                    foundInClass = superClass;
                }
            }
            if (method == null)
            {
                throw new Exceptions.RuntimeError(super.method, "Undefined property '" + super.method.lexeme + "'.");
            }
            return(method.bind(_object));
        }
示例#2
0
        public object call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance    = new LoxInstance(this);
            LoxFunction initializer = findMethod("init");

            if (initializer != null)
            {
                initializer.bind(instance).call(interpreter, arguments);
            }
            return(instance);
        }
示例#3
0
        public object call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance    = new LoxInstance(this);
            LoxFunction initializer = methods.ContainsKey("init") ? methods["init"] : null;

            if (initializer != null)
            {
                initializer.bind(instance).call(interpreter, arguments);
            }
            return(instance);
        }
示例#4
0
        public object get(Token name)
        {
            if (fields.ContainsKey(name.lexeme))
            {
                return(fields[name.lexeme]);
            }
            LoxFunction method = _class.findMethod(name.lexeme);

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

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