-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
608 lines (480 loc) · 16.4 KB
/
Parser.java
File metadata and controls
608 lines (480 loc) · 16.4 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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package dev.alephpt.Dis;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static dev.alephpt.Dis.TokenType.*;
class Parser {
private static class ParserError extends RuntimeException {}
private final List<Token> tokens;
private int current = 0;
Parser(List<Token> tokens) {
this.tokens = tokens;
}
List<Statement> parse() {
List<Statement> statements = new ArrayList<>();
while (!isAtEnd()) { statements.add(definition()); }
return statements;
}
private Express expression() { return assignment(); }
private Statement definition() {
try {
if (match(OBJ)) { return objDefinition(); }
if (match(FORM)) { return formDefinition(); }
if (match(ENUM)) { return enumDefinition(); }
if (match(OP)) { return operation("operation"); }
if (match(DEFINE)) { return varDefinition(); }
return statement();
} catch (ParserError error) {
synchronize();
return null;
}
}
private Statement objDefinition() {
Token name = consume(IDENTIFIER, "Your object needs a name.");
consume(L_BRACE, "Object body requires starting with '{' opening brace.");
List<Statement> body = new ArrayList<>();
while(!check(R_BRACE) && !isAtEnd()) {
if(match(OP) || check(PILOT)) {
body.add(operation("method"));
} else if (match(DEFINE)) {
body.add(varDefinition());
}
else {
throw error(previous(), "The Body of an Object can only contain Operations, Definitions and an Initializer.");
}
}
consume(R_BRACE, "Object body requires ending with '}' closing brace.");
return new Statement.Obj(name, body);
}
private Statement formDefinition() {
Token name = consume(IDENTIFIER, "Your form needs a name.");
consume(L_BRACE, "Form body requires starting with '{' opening brace.");
List<Statement.Variable> members = new ArrayList<>();
while(!check(R_BRACE) && !isAtEnd()) {
consume(DEFINE, "Member name must be defined. Try using 'def " + peek() + "'?");
Token varname = consume(IDENTIFIER, "Member name expected in form '" + name.lexeme + "'.");
Express initial = null;
if (match(L_ASSIGN)) { initial = expression(); }
consume(LINE_END, "Expected endline value '.' after declaration.");
members.add(new Statement.Variable(varname, initial));
}
consume(R_BRACE, "Form body requires ending with '}' closing brace.");
return new Statement.Form(name, members);
}
private Statement enumDefinition() {
Token name = consume(IDENTIFIER, "Your form needs a name.");
consume(L_BRACE, "Form body requires starting with '{' opening brace.");
List<Express.Variable> elements = new ArrayList<>();
while(!check(R_BRACE) && !isAtEnd()) {
Token initName = consume(IDENTIFIER, "Element name expexted in enum element definition.");
consume(LINE_END, "Expected endline value '.' after enum element declaration.");
elements.add(new Express.Variable(initName));
}
consume(R_BRACE, "Form body requires ending with '}' closing brace.");
return new Statement.Enum(name, elements);
}
private Statement statement() {
if (match(AS)) { return asStatement(); }
if (match(WHEN)) { return whenStatement(); }
if (match(LOG)) { return printStatement(); }
if (match(RETURN)) { return returnStatement(); }
if (match(WHILE)) { return whileStatement(); }
if (match(BODY_START)) { return new Statement.Body(block()); }
return expressionStatement();
}
////////
// AS //
////////
private Statement asStatement() {
consume(COMMA, "',' expected after 'as'.");
boolean nullinit = false;
boolean initdef = false;
Statement.Expression initializerExpr = null;
Statement.Variable initializerVar = null;
Token initName = null;
Express initVal = null;
Express increment = null;
Express condition = null;
Token operator = null;
Express right = null;
Token counter = null;
// initializer conditions
if(match(LINE_END)) {
nullinit = true;
}
else if(match(DEFINE)) {
initName = consume(IDENTIFIER, "Variable name expected in definition.");
consume(L_ASSIGN, "Assignment expected for scoped 'as' declaration.");
initVal = expression();
consume(LINE_END, "Expected endline value '.' inside scoped 'as' declaration.");
initializerVar = new Statement.Variable(initName, initVal);
initdef = true;
}
else {
if(match(IDENTIFIER)) {
initName = previous();
initVal = new Express.Variable(initName);
initializerExpr = new Statement.Expression(initVal);
}
consume(LINE_END, "Expected endline value '.' inside scoped 'as' declaration.");
}
// increment conditions
consume(L_PAR, "Opening '(' expected for increment body.");
if(!check(R_PAR) && !nullinit) {
if (match(PLUSPLUS)) {
counter = previous();
}
else if (match(MINUSMINUS)) {
counter = previous();
}
else {
increment = assignment();
}
}
consume(R_PAR, "Closing ')' expected after increment expression.");
// conditional conditions
if (!check(COLON) && !nullinit) {
if(match(INEQ, EQEQ)) {
operator = previous();
right = comparison();
}
if(match(GREATER, GREAT_EQ, LESSER, LESS_EQ)){
operator = previous();
right = term();
}
if(initdef){
condition = new Express.Binary(new Express.Variable(initName), operator, right);
} else {
condition = new Express.Binary(initVal, operator, right);
}
}
consume(COLON, "':' expected after as clauses.");
// body of the loop
Statement body = statement();
// check for runtime error IF counter/increment THEN IF !nullinit
// if the incrementer is not null, we want to increment at the end
if ((increment != null || counter != null) && !nullinit) {
if(counter != null){
body = new Statement.Body(
Arrays.asList(
body,
new Statement.Expression(new Express.Count(counter, new Express.Variable(initName), initName))));
} else {
body = new Statement.Body(
Arrays.asList(
body,
new Statement.Expression(new Express.Assign(initName, increment))));
}
}
if (condition == null) { condition = new Express.Literal(true); }
body = new Statement.While(condition, body);
if (initializerVar != null || initializerExpr != null) {
if(initdef){
body = new Statement.Body(Arrays.asList(initializerVar, body));
} else {
body = new Statement.Body(Arrays.asList(initializerExpr, body));
}
}
else if (!nullinit) {
body = new Statement.Body(Arrays.asList(new Statement.Expression(initVal), body));
}
return body;
}
//////////
// WHEN //
//////////
private Statement whenStatement() {
consume(COMMA, "',' expected after 'when'.");
Express condition = expression();
consume(COLON, "closing ':' expected after when conditional.");
Statement thenBranch = statement();
List<Statement.Or> orBranches = new ArrayList<>();
Statement elseBranch = null;
while (match(OR) && !isAtEnd()) {
orBranches.add(orStatement());
}
if (match(ELSE)) {
consume(COLON, "':' expected after 'else'.");
elseBranch = statement();
}
return new Statement.When(condition, thenBranch, orBranches, elseBranch);
}
////////
// OR //
////////
private Statement.Or orStatement() {
consume(COMMA, "',' expected after 'or'.");
Express condition = expression();
consume(COLON, "closing ':' expected after or conditional.");
Statement orBranch = statement();
return new Statement.Or(condition, orBranch);
}
/////////
// LOG //
/////////
private Statement printStatement() {
consume(R_ASSIGN, "Directional Executive Token '->' expected after Log Statement.");
Express value = expression();
consume(LINE_END, "Expected endline value '.' after log statement.");
return new Statement.Print(value);
}
////////////
// RETURN //
////////////
private Statement returnStatement() {
Token keyword = previous();
Express value = null;
if(!check(LINE_END)){
value = expression();
}
consume(LINE_END, "endline '.' expected after return statement.");
return new Statement.Return(keyword, value);
}
////////
// OP //
////////
private Statement.Operation operation(String kind) {
Token name = consume(IDENTIFIER, "expected " + kind + " name.");
consume(L_ASSIGN, "Imperative Left Assignment operator '<-' expected after " + kind + " declaration.");
List<Token> params = new ArrayList<>();
if(!check(COLON)) {
do {
if(params.size() >= 255) { error(peek(), "parameters exceed maximum 255 Inputs."); }
params.add(consume(IDENTIFIER, "parameter name expected."));
} while (match(COMMA));
}
consume(COLON, "closing ':' expected after " + kind + " parameters.");
consume(BODY_START, "opening '|' expected before " + kind + " body.");
List<Statement> body = block();
return new Statement.Operation(name, params, body);
}
/////////
// DEF //
/////////
private Statement varDefinition() {
Token name = consume(IDENTIFIER, "Variable name expected.");
Express initial = null;
if (match(L_ASSIGN)) { initial = expression(); }
consume(LINE_END, "Expected endline value '.' after variable declaration.");
return new Statement.Variable(name, initial);
}
///////////
// WHILE //
///////////
private Statement whileStatement() {
consume(COMMA, "',' expected after 'while'.");
Express condition = expression();
consume(COLON, "':' expected after while condition.");
Statement body = statement();
return new Statement.While(condition, body);
}
// expression Statement //
private Statement expressionStatement() {
Express express = expression();
consume(LINE_END, "Expected endline value '.' after expression.");
return new Statement.Expression(express);
}
private List<Statement> block() {
List<Statement> statements = new ArrayList<>();
while (!check(BODY_END) && !isAtEnd()) {
statements.add(definition());
}
consume(BODY_END, "Expecting '~' after body.");
return statements;
}
private Express assignment() {
Express expr = or();
if (match(L_ASSIGN)) {
Token equals = previous();
Express value = assignment();
if (expr instanceof Express.Variable) {
Token name = ((Express.Variable)expr).name;
return new Express.Assign(name, value);
} else if (expr instanceof Express.GetProps) {
Express.GetProps get = (Express.GetProps)expr;
return new Express.SetProps(get.object, new Token(PUBLIC, "public", null, 0), get.name, value);
}
error(equals, "Invalid assignment target.");
}
return expr;
}
private Express or() {
Express expr = and();
while (match(OR_OP)) {
Token operator = previous();
Express right = and();
expr = new Express.Logical(expr, operator, right);
}
return expr;
}
private Express and() {
Express expr = count();
while (match(AND_OP)) {
Token operator = previous();
Express right = count();
expr = new Express.Logical(expr, operator, right);
}
return expr;
}
private Express count() {
Express expr = equality();
if (match(PLUSPLUS)) {
Token operator = previous();
Token name = ((Express.Variable)expr).name;
return new Express.Count(operator, expr, name);
}
if (match(MINUSMINUS)) {
Token operator = previous();
Token name = ((Express.Variable)expr).name;
return new Express.Count(operator, expr, name);
}
return expr;
}
private Express equality() {
Express expr = comparison();
while (match(INEQ, EQEQ)) {
Token operator = previous();
Express right = comparison();
expr = new Express.Binary(expr, operator, right);
}
return expr;
}
private Express comparison() {
Express expr = term();
while (match(GREATER, GREAT_EQ, LESSER, LESS_EQ)) {
Token operator = previous();
Express right = term();
expr = new Express.Binary(expr, operator, right);
}
return expr;
}
private Express term() {
Express expr = factor();
while (match(MINUS, PLUS)) {
Token operator = previous();
Express right = factor();
expr = new Express.Binary(expr, operator, right);
}
return expr;
}
private Express factor() {
Express expr = unary();
while(match(WHACK, STAR)){
Token operator = previous();
Express right = unary();
expr = new Express.Binary(expr, operator, right);
}
return expr;
}
private Express unary() {
if(match(NOT, MINUS)){
Token operator = previous();
Express right = unary();
return new Express.Unary(operator, right);
}
return scoping();
}
private Express scoping() {
if(match(GLOBAL)) {
consume(LINE_END, "'global' keyword requires '.' decimal indexing. (e.g. 'global.foo')");
if (match(IDENTIFIER)) { return new Express.GlobalVariable(previous()); }
} else if (match(PARENT)) {
consume(LINE_END, "'parent' keyword requires '.' decimal indexing. (e.g. 'parent.foo')");
if (match(IDENTIFIER)) { return new Express.ParentVariable(previous()); }
}
return calling();
}
private Express calling() {
Express expr = primary();
while (true) {
if(match(R_ASSIGN)){
expr = finishCalling(expr);
} else
if (match(INDEX)) {
Token name = consume(IDENTIFIER, "Expected property name after index");
expr = new Express.GetProps(expr, name);
} else
if (match(L_BRACK)) {
Token name = consume(IDENTIFIER, "Expected property name after opening bracket");
expr = new Express.GetProps(expr, name);
consume(R_BRACK, "Expected closing bracket after property name.");
} else {
break;
}
}
return expr;
}
private Express finishCalling(Express called) {
List<Express> args = new ArrayList<>();
if (!check(LINE_END) && !check(L_BRACK) && !check(INDEX)) {
do {
if (args.size() >= 255) { error(peek(), "Maximum of 255 Arguments Exceeded."); }
args.add(expression());
} while (match(COMMA));
}
// consume(LINE_END, "Calling '" + called + "' requires End Line '.' value.");
return new Express.Calling(called, args);
}
private Express primary() {
if (match(FALSE)) { return new Express.Literal(false); }
if (match(TRUE)) { return new Express.Literal(true); }
if (match(NONE)) { return new Express.Literal(null); }
if (match(NUMERAL, STRING)) { return new Express.Literal(previous().literal); }
if (match(IDENTIFIER)) { return new Express.Variable(previous()); }
if (match(SELF)) { return new Express.Self(previous()); }
if (match(L_PAR)) {
Express expr = expression();
consume(R_PAR, "Expecting ')' after expression");
return new Express.Grouping(expr);
}
throw error(previous(), peek() + "Invalid Expression Found. Something is missing.\n\t Instructions Died at the End of Execution . . RIP.");
}
private boolean match(TokenType... types) {
for (TokenType type: types) {
if(check(type)) {
advance();
return true;
}
}
return false;
}
private Token consume(TokenType type, String message) {
if (check(type)) { return advance(); }
throw error(peek(), message);
}
private boolean check(TokenType type) {
if (isAtEnd()) { return false; }
return peek().type == type;
}
private Token advance() {
if (!isAtEnd()) { current++; }
return previous();
}
private boolean isAtEnd() { return peek().type == EOF; }
private Token peek() { return tokens.get(current); }
private Token previous() { return tokens.get(current - 1); }
private ParserError error(Token token, String message) {
DisC.error(token, message);
return new ParserError();
}
private void synchronize() {
advance();
while (!isAtEnd()) {
if(previous().type == LINE_END) return;
switch(peek().type) {
case OP:
case OBJ:
case DEFINE:
case LOG:
case WHEN:
case OR:
case ELSE:
case AS:
case WHILE:
case RETURN:
return;
}
advance();
}
}
}