User:Feuermonster/Brainpiler

From Esolang
Jump to navigation Jump to search
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.CodeDom.Compiler;



namespace Brainpiler

{

    class Program

    {

        static void Main(string[] args)

        {

            if (args.Length < 1)

            {

                System.Console.WriteLine("brainpiler <file>");

                System.Environment.Exit(-1);

            }

            string code = System.IO.File.ReadAllText(args[0]);

            List<String> cs_code = new List<string>();

            cs_code.Add("namespace bf { ");

            cs_code.Add("public class Brainfuck {");

            cs_code.Add(" public static void Main(string[] args) {");

            cs_code.Add("byte[] tape = new byte[30000]; int ptr = 15000;");





            for (int i = 0; i < code.Length; i++)

            {

                switch (code[i])

                {

                    case '+':

                        cs_code.Add("if (tape[ptr] == byte.MaxValue)");

                        cs_code.Add("tape[ptr] = 0;");

                        cs_code.Add("else");

                        cs_code.Add(" tape[ptr]++;");

                        break;

                    case '-':

                        cs_code.Add("if (tape[ptr] == 0)");

                        cs_code.Add("tape[ptr] = byte.MaxValue;");

                        cs_code.Add("else");

                        cs_code.Add("tape[ptr]--;");

                        break;

                    case '<':

                        cs_code.Add("if (ptr == 0)");

                        cs_code.Add("ptr = tape.Length - 1;");

                        cs_code.Add("else");

                        cs_code.Add("ptr--;");

                        break;

                    case '>':

                        cs_code.Add("if (ptr == tape.Length - 1)");

                        cs_code.Add("ptr = 0;");

                        cs_code.Add("else");

                        cs_code.Add("ptr++;");

                        break;

                    case '[':

                        cs_code.Add("while(tape[ptr] != 0) {");

                        break;

                    case ']':

                        cs_code.Add("}");

                        break;

                    case '.':

                        cs_code.Add("System.Console.Write(((char)tape[ptr]).ToString());");

                        break;

                    case ',':

                        cs_code.Add("tape[ptr] = (byte)System.Console.ReadKey().KeyChar;");

                        break;

                }

            }



            cs_code.Add("  } ");

            cs_code.Add("}");

            cs_code.Add("}");



            string caa = "";

            foreach (string str in cs_code)

            {

                caa += str + "\n";

            }





            Microsoft.CSharp.CSharpCodeProvider c = new Microsoft.CSharp.CSharpCodeProvider();

            String[] referenceAssemblies = { "System.dll" };

            CompilerParameters cp = new CompilerParameters(referenceAssemblies, "compiled.exe", false);

            // Generate an executable rather than a DLL file.

            cp.GenerateExecutable = true;



            System.CodeDom.Compiler.CompilerResults cr = c.CompileAssemblyFromSource(cp, caa);  

            foreach(System.CodeDom.Compiler.CompilerError ce in cr.Errors) {

                Console.WriteLine(ce.ErrorText + " line: " + ce.Line.ToString() + " str: " + ce.ToString() );

                Console.WriteLine("which is: " + cs_code[ce.Line]);

            }

            System.Reflection.Assembly a = cr.CompiledAssembly;  

            

        }

    }

}