-
Notifications
You must be signed in to change notification settings - Fork 0
/
brolang.d
219 lines (206 loc) · 4.63 KB
/
brolang.d
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
module brolang;
import std.algorithm : group, map;
import std.range : repeat, chain;
import std.string : join;
import std.conv : to;
import std.stdio;
import std.array;
import std.string;
import std.regex;
import std.conv;
import std.range;
private string bfToD(string code)
{
return "import std; void main() { ubyte[30000] field; size_t ptr;" ~ code.group.map!((g) {
const dchar token = g[0];
size_t count = g[1];
switch (token)
{
case '+':
return "field[ptr]+=" ~ count.to!string ~ ";";
case '-':
return "field[ptr]-=" ~ count.to!string ~ ";";
case '[':
return "while(field[ptr]){".repeat(count).join;
case ']':
return "}".repeat(count).join;
case ',':
return "field[ptr]=stdin.byChunk(1).front[0];".repeat(count).join;
case '.':
return "write(cast(char)field[ptr]);".repeat(count).join;
case '>':
return "ptr+=" ~ count.to!string ~ ";";
case '<':
return "ptr-=" ~ count.to!string ~ ";";
default:
return "";
}
}).join('\n') ~ `writeln("");}`;
}
private string bfToPsph(string code)
{
int i = 0;
int inc = 0;
int labelCount = 0;
int[] labelStack;
string psph =
"v_ptr d_ptr
v_int32 count
v_uint8 d0
ldptr d0
store d_ptr
ldptrv d_ptr
store count
";
foreach (token; code.stride(1))
{
switch (token)
{
case '+':
psph ~= "
ldu8v [d_ptr]
inc
store [d_ptr]";
break;
case '-':
inc++;
psph ~= "
ldu8v [d_ptr]
dec
store [d_ptr]";
break;
case '[':
psph ~= "
ldu8v [d_ptr]
jmpf exit_loop_" ~ to!string(labelCount) ~ "
golbl_" ~ to!string(labelCount) ~ ":";
labelStack ~= labelCount;
labelCount++;
break;
case ']':
const int labelNum = labelStack.back;
labelStack.popBack();
psph ~= "
ldu8v [d_ptr]
jmpt golbl_" ~ to!string(labelNum) ~ "
exit_loop_" ~ to!string(labelNum) ~ ":";
break;
case ',':
psph ~= "
syscall 0x02
store [d_ptr]";
break;
case '.':
psph ~= "
ldsav [d_ptr]
syscall 0x01";
break;
case '<':
psph ~= "
ldptrv d_ptr
dec
store d_ptr";
break;
case '>':
i++;
psph ~= "
ldptrv d_ptr
inc
store d_ptr
jmp skp_" ~ to!string(i) ~ "
dcl_" ~ to!string(i) ~ ":
v_uint8 data" ~ to!string(i) ~ "
ldi32v count
inc
store count
jmp exit_" ~ to!string(i) ~ "
skp_" ~ to!string(i) ~ ":
ldptrv d_ptr
ldi32v count
gt
jmpt dcl_" ~ to!string(i) ~ "
exit_" ~ to!string(i) ~ ":";
break;
default:
psph ~= "";
}
}
return psph;
}
private string broToBf(string code)
{
auto re = regex(r"\n|\t|\r", "g");
code = replaceAll(code, re, "");
const string[] tokens = code.split(" ");
int buffer = 0;
string output = "";
foreach (string t; tokens)
{
switch (t)
{
case "bro":
buffer++;
break;
case "Bro":
switch (buffer)
{
case 1:
output ~= "+";
break;
case 2:
output ~= "-";
break;
case 3:
output ~= "[";
break;
case 4:
output ~= "]";
break;
case 5:
output ~= ",";
break;
case 6:
output ~= ".";
break;
case 7:
output ~= ">";
break;
case 8:
output ~= "<";
break;
default:
throw new StringException("Unexpected bro, bro!");
}
buffer = 0;
break;
case "":
break;
case " ":
break;
default:
throw new StringException("Invalid command " ~ t ~ ", bro!");
}
}
return output;
}
void main(string[] args)
{
string code = "";
string lastIn = "";
auto ctr = ctRegex!(r"bro|Bro|\+|-|\[|\]|>|<|\.|,");
do
{
lastIn = stdin.readln();
code ~= lastIn;
}
while (!matchFirst(lastIn, ctr).empty);
if(args[1] == "d") {
writeln(bfToD(broToBf(code)));
} else if(args[1] == "bf2d") {
writeln(bfToD(code));
} else if(args[1] == "psph") {
writeln(bfToPsph(broToBf(code)));
} else if(args[1] == "bf2psph") {
writeln(bfToPsph(code));
}
}