Exemple #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();
        }
Exemple #2
0
        private InsertMethodsPass CreatePass(params Function[] functions)
        {
            var scope = GlobalScope.Generate();

            foreach (var fn in functions)
            {
                scope.DeclareFunction(fn);
            }

            return(new InsertMethodsPass(scope.GlobalScope));
        }