예제 #1
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);
    }
예제 #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);
        }
예제 #3
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));
    }
예제 #4
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}'");
        }
예제 #5
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}'.");
    }