Пример #1
0
        public object visitFunctionStmt(Stmt.Function stmt)
        {
            JingleFunc function = new JingleFunc(stmt, environment, false);

            environment.define(stmt.name.lexeme, function);
            return(null);
        }
Пример #2
0
        public int arity()
        {
            JingleFunc initializer = findMethod("init");

            if (initializer == null)
            {
                return(0);
            }

            return(initializer.arity());
        }
Пример #3
0
        public object call(Interpreter interpreter, List <object> arguments)
        {
            Instance   instance    = new Instance(this);
            JingleFunc initializer = findMethod("init");

            if (initializer != null)
            {
                initializer.bind(instance).call(interpreter, arguments);
            }

            return(instance);
        }
Пример #4
0
        public object visitSuperExpr(Expr.Super expr)
        {
            int         distance   = locals[expr];
            JingleClass superclass = (JingleClass)environment.getAt(distance, "super");

            Instance object_ = (Instance)environment.getAt(distance - 1, "this");

            JingleFunc method = superclass.findMethod(expr.method.lexeme);

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

            return(method.bind(object_));
        }
Пример #5
0
        public object visitClassStmt(Stmt.Class stmt)
        {
            object superclass = null;

            if (stmt.superclass != null)
            {
                superclass = evaluate(stmt.superclass);
                if (!(superclass is JingleClass))
                {
                    throw new RuntimeError(stmt.superclass.name, "Superclass must be a class.");
                }
            }

            environment.define(stmt.name.lexeme, null);

            if (stmt.superclass != null)
            {
                environment = new Environment(environment);
                environment.define("super", superclass);
            }

            Dictionary <string, JingleFunc> methods = new Dictionary <string, JingleFunc>();

            foreach (Stmt.Function method in stmt.methods)
            {
                JingleFunc function = new JingleFunc(method, environment, method.name.lexeme.Equals("init"));
                methods.Add(method.name.lexeme, function);
            }

            JingleClass jnclass = new JingleClass(stmt.name.lexeme, (JingleClass)superclass, methods);

            if (superclass != null)
            {
                environment = environment.enclosing;
            }
            environment.assign(stmt.name, jnclass);
            return(null);
        }