Beispiel #1
0
        /// <summary>
        /// Bind 'this'
        /// </summary>
        /// <param name="instance">The instance</param>
        /// <returns></returns>
        public LoxFunction Bind(LoxInstance instance)
        {
            LoxEnvironment environment = new LoxEnvironment(_closure);

            environment.Define("this", instance);
            return(new LoxFunction(_declaration, environment, _is_initializer));
        }
Beispiel #2
0
        public object Call(Interpreter interpreter, IList <object> arguments)
        {
            // Environment
            LoxEnvironment environment = new LoxEnvironment(_closure);

            // Arguments
            for (int i = 0; i < _declaration.Parameters.Count(); i++)
            {
                environment.Define(_declaration.Parameters[i].Lexeme, arguments[i]);
            }

            // Execute
            try
            {
                interpreter.ExecuteBlock(_declaration.Body, environment);
            }
            catch (ReturnException return_value)
            {
                // We hit a return statement
                return(return_value.Value);
            }

            if (_is_initializer)
            {
                return(_closure.GetAt(0, "this"));
            }

            return(null);
        }
Beispiel #3
0
        public Interpreter(IErrorHandler error_handler)
        {
            _error_handler = error_handler;

            // We need to set this here in C#
            _environment = _globals;

            // Add some native functions
            _globals.Define("clock", new NativeFunctions.Clock());
        }
Beispiel #4
0
        private LoxEnvironment Ancestor(int distance)
        {
            LoxEnvironment environment = this;

            for (int i = 0; i < distance; i++)
            {
                environment = environment.Enclosing;
            }

            return(environment);
        }
Beispiel #5
0
        /// <summary>
        /// Ececute a block of statements
        /// </summary>
        /// <param name="statements">The statements</param>
        /// <param name="environment">The environment</param>
        public void ExecuteBlock(IEnumerable <Stmt> statements, LoxEnvironment environment)
        {
            // Save the current environment
            LoxEnvironment previous = this._environment;

            try
            {
                // Set the new environment
                this._environment = environment;

                foreach (Stmt statement in statements)
                {
                    Execute(statement);
                }
            }
            finally
            {
                // Restore the old environment
                this._environment = previous;
            }
        }
Beispiel #6
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);
        }
Beispiel #7
0
 public LoxEnvironment(LoxEnvironment enclosing)
 {
     this.Enclosing = enclosing;
 }
Beispiel #8
0
 public LoxEnvironment()
 {
     this.Enclosing = null;
 }
Beispiel #9
0
 public LoxFunction(Stmt.Function declaration, LoxEnvironment closure, bool is_initializer)
 {
     _declaration    = declaration;
     _closure        = closure;
     _is_initializer = is_initializer;
 }