Beispiel #1
0
        public int Arity()
        {
            LoxFunction initialiser = FindMethod("init");

            if (initialiser == null)
            {
                return(0);
            }
            return(initialiser.Arity());
        }
Beispiel #2
0
        public object Call(Interpreter interpreter, List <object> arguments)
        {
            LoxInstance instance   = new LoxInstance(this);
            LoxFunction initaliser = FindMethod("init");

            if (initaliser != null)
            {
                initaliser.Bind(instance)                   // runtime binding
                .Call(interpreter, arguments);              // Create instnace.
            }
            return(instance);
        }
Beispiel #3
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);
    }
Beispiel #4
0
    public object VisitSuperExpr(Expr.Super expr)
    {
        int         distance   = Locals[expr];
        LoxClass    superclass = (LoxClass)Environment.GetAt(distance, "super");
        LoxInstance instance   = (LoxInstance)Environment.GetAt(
            distance - 1, "this");
        LoxFunction method = superclass.FindMethod(expr.Method.Lexeme);

        if (method == null)
        {
            throw new RuntimeException(expr.Method,
                                       $"Undefined property '{expr.Method.Lexeme}'.");
        }

        return(method.Bind(instance));
    }
Beispiel #5
0
    public object VisitClassStmt(Stmt.Class stmt)
    {
        object superclass = null;

        if (stmt.Superclass != null)
        {
            superclass = Evaluate(stmt.Superclass);

            if (!(superclass is LoxClass))
            {
                throw new RuntimeException(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, LoxFunction> methods =
            new Dictionary <string, LoxFunction>();

        foreach (Stmt.Function method in stmt.Methods)
        {
            LoxFunction function = new LoxFunction(method, Environment,
                                                   method.Name.Lexeme.Equals("init"));
            methods[method.Name.Lexeme] = function;
        }

        LoxClass klass = new LoxClass(stmt.Name.Lexeme,
                                      (LoxClass)superclass, methods);

        if (superclass != null)
        {
            Environment = Environment.Enclosing;
        }

        Environment.Assign(stmt.Name, klass);
        return(null);
    }
Beispiel #6
0
        public object Get(Token name)
        {
            if (fields.ContainsKey(name.Lexeme))
            {
                return(fields[name.Lexeme]);
            }
            /// When we are returning a method
            /// Before it's actually called '()'
            /// Ensure this class instance environment as a closure around the method,
            /// Which means 'this' is correct no matter where the method is called eg callback.
            LoxFunction method = loxClass.FindMethod(name.Lexeme);

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

            throw new RuntimeError(name, $"Undefined property '{name.Lexeme}'");
        }
Beispiel #7
0
    public object Get(Token name)
    {
        object value;

        if (Fields.TryGetValue(name.Lexeme, out value))
        {
            return(value);
        }

        LoxFunction method = Klass.FindMethod(name.Lexeme);

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

        throw new RuntimeException(name,
                                   $"Undefined property '{name.Lexeme}'.");
    }