forked from jontheapple/pikachu-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPikachu.java
355 lines (348 loc) · 11.9 KB
/
Pikachu.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import java.util.Stack;
import java.util.ArrayList;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Pikachu{
Stack<Integer> pi;
Stack<Integer> pika;
File input;
Scanner fileScan;
int lineNum;
public Pikachu(String filename) throws FileNotFoundException{
pi = new Stack<Integer>();
pika = new Stack<Integer>();
input = new File(filename);
lineNum = 0;
} //Pikachu()
public String run() throws FileNotFoundException{
int go = 1; //start program at line 1
StringBuilder builder = new StringBuilder();
String last;
String lastLast;
while (go > 0){
if (fileScan != null) fileScan.close(); //goto implementation will crush if too many files are open!
fileScan = new Scanner(input);
lineNum = 0;
for (int i = 1; i < go; i++){ //go to line "go"
fileScan.nextLine();
lineNum++;
}
go = 0; //if we get to end of while loop without setting "go" to a value greater than 0, exit while loop.
while (fileScan.hasNext()){
last = "";
lastLast = "";
lineNum++;
Scanner lineScan = new Scanner(fileScan.nextLine());
ArrayList<String> thisLine = new ArrayList<String>();
while (lineScan.hasNext()){
String next = lineScan.next();
if (lastLast.equals(last) && next.equals(last)){
System.err.println("Error: Three of the same term in a row found on line " + lineNum);
System.exit(0);
} else{
lastLast = last;
last = next;
}
thisLine.add(next);
}
if (thisLine.size() > 0){
if (thisLine.get(0).length() > 2){
if (thisLine.get(0).charAt(0) == 47 && thisLine.get(0).charAt(1) == 47){
continue;
}
}
}
if (thisLine.size() < 2){
System.err.println("Error: Line " + lineNum + " found with less than 2 terms.");
System.exit(0);
} else if (thisLine.size() == 2){
switch (thisLine.get(0)){
case "pi":
switch (thisLine.get(1)){
case "pi": //"pi pi" is not valid
System.err.println("Error: \"pi pi\" is not valid on line " + lineNum);
System.exit(0);
case "pika": //"pi pika" copies the top of pi pikachu to pika pikachu
pika.push(pi.peek());
break;
case "pikachu": //"pi pikachu" pops the top element of pi pikachu
pi.pop();
break;
default:
System.err.println("Error: Non-pikachu language found on line " + lineNum);
System.exit(0);
} //switch (thisLine.get(1))
break;
case "pika":
switch (thisLine.get(1)){
case "pi": //"pika pi" copies top of pika pikachu to pi pikachu
pi.push(pika.peek());
break;
case "pika": //if the tops of the two stacks are inequal, go to line n where n is the number of terms in the next line
if (!fileScan.hasNext()){
System.err.println("Error: \"pika pika\" found on last line of input file");
System.exit(0);
} else{
if (!pi.peek().equals(pika.peek())){
Scanner counter = new Scanner(fileScan.nextLine());
while (counter.hasNext()){
counter.next();
go++;
} //while (counter.hasNext())
} else{
System.err.println("Hit jne --> condition skips");
fileScan.nextLine();
lineNum++;
}
} //else
break;
case "pikachu": //"pika pikachu" pops the top element of pika pikachu
pika.pop();
break;
default:
System.err.println("Error: Non-pikachu language found on line " + lineNum);
System.exit(0);
} //switch(thisLine.get(1))
break;
case "pikachu":
switch (thisLine.get(1)){
case "pi":
System.err.println("Error: \"pikachu pi\" is not valid on line " + lineNum);
System.exit(0);
case "pika":
System.err.println("Error: \"pikachu pika\" is not valid on line " + lineNum);
System.exit(0);
case "pikachu":
if (!fileScan.hasNext()){
System.err.println("Error: \"pikachu pikachu\" found on last line of input file");
System.exit(0);
} else{
if (pi.peek().equals(pika.peek())){
Scanner counter = new Scanner(fileScan.nextLine());
while (counter.hasNext()){
counter.next();
go++;
} //while (counter.hasNext())
} else{
fileScan.nextLine();
lineNum++;
}
} //else
break;
default:
System.err.println("Error: Non-pikachu language found on line " + lineNum);
System.exit(0);
} //switch(thisLine.get(1))
break;
default:
System.err.println("Error: Non-pikachu language found on line " + lineNum);
System.exit(0);
} //switch (thisLine.get(0))
if (go > 0){
break;
}
} else if (thisLine.size() == 4){
if (!thisLine.get(3).equals("pikachu") || thisLine.get(2).equals("pikachu")){ //4 terms that don't end in "pikachu" or where the third term is "pikachu" doesn't exist
System.err.println("Error: Found line with no meaning on line " + lineNum);
System.exit(0);
} else{
switch (thisLine.get(0)){
case "pi":
switch(thisLine.get(1)){
case "pi":
fourTermsPushTwo(thisLine.get(2));
break;
case "pika":
if (thisLine.get(2).equals("pi")){
if (pi.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pi pikachu");
System.exit(0);
}
int temp = pi.pop().intValue();
int sum = temp + pi.peek().intValue();
pi.push(new Integer(temp));
pi.push(new Integer(sum));
} else if (thisLine.get(2).equals("pika")){
if (pika.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pika pikachu");
System.exit(0);
}
int temp = pika.pop().intValue();
int sum = temp + pika.peek().intValue();
pika.push(new Integer(temp));
pika.push(new Integer(sum));
}
break;
case "pikachu":
if (thisLine.get(2).equals("pi")){
if (pi.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pi pikachu");
System.exit(0);
}
int temp = pi.pop().intValue();
int prod = temp * pi.peek().intValue();
pi.push(new Integer(temp));
pi.push(new Integer(prod));
} else if (thisLine.get(2).equals("pika")){
if (pika.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pika pikachu");
System.exit(0);
}
int temp = pika.pop().intValue();
int prod = temp * pika.peek().intValue();
pika.push(new Integer(temp));
pika.push(new Integer(prod));
}
break;
}
break;
case "pika":
switch(thisLine.get(1)){
case "pi":
if (thisLine.get(2).equals("pi")){
if (pi.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pi pikachu");
System.exit(0);
}
int temp = pi.pop().intValue();
int diff = pi.peek().intValue() - temp;
pi.push(new Integer(temp));
pi.push(new Integer(diff));
} else if (thisLine.get(2).equals("pika")){
if (pika.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pika pikachu");
System.exit(0);
}
int temp = pika.pop().intValue();
int diff = temp - pika.peek().intValue();
pika.push(new Integer(temp));
pika.push(new Integer(diff));
}
break;
case "pika":
fourTermsPushTwo(thisLine.get(2));
break;
case "pikachu":
if (thisLine.get(2).equals("pi")){
builder.append(pi.pop());
} else{
builder.append(pika.pop());
}
break;
}
break;
case "pikachu":
switch(thisLine.get(1)){
case "pi":
if (thisLine.get(2).equals("pi")){
if (pi.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pi pikachu");
System.exit(0);
}
int temp = pi.pop().intValue();
if (pi.peek().intValue() == 0){
System.err.println("Error: Division by 0 on line " + lineNum);
}
int quot = pi.peek().intValue() / temp;
pi.push(new Integer(temp));
pi.push(new Integer(quot));
} else if (thisLine.get(2).equals("pika")){
if (pika.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pika pikachu");
System.exit(0);
}
int temp = pika.pop().intValue();
if (pika.peek().intValue() == 0){
System.err.println("Error: Division by 0 on line " + lineNum);
}
int quot = temp / pika.peek().intValue();
pika.push(new Integer(temp));
pika.push(new Integer(quot));
}
break;
case "pika":
fourTermsPushTwo(thisLine.get(2));
break;
case "pikachu":
if (thisLine.get(2).equals("pi")){
builder.append((char)pi.pop().intValue());
} else{
builder.append((char)pika.pop().intValue());
}
break;
}
break;
default:
}
}
} else{
if (!thisLine.get(thisLine.size() - 2).equals("pikachu") && thisLine.get(thisLine.size() - 1).equals("pikachu")){
if (thisLine.get(thisLine.size() - 2).equals("pi")){ //push to pi pikachu
pi.push(new Integer(thisLine.size() - 2));
} else if (thisLine.get(thisLine.size() - 2).equals("pika")){ //push to pika pikachu
pika.push(new Integer(thisLine.size() - 2));
} else{
System.err.println("Error: Found line with no meaning on line " + lineNum);
System.exit(0);
}
}
else{
System.err.println("Error: Unrecognized string on line " + lineNum);
System.exit(0);
}
} //else
} //while (pikachu.fileScan.hasNext())
} //while (go > 0)
return builder.toString();
} //run()
public void fourTermsPushTwo(String third){
if (third.equals("pi")){
pi.push(new Integer(2));
} else if (third.equals("pika")){
pika.push(new Integer(2));
} else{
System.err.println("Error: Found line with no meaning on line " + lineNum);
System.exit(0);
}
}
public void addTopTwo(String third){
if (third.equals("pi")){
if (pi.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pi pikachu");
System.exit(0);
}
int temp = pi.pop().intValue();
int sum = temp + pi.peek().intValue();
pi.push(new Integer(temp));
pi.push(new Integer(sum));
}
if (third.equals("pika")){
if (pika.size() < 2){
System.err.println("Error: Line " + lineNum + ", there are less than 2 elements in pika pikachu");
System.exit(0);
}
int temp = pika.pop().intValue();
int sum = temp + pika.peek().intValue();
pika.push(new Integer(temp));
pika.push(new Integer(sum));
}
}
public void pushInputs(ArrayList<Integer> inputs){
for (Integer input : inputs){
pi.push(input);
}
}
public ArrayList<String> debugVomitPikachus(){
ArrayList<String> contents = new ArrayList<String>();
contents.add("Pi Pikachu contains:");
while (!pi.empty()){
contents.add(pi.pop().toString());
}
contents.add("Pika Pikachu contains:");
while (!pika.empty()){
contents.add(pika.pop().toString());
}
return contents;
} //debugVomitPikachus()
} //Pikachu class