Exemplo n.º 1
0
        public LoxFunctionCallable Bind(LoxInstance instance)
        {
            var binding = new LoxEnvironment(_closure);

            binding.Bind(instance);
            return(new LoxFunctionCallable(_functionStmt, binding, _isInitializer));
        }
Exemplo n.º 2
0
        public InterpretingVisitor()
        {
            GlobalEnvironment   = new LoxEnvironment();
            _assertions         = new List <LoxAssertionResult>();
            _scopeMap           = new Dictionary <Expr, int>();
            _currentEnvironment = GlobalEnvironment;

            var start = DateTime.UtcNow.Ticks;

            GlobalEnvironment.Define("clock", new LoxNativeCallable(0, (args) => (double)(DateTime.UtcNow.Ticks - start) / 10000));
            GlobalEnvironment.Define("assert", AssertLoxNativeCallable.CreateAssertLoxNativeCallable(_assertions));
            GlobalEnvironment.Define("stringify", new LoxNativeCallable(1, (arg) => arg[0]?.ToString() ?? "nil"));
        }
Exemplo n.º 3
0
        public void Execute(IEnumerable <Stmt> statements, LoxEnvironment customEnvironment)
        {
            var holding = _currentEnvironment;

            try {
                _currentEnvironment = customEnvironment;

                foreach (var innerStmt in statements)
                {
                    Execute(innerStmt);
                }
            } finally {
                _currentEnvironment = holding;
            }
        }
Exemplo n.º 4
0
        public object Call(InterpretingVisitor interpreter, List <object> arguments)
        {
            var environment = new LoxEnvironment(_closure);

            for (var i = 0; i < Arity(); i++)
            {
                environment.Define(_functionStmt.Params[i].Lexeme, arguments[i]);
            }

            try {
                interpreter.Execute(_functionStmt.Body, environment);
            } catch (LoxReturnException ex) {
                if (_isInitializer)
                {
                    throw new LoxReturnException(_closure.GetAt(0, new Token(TokenType.This, "this", null, 0)));
                }

                throw;
            }

            return(null);
        }
Exemplo n.º 5
0
 public LoxEnvironment()
 {
     _enclosingEnvironment = null;
     _state = new Dictionary <string, object>();
 }
Exemplo n.º 6
0
 public LoxEnvironment(LoxEnvironment environment)
 {
     _enclosingEnvironment = environment;
     _state = new Dictionary <string, object>();
 }
Exemplo n.º 7
0
 public LoxFunctionCallable(FunctionStmt stmt, LoxEnvironment closure, bool isInitializer = false)
 {
     _functionStmt  = stmt;
     _closure       = closure;
     _isInitializer = isInitializer;
 }