-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
866 lines (766 loc) · 17.1 KB
/
main.c
File metadata and controls
866 lines (766 loc) · 17.1 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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.c" // my own utils functions libraary?
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
// WMOC-PL0: Writing My Own Compiler for PL/0 language
// this is me writing my own compiler, following the blog at
// https://briancallahan.net/blog/20210814.html
/* PL/0 compiler
program = block "." .
block = [ "const" ident "=" number { "," ident "=" number } ";" ]
[ "var" ident { "," ident } ";" ]
{ "procedure" ident ";" block ";" } statement .
statement = [ ident ":=" expression
| "call" ident
| "begin" statement { ";" statement } "end"
| "if" condition "then" statement
| "while" condition "do" statement ] .
| "printInt" ident
| "printChar" into ident
| "readInt" into ident
condition = "odd" expression
| expression ( "=" | "#" | "<" | ">" ) expression .
expression = [ "+" | "-" ] term { ( "+" | "-" ) term } .
term = factor { ( "*" | "/" ) factor } .
factor = ident
| number
| "(" expression ")" .
ident = "A-Za-z_" { "A-Za-z0-9_" } .
number = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" .
*/
// -------------- DEFINES (TOKENS) -------------------
// TK_ -> means a token
#define TK_DOT '.'
#define TK_IDENTIFIER 'I'
#define TK_NUMBER 'N'
#define TK_CONST 'C'
#define TK_VAR 'V'
#define TK_PROCEDURE 'P'
#define TK_CALL 'c'
#define TK_BEGIN 'B'
#define TK_END 'E'
#define TK_IF 'i'
#define TK_THEN 'T'
#define TK_WHILE 'W'
#define TK_DO 'D'
#define TK_ODD 'O'
#define TK_COMMA ','
#define TK_SEMICOLON ';'
#define TK_MULTIPLY '*'
#define TK_DIVIDE '/'
#define TK_ADD '+'
#define TK_MINUS '-'
#define TK_EQUAL '='
#define TK_GREATER '>'
#define TK_LESSER '<'
#define TK_NOTEQ '#' // ~=
#define TK_ASSIGN ':' // :=
#define TK_LPAREN '('
#define TK_RPAREN ')'
// I/O tokens
#define TK_PRINTINT 'p'
#define TK_PRINTCHAR 'H'
#define TK_READINT 'R'
#define TK_READCHAR 'h'
#define TK_INTO 'n'
#define WMOC_VERSION "1.0.0"
#define CHECK_LHS 0
#define CHECK_RHS 1
#define CHECK_CALL 2
/*
* MISC functions
*/
static unsigned long line = 1;
char *buffer, *raw;
char *token;
char type;
int proc; // to check if the curr procedure is int main in c or not
int depth; // nesting depth meter
FILE *output;
static void error(char *fmt, ...) {
va_list args;
ERED;
fprintf(stderr, "wmoc: Error on line %lu \n", line);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fputc('\n', stderr);
ERESET;
exit(1);
}
static void readin(char *filename) {
int fd;
struct stat file_stats;
if (strrchr(filename, '.') == NULL) {
error(".pl0 file extension required");
}
if (strcmp(strrchr(filename, '.'), ".pl0") != 0) {
error(".pl0 file extension required");
}
fd = open(filename, O_RDONLY);
if (fd == -1) {
error("Error opening a file");
}
if (fstat(fd, &file_stats) == -1) {
error("Error getting the size of the file");
}
buffer = malloc(sizeof(char) * (file_stats.st_size + 1));
if (buffer == NULL) {
error("Malloc error");
}
if (read(fd, buffer, file_stats.st_size) != file_stats.st_size) {
error("Cant read the data");
}
buffer[file_stats.st_size] = '\0';
raw = buffer;
close(fd);
}
/*
* SYMBOL TABLE
*/
struct SymbolTable {
int depth;
char *name;
int type;
struct SymbolTable *next;
};
static struct SymbolTable *head;
static struct SymbolTable *createsymb() {
struct SymbolTable *new;
if ((new = (struct SymbolTable *)malloc(sizeof(struct SymbolTable))) ==
NULL) {
error("malloc failed");
}
new->next = NULL;
return new;
}
static void initsymbtab(void) {
struct SymbolTable *new = createsymb();
new->type = TK_PROCEDURE;
new->depth = 0;
new->name = "main";
head = new;
}
static void addsymb(int type) {
struct SymbolTable *new = createsymb();
struct SymbolTable *curr;
// printf("adsymbols called with %s %c\n", token, type);
curr = head;
// printf("this is the pointer %p\n", curr);
while (curr->next != NULL) {
// printf("> %s %c\n", curr->name, curr->type);
if (curr->type == type) {
if (strcmp(curr->name, token) == 0) {
if (curr->depth == depth - 1) {
error("Duplicate symbols");
}
}
}
curr = curr->next;
}
// NOTE: we do depth-1 bec we increment the depth level immediately after the
// check to make sure we don't have nested procedures and so the depth
// variable is always one level higher than we're currently at
new->type = type;
new->depth = depth - 1;
if ((new->name = strdup(token)) == NULL) {
error("Malloc failed");
}
curr->next = new;
}
static void destsymb(void) {
struct SymbolTable *curr, *prev;
again:
curr = head;
while (curr->next != NULL) {
prev = curr;
curr = curr->next;
}
if (curr->type != TK_PROCEDURE) {
free(curr->name);
free(curr);
prev->next = NULL;
goto again;
}
}
/*
* SEMANTICS
*/
static void symcheck(int check) {
struct SymbolTable *curr, *found = NULL;
// curr = head;
// while (curr != NULL) {
// printf(">> %s %c\n", curr->name, curr->type);
// curr = curr->next;
// }
curr = head;
// printf("SYMCHECK called %s ", token);
while (curr != NULL) {
if (strcmp(curr->name, token) == 0) {
found = curr;
}
// printf("> %s: %s %c\n", token, curr->name, curr->type);
curr = curr->next;
}
if (found == NULL) {
error("Identifier %s not found", token);
}
// printf("%s %c \n", found->name, found->type);
switch (check) {
case CHECK_LHS:
if (found->type != TK_VAR) {
// printf("%s %c -< \n", found->name, found->type);
error("Can only assign to type var: %s", token);
}
break;
case CHECK_RHS:
if (found->type == TK_PROCEDURE) {
error("must not be a procedure: %s", token);
}
break;
case CHECK_CALL:
if (found->type != TK_PROCEDURE) {
error("can only call a procedure: %s", token);
}
break;
}
}
/*
* LEXER
*/
void comment() {
char ch;
while ((ch = *raw++) != '}') {
if (ch == '\0') {
error("undetermined comment");
}
if (ch == '\n')
line++;
}
}
static int identifier() {
char *ptemp;
ptemp = raw;
int len = 0;
while (isalnum(*raw) || *raw == '_') {
len++;
raw++;
}
free(token);
if ((token = malloc(len + 1)) == NULL) {
error("Malloc failed");
}
for (int i = 0; i < len; i++) {
token[i] = *ptemp++;
}
token[len] = '\0';
--raw;
if (strcmp(token, "if") == 0) {
return TK_IF;
} else if (strcmp(token, "do") == 0) {
return TK_DO;
} else if (strcmp(token, "then") == 0) {
return TK_THEN;
} else if (strcmp(token, "while") == 0) {
return TK_WHILE;
} else if (strcmp(token, "odd") == 0) {
return TK_ODD;
} else if (strcmp(token, "const") == 0) {
return TK_CONST;
} else if (strcmp(token, "call") == 0) {
return TK_CALL;
} else if (strcmp(token, "procedure") == 0) {
return TK_PROCEDURE;
} else if (strcmp(token, "var") == 0) {
return TK_VAR;
} else if (strcmp(token, "begin") == 0) {
return TK_BEGIN;
} else if (strcmp(token, "end") == 0) {
return TK_END;
} else if (strcmp(token, "printInt") == 0) {
return TK_PRINTINT;
} else if (strcmp(token, "printChar") == 0) {
return TK_PRINTCHAR;
} else if (strcmp(token, "readInt") == 0) {
return TK_READINT;
} else if (strcmp(token, "readChar") == 0) {
return TK_READCHAR;
} else if (strcmp(token, "into") == 0) {
return TK_INTO;
} else {
return TK_IDENTIFIER;
}
}
static int number() {
char *p;
p = raw;
int len = 0;
while (isdigit(*raw) || *raw == '_') {
raw++;
len++;
}
// if (*raw != ' ' && *raw != '\t' && *raw != '\n' && *raw != ';') {
// error("Not a number");
// }
free(token);
if ((token = malloc(len + 1)) == NULL) {
error("Malloc failed");
}
for (int i = 0; i < len; i++) {
token[i] = *(p++);
}
token[len] = '\0';
--raw;
// printf("end of the number is %c\n", *raw);
return TK_NUMBER;
}
static int lex() {
redo:
while (*raw == ' ' || *raw == '\t' || *raw == '\n') {
if (*raw++ == '\n')
line++;
}
if (*raw == '{') {
comment();
goto redo;
}
if (isalpha(*raw) || *raw == '_') {
return identifier();
}
if (isdigit(*raw)) {
return number();
}
switch (*raw) {
case '*':
case '/':
case '+':
case ';':
case '-':
case '(':
case ')':
case '<':
case '>':
case '.':
case ',':
case '=':
case '#':
return (*raw);
case ':':
if (*(++raw) != '=') {
error("Invalid token ':%c'", *raw);
} else {
return TK_ASSIGN;
}
case '\0':
return 0;
default:
error("Unidentified token");
}
return 0;
}
/*
* Code Generator
*/
static void aout(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
(void)vfprintf(output, fmt, ap);
va_end(ap);
}
static void cg_end(void) {
aout("/* PL/0 Compiler WMOC v%s */\n", WMOC_VERSION);
}
static void cg_const(void) { aout("const long %s=", token); }
static void cg_symb(void) {
switch (type) {
case TK_IDENTIFIER:
case TK_NUMBER:
aout("%s", token);
break;
case TK_DIVIDE:
aout("/");
break;
case TK_MULTIPLY:
aout("*");
break;
case TK_ADD:
aout("+");
break;
case TK_MINUS:
aout("-");
break;
case TK_BEGIN:
aout("{\n");
break;
case TK_END:
aout(";\n}\n");
break;
case TK_IF:
aout("if(");
break;
case TK_THEN:
case TK_DO:
aout(")");
break;
case TK_ODD:
aout("(");
break;
case TK_WHILE:
aout("while(");
break;
case TK_EQUAL:
aout("==");
break;
case TK_COMMA:
aout(",");
break;
case TK_ASSIGN:
aout("=");
break;
case TK_NOTEQ:
aout("!=");
break;
case TK_LESSER:
aout("<");
break;
case TK_GREATER:
aout(">");
break;
case TK_LPAREN:
aout("(");
break;
case TK_RPAREN:
aout(")");
}
}
static void cg_semicolon(void) { aout(";\n"); }
static void cg_var(void) { aout("long %s;\n", token); }
static void cg_nl(void) { aout("\n"); }
static void cg_procedure(void) {
if (proc == 0) {
aout("int main(int argc, char **argv) ");
} else {
aout("void %s() ", token);
}
aout("{\n");
}
static void cg_epilogue(void) {
aout(";\n");
if (proc == 0) {
aout("return 0;");
}
aout("\n}\n\n");
}
static void cg_call(void) { aout("%s();\n"); }
static void cg_odd(void) { aout(") & 1"); }
static void cg_printchar(void) {
aout("(void) fprintf(stdout, \"%%c\", (unsigned char) %s);", token);
}
static void cg_printint(void) {
aout("(void) fprintf(stdout, \"%%ld\\n\", (long) %s);\n", token);
}
static void cg_init(void) {
aout("#include <stdio.h>\n");
aout("static char __stdin[24];\n\n");
}
static void cg_readchar(void) {
aout("(void) fprintf(stdout, \"> \";");
aout("%s = (unsigned char) fgetc(stdin);", token);
}
static void cg_readint(void) {
aout("(void) fprintf(stdout, \"> \";");
aout("(void) fscanf(stdin, \"%%ld\", %s)", token);
}
/* PARSER */
// basically type is the current token and token is the current lexeme, yeah ik
// and raw is the pointer to the string received from the file
// next gives the next token
static void next() {
type = lex();
// printf("next token: %s %c\n", token, type);
raw++;
}
// expect the next token as match
static void expect(char match) {
if (match != type) {
// printf("this is the error: %c %c \n", match, type);
error("Syntax Error");
}
next();
}
static void expression();
static void factor() {
if (type == TK_IDENTIFIER) {
symcheck(CHECK_RHS);
cg_symb();
next();
} else if (type == TK_NUMBER) {
cg_symb();
next();
} else if (type == TK_LPAREN) {
cg_symb();
expect(TK_LPAREN);
expression();
if (type == TK_RPAREN) {
cg_symb();
}
expect(TK_RPAREN);
}
}
static void term() {
// printf("a term\n");
factor();
while (type == TK_MULTIPLY || type == TK_DIVIDE) {
cg_symb();
next();
factor();
}
}
static void expression() {
// printf("this is expression\n");
if (type == TK_ADD || type == TK_MINUS) {
cg_symb();
next();
}
// printf("this is me %s %c\n", token, type);
term();
while (type == TK_ADD || type == TK_MINUS) {
cg_symb();
next();
term();
}
}
static void condition() {
// printf("this is a condition\n");
if (type == TK_ODD) {
cg_symb();
expect(TK_ODD);
expression();
cg_odd();
} else {
expression();
switch (type) {
case TK_EQUAL:
case TK_NOTEQ:
case TK_LESSER:
case TK_GREATER:
cg_symb();
next();
break;
default:
error("Invalid conditional");
}
expression();
}
}
static void statement() {
// printf("this is a statement\n");
if (type == TK_IDENTIFIER) {
symcheck(CHECK_LHS);
cg_symb();
expect(TK_IDENTIFIER);
if (type == TK_ASSIGN)
cg_symb();
expect(TK_ASSIGN);
expression();
} else if (type == TK_CALL) {
expect(TK_CALL);
if (type == TK_IDENTIFIER) {
symcheck(CHECK_CALL);
cg_call();
}
expect(TK_IDENTIFIER);
} else if (type == TK_BEGIN) {
cg_symb();
expect(TK_BEGIN);
statement();
while (type == TK_SEMICOLON) {
cg_semicolon();
expect(TK_SEMICOLON);
statement();
}
if (type == TK_END) {
cg_symb();
}
expect(TK_END);
} else if (type == TK_IF) {
cg_symb();
expect(TK_IF);
condition();
if (type == TK_THEN) {
cg_symb();
}
expect(TK_THEN);
statement();
} else if (type == TK_WHILE) {
cg_symb();
expect(TK_WHILE);
condition();
if (type == TK_DO) {
cg_symb();
}
expect(TK_DO);
statement();
} else if (type == TK_PRINTINT) {
expect(TK_PRINTINT);
if (type == TK_IDENTIFIER || type == TK_NUMBER) {
if (type == TK_IDENTIFIER) {
symcheck(CHECK_RHS);
}
cg_printint();
}
if (type == TK_IDENTIFIER) {
expect(TK_IDENTIFIER);
} else if (type == TK_NUMBER) {
expect(TK_NUMBER);
} else {
error("Expected an identifier or a number");
}
} else if (type == TK_PRINTCHAR) {
expect(TK_PRINTCHAR);
if (type == TK_IDENTIFIER || type == TK_NUMBER) {
if (type == TK_IDENTIFIER) {
symcheck(CHECK_RHS);
}
cg_printchar();
}
if (type == TK_IDENTIFIER) {
expect(TK_IDENTIFIER);
} else if (type == TK_NUMBER) {
expect(TK_NUMBER);
} else {
error("Expected an identifier or a number");
}
} else if (type == TK_READINT) {
expect(TK_READINT);
expect(TK_INTO);
if (type == TK_IDENTIFIER) {
symcheck(CHECK_LHS);
cg_readint();
}
expect(TK_IDENTIFIER);
} else if (type == TK_READCHAR) {
expect(TK_READCHAR);
expect(TK_INTO);
if (type == TK_IDENTIFIER) {
symcheck(CHECK_LHS);
cg_readchar();
}
expect(TK_IDENTIFIER);
}
}
// for processing the blocks in the language
static void block() {
// printf("this is a block \n");
if (depth++ > 3) {
error("Nesting depth increased");
}
// checking for const lines
if (type == TK_CONST) {
expect(TK_CONST);
if (type == TK_IDENTIFIER) {
addsymb(TK_CONST);
cg_const();
}
expect(TK_IDENTIFIER);
expect(TK_EQUAL);
if (type == TK_NUMBER) {
cg_symb();
cg_semicolon();
}
expect(TK_NUMBER);
while (type == TK_COMMA) {
expect(TK_COMMA);
if (type == TK_IDENTIFIER) {
addsymb(TK_CONST);
cg_const();
}
expect(TK_IDENTIFIER);
expect(TK_EQUAL);
if (type == TK_NUMBER) {
cg_symb();
cg_semicolon();
}
expect(TK_NUMBER);
}
expect(TK_SEMICOLON);
}
// now var lines
if (type == TK_VAR) {
expect(TK_VAR);
if (type == TK_IDENTIFIER) {
addsymb(TK_VAR);
cg_var();
}
expect(TK_IDENTIFIER);
while (type == TK_COMMA) {
expect(TK_COMMA);
if (type == TK_IDENTIFIER) {
addsymb(TK_VAR);
cg_var();
}
expect(TK_IDENTIFIER);
}
expect(TK_SEMICOLON);
cg_nl();
}
while (type == TK_PROCEDURE) {
proc = 1;
expect(TK_PROCEDURE);
if (type == TK_IDENTIFIER) {
addsymb(TK_PROCEDURE);
cg_procedure();
}
expect(TK_IDENTIFIER);
expect(TK_SEMICOLON);
block();
expect(TK_SEMICOLON);
proc = 0;
destsymb();
}
if (proc == 0) {
cg_procedure();
}
statement();
cg_epilogue();
if (--depth < 0) {
error("nesting depth fell below 0, huh?");
}
// expect(TK_DOT);
}
static void parse(void) {
// first enforcing the block . rule in the program
cg_init();
next();
block();
// printf("this is the last token %s\n", token);
expect(TK_DOT);
if (type != '\0') {
printf("Extra tokens present at the end of the file\n");
}
cg_end();
}
/*
* MAIN FUNC
*/
int main(int argc, char **argv) {
if (argc < 2) {
printerr("usage: wmoc file.pl0\n");
exit(1);
}
char *startpt;
// opening the file
readin(argv[1]);
startpt = buffer;
// opening the output file
output = fopen(argv[2], "w");
initsymbtab();
parse();
free(startpt);
return 0;
}