forked from Gman0064/aubielang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
541 lines (475 loc) · 17.7 KB
/
Parser.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import java.util.*;
public class Parser {
static int programCounter = 0;
String[] arr1 = {"done","next","loop","def","func","sum","sub","mult","pow","fact","div","if","elif","else","end","equals","nequals","lequals","gequals","lthan","gthan","print","mod","log"};
String[] arr2 = {"num","string"};
public HashSet<String> kwds = new HashSet<String>(Arrays.asList(arr1));
public HashSet<String> types = new HashSet<String>(Arrays.asList(arr2));
// Input stream of AUBIE commands
public Scanner commands;
// Variable storage
public static HashMap<String,Variable> vars;
// Function storage
public static HashMap<String,Function> funcs = new HashMap<String,Function>();
// Constructor, provides input stream for file parser
public Parser(Scanner input, HashMap<String,Variable> varIns) {
commands = input;
vars = varIns;
}
// Processes commands
public void main() {
while (commands.hasNext()) {
parse(commands.nextLine());
}
}
// Parses lines for commands and values
public Object parse(String statementt) {
if (statementt == null) throw new Error("null");
String statement = statementt.trim();
// System.out.println(statement);
String[] stateSplit = statement.split(" ");
String command = stateSplit[0];
String rest = "";
if (statement.startsWith("#")) return "Commented Line";
if (statement.contains("\"")) {
if (statement.charAt(0) == '"' && statement.charAt(0) == statement.charAt(statement.length()-1)) {
String result = statement.substring(1,statement.length()-1);
return result;
}
} else if (stateSplit.length == 1 && !statement.equals("")) {
if (isDefined(command)) {
if (this.vars.get(command).type.equals("num")) {
return Double.parseDouble(this.vars.get(command).value);
} else {
return this.vars.get(command).value;
}
} else if (!funcs.containsKey(command)) {
return Double.parseDouble(command);
}
}
rest = statement.substring(command.length()).trim();
if (command.equals("print")) {
Object result = parse(rest);
System.out.println(result.toString());
return result;
} else if (command.equals("def")) {
def(rest);
return "Variable Defined";
} else if (command.equals("func")) {
makeFunction(rest);
return "Function Defined";
} else if (funcs.containsKey(command)) {
runFunc(command,rest);
return "Function Ran";
} else if (command.equals("sum")) {
return sum(rest);
} else if (command.equals("sub")) {
return subtract(rest);
} else if (command.equals("mult")) {
return mult(rest);
} else if (command.equals("pow")) {
return power(rest);
} else if (command.equals("mod")) {
return mod(rest);
} else if (command.equals("log")) {
return log(rest);
} else if (command.equals("div")) {
return div(rest);
} else if (command.equals("fact")) {
return fact(rest);
} else if (command.equals("")) {
return "Empty Line";
}else if (command.equals("equals")){
return equals(rest);
} else if (command.equals("nequals")){
return nequals(rest);
} else if (command.equals("lequals")){
return lequals(rest);
} else if (command.equals("gequals")){
return gequals(rest);
} else if (command.equals("gthan")){
return gthan(rest);
} else if (command.equals("lthan")){
return lthan(rest);
} else if (command.equals("loop")){
return forLoop(rest);
} else if (command.equals("if")) {
ifMethod(rest);
return "Yahoosie";
}
throw new Error("Command \"" + command + "\" Not Found");
}
// Checks if a variable is defined
public boolean isDefined(String name) {
if (this.vars.containsKey(name)) return true;
return false;
}
// Defines a variable
public void def(String args) {
// name type value
String[] arguments = args.split(" ");
if (arguments.length < 3) throw new Error("Invalid Arguments");
String name = arguments[0];
String type = arguments[1];
if (!types.contains(type)) throw new Error("Invalid Type");
if (kwds.contains(name)) throw new Error("Invalid Name");
String args3 = "";
for (int i = 2; i < arguments.length; i++) {
args3 += arguments[i] + " ";
}
if (type.equals("num")) {
Double value = (Double) parse(args3.trim());
this.vars.put(name,new Variable(type,value.toString()));
} else {
String value = (String) parse(args3.trim());
this.vars.put(name,new Variable(type,value.toString()));
}
}
// Allows users to define functions with parameters
// Parameters are given as: type name type name . . .
public void makeFunction(String statement) {
HashMap<Integer,String[]> varis = new HashMap<Integer,String[]>();
String cmds = "";
String[] args = statement.split(" ");
if (args.length % 2 != 1) throw new Error("Bad Arguments");
String name = args[0];
if (kwds.contains(name)) throw new Error("Invalid Name");
String curr;
while (commands.hasNext()) {
curr = commands.nextLine().trim();
if (curr.equals("end")) break;
cmds += curr + "\n";
}
for (int i = 1; i < args.length; i += 2) {
String vt = args[i];
String vn = args[i+1];
if (!types.contains(vt)) throw new Error("Invalid Type");
if (kwds.contains(vn)) throw new Error("Invalid Name");
varis.put(i/2,new String[]{vn,vt});
}
funcs.put(name,new Function(varis,cmds));
}
public void runFunc(String name, String args) {
String[] argSplit = args.split(" ");
Function f = funcs.get(name);
HashMap<String,Variable> myVars = new HashMap<String,Variable>();
if (args.equals("")) argSplit = new String[0];
if (argSplit.length != f.vars.size()) throw new Error("Baddio");
for (int i = 0; i < argSplit.length; i++) {
String vName = f.vars.get(i)[0];
String vType = f.vars.get(i)[1];
String vVal = argSplit[i];
// Temp
if (vType.equals("num")) {
Double.parseDouble(vVal);
}
myVars.put(vName, new Variable(vType, vVal));
}
Scanner fScan = new Scanner(f.sc);
fScan.useDelimiter("\n");
Parser p = new Parser(new Scanner(f.sc),myVars);
while (p.commands.hasNext()) p.parse(p.commands.nextLine());
}
// Multiplies a list of numbers
public double mult(String statement) {
double product = 1;
String[] words = statement.split(" ");
ArrayList<Double> nums = new ArrayList<Double>();
//Iterate through parameters of our operator
for (int x = 0; x < words.length; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
//Multiply our numbers
for (int x = 0; x < nums.size(); x++) {
product *= nums.get(x);
}
return product;
}
// Adds a list of numbers
public double sum(String statement) {
double sum = 0;
String[] words = statement.split(" ");
ArrayList<Double> nums = new ArrayList<Double>();
// Iterate through our string for numbers to add
for (int x = 0; x < words.length; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
for (int x = 0; x < nums.size(); x++) {
sum += nums.get(x);
}
return sum;
}
// Raises first argument to the power of second argument
public double power(String statement) {
double pow = 1;
String[] words = statement.split(" ");
ArrayList<Double> nums = new ArrayList<Double>();
// Iterate through our string for numbers to add
if (words.length == 2) {
for (int x = 0; x < words.length; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
pow = Math.pow(nums.get(0), nums.get(1));
return pow;
} else {
throw new Error("Invalid arguments");
}
}
// Subtracts the numbers in a list from the first given argument
public double subtract(String statement) {
double result = 0;
String[] words = statement.split(" ");
ArrayList<Double> nums = new ArrayList<Double>();
// Iterate through our string for numbers to add
for (int x = 0; x < words.length; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
for (int x = 0; x < nums.size(); x++) {
if (x == 0) {
result = nums.get(0);
} else {
result -= nums.get(x);
}
}
return result;
}
// Performs x mod y given x as first argument and y as second argument
public double mod(String statement) {
double modulus = -1.0;
String[] words = statement.split(" ");
if (words.length != 2) {
throw new Error("Invalid arguments");
} else {
ArrayList<Double> nums = new ArrayList<Double>();
// Iterate through our string for numbers to add
for (int x = 0; x < 2; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
modulus = nums.get(0);
Double numB = nums.get(1);
if (numB <= 0) throw new Error();
while (modulus >= numB) {
// System.out.println(modulus);
modulus = modulus - numB;
}
}
return modulus;
}
// Divides the first number by a list of numbers
public double div(String statement) {
double div = 1;
String[] words = statement.split(" ");
ArrayList<Double> nums = new ArrayList<Double>();
// Iterate through our string for numbers to add
for (int x = 0; x < words.length; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
for (int x = 0; x < nums.size(); x++) {
if (x == 0) {
div = nums.get(0);
} else {
div /= nums.get(x);
}
}
return div;
}
// Performs log base x of y
public double log(String statement) {
double logVal = 0.0;
String[] words = statement.split(" ");
if (words.length != 2) {
throw new Error("Invalid arguments");
} else {
ArrayList<Double> nums = new ArrayList<Double>();
// Iterate through our string for numbers to add
for (int x = 0; x < 2; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
logVal = Math.log(nums.get(1)) / Math.log(nums.get(0));
}
return logVal;
}
// Performs x factorial, where x is treated as a whole number regardless of precision
public double fact(String statement) {
double result = 0;
String[] words = statement.split(" ");
ArrayList<Double> nums = new ArrayList<Double>();
//Iterate through our string for numbers to add
for (int x = 0; x < words.length; x++) {
try {
Double num = (Double) parse(words[x]);
nums.add(num);
} catch(Exception e) {
throw new Error("Invalid element type");
}
}
result = nums.get(0);
if(result == 0) {
return 0;
}
else {
result = 1;
for (int i = 1; i <= nums.get(0); i++)
result *= i;
}
return result;
}
// Checks if two values are equal
public boolean equals(String statement) {
String[] words = statement.split(" ");
if(words.length == 2)
{
Object a = parse(words[0]);
Object b = parse(words[1]);
return a.equals(b);
}
throw new Error("Invalid Arguments");
}
// Checks if two values are not equal
public boolean nequals(String statement) {
return !equals(statement);
}
// Checks if two
public boolean lequals(String statement) {
String[] words = statement.split(" ");
if(words.length == 2)
{
Object a = parse(words[0]);
Object b = parse(words[1]);
return comparing(a,b) <= 0;
}
throw new Error("Invalid arguments");
}
public boolean gequals(String statement) {
String[] words = statement.split(" ");
if(words.length == 2)
{
Object a = parse(words[0]);
Object b = parse(words[1]);
return comparing(a,b) >= 0;
}
throw new Error("Invalid arguments");
}
public boolean lthan(String statement) {
String[] words = statement.split(" ");
if(words.length == 2)
{
Object a = parse(words[0]);
Object b = parse(words[1]);
return comparing(a,b) < 0;
}
throw new Error("Invalid arguments");
}
public boolean gthan(String statement) {
String[] words = statement.split(" ");
if(words.length == 2)
{
Object a = parse(words[0]);
Object b = parse(words[1]);
return comparing(a,b) > 0;
}
throw new Error("Invalid arguments");
}
public int comparing(Object obj1, Object obj2) {
if (obj1 instanceof Double && obj2 instanceof Double) {
return Double.compare((Double) obj1, (Double) obj2);
} else if (obj1 instanceof String && obj2 instanceof String) {
return obj1.toString().compareTo(obj2.toString());
} else {
throw new Error("Type Mismatch Error");
}
}
public boolean forLoop(String input)
{
String[] Input = input.split(" ");
Double high = 0.0;
if(Input.length == 1)
{
high = (Double) parse(Input[0]);
}
else
{
throw new Error();
}
String nextInput = commands.nextLine().trim();
ArrayList<String> totalCommands = new ArrayList<String>();
while(!nextInput.equals("next"))
{
totalCommands.add(nextInput);
nextInput = commands.nextLine().trim();
}
for(Double i = 0.0; i < high; i++)
{
for(int j = 0; j < totalCommands.size(); j++)
{
parse(totalCommands.get(j));
}
}
return true;
}
public void ifMethod(String rest) {
// System.out.println("If Started :)");
ArrayList<String> ifs = new ArrayList<String>();
String curr = "";
while (this.commands.hasNext()) {
// System.out.println("Looping");
curr = this.commands.nextLine().trim();
// System.out.println("Curr: " + curr);
if (curr.equals("done") || curr.equals("else") || curr.split(" ")[0].equals("elif")) {
// System.out.println("I Broke");
break;
}
ifs.add(curr);
}
if ((Boolean) parse(rest)) {
for (String cmd : ifs) {
parse(cmd);
}
while (this.commands.hasNext()) {
if (this.commands.nextLine().trim().equals("done")) break;
}
} else {
if (curr.split(" ")[0].equals("elif")) {
String expression = curr.substring(curr.split(" ")[0].length()).trim();
ifMethod(expression);
} else {
ifMethod("equals 1 1");
}
}
}
}