Exemplo n.º 1
0
        public int Arity()
        {
            LoxFunction initilizer = FindMethod("init");

            if (initilizer == null)
            {
                return(0);
            }
            return(initilizer.Arity());
        }
Exemplo n.º 2
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance   = new LoxInstance(this);
            LoxFunction initilizer = FindMethod("init");

            if (initilizer != null)
            {
                initilizer.Bind(instance).Call(interpreter, arguments);
            }
            return(instance);
        }
Exemplo n.º 3
0
        internal object Get(Token name)
        {
            if (fields.ContainsKey(name.lexeme))
            {
                return(fields[name.lexeme]);
            }

            LoxFunction method = klass.FindMethod(name.lexeme);

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

            throw new RuntimeError(name, "Undefined property '" + name.lexeme + "'.");
        }
Exemplo n.º 4
0
        public object VisitClassStmt(Class stmt)
        {
            environment.Define(stmt.name.lexeme, null);

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

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

            LoxClass klass = new LoxClass(stmt.name.lexeme, methods);

            environment.Assign(stmt.name, klass);
            return(null);
        }