Beispiel #1
0
        public object Call(Interpreter interpreter, IList <object> arguments)
        {
            LoxInstance instance = new LoxInstance(this);

            // Constructor
            LoxFunction initializer = _methods.Get("init");

            if (initializer != null)
            {
                initializer.Bind(instance).Call(interpreter, arguments);
            }

            return(instance);
        }
Beispiel #2
0
        /// <summary>
        /// Get a property
        /// </summary>
        /// <param name="name">The name</param>
        /// <returns>The property</returns>
        public object Get(Token name)
        {
            // Field?
            if (_fields.TryGetValue(name.Lexeme, out object val))
            {
                return(val);
            }

            // Method?
            LoxFunction method = _class.FindMethod(this, name.Lexeme);

            if (method != null)
            {
                return(method);
            }

            throw new RuntimeErrorException(name, $"Undefined property '{name.Lexeme}'.");
        }
Beispiel #3
0
        public object Visit(Expr.Super expr)
        {
            // Look up the superclass
            int?     distance   = _locals.Get(expr);
            LoxClass superclass = (LoxClass)_environment.GetAt(distance.Value, "super");

            // "this" is always one level nearer than "super"
            LoxInstance obj = (LoxInstance)_environment.GetAt(distance.Value - 1, "this");

            // Lookup the method
            LoxFunction method = superclass.FindMethod(obj, expr.Method.Lexeme);

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

            return(method);
        }
Beispiel #4
0
        public object Visit(Stmt.Class stmt)
        {
            _environment.Define(stmt.Name.Lexeme, null);

            // Superclass
            object superclass = null;

            if (stmt.Superclass != null)
            {
                superclass = Evaluate(stmt.Superclass);
                if (!(superclass is LoxClass))
                {
                    throw new RuntimeErrorException(stmt.Superclass.Name, "Superclass must be a class.");
                }

                _environment = new LoxEnvironment(_environment);
                _environment.Define("super", superclass);
            }


            // Methods
            HashMap <string, LoxFunction> methods = new HashMap <string, LoxFunction>();

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

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

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

            _environment.Assign(stmt.Name, @class);
            return(null);
        }