-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.java
More file actions
262 lines (253 loc) · 8.54 KB
/
Interpreter.java
File metadata and controls
262 lines (253 loc) · 8.54 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import java.util.ArrayList;
public class Interpreter {
// Constant for floating point rounding
final double EPLSION = 0.000001d;
// Opcode is the operation to be done
private char opcode;
// Code for the program
private ArrayList<String> program;
// Single ArrayList to hold the "history." Controls the flow of the program and stores the results of the operations
private ArrayList<Double> total;
// Result of the operation
private double result;
// Condition for the IF statement
private boolean condition;
// Loop variables
private boolean loop = false;
private int loopIndex = 0;
private int lineNumber = 0;
int LoopStart;
int LoopEnd;
int LoopStep;
// Constructor for when program is given.
public Interpreter(ArrayList<String> program ) {
// Loads the program into the interpreter
this.program = program;
this.total = new ArrayList<Double>();
this.condition = false;
this.opcode = ' ';
}
//Constructor for when program is not given.
public Interpreter() {
this.total = new ArrayList<Double>();
this.condition = false;
this.opcode = ' ';
}
// Load a program into the program variable
public void loadProgram(ArrayList<String> program) {
this.program = program;
}
// Execute the program
public void execProgram(){
// If there is no program, print error
if(program == null){
System.out.print("Program not loaded");
return;
}
exec();
}
private void exec(){
// For each line in the program
for(int statement = 0; statement < program.size(); statement++){
// If it is a comment or blank line continue
if(program.get(statement).contains("//")) continue;
if(program.get(statement).equals("")) continue;
// Set the line number
lineNumber = statement + 1;
// Split the line into an array
String[] line = program.get(statement).trim().split(" ");
// Flow control for loops
if(loop){
int statementStart = -1;
int statementEnd = -1;
// Find the start and end of the loop
while(true){
if(program.get(statement).contains("{")) {
statementStart = statement+1;
}
if(program.get(statement).contains("}")){
statementEnd = statement-1;
break;
}
statement++;
}
// Error handling
if(statementStart == -1 || statementEnd == -1){
System.out.print("\nError: Line " + lineNumber + " Invalid loop statement\n");
return;
}
// Specified loop
for(loopIndex = LoopStart; (LoopStep > 0 ? loopIndex <= LoopEnd : loopIndex >= LoopEnd); loopIndex+=LoopStep){
// Loop through the lines within the loop
for(int i = statementStart; i <= statementEnd; i++){
if(program.get(i).contains("//")) continue;
if(program.get(i).equals("")) continue;
lineNumber = i+1;
String[] lineLoop = program.get(i).trim().split(" ");
evalLine(lineLoop);
}
statement++;
}
statement = statementEnd+1;
loop = false;
continue;
}
// Evaluate the line
evalLine(line);
}
}
private void evalLine(String[] line){
for(int i=0; i<line.length; i++){
line[i] = line[i].trim();
// Check for keywords
if(line[i].equals("IF")){
if(condition){
continue;
}
else{
break;
}
}
if(line[i].equals("PUSH")){
opcode = ' ';
}
if(line[i].equals("PRINT")){
PRINT();
continue;
}
if(line[i].equals("PRINTC")){
PRINTC();
continue;
}
if(line[i].equals("PRINTLN")){
System.out.println();
continue;
}
if(line[i].equals("CLEAR")){
CLEAR();
continue;
}
if(line[i].equals("MEMCLEAR")){
MEMCLEAR();
continue;
}
if(line[i].equals("INDEX")){
total.add(Double.valueOf(loopIndex));
continue;
}
if(line[i].equals("FOR")){
LoopStart = Integer.parseInt(line[i+1]);
LoopEnd = Integer.parseInt(line[i+2]);
LoopStep = Integer.parseInt(line[i+3]);
if(loop)
{
System.out.print("\nError: Line " + lineNumber + " Nested loops are not allowed\n");
return;
}
loop = true;
break;
}
// If it is not a keyword, we check if it is a number, or if it is an operation. If it is neither we print a warning and continue
if(isDouble(line[i])){
total.add(Double.valueOf(line[i]));
}
else{
getOpcode(line[i]);
continue;
}
// If there are enough values to perform an operation we do so
int arraySize = total.size();
if(arraySize > 1){
if(opcode == '+'){
result = total.get(arraySize-2) + total.get(arraySize-1);
}
else if(opcode == '-'){
result = total.get(arraySize-2) - total.get(arraySize-1);
}
else if(opcode == '*'){
result = total.get(arraySize-2) * total.get(arraySize-1);
}
else if(opcode == '/'){
result = total.get(arraySize-2) / total.get(arraySize-1);
}
else if(opcode == '%'){
result = total.get(arraySize-2) % total.get(arraySize-1);
}
else if(opcode == '='){
condition = Math.abs(total.get(arraySize-2) - total.get(arraySize-1)) < EPLSION;
opcode = ' ';
}
else {
continue;
}
total.add(result);
}
}
}
private void PRINT(){
// Control for printing integers or doubles
if(total.get(total.size()-1) % 1 == 0){
System.out.print(total.get(total.size()-1).intValue());
}
else{
System.out.print(total.get(total.size()-1).doubleValue());
}
}
private void PRINTC(){
int total_i = (int) total.get(total.size()-1).doubleValue();
System.out.print(Character.toString((char) total_i));
}
private void CLEAR(){
total.clear();
}
private void MEMCLEAR(){
CLEAR();
opcode = ' ';
result = 0;
condition = false;
}
private boolean getOpcode(String line){
if(line.equals("+")){
opcode = '+';
return true;
}
else if(line.equals("-")){
opcode = '-';
return true;
}
else if(line.equals("*")){
opcode = '*';
return true;
}
else if(line.equals("/")){
opcode = '/';
return true;
}
else if(line.equals("%")){
opcode = '%';
return true;
}
else if(line.equals("=")){
opcode = '=';
return true;
}
else if(line.equals("!=")){
opcode = '!';
return true;
}
else{
//System.out.println("\nText="+line.toString());
System.out.print("\nWarning: Line " + lineNumber + " Invalid opperation - Results may not be logically valid\n");
return false;
}
}
// Utility function to check if a string is a double
boolean isDouble(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}