-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
687 lines (657 loc) · 22.9 KB
/
parser.cpp
File metadata and controls
687 lines (657 loc) · 22.9 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#include "lexer.h"
#include "parser.h"
#include "Type.h"
#include "Expression.h"
#include "Variable.h"
#include "Container.h"
#include "Operator.h"
#include <iostream>
#include <map>
#include <list>
#include <stack>
#include <iostream>
#define debug(x) std::cout << x << '\n'
Expression* parseExpr(char* &cur);
std::list<Expression*> parseBlock(char* &cur);
void parseContainer(char* &cur, Container* container);
inline char* next(char* cur) {
switch (*cur) {
case PBOOLEAN:
case POPERATOR:
case PSPECIAL:
case PCHAR:
debug("next(" << (int)*cur << "," << (int)*(cur+1) << ")");
return cur + 2;
case PNAME:
case PINTEGER:
debug("next(" << (int)*cur << "," << *(int*)(cur+1) << ")");
return cur + sizeof(long) + 1;
case PREAL:
debug("next(" << (int)*cur << "," << *(float*)(cur+1) << ")");
return cur + sizeof(float) + 1;
case PSTRING:
debug("next(" << (int)*cur << "," << (cur+1) << ")");
char* res = cur + 1;
while (*(res++)); //keeps going until next is one past null
return res;
}
return 0;
}
inline bool reserved(int name) {
return MAX_PROTECTED > name;
}
Type* parseType(char* &cur) {
debug("parseType");
cur = next(cur);
if (cur[0] == POPERATOR && cur[1] == OP_DEREFGET)
return Type::getPointerType(parseType(cur));
int name = *(int*)(cur+1);
switch (name) {
case RES_ARRAY:
cur = next(cur);
if (cur[0] == PSPECIAL && cur[1] == SPC_LBRACE) {
char* tkfrom = next(cur);
char* tkto = next(next(tkfrom));
if (tkfrom[0] != tkto[0]) {
debug((int)tkfrom[0]);
debug((int)tkto[0]);
debug("we're fucked");
throw -1;
}
int from, to;
switch (tkfrom[0]) {
case PCHAR:
from = tkfrom[1];
to = tkto[1];
break;
case PINTEGER:
from = *(int*)(tkfrom+1);
to = *(int*)(tkto+1);
break;
default:
debug("its not that bad");
throw -1;
}
cur = next(next(tkto)); //skip the ] of
Type* type = parseType(cur);
return Type::getBoundArrayType(type, from, to);
} else { //Already ate the of
return Type::getDynamicArrayType(parseType(cur));
}
case RES_RECORD: {
std::map<int,Type*> fields;
while (*cur) {
cur = next(cur);
int field = *(int*)&cur[1];
if (reserved(field)) {
return Type::getRecordType(fields);
}
cur = next(cur);
Type* type = parseType(cur);
cur = next(cur);
fields[field] = type;
}
}
default:
return Type::getType(name);
}
}
std::map<int,Type*> parseTypes(char* &cur) {
debug("parseTypes");
std::map<int,Type*> types;
do {
char* tok = next(cur);
int name = *(int*)&tok[1];
if (reserved(name)) {
return types;
}
cur = next(tok); //skip =
Type* type = parseType(cur);
cur = next(cur); //skip ;
types[name] = type;
} while (*cur);
return types;
}
std::map<int,Expression*> parseConsts(char* &cur) {
debug("parseConsts");
std::map<int,Expression*> consts;
char* tok = next(cur);
while (*tok) {
int name = *(int*)(tok+1);
if (reserved(name)) {
return consts;
debug("consts_return =" << name);
}
cur = next(tok); //skip =
Expression* expr = parseExpr(cur); //eats the ;
consts[name] = expr;
tok = next(cur);
}
return consts;
}
std::list<Variable*> parseVars(char* &cur) {
debug("parseVars");
std::list<Variable*> vars;
std::stack<int> untyped;
while (*cur) {
char* tok = next(cur);
if (tok[0] == PSPECIAL) {
switch (tok[1]) { //silent skip ,
case SPC_COLON: {
Type* type = parseType(tok);
while (!untyped.empty()) {
vars.push_back(new Variable(untyped.top(), type));
untyped.pop();
}
cur = next(tok);
}
default:
cur = tok;
}
} else {
int name = *(int*)(tok+1);
if (reserved(name)) {
debug("numvars=" << vars.size());
return vars;
}
untyped.push(name);
cur = tok;
}
}
return vars;
}
Method* parseMethod(char* &cur) {
debug("parseMethod");
cur = next(cur);
Method* meth = new Method(*(int*)(cur+1));
std::stack<int> untyped;
bool byref = false;
bool hasArgs = false;
bool moreArgs = true;
while (*cur) {
cur = next(cur);
if (cur[0] == PSPECIAL) {
switch (cur[1]) {
case SPC_RPAREN:
moreArgs = false;
break;
case SPC_LPAREN:
hasArgs = true;
break;
case SPC_COLON: {
Type* type = parseType(cur);
if (!hasArgs || !moreArgs) {
meth->type = type;
cur = next(cur);
goto end_defparse;
} else {
while (!untyped.empty()) {
meth->arguments.push_back(new Variable(untyped.top(), type, byref));
untyped.pop();
}
byref = false;
}
break;
}
case SPC_SEMICOLON:
goto end_defparse;
}
} else {
int name = *(int*)(cur+1);
if (name == RES_VAR) {
byref = true;
} else {
untyped.push(name);
}
}
}
end_defparse:
parseContainer(cur,meth);
return meth;
}
std::map<char,char> precedence_map() {
std::map <char,char> prec;
prec[OP_ARRAYGET] = 6; //Careful
prec[OP_FIELDGET] = 6; //Careful
prec[OP_DEREFGET] = 6; //Careful
prec[OP_ADDR] = 5; //Careful
prec[OP_NOT] = 4;
prec[OP_NEG] = 4; //Careful, this has to be parsed from a urinary -
prec[OP_MUL] = 3;
prec[OP_FDIV] = 3;
prec[OP_IDIV] = 3;
prec[OP_MOD] = 3;
prec[OP_AND] = 3;
prec[OP_OR] = 2;
prec[OP_ADD] = 2;
prec[OP_SUB] = 2;
prec[OP_EQU] = 1;
prec[OP_NEQ] = 1;
prec[OP_LESS] = 1;
prec[OP_LESSEQ] = 1;
prec[OP_GREAT] = 1;
prec[OP_GREATEQ] = 1;
prec[OP_ASGN] = 0;
prec[OP_ARRAYSET] = 0; //Careful
prec[OP_FIELDSET] = 0; //Careful
prec[OP_DEREFSET] = 0; //Careful
return prec;
}
typedef struct {
Operator* op;
char prec;
} opprec;
inline opprec mk_opprec(Operator* op, char prec) {
opprec res;
res.op = op;
res.prec = prec;
return res;
}
Expression* parseExpr(char* &cur) {
debug("parseExpr");
static std::map<char,char> precedence = precedence_map();
std::list<Element*> expr;
std::stack<std::stack<opprec>*> parseStack;
std::stack<opprec>* oper = new std::stack<opprec>();
char lastType = -1, nextType = -1;
while (*cur) {
char* tok = next(cur);
lastType = nextType;
nextType = tok[0];
switch (nextType) {
case PSPECIAL:
switch (tok[1]) {
case SPC_SEMICOLON:
case SPC_RBRACE:
case SPC_COMMA:
cur = tok;
goto end_exprparse;
case SPC_RPAREN:
if (parseStack.empty()) {
goto end_exprparse;
}
while (!oper->empty()) {
expr.push_back(oper->top().op);
oper->pop();
}
delete oper;
oper = parseStack.top();
parseStack.pop();
nextType = -1;
goto next_exprparse;
case SPC_LPAREN:
parseStack.push(oper);
oper = new std::stack<opprec>();
goto next_exprparse;
case SPC_LBRACE: {
std::list<Expression*> indexes;
do {
indexes.push_back(parseExpr(tok));
} while (tok[0] != PSPECIAL && tok[1] != SPC_RBRACE);
char* temp = next(tok);
if (temp[0] == POPERATOR && temp[1] == OP_ASGN) {
tok = temp;
char prec = precedence[OP_ARRAYSET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(new ArraySet(indexes),prec));
} else {
char prec = precedence[OP_ARRAYSET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(new ArrayGet(indexes),prec));
}
} goto next_exprparse;
} break;
case POPERATOR: {
switch (tok[1]) {
case OP_DEREFGET: {
char* temp = next(tok);
if (temp[0] == POPERATOR && temp[1] == OP_ASGN) {
tok = temp;
char prec = precedence[OP_DEREFSET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(Operator::get(OP_DEREFSET),prec));
} else {
char prec = precedence[OP_DEREFGET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(Operator::get(OP_DEREFGET),prec));
}
} goto next_exprparse;
case OP_FIELDGET: {
tok = next(tok);
int name = *(int*)(tok+1);
char* temp = next(tok);
if (temp[0] == POPERATOR && temp[1] == OP_ASGN) {
tok = temp;
char prec = precedence[OP_ARRAYSET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(new FieldSet(name),prec));
} else {
char prec = precedence[OP_ARRAYSET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(new FieldGet(name),prec));
}
} goto next_exprparse;
}
if (lastType == POPERATOR && tok[0] == POPERATOR && tok[1] == OP_SUB) //special operator renaming
tok[1] = OP_NEG;
char prec = precedence[OP_ARRAYSET];
while (!oper->empty() && prec <= oper->top().prec) {
expr.push_back(oper->top().op);
oper->pop();
}
oper->push(mk_opprec(Operator::get(tok[1]),prec));
} break;
case PNAME: {//Could be a variable or method
int name = *(int*)(tok+1);
switch (name) { //handle faux-functions
case RES_SIZE: {
tok = next(tok);
Expression* array = parseExpr(tok); //shouldn't eat the )
tok = next(tok);
expr.push_back(new Size(array));
} goto next_exprparse;
case RES_RESIZE:{
tok = next(tok);
Expression* array = parseExpr(tok); //eats the ,
Expression* dim = parseExpr(tok); //shouldn't eat the )
tok = next(tok);
expr.push_back(new Resize(array,dim));
} goto next_exprparse;
case RES_ARRAY:{
tok = next(tok);
std::list<Expression*> elems;
char* temp = next(tok);
while (temp[0] != PSPECIAL || temp[0] != SPC_RPAREN) {
elems.push_back(parseExpr(tok)); //eats the ,
temp = next(tok);
}
tok = temp;
expr.push_back(new ArrayDef(elems));
} goto next_exprparse;
}
if (reserved(name)) goto end_exprparse; //All reserved words break expression parsing
if (*tok) {
char* temp = next(tok);
if (temp[0] == PSPECIAL && temp[1] == SPC_LPAREN) {
std::list<Expression*> args;
tok = temp;
while (*temp) {
temp = next(tok);
if (temp[0] == PSPECIAL && temp[1] == SPC_RPAREN) {
tok = temp;
break;
}
args.push_back(parseExpr(tok));
}
expr.push_back(new Symbol(name,args));
} else {
expr.push_back(new Symbol(name));
}
} else {
expr.push_back(new Symbol(name));
}
} break;
case PINTEGER:
expr.push_back(new IntegerValue(*(int*)(tok+1)));
break;
case PREAL:
expr.push_back(new RealValue(*(float*)(tok+1)));
break;
case PSTRING:
expr.push_back(new StringValue(tok+1));
break;
case PBOOLEAN:
expr.push_back(new BooleanValue(tok[1] != BOOL_FALSE));
break;
case PCHAR:
expr.push_back(new CharValue(tok[1]));
}
next_exprparse:
debug("next_exprparse");
cur = tok;
}
end_exprparse:
debug("end_exprparse");
while (!oper->empty()) {
expr.push_back(oper->top().op);
oper->pop();
}
debug("expr_created");
return new Expression(expr);
}
Until* parseUntil(char* &cur) {
debug("parseUntil");
std::list<Expression*> block = parseBlock(cur); //eats the until
Expression* cond = parseExpr(cur);
return new Until(block,cond);
}
Case* parseCase(char* &cur) {
debug("parseCase");
cur = next(cur); //skip case
Expression* cond = parseExpr(cur);
cur = next(cur); //skip of
Case* pcase = new Case(cond);
parsecase:
while (*cur) {
cur = next(cur);
if (cur[0] == PNAME) {
switch (*(int*)(cur+1)) {
case RES_ELSE: {
std::list<Expression*> def = parseBlock(cur);
pcase->setDefault(def);
cur = next(cur); //skip end
}
case RES_END:
cur = next(cur); //skip ;
return pcase;
}
}
std::stack<int> switches;
do {
if (cur[0] == PINTEGER) {
switches.push(*(int*)(cur+1));
} else if (cur[0] == PCHAR) {
switches.push(cur[1]);
}
cur = next(cur);
} while (cur[0] != PSPECIAL || cur[1] != SPC_COLON); //breaks on colon
std::list<Expression*> block = parseBlock(cur);
while (!switches.empty()) {
pcase->addBranch(switches.top(),block);
switches.pop();
}
}
return pcase;
}
For* parseFor(char* &cur) {
debug("parseFor");
cur = next(cur); //skip for
int var = *(int*)(cur+1);
cur = next(cur); //skip :=
Expression* begin = parseExpr(cur);
cur = next(cur); //grap the to/downto
bool inc = cur[1] == RES_TO;
cur = next(cur);
Expression* end = parseExpr(cur);
cur = next(cur); //skip do
//System.out.println("End: " + end);
std::list<Expression*> block = parseBlock(cur);
return new For(var,begin,inc,end,block);
}
While* parseWhile(char* &cur) {
debug("parseWhile");
cur = next(cur); //skip while
Expression* cond = parseExpr(cur);
cur = next(cur); //skip do
std::list<Expression*> block = parseBlock(cur);
return new While(cond,block);
}
If* parseIf(char* &cur) {
debug("parseIf");
cur = next(cur); //skip if
Expression* cond = parseExpr(cur);
cur = next(cur); //skip then
std::list<Expression*> block = parseBlock(cur);
If* res = new If(cond,block);
while (*cur) {
char* tok = next(cur);
if (tok[0] == PNAME && *(int*)(tok+1) == RES_ELSE) {
char* temp = next(tok);
if (temp[0] == PNAME && *(int*)(temp+1) == RES_IF) {
cond = parseExpr(temp);
temp = next(temp); //skip then
block = parseBlock(temp);
res->addBranch(cond, block);
cur = temp;
} else {
block = parseBlock(tok);
res->setDefault(block);
cur = tok;
}
} else {
break;
}
}
return res;
}
Try* parseTry(char* &cur) {
debug("parseTry");
std::list<Expression*> danger = parseBlock(cur);
std::list<Expression*> saftey;
std::list<Expression*> always;
char* tok = next(cur);
switch (*(int*)(tok+1)) {
case RES_EXCEPT:
saftey = parseBlock(cur);
tok = next(cur);
if (tok[0] != PNAME || *(int*)(tok+1) != RES_FINALLY)
break;
case RES_FINALLY:
always = parseBlock(cur);
}
return new Try(danger,saftey,always);
}
std::list<Expression*> parseBlock(char* &cur) {
debug("parseBlock");
std::list<Expression*> block;
bool fullblock;
char* tok = next(cur);
if (tok[0] == PNAME) {
switch (*(int*)(tok+1)) {
case RES_BEGIN:
case RES_REPEAT:
case RES_TRY:
case RES_EXCEPT:
case RES_FINALLY:
fullblock = true;
cur = tok;
break;
default:
fullblock = false;
}
debug("fullblock=" << fullblock);
do {
tok = next(cur);
debug("block_switch=" << *(int*)(tok+1));
switch (*(int*)(tok+1)) { //Expressions alway begin with a PName?
case RES_END:
cur = next(tok);
if (cur[0] != PSPECIAL || cur[1] != SPC_SEMICOLON)
cur = tok;
goto end_blockparse;
case RES_ELSE:
case RES_UNTIL:
cur = tok;
goto end_blockparse;
case RES_EXCEPT:
case RES_FINALLY:
goto end_blockparse;
case RES_IF:
block.push_back(parseIf(cur));
break;
case RES_WHILE:
block.push_back(parseWhile(cur));
break;
case RES_CASE:
block.push_back(parseCase(cur));
break;
case RES_REPEAT:
block.push_back(parseUntil(cur));
break;
case RES_FOR:
block.push_back(parseFor(cur));
break;
case RES_TRY:
block.push_back(parseTry(cur));
break;
default: //Name but not reserved
block.push_back(parseExpr(cur));
break;
}
} while (fullblock);
}
end_blockparse:
return block;
}
void parseContainer(char* &cur, Container* container) {
debug("parseContainer");
char* tok;
do {
tok = next(cur);
if (!*tok) return; // empty body or eof
switch (*(int*)(tok+1)) {
case RES_CONST: { //multi
std::map<int,Expression*> consts = parseConsts(tok);
container->constants.insert(consts.begin(),consts.end());
} break;
case RES_TYPE: { //multi
std::map<int,Type*> types = parseTypes(tok);
container->types.insert(types.begin(),types.end());
} break;
case RES_VAR: { //multi
std::list<Variable*> vars = parseVars(tok);
container->variables.insert(container->variables.end(),vars.begin(),vars.end());
} break;
case RES_PROCEDURE: //multi
case RES_FUNCTION: { //multi
Method* meth = parseMethod(tok);
container->methods.push_back(meth);
} break;
case RES_BEGIN: { //single
std::list<Expression*> block = parseBlock(cur);
fillBlock(&container->block,&block);
return;
}
}
cur = tok;
} while (*cur);
}
Program* parse(char* tokens) {
debug("parse");
char* cur = tokens;
if (cur[0] != PNAME || *(int*)(cur+1) != RES_PROGRAM)
return 0;
cur = next(cur); //name
Program* prog = new Program(*(int*)(cur+1));
cur = next(cur); //skip ;
parseContainer(cur,prog);
return prog;
}