-
Notifications
You must be signed in to change notification settings - Fork 14
/
pl0c.pl0
2242 lines (2056 loc) · 39.8 KB
/
pl0c.pl0
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
Copyright (c) 2021 Brian Callahan <[email protected]>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
}
{
pl0c -- PL/0 compiler written in PL/0.
}
const
CHECK_LHS = 0,
CHECK_RHS = 1,
CHECK_CALL = 2,
TOK_IDENT = 'I',
TOK_NUMBER = 'N',
TOK_CONST = 'C',
TOK_VAR = 'V',
TOK_PROCEDURE = 'P',
TOK_CALL = 'c',
TOK_BEGIN = 'B',
TOK_END = 'E',
TOK_IF = 'i',
TOK_THEN = 'T',
TOK_ELSE = 'e',
TOK_WHILE = 'W',
TOK_DO = 'D',
TOK_ODD = 'O',
TOK_WRITEINT = 'w',
TOK_WRITECHAR = 'H',
TOK_WRITESTR = 'S',
TOK_READINT = 'R',
TOK_READCHAR = 'h',
TOK_INTO = 'n',
TOK_PACKED = 'p',
TOK_SIZE = 's',
TOK_EXIT = 'X',
TOK_SYSTEM = 'Y',
TOK_AND = '&',
TOK_OR = '|',
TOK_NOT = '~',
TOK_FORWARD = 'F',
TOK_DOT = '.',
TOK_EQUAL = '=',
TOK_COMMA = ',',
TOK_SEMICOLON = ';',
TOK_ASSIGN = ':',
TOK_HASH = '#',
TOK_LTHAN = '<',
TOK_GTHAN = '>',
TOK_LTHANE = '{',
TOK_GTHANE = '}',
TOK_PLUS = '+',
TOK_MINUS = '-',
TOK_MULTIPLY = '*',
TOK_DIVIDE = '/',
TOK_MODULO = '%',
TOK_LPAREN = '(',
TOK_RPAREN = ')',
TOK_LBRACK = '[',
TOK_RBRACK = ']',
TOK_STRING = '"'
;
var
packed raw size 1048576, { 1 MB }
loc, { Where we are in the input code }
symtab size 1048576, { 8 MB, can hold up to 29960 symbols }
symtype, { For addsymbol }
packed token size 256, { 255 characters + '\0' }
type, { token type }
expectedtype, { expected token type }
typetoprint, { For printtype }
packed str size 256, { For cmpstr }
symtabcnt, { To keep track of number of symtab entries }
ret, { Return code for procedures that need one }
packed keywords size 256, { Keywords table }
keywordidx size 64, { Index into keywords table }
depth, { Track nested subroutines }
proc, { Track when we're inside a subroutine }
ispacked, { If we have a packed array }
value, { For number processing }
line { line number }
;
forward expression;
{ Misc. functions }
procedure error;
begin
writeStr '\npl0c: error: ';
writeInt line;
writeStr ': ';
end;
procedure setupkeywords;
var i;
begin
i := 0;
keywordidx[0] := TOK_CONST;
keywordidx[1] := i;
keywords[i + 0] := 'c';
keywords[i + 1] := 'o';
keywords[i + 2] := 'n';
keywords[i + 3] := 's';
keywords[i + 4] := 't';
keywords[i + 5] := '\0';
i := i + 6;
keywordidx[2] := TOK_VAR;
keywordidx[3] := i;
keywords[i + 0] := 'v';
keywords[i + 1] := 'a';
keywords[i + 2] := 'r';
keywords[i + 3] := '\0';
i := i + 4;
keywordidx[4] := TOK_PROCEDURE;
keywordidx[5] := i;
keywords[i + 0] := 'p';
keywords[i + 1] := 'r';
keywords[i + 2] := 'o';
keywords[i + 3] := 'c';
keywords[i + 4] := 'e';
keywords[i + 5] := 'd';
keywords[i + 6] := 'u';
keywords[i + 7] := 'r';
keywords[i + 8] := 'e';
keywords[i + 9] := '\0';
i := i + 10;
keywordidx[6] := TOK_CALL;
keywordidx[7] := i;
keywords[i + 0] := 'c';
keywords[i + 1] := 'a';
keywords[i + 2] := 'l';
keywords[i + 3] := 'l';
keywords[i + 4] := '\0';
i := i + 5;
keywordidx[8] := TOK_BEGIN;
keywordidx[9] := i;
keywords[i + 0] := 'b';
keywords[i + 1] := 'e';
keywords[i + 2] := 'g';
keywords[i + 3] := 'i';
keywords[i + 4] := 'n';
keywords[i + 5] := '\0';
i := i + 6;
keywordidx[10] := TOK_END;
keywordidx[11] := i;
keywords[i + 0] := 'e';
keywords[i + 1] := 'n';
keywords[i + 2] := 'd';
keywords[i + 3] := '\0';
i := i + 4;
keywordidx[12] := TOK_IF;
keywordidx[13] := i;
keywords[i + 0] := 'i';
keywords[i + 1] := 'f';
keywords[i + 2] := '\0';
i := i + 3;
keywordidx[14] := TOK_THEN;
keywordidx[15] := i;
keywords[i + 0] := 't';
keywords[i + 1] := 'h';
keywords[i + 2] := 'e';
keywords[i + 3] := 'n';
keywords[i + 4] := '\0';
i := i + 5;
keywordidx[16] := TOK_ELSE;
keywordidx[17] := i;
keywords[i + 0] := 'e';
keywords[i + 1] := 'l';
keywords[i + 2] := 's';
keywords[i + 3] := 'e';
keywords[i + 4] := '\0';
i := i + 5;
keywordidx[18] := TOK_WHILE;
keywordidx[19] := i;
keywords[i + 0] := 'w';
keywords[i + 1] := 'h';
keywords[i + 2] := 'i';
keywords[i + 3] := 'l';
keywords[i + 4] := 'e';
keywords[i + 5] := '\0';
i := i + 6;
keywordidx[20] := TOK_DO;
keywordidx[21] := i;
keywords[i + 0] := 'd';
keywords[i + 1] := 'o';
keywords[i + 2] := '\0';
i := i + 3;
keywordidx[22] := TOK_ODD;
keywordidx[23] := i;
keywords[i + 0] := 'o';
keywords[i + 1] := 'd';
keywords[i + 2] := 'd';
keywords[i + 3] := '\0';
i := i + 4;
keywordidx[24] := TOK_WRITEINT;
keywordidx[25] := i;
keywords[i + 0] := 'w';
keywords[i + 1] := 'r';
keywords[i + 2] := 'i';
keywords[i + 3] := 't';
keywords[i + 4] := 'e';
keywords[i + 5] := 'I';
keywords[i + 6] := 'n';
keywords[i + 7] := 't';
keywords[i + 8] := '\0';
i := i + 9;
keywordidx[26] := TOK_WRITECHAR;
keywordidx[27] := i;
keywords[i + 0] := 'w';
keywords[i + 1] := 'r';
keywords[i + 2] := 'i';
keywords[i + 3] := 't';
keywords[i + 4] := 'e';
keywords[i + 5] := 'C';
keywords[i + 6] := 'h';
keywords[i + 7] := 'a';
keywords[i + 8] := 'r';
keywords[i + 9] := '\0';
i := i + 10;
keywordidx[28] := TOK_WRITESTR;
keywordidx[29] := i;
keywords[i + 0] := 'w';
keywords[i + 1] := 'r';
keywords[i + 2] := 'i';
keywords[i + 3] := 't';
keywords[i + 4] := 'e';
keywords[i + 5] := 'S';
keywords[i + 6] := 't';
keywords[i + 7] := 'r';
keywords[i + 8] := '\0';
i := i + 9;
keywordidx[30] := TOK_READINT;
keywordidx[31] := i;
keywords[i + 0] := 'r';
keywords[i + 1] := 'e';
keywords[i + 2] := 'a';
keywords[i + 3] := 'd';
keywords[i + 4] := 'I';
keywords[i + 5] := 'n';
keywords[i + 6] := 't';
keywords[i + 7] := '\0';
i := i + 8;
keywordidx[32] := TOK_READCHAR;
keywordidx[33] := i;
keywords[i + 0] := 'r';
keywords[i + 1] := 'e';
keywords[i + 2] := 'a';
keywords[i + 3] := 'd';
keywords[i + 4] := 'C';
keywords[i + 5] := 'h';
keywords[i + 6] := 'a';
keywords[i + 7] := 'r';
keywords[i + 8] := '\0';
i := i + 9;
keywordidx[34] := TOK_INTO;
keywordidx[35] := i;
keywords[i + 0] := 'i';
keywords[i + 1] := 'n';
keywords[i + 2] := 't';
keywords[i + 3] := 'o';
keywords[i + 4] := '\0';
i := i + 5;
keywordidx[36] := TOK_SIZE;
keywordidx[37] := i;
keywords[i + 0] := 's';
keywords[i + 1] := 'i';
keywords[i + 2] := 'z';
keywords[i + 3] := 'e';
keywords[i + 4] := '\0';
i := i + 5;
keywordidx[38] := TOK_EXIT;
keywordidx[39] := i;
keywords[i + 0] := 'e';
keywords[i + 1] := 'x';
keywords[i + 2] := 'i';
keywords[i + 3] := 't';
keywords[i + 4] := '\0';
i := i + 5;
keywordidx[40] := TOK_AND;
keywordidx[41] := i;
keywords[i + 0] := 'a';
keywords[i + 1] := 'n';
keywords[i + 2] := 'd';
keywords[i + 3] := '\0';
i := i + 4;
keywordidx[42] := TOK_OR;
keywordidx[43] := i;
keywords[i + 0] := 'o';
keywords[i + 1] := 'r';
keywords[i + 2] := '\0';
i := i + 3;
keywordidx[44] := TOK_NOT;
keywordidx[45] := i;
keywords[i + 0] := 'n';
keywords[i + 1] := 'o';
keywords[i + 2] := 't';
keywords[i + 3] := '\0';
i := i + 4;
keywordidx[46] := TOK_MODULO;
keywordidx[47] := i;
keywords[i + 0] := 'm';
keywords[i + 1] := 'o';
keywords[i + 2] := 'd';
keywords[i + 3] := '\0';
i := i + 4;
keywordidx[48] := TOK_FORWARD;
keywordidx[49] := i;
keywords[i + 0] := 'f';
keywords[i + 1] := 'o';
keywords[i + 2] := 'r';
keywords[i + 3] := 'w';
keywords[i + 4] := 'a';
keywords[i + 5] := 'r';
keywords[i + 6] := 'd';
keywords[i + 7] := '\0';
i := i + 8;
keywordidx[50] := TOK_PACKED;
keywordidx[51] := i;
keywords[i + 0] := 'p';
keywords[i + 1] := 'a';
keywords[i + 2] := 'c';
keywords[i + 3] := 'k';
keywords[i + 4] := 'e';
keywords[i + 5] := 'd';
keywords[i + 6] := '\0';
i := i + 7;
keywordidx[52] := TOK_SYSTEM;
keywordidx[53] := i;
keywords[i + 0] := 's';
keywords[i + 1] := 'y';
keywords[i + 2] := 's';
keywords[i + 3] := 't';
keywords[i + 4] := 'e';
keywords[i + 5] := 'm';
keywords[i + 6] := '\0';
i := i + 7;
keywordidx[54] := -1
end;
procedure findkeyword;
var keyword, k, i, eq;
begin
keyword := 0;
ret := 1;
{ while ret # 0 and keywordidx[kw * 2] # -1 }
while (ret * (keywordidx[keyword * 2] + 1)) # 0 do
begin
type := keywordidx[keyword * 2];
k := keywordidx[keyword * 2 + 1];
eq := 1;
i := 0;
{ while eq # 0 and keywords[k] # 0 }
while (eq * keywords[k]) # 0 do
begin
if keywords[k] # token[i] then
eq := 0;
k := k + 1;
i := i + 1
end;
if token[i] # '\0' then
eq := 0;
if eq = 1 then
ret := 0;
keyword := keyword + 1;
end;
end;
procedure cmpstr;
var i;
begin
i := 0;
ret := 0;
while i < 256 do
begin
if token[i] # str[i] then
ret := 1;
i := i + 1
end;
end;
procedure clrstr;
var i;
begin
i := 0;
while i < 256 do
begin
str[i] := '\0';
i := i + 1
end;
end;
procedure readin;
var ch, i;
begin
i := 0;
readChar into ch;
while ch # -1 do
begin
raw[i] := ch;
i := i + 1;
if i = 1048576 then { File too big! }
begin
call error;
writeStr 'file too big\n';
exit 1;
end;
readChar into ch;
end;
loc := 0 { Put the location pointer at the beginning }
end;
{ Lexer }
procedure comment;
begin
while raw[loc] # '}' do
begin
if raw[loc] = '\0' then
begin
call error;
writeStr 'unterminated comment\n';
exit 1;
end
else if raw[loc] = '\n' then
line := line + 1;
loc := loc + 1
end;
end;
procedure ident;
var i, loop;
begin
i := 0;
loop := 1;
while i < 256 do
begin
token[i] := '\0';
i := i + 1
end;
i := 0;
while loop # 0 do
begin
loop := 0;
if raw[loc] >= 'A' then
begin
if raw[loc] <= 'Z' then
begin
loop := 1;
token[i] := raw[loc];
i := i + 1;
if i = 32 then
begin
call error;
writeStr 'identifier too long\n';
exit 1
end;
end;
end;
if loop = 0 then
begin
if raw[loc] >= 'a' then
begin
if raw[loc] <= 'z' then
begin
loop := 1;
token[i] := raw[loc];
i := i + 1;
if i = 32 then
begin
call error;
writeStr 'identifier too long\n';
exit 1
end;
end;
end;
end;
if loop = 0 then
begin
if raw[loc] >= '0' then
begin
if raw[loc] <= '9' then
begin
loop := 1;
token[i] := raw[loc];
i := i + 1;
if i = 32 then
begin
call error;
writeStr 'identifier too long\n';
exit 1
end;
end;
end;
end;
if loop = 0 then
begin
if raw[loc] = '_' then
begin
loop := 1;
token[i] := raw[loc];
i := i + 1;
if i = 32 then
begin
call error;
writeStr 'identifier too long\n';
exit 1
end;
end;
end;
if loop # 0 then
loc := loc + 1;
end;
loc := loc - 1;
call findkeyword;
if ret # 0 then
type := TOK_IDENT;
end;
procedure number;
var i, loop;
begin
i := 0;
loop := 1;
value := 0;
while i < 256 do
begin
token[i] := '\0';
i := i + 1
end;
i := 0;
while loop # 0 do
begin
loop := 0;
if raw[loc] >= '0' then
begin
if raw[loc] <= '9' then
begin
loop := 1;
token[i] := raw[loc];
i := i + 1;
if i = 21 then
begin
call error;
writeStr 'number too long\n';
exit 1;
end;
{ XXX: sanity checking }
value := value * 10;
value := value + (raw[loc] - '0')
end;
end;
if loop = 0 then
begin
if raw[loc] = '_' then
loop := 1;
end;
if loop # 0 then
loc := loc + 1;
end;
loc := loc - 1;
type := TOK_NUMBER;
end;
procedure string;
var i, loop;
begin
i := 0;
loop := 1;
while i < 256 do
begin
token[i] := '\0';
i := i + 1
end;
i := 1;
type := TOK_STRING;
loc := loc + 1;
token[0] := '"';
while loop # 0 do
begin
if raw[loc] = '''' then
begin
loc := loc + 1;
if raw[loc] # '''' then
loop := 0
else
begin
{ XXX: sanity check }
token[i] := '\\';
i := i + 1;
if i = 255 then
begin
call error;
writeStr 'string too long\n';
exit 1;
end;
token[i] := raw[loc];
i := i + 1;
if i = 255 then
begin
call error;
writeStr 'number too long\n';
exit 1;
end;
end;
end
else if raw[loc] = '\n' then
begin
call error;
writeStr 'unterminated string\n';
exit 1;
end
else if raw[loc] = '\0' then
begin
call error;
writeStr 'unterminated string\n';
exit 1;
end
else
begin
{ XXX: sanity check }
token[i] := raw[loc];
i := i + 1;
if i = 255 then
begin
call error;
writeStr 'string too long\n';
exit 1;
end;
end;
if loop # 0 then
loc := loc + 1;
end;
loc := loc - 1;
token[i] := '"';
if i = 2 then
begin
token[0] := '''';
token[2] := '''';
type := TOK_NUMBER
end
else if i = 3 then
begin
if token[1] = '\\' then
begin
token[0] := '''';
token[3] := '''';
type := TOK_NUMBER
end;
end;
end;
procedure lex;
var i, whitespace, isident, isnumber;
begin
isident := 0;
isnumber := 0;
i := 0;
whitespace := 1;
while whitespace = 1 do
begin
if raw[loc] = ' ' then
begin
whitespace := 1;
loc := loc + 1;
end
else if raw[loc] = '\t' then
begin
whitespace := 1;
loc := loc + 1;
end
else if raw[loc] = '\n' then
begin
whitespace := 1;
loc := loc + 1;
line := line + 1;
end
else
whitespace := 0
end;
if raw[loc] >= 'A' then
begin
if raw[loc] <= 'Z' then
begin
isident := 1;
call ident;
end;
end;
if isident = 0 then
begin
if raw[loc] >= 'a' then
begin
if raw[loc] <= 'z' then
begin
isident := 1;
call ident;
end;
end;
end;
if isident = 0 then
begin
if raw[loc] = '_' then
begin
isident := 1;
call ident;
end;
end;
if isident = 0 then
begin
if raw[loc] >= '0' then
begin
if raw[loc] <= '9' then
begin
isnumber := 1;
call number;
end;
end;
end;
if isident = 0 then
begin
if isnumber = 0 then
begin
if raw[loc] = '{' then
begin
call comment;
type := -1
end
else if raw[loc] = '.' then
type := TOK_DOT
else if raw[loc] = '=' then
type := TOK_EQUAL
else if raw[loc] = ',' then
type := TOK_COMMA
else if raw[loc] = ';' then
type := TOK_SEMICOLON
else if raw[loc] = '#' then
type := TOK_HASH
else if raw[loc] = '+' then
type := TOK_PLUS
else if raw[loc] = '-' then
type := TOK_MINUS
else if raw[loc] = '*' then
type := TOK_MULTIPLY
else if raw[loc] = '/' then
type := TOK_DIVIDE
else if raw[loc] = '%' then
type := TOK_MODULO
else if raw[loc] = '(' then
type := TOK_LPAREN
else if raw[loc] = ')' then
type := TOK_RPAREN
else if raw[loc] = '[' then
type := TOK_LBRACK
else if raw[loc] = ']' then
type := TOK_RBRACK
else if raw[loc] = '<' then
begin
loc := loc + 1;
if raw[loc] = '=' then
type := TOK_LTHANE
else if raw[loc] = '>' then
type := TOK_HASH
else
begin
loc := loc - 1;
type := TOK_LTHAN
end;
end
else if raw[loc] = '>' then
begin
loc := loc + 1;
if raw[loc] = '=' then
type := TOK_GTHANE
else
begin
loc := loc - 1;
type := TOK_GTHAN
end;
end
else if raw[loc] = ':' then
begin
loc := loc + 1;
if raw[loc] # '=' then
begin
call error;
writeStr 'unknown token: ''';
writeChar raw[loc];
writeStr '''\n';
exit 1;
end;
type := TOK_ASSIGN;
end
else if raw[loc] = '''' then
begin
call string;
end
else if raw[loc] = '\0' then
type := 0
else
begin
call error;
writeStr 'unknown token: ''';
writeChar raw[loc];
writeStr '''\n';
exit 1;
end;
end;
end;
loc := loc + 1
end;
{ Code generator }
procedure cg_array;
begin
writeChar '[';
writeStr token;
writeChar ']'
end;
procedure cg_call;
begin
writeStr token;
writeStr '();\n'
end;
procedure cg_closeblock;
begin
writeStr ';}\n'
end;
procedure cg_const;
begin
if proc = 0 then
writeStr 'static ';
writeStr 'const long ';
writeStr token;
writeChar '='
end;
procedure cg_crlf;
begin
writeChar '\n'
end;
procedure cg_end;
begin
writeStr '/* PL/0 compiler 1.0.2 */\n'
end;
procedure cg_epilogue;
begin
writeChar ';';
if proc = 0 then
writeStr '\nreturn 0;';
writeStr '\n}\n\n'
end;
procedure cg_exit;
begin
writeStr 'exit('
end;
procedure cg_forward;
begin
writeStr 'static void ';
writeStr token;
writeStr '(void);\n'
end;
procedure cg_init;
begin
writeStr '#include <limits.h>\n';
writeStr '#include <stdio.h>\n';
writeStr '#include <stdlib.h>\n';
writeStr '#include <string.h>\n\n';
writeStr 'static char __stdin[24];\n';
writeStr 'static const char *__errstr;\n\n';
writeStr 'static long __writestridx;\n\n';
writeStr '#ifndef HAVE_STRTONUM\n';
writeStr '#include ';
writeChar '"';
writeStr 'strtonum.c';
writeChar '"';
writeChar '\n';
writeStr '#endif\n\n'
end;
procedure cg_odd;
begin
writeStr ')&1'
end;
procedure cg_procedure;
begin
if proc = 0 then
begin
writeStr 'int\n';
writeStr 'main(int argc, char *argv[])\n'
end
else
begin
writeStr 'static void\n';
writeStr token;
writeStr '(void)\n'
end;
writeStr '{\n'
end;
procedure cg_readchar;
begin
writeStr token;
writeStr '=(int) fgetc(stdin);'
end;
procedure cg_readint;
begin
writeStr '(void) fgets(__stdin, sizeof(__stdin), stdin);\n';
writeStr 'if(__stdin[strlen(__stdin) - 1] == ''\\n'')';
writeStr '__stdin[strlen(__stdin) - 1] = ''\\0'';';
writeStr token;
writeStr '=(long) strtonum(__stdin, LONG_MIN, LONG_MAX, &__errstr);\n';
writeStr 'if(__errstr!=NULL){';
writeStr '(void) fprintf(stderr, ';
writeChar '"';
writeStr 'invalid number: %%s';
writeChar '"';
writeStr ', __stdin);\n';
writeStr 'exit(1);';
writeChar '}'
end;
procedure cg_rparen;
begin
writeChar ')'
end;
procedure cg_semicolon;
begin
writeStr ';\n'
end;
procedure cg_symbol;