Skip to content

Commit 9cf6a27

Browse files
committed
New optimizing version
1 parent 0eee6e6 commit 9cf6a27

File tree

2 files changed

+108
-32
lines changed

2 files changed

+108
-32
lines changed

Program.cs

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
using System.CodeDom.Compiler;
33
using Microsoft.CSharp;
44
using System.IO;
5+
using System.Runtime.Serialization.Formatters;
6+
using System.Linq;
7+
using System.Collections.Generic;
8+
using System.Runtime.Remoting.Messaging;
9+
510
namespace BrainFexec
611
{
712
internal class Program
813
{
914
private static string code = "", fn = "";
1015
static void Main(string[] args)
1116
{
12-
switch (args.Length)
17+
int len = args.Length;
18+
bool opt = false;
19+
if(args.ToList().Exists(x => x == "-o")) {
20+
len--;
21+
opt = true;
22+
}
23+
switch (len)
1324
{
1425
case 1:
1526
{
@@ -27,7 +38,7 @@ static void Main(string[] args)
2738
}
2839
default:
2940
{
30-
Console.WriteLine("Usage:\nBrainFExec <input> [output]\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'.");
41+
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");
3142
return;
3243
}
3344
}
@@ -39,41 +50,99 @@ static void Main(string[] args)
3950
param.MainClass = "BF";
4051
param.GenerateInMemory = true;
4152
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";
42-
for (int i = 0; i < t.Length; i++)
53+
if (!opt)
4354
{
44-
if (t[i] == '+')
45-
{
46-
brainfuck += "tape[p]=(tape[p]==255?0:tape[p]+1);";
47-
}
48-
if (t[i] == '-')
49-
{
50-
brainfuck += "tape[p]=(tape[p]==0?255:tape[p]-1);";
51-
}
52-
if (t[i] == ',')
53-
{
54-
brainfuck += "tape[p]=Console.Read();";
55-
}
56-
if (t[i] == '.')
55+
for (int i = 0; i < t.Length; i++)
5756
{
58-
brainfuck += "Console.Write(Convert.ToChar(tape[p]));";
59-
}
60-
if (t[i] == '>')
61-
{
62-
brainfuck += "p++;";
63-
}
64-
if (t[i] == '<')
65-
{
66-
brainfuck += "p--;";
57+
if (t[i] == '+')
58+
{
59+
brainfuck += "tape[p]=(tape[p]==255?0:tape[p]+1);";
60+
}
61+
if (t[i] == '-')
62+
{
63+
brainfuck += "tape[p]=(tape[p]==0?255:tape[p]-1);";
64+
}
65+
if (t[i] == ',')
66+
{
67+
brainfuck += "tape[p]=Console.Read();";
68+
}
69+
if (t[i] == '.')
70+
{
71+
brainfuck += "Console.Write(Convert.ToChar(tape[p]));";
72+
}
73+
if (t[i] == '>')
74+
{
75+
brainfuck += "p++;";
76+
}
77+
if (t[i] == '<')
78+
{
79+
brainfuck += "p--;";
80+
}
81+
if (t[i] == '[')
82+
{
83+
brainfuck += "while(tape[p]!=0){";
84+
}
85+
if (t[i] == ']')
86+
{
87+
brainfuck += "}";
88+
}
89+
brainfuck += "\r\n";
6790
}
68-
if (t[i] == '[')
91+
}
92+
else
93+
{
94+
List<char> tempsymbol = new List<char>();
95+
List<int> tempnum = new List<int>();
96+
for(int i=0;i<t.Length; i++)
6997
{
70-
brainfuck += "while(tape[p]!=0){";
98+
char c = t[i];
99+
if (c != '+' && c != '-' && c != '>' && c != '<' && c != ',' && c != '.' && c != '[' && c != ']') continue;
100+
if (tempnum.Count != 0 && (c == '+' || c == '-' || c == '>' || c == '<') && tempsymbol[tempsymbol.Count - 1] == c)
101+
{
102+
tempnum[tempnum.Count - 1]++;
103+
}
104+
else
105+
{
106+
tempnum.Add(1);
107+
tempsymbol.Add(c);
108+
}
71109
}
72-
if (t[i] == ']')
110+
for (int i = 0; i < tempsymbol.Count; i++)
73111
{
74-
brainfuck += "}";
112+
if (tempsymbol[i] == '+')
113+
{
114+
brainfuck += "tape[p]=(tape[p]+"+Convert.ToString(tempnum[i])+")%256;";
115+
}
116+
if (tempsymbol[i] == '-')
117+
{
118+
brainfuck += "tape[p]=(tape[p]-" + Convert.ToString(tempnum[i]) + "+256)%256;";
119+
}
120+
if (tempsymbol[i] == ',')
121+
{
122+
brainfuck += "tape[p]=Console.Read();";
123+
}
124+
if (tempsymbol[i] == '.')
125+
{
126+
brainfuck += "Console.Write(Convert.ToChar(tape[p]));";
127+
}
128+
if (tempsymbol[i] == '>')
129+
{
130+
brainfuck += "p+=" + Convert.ToString(tempnum[i]) + ";";
131+
}
132+
if (tempsymbol[i] == '<')
133+
{
134+
brainfuck += "p-=" + Convert.ToString(tempnum[i]) + ";";
135+
}
136+
if (tempsymbol[i] == '[')
137+
{
138+
brainfuck += "while(tape[p]!=0){";
139+
}
140+
if (tempsymbol[i] == ']')
141+
{
142+
brainfuck += "}";
143+
}
144+
brainfuck += "\r\n";
75145
}
76-
brainfuck += "\r\n";
77146
}
78147
brainfuck += "}\r\n}";
79148
CompilerResults result = compiler.CompileAssemblyFromSource(param, brainfuck);

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22
BrainFExec is a [BrainFuck](https://esolangs.org/wiki/BrainFuck) compiler to Windows executable in C#.
33
## Usage
44
```commandline
5-
BrainFExec <input> [output]
5+
BrainFExec <input> [output] [-o]
66
```
77
Compiles BrainFuck code into a Windows executable:
88
* input: Input BrainFuck file name
99
* output: Output BrainFuck file name
10+
* -o: Optimize
1011

1112
If `output` is not passed, BrainFExec automatically decides the file name: For example, if the input BrainFuck filename is `a.bf`, it sets the output file to `a.exe`.
1213
## How it works
1314
It first converts BrainFuck to C#, then uses dynamic compiling to compile the resulting C# program to an executable.
1415
## Requires
15-
.NET < 5
16+
.NET < 5
17+
## Changelog
18+
* 1.0.0
19+
* First version
20+
* 1.0.1
21+
* Added support for optimizing
22+
* Bugfix of help prompt

0 commit comments

Comments
 (0)