-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter_Java_version.java
94 lines (85 loc) · 3.05 KB
/
Interpreter_Java_version.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
// Java version
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
public class Interpreter_Java_version {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("brainf.txt");
Scanner reader = new Scanner(file);
StringBuilder code = new StringBuilder();
while (reader.hasNextLine())
code.append(reader.nextLine());
ArrayList<Character> legalCharacters = new ArrayList<>(Arrays.asList('+', '-', '<', '>', '.', ',', '[', ']', '='));
ArrayList<String> tokens = new ArrayList<>();
int looper = 0;
for (int i = 0; i < code.length(); i++) {
if (legalCharacters.contains(code.charAt(i)))
tokens.add("" + code.charAt(i));
if (code.charAt(i) == '[')
looper++;
if (code.charAt(i) == ']')
looper--;
}
if (looper != 0)
throw new IllegalStateException("The number of '[' is not equal to the number of ']'");
interpret(tokens);
}
private static void interpret(ArrayList<String> brainfTokens) {
int[] memory=new int[30_000];
int pointer=0;
boolean isLooping=false;
Stack<Integer> loopStack=new Stack<>();
int innerLoops=0;
Scanner scanner=new Scanner(System.in);
for (int i = 0; i < brainfTokens.size(); i++) {
String brainfToken = brainfTokens.get(i);
if (isLooping) {
if (brainfToken.equals("["))
innerLoops++;
if (brainfToken.equals("]")) {
if (innerLoops == 0)
isLooping = false;
else innerLoops--;
}
continue;
}
switch (brainfToken) {
case "+":
memory[pointer]++;
break;
case "-":
memory[pointer]--;
break;
case ">":
pointer++;
break;
case "<":
pointer--;
if (pointer < 0)
throw new IllegalStateException("Pointer value less than 0 is not allowed");
break;
case ",":
memory[pointer] =scanner.next().charAt(0);
break;
case ".":
System.out.print((char)(memory[pointer]));
break;
case "[":
if (memory[pointer] == 0)
isLooping = true;
else
loopStack.push(i);
break;
case "]":
if (memory[pointer]!=0)
i=loopStack.peek();
else
loopStack.pop();
break;
}
}
}
}