Esempio n. 1
0
        public static void Compile(TextReader reader, TextWriter writer, bool verbose)
        {
            // parse input
            var charReader   = new CharReader(reader);
            var tokenizer    = new Tokenizer(charReader);
            var astGenerator = new AstGenerator(tokenizer);
            var statement    = astGenerator.ParseCompilationUnit();

            // manipulate ast
            var scope = GlobalScope.Generate();

            statement = statement.Accept(scope, new AstParser());
            if (verbose)
            {
                DumpAstStatements(statement);
            }

            // generate instructions
            var instructions = statement.Accept(null, new InstructionGenerator());

            if (verbose)
            {
                DumpInstrutions("raw", instructions);
            }

            // transform instructions
            var passList = new ICompilerPass[]
            {
                new InsertMethodsPass(scope.GlobalScope),
                new JumpsToFsmPass(),
            };

            foreach (var pass in passList)
            {
                instructions = pass.Pass(instructions);
                if (verbose)
                {
                    DumpInstrutions(pass.GetType().Name, instructions);
                }
            }

            // instructions to brainfuck
            CodeWriter cw = new CodeWriter(writer);

            if (verbose)
            {
                DumpBrainfuckDebug(cw);
            }

            cw.Begin();
            foreach (var instruction in instructions)
            {
                cw.Write(instruction);
            }

            cw.End();
        }
Esempio n. 2
0
        public void AssertAreEqual(ICompilerPass pass, Instruction[] expected, Instruction[] passInput)
        {
            var actual = pass.Pass(new List <Instruction>(passInput));
            var msg    = $@"
input    = {GetInstructionString(passInput)}
expected = {GetInstructionString(expected)}
actual   = {GetInstructionString(actual)}
";

            Assert.IsTrue(expected.SequenceEqual(actual), msg);
        }
Esempio n. 3
0
        private static bool TryRunPass(ICompilerPass pass)
        {
            try
            {
                pass.Run();
            }
            catch (SourceCodeErrorException ex)
            {
                HandleError(ex, pass.PassName());
                return(false);
            }

            return(true);
        }
Esempio n. 4
0
        private void ExecutePass(ICompilerPass pass, CompilationUnit unit)
        {
            if (PrePassExecution != null)
            {
                PrePassExecution(this, pass, unit, container.ErrorReportService);
            }

            pass.ExecutePass(unit);

            if (PostPassExecution != null)
            {
                PostPassExecution(this, pass, unit, container.ErrorReportService);
            }
        }
Esempio n. 5
0
        private bool RunPasses(CompilationUnit cunit)
        {
            if (!container.ErrorReportService.HasErrors)
            {
                ICompilerPass builderSkeleton =
                    container[typeof(CreateBuilderSkeleton)] as ICompilerPass;

                ExecutePass(builderSkeleton, cunit);
            }

            if (!container.ErrorReportService.HasErrors)
            {
                ICompilerPass scopePass =
                    container[typeof(ScopePass)] as ICompilerPass;

                ExecutePass(scopePass, cunit);
            }

            if (!container.ErrorReportService.HasErrors)
            {
                ICompilerPass typeResPass =
                    container[typeof(TypeResolutionPass)] as ICompilerPass;

                ExecutePass(typeResPass, cunit);
            }

            if (!container.ErrorReportService.HasErrors)
            {
                ICompilerPass emission =
                    container[typeof(Emission)] as ICompilerPass;

                emission.ExecutePass(cunit);
            }

            return(!container.ErrorReportService.HasErrors);
        }
Esempio n. 6
0
		private void PostPassExecution(object sender, ICompilerPass pass, CompilationUnit unit, IErrorReport errorService)
		{
			CreateAst(unit, resultingAST.Nodes);
		}
Esempio n. 7
0
		private void ExecutePass(ICompilerPass pass, CompilationUnit unit)
		{
			if (PrePassExecution != null)
			{
				PrePassExecution(this, pass, unit, container.ErrorReportService);
			}

			pass.ExecutePass(unit);

			if (PostPassExecution != null)
			{
				PostPassExecution(this, pass, unit, container.ErrorReportService);
			}
		}
Esempio n. 8
0
 private void PostPassExecution(object sender, ICompilerPass pass, CompilationUnit unit, IErrorReport errorService)
 {
     CreateAst(unit, resultingAST.Nodes);
 }