-
Notifications
You must be signed in to change notification settings - Fork 2
/
BFInterpreter.java
217 lines (166 loc) · 8.29 KB
/
BFInterpreter.java
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
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.*;
import java.util.*;
import java.nio.charset.Charset;
import org.antlr.runtime.TokenStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.commons.io.IOUtils;
public class BFInterpreter extends BrainfuqBaseVisitor {
//private static Logger logger = Logger.getLogger(BFInterpreter.class.getName());
//ANSI COLOURS
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
Boolean isMoving = false;
private int LENGTH = 65443;
byte[] tape = new byte[LENGTH];
int dataPointer = 0;
//For the input
private Scanner sc = new Scanner(System.in);
@Override
public Object visitTop(BrainfuqParser.TopContext ctx) {
// System.out.print("Enterted Top\n");
visitChildren(ctx);
return null;
}
@Override
public Object visitProg(BrainfuqParser.ProgContext ctx) {
// System.out.print("Enterted Prog\n");
visitChildren(ctx);
return null;
}
@Override
public Object visitOps(BrainfuqParser.OpsContext ctx) {
// System.out.print("Enterted Ops\n");
switch (ctx.op.getType()) {
case BrainfuqLexer.INPUT:
tape[dataPointer] = (byte) sc.next().charAt(0);
break;
case BrainfuqLexer.OUTPUT:
if (isMoving) {
System.out.print((char) tape[dataPointer]);
} else {
System.out.print(ANSI_GREEN + (char) tape[dataPointer] + ANSI_RESET);
}
break;
case BrainfuqLexer.INC:
tape[dataPointer]++;
break;
case BrainfuqLexer.DEC:
tape[dataPointer]--;
break;
case BrainfuqLexer.P_INC:
dataPointer = (dataPointer == LENGTH-1) ? 0 : dataPointer + 1;
break;
case BrainfuqLexer.P_DEC:
dataPointer = (dataPointer == 0) ? LENGTH-1 : dataPointer - 1;
break;
}
return null;
}
@Override
public Object visitLoop(BrainfuqParser.LoopContext ctx) {
// System.out.print("Enterted Loop\n");
while (tape[dataPointer] != 0) {
visitChildren(ctx);
}
return null;
}
public static void main(String[] args) throws IOException {
BFInterpreter bfInt = new BFInterpreter();
final String BRAINFUCK = "Brain" + ANSI_RED + "fuck" + ANSI_RESET;
final String HELPCODE = ANSI_CYAN_BACKGROUND + ":help" + ANSI_RESET;
final String BFHEADER = ANSI_RED + "BF" + ANSI_RESET;
if (args[0].equals("-i")) {
//Stack<String> commandStack = new Stack<String>();
Scanner commands = new Scanner(System.in);
System.out.print("\n Welcome to " + BRAINFUCK + " REPL(an interactive programming environment) :)\n");
System.out.print(" " + BRAINFUCK + " is trivial yet very difficult to write.\n If you want to get a hint of the grammar, just type in " + HELPCODE + "\n\n");
while (true) {
System.out.print(BFHEADER + " ⚡️ ");
String command = commands.next();
//TODO: Implement a Command Stack and detect arrow keys to select through previous commands
//if (command.equals("^[[A")) {
//System.out.print("foooo");
//}
//commandStack.push(command);
System.out.print("\n ");
if (command.equals(":help") || command.equals(":h")) {
System.out.print("> : Move data pointer one cell to the right\n" +
" < : Move data pointer one cell to the left\n" +
" + : Increment the value at the data pointer\n" +
" - : Decrement the value at the data pointer\n" +
" . : Print out the value at the data pointer (as ASCII)\n" +
" , : Accept a byte of input and store it as the value at the pointer\n" +
" [ : Jump to the matching ] unless the value at the pointer is 0\n" +
" ] : Jump backwards to the matching [ unless the value at the data pointer is not 0\n\n");
} else if (command.equals(":emoji") || command.equals(":e")) {
System.out.print("👉 : Move data pointer one cell to the right\n" +
" 👈 : Move data pointer one cell to the left\n" +
" 👆 : Increment the value at the data pointer\n" +
" 👇 : Decrement the value at the data pointer\n" +
" 🌚 : Print out the value at the data pointer (as ASCII)\n" +
" 🌝 : Accept a byte of input and store it as the value at the pointer\n" +
" 🌜 : Jump to the matching 🌛 unless the value at the pointer is 0\n" +
" 🌛 : Jump backwards to the matching 🌜 unless the value at the data pointer is not 0\n\n");
} else if (command.equals(":quit") || command.equals(":q")) {
System.out.print("It's been fun playing with you! See you next time!\n\n");
return;
} else {
command = command.replaceAll("🌝", ",");
command = command.replaceAll("🌚", ".");
command = command.replaceAll("👆", "+");
command = command.replaceAll("👇", "-");
command = command.replaceAll("👉", ">");
command = command.replaceAll("👈", "<");
command = command.replaceAll("🌜", "[");
command = command.replaceAll("🌛", "]");
ANTLRInputStream input = new ANTLRInputStream(command);
BrainfuqLexer lexer = new BrainfuqLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
BrainfuqParser parser = new BrainfuqParser(tokens);
bfInt.visit(parser.top());
System.out.print("\n\n");
//Resetting the state for the next repl
bfInt.dataPointer = 0;
bfInt.tape = null;
bfInt.tape = new byte[bfInt.LENGTH];
}
}
} else {
try {
FileInputStream inStream = new FileInputStream(args[0]);
//Temporary fix for Hanoi Tower moving program
if (args[0].equals("Hanoi.bf")) {
bfInt.isMoving = true;
}
String code = IOUtils.toString(inStream, Charset.defaultCharset());
code = code.replaceAll("🌝", ",");
code = code.replaceAll("🌚", ".");
code = code.replaceAll("👆", "+");
code = code.replaceAll("👇", "-");
code = code.replaceAll("👉", ">");
code = code.replaceAll("👈", "<");
code = code.replaceAll("🌜", "[");
code = code.replaceAll("🌛", "]");
final ANTLRInputStream input = new ANTLRInputStream(code);
BrainfuqLexer lexer = new BrainfuqLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
BrainfuqParser parser = new BrainfuqParser(tokens);
bfInt.visit(parser.top());
// logger.log(Level.INFO, "p: {0}; tape: {1}", new Object[] {
// bfInt.p,
// Arrays.toString(bfInt.tape)
// });
} catch (IOException foo) {
foo.printStackTrace();
}
}
}
}