-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
160 lines (159 loc) · 5.97 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
namespace BrainFexec
{
internal class Program
{
private static string code = "", fn = "";
static void Main(string[] args)
{
int len = args.Length;
bool opt = false;
if(args.ToList().Exists(x => x == "-o")) {
len--;
opt = true;
}
switch (len)
{
case 1:
{
code = File.ReadAllText(args[0]);
string[] s = args[0].Split('\\');
string [] S = s[s.Length - 1].Split('.');
fn = S[0] + ".exe";
break;
}
case 2:
{
code = File.ReadAllText(args[0]);
fn = args[1];
break;
}
default:
{
Console.WriteLine("Usage:\nBrainFExec <input> [output] [-o]\nBrainFuck to EXE compiler, compiles BrainFuck into a Windows executable.\ninput: Input file name\noutput: Output file name, if this argument is not passed and the input file name is 'a.bf', output file name will be 'a.exe'.\n-o: Optimize");
return;
}
}
CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters param = new CompilerParameters();
param.ReferencedAssemblies.Add("System.dll");
param.GenerateExecutable = true;
param.OutputAssembly = fn;
param.MainClass = "BF";
param.GenerateInMemory = true;
string t = code, brainfuck = "using System;\r\npublic class BF{\r\nprivate static int[] tape=new int[1000000];\r\nprivate static int p=0;\r\nstatic void Main(string[] args){\r\n";
if (!opt)
{
for (int i = 0; i < t.Length; i++)
{
if (t[i] == '+')
{
brainfuck += "tape[p]=(tape[p]==255?0:tape[p]+1);";
}
if (t[i] == '-')
{
brainfuck += "tape[p]=(tape[p]==0?255:tape[p]-1);";
}
if (t[i] == ',')
{
brainfuck += "tape[p]=Console.Read();";
}
if (t[i] == '.')
{
brainfuck += "Console.Write(Convert.ToChar(tape[p]));";
}
if (t[i] == '>')
{
brainfuck += "p++;";
}
if (t[i] == '<')
{
brainfuck += "p--;";
}
if (t[i] == '[')
{
brainfuck += "while(tape[p]!=0){";
}
if (t[i] == ']')
{
brainfuck += "}";
}
brainfuck += "\r\n";
}
}
else
{
List<char> tempsymbol = new List<char>();
List<int> tempnum = new List<int>();
for(int i=0;i<t.Length; i++)
{
char c = t[i];
if (c != '+' && c != '-' && c != '>' && c != '<' && c != ',' && c != '.' && c != '[' && c != ']') continue;
if (tempnum.Count != 0 && (c == '+' || c == '-' || c == '>' || c == '<') && tempsymbol[tempsymbol.Count - 1] == c)
{
tempnum[tempnum.Count - 1]++;
}
else
{
tempnum.Add(1);
tempsymbol.Add(c);
}
}
for (int i = 0; i < tempsymbol.Count; i++)
{
if (tempsymbol[i] == '+')
{
brainfuck += "tape[p]=(tape[p]+"+Convert.ToString(tempnum[i])+")%256;";
}
if (tempsymbol[i] == '-')
{
brainfuck += "tape[p]=(tape[p]-" + Convert.ToString(tempnum[i]) + "+256)%256;";
}
if (tempsymbol[i] == ',')
{
brainfuck += "tape[p]=Console.Read();";
}
if (tempsymbol[i] == '.')
{
brainfuck += "Console.Write(Convert.ToChar(tape[p]));";
}
if (tempsymbol[i] == '>')
{
brainfuck += "p+=" + Convert.ToString(tempnum[i]) + ";";
}
if (tempsymbol[i] == '<')
{
brainfuck += "p-=" + Convert.ToString(tempnum[i]) + ";";
}
if (tempsymbol[i] == '[')
{
brainfuck += "while(tape[p]!=0){";
}
if (tempsymbol[i] == ']')
{
brainfuck += "}";
}
brainfuck += "\r\n";
}
}
brainfuck += "}\r\n}";
CompilerResults result = compiler.CompileAssemblyFromSource(param, brainfuck);
if (result.Errors.HasErrors)
{
Console.WriteLine("Unmatched bracket");
}
else
{
Console.WriteLine("Done!");
Console.WriteLine("Successfully compiled to " + fn);
}
}
}
}