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(); }
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); }
private static bool TryRunPass(ICompilerPass pass) { try { pass.Run(); } catch (SourceCodeErrorException ex) { HandleError(ex, pass.PassName()); return(false); } return(true); }
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); } }
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); }
private void PostPassExecution(object sender, ICompilerPass pass, CompilationUnit unit, IErrorReport errorService) { CreateAst(unit, resultingAST.Nodes); }