-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrainFuck.java
More file actions
174 lines (153 loc) · 5 KB
/
BrainFuck.java
File metadata and controls
174 lines (153 loc) · 5 KB
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
sealed interface BFCmd {
record INC (int times) implements BFCmd {}
record DEC (int times) implements BFCmd {}
record RIGHT (int times) implements BFCmd {}
record LEFT (int times) implements BFCmd {}
record INPUT () implements BFCmd {}
record OUTPUT (int times) implements BFCmd {}
record JZ (int addr) implements BFCmd {}
record JNZ (int addr) implements BFCmd {}
}
class Lexer {
final static String validCmds = "<>+-.,[]";
final String src;
int pos = 0;
boolean isValidBFCmd(char c) {
return validCmds.indexOf(c) != -1;
}
Lexer(final String src) { this.src = src; }
char get() {
while (this.pos < src.length() && !isValidBFCmd(src.charAt(this.pos))) {
this.pos++;
}
if (this.pos >= src.length()) return 0;
return src.charAt(this.pos);
}
void advance() {
this.pos++;
}
}
List<BFCmd> parse(final String src) {
var cmds = new ArrayList<BFCmd>();
var lexer = new Lexer(src);
char c = 0;
var stack = new ArrayList<Integer>();
while ((c = lexer.get()) != 0) {
lexer.advance();
BFCmd cmd = null;
switch (c) {
case '+':
case '-':
case '>':
case '<':
case '.': {
int times = 1;
while (lexer.get() == c) {
lexer.advance();
times++;
}
cmd = switch(c) {
case '+' -> new BFCmd.INC(times);
case '-' -> new BFCmd.DEC(times);
case '>' -> new BFCmd.RIGHT(times);
case '<' -> new BFCmd.LEFT(times);
case '.' -> new BFCmd.OUTPUT(times);
default -> throw new IllegalArgumentException("Illegal command: " + c);
};
} break;
case ',': {
cmd = new BFCmd.INPUT();
} break;
case '[': {
int addr = cmds.size();
cmd = new BFCmd.JZ(0);
stack.add(addr);
} break;
case ']': {
if (stack.isEmpty()) {
throw new IllegalStateException("Unbalanced parenthesis: at [" + (lexer.pos - 1) + "]");
}
int addr = cmds.size();
int jmpAddr = stack.removeLast();
cmd = new BFCmd.JNZ(jmpAddr + 1);
// backpatch
assert cmds.get(jmpAddr) instanceof BFCmd.JZ;
cmds.set(jmpAddr, new BFCmd.JZ(addr + 1));
} break;
default:
throw new IllegalArgumentException("Illegal command: " + c);
};
cmds.add(cmd);
}
return Collections.unmodifiableList(cmds);
}
void interpret(final List<BFCmd> cmds) {
int ip = 0;
int dp = 0;
var DM_SIZE = 65536;
var dataMemory = new int[DM_SIZE];
while (ip < cmds.size()) {
var cmd = cmds.get(ip);
switch (cmd) {
case BFCmd.INC (int times): {
dataMemory[dp] = (dataMemory[dp] + times) % 256;
ip++;
} break;
case BFCmd.DEC (int times): {
dataMemory[dp] = ((dataMemory[dp] - times) % 256 + 256) % 256;
ip++;
} break;
case BFCmd.RIGHT (int times): {
dp += times;
assert dp < DM_SIZE;
ip++;
} break;
case BFCmd.LEFT (int times): {
dp -= times;
assert dp >= 0;
ip++;
} break;
case BFCmd.INPUT () : {
char read = IO.readln().charAt(0);
dataMemory[dp] = read;
ip++;
} break;
case BFCmd.OUTPUT (int times): {
for (int i = 0; i < times; i++) {
IO.print((char) dataMemory[dp]);
}
ip++;
} break;
case BFCmd.JZ (int addr) : {
if (dataMemory[dp] == 0) {
ip = addr;
} else {
ip++;
}
} break;
case BFCmd.JNZ (int addr) : {
if (dataMemory[dp] != 0) {
ip = addr;
} else {
ip++;
}
} break;
}
}
}
void dbgCmd(final List<BFCmd> cmds) {
for (int i = 0; i < cmds.size(); i++) {
IO.print(i + ": ");
IO.println(cmds.get(i));
}
}
void main(String... args) throws IOException {
if (args.length != 1) {
IO.println("Usage: " + "bf" + " <input.bf>");
System.exit(1);
}
final var filePath = Path.of(args[0]);
final var src = Files.readString(filePath);
final var cmds = parse(src);
interpret(cmds);
}