Exemplo n.º 1
0
        protected void runProgram(string program, Dictionary <string, object> variablesIn, int expectedIterations, out FrameContext context)
        {
            var inputStream = new AntlrInputStream(program);
            var lexer       = new CloacaLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            var errorListener = new ParseErrorListener();
            var parser        = new CloacaParser(commonTokenStream);

            parser.AddErrorListener(errorListener);

            var antlrVisitorContext = parser.file_input();

            Assert.That(errorListener.Errors.Count, Is.Zero, "There were parse errors:\n" + errorListener.Report());

            var visitor = new CloacaBytecodeVisitor(variablesIn);

            visitor.Visit(antlrVisitorContext);

            // We'll do a disassembly here but won't assert against it. We just want to make sure it doesn't crash.
            CodeObject compiledProgram = visitor.RootProgram.Build();

            Dis.dis(compiledProgram);

            // TODO: This dependency association is kind of gross. It's almost circular and is broken by assigning
            // the interpreter reference to the schedular after its initial constructor.
            var scheduler   = new Scheduler();
            var interpreter = new Interpreter(scheduler);

            // Create native resource manager which will handle builtins that provide access to 'native' resources such as files.
            var nativeResourceManager = new NativeResourceManager();

            nativeResourceManager.RegisterProvider <INativeFileProvider>(new DefaultFileProvider());
            nativeResourceManager.RegisterBuiltins(interpreter);

            interpreter.DumpState = true;
            scheduler.SetInterpreter(interpreter);

            var receipt = scheduler.Schedule(compiledProgram);

            context = receipt.Frame;
            foreach (string varName in variablesIn.Keys)
            {
                context.SetVariable(varName, variablesIn[varName]);
            }

            // Waiting on the task makes sure we get punched in the face by any exceptions it throws.
            // But they'll come rolling in as AggregateExceptions so we'll have to unpack them.
            var scheduler_task = scheduler.RunUntilDone();

            scheduler_task.Wait();
            Assert.That(receipt.Completed);
            if (receipt.EscapedExceptionInfo != null)
            {
                receipt.EscapedExceptionInfo.Throw();
            }

            Assert.That(scheduler.TickCount, Is.EqualTo(expectedIterations));
        }
Exemplo n.º 2
0
 public ProgramExecutor(InputOutputStream stdout)
 {
     _stdout                = stdout;
     _scheduler             = new Scheduler();
     _interpreter           = new Interpreter(_scheduler);
     _nativeResourceManager = new NativeResourceManager();
     _nativeResourceManager.RegisterProvider <INativeFileProvider>(new DefaultFileProvider());
     _nativeResourceManager.RegisterProvider <INativeStdioProvider>(new DefaultNativeStdioProvider(null, stdout));
     _nativeResourceManager.RegisterBuiltins(_interpreter);
     _scheduler.SetInterpreter(_interpreter);
 }