-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpl0.c
2702 lines (2615 loc) · 67.5 KB
/
pl0.c
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
// pl0 compiler source code
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include "pl0.h"
#include "set.c"
//////////////////////////////////////////////////////////////////////
// print error message.
void error(int n)
{
int i;
printf(" ");
for (i = 1; i <= cc - 1; i++)
printf(" ");
printf("^\n");
printf("Error %3d: %s\n", n, err_msg[n]);
err++;
} // error
//////////////////////////////////////////////////////////////////////
void getch(void)
{
if (cc == ll)
{
if (feof(infile))
{
printf("\nPROGRAM INCOMPLETE\n");
exit(1);
}
ll = cc = 0;
printf("%5d ", cx);
while ( (!feof(infile)) // added & modified by alex 01-02-09
&& ((ch = getc(infile)) != '\n'))
{
printf("%c", ch);
if ( (++ll) >= 80) // added by nanahka 17-12-20
{
printf("Fatal Error: Too many characters in a line.\n");
exit(EXIT_FAILURE);
}
line[ll] = ch;
} // while
printf("\n");
line[++ll] = ' ';
}
ch = line[++cc];
} // getch
//////////////////////////////////////////////////////////////////////
void retreat() // added by nanahka 17-12-20
{ // for LL(2) feature: lookahead another token and put it back afterwards
cc = cc_p;
getch();
sym = sym_p;
}
//////////////////////////////////////////////////////////////////////
// gets a symbol from input stream.
void getsym(void)
{
int i, k;
char a[MAXIDLEN + 1];
// added & modified by nanahka 17-09-21 as labwork_1
// an un-recursive method to eliminate repeated comments
////////////////////////////////////////////////
// two kinds of comments supported
// 1. line comment: //...
// 2. block comment: /*...*/
////////////////////////////////////////////////
do
{
while (ch == ' '||ch == '\t')
getch();
if (ch == '/')
{
getch();
if (ch == '/')
{
cc = ll = 0;
ch = ' ';
}
else if (ch == '*')
{
do
{
ch = ' ';
while (ch != '*')
getch();
getch();
}
while (ch != '/');
ch = ' ';
}
else
{
sym = SYM_SLASH;
return;
}
}
}
while (ch == ' ');
cc_p = cc - 1; // added by nanahka 17-12-20
sym_p = sym;
if (isalpha(ch) || ch == '_') // added by nanahka 17-12-20
{ // symbol is a reserved word or an identifier.
k = 0;
do
{
if (k < MAXIDLEN)
a[k++] = ch;
getch();
}
while (isalpha(ch) || isdigit(ch) || ch == '_');
a[k] = 0;
strcpy(id, a);
word[0] = id;
i = NRW;
while (strcmp(id, word[i--]));
if (++i)
sym = wsym[i]; // symbol is a reserved word
else
sym = SYM_IDENTIFIER; // symbol is an identifier
}
else if (isdigit(ch))
{ // symbol is a number.
k = num = 0;
sym = SYM_NUMBER;
do
{
num = num * 10 + ch - '0';
k++;
getch();
}
while (isdigit(ch));
if (k > MAXNUMLEN)
error(25); // The number is too great.
}
else if (ch == ':')
{
getch();
if (ch == '=')
{
sym = SYM_BECOMES; // :=
getch();
}
else
{
sym = SYM_COLON; //: // modified by lzp 17/12/19
}
}
else if (ch == '>')
{
getch();
if (ch == '=')
{
sym = SYM_GEQ; // >=
getch();
}
else
{
sym = SYM_GTR; // >
}
}
else if (ch == '<')
{
getch();
if (ch == '=')
{
sym = SYM_LEQ; // <=
getch();
}
else if (ch == '>')
{
sym = SYM_NEQ; // <>
getch();
}
else
{
sym = SYM_LES; // <
}
}
else if (ch == '&')
{
getch();
if (ch == '&')
{
sym = SYM_AND;
getch();
}
else
{
sym = SYM_AMPERSAND; // modified by nanahka 17-12-15
}
}
else if (ch == '|')
{
getch();
if (ch == '|')
{
sym = SYM_OR;
}
else
{
error(48); //missing '|'
}
getch();
}
else
{ // other tokens
i = NSYM;
csym[0] = ch;
while (csym[i--] != ch);
if (++i)
{
sym = ssym[i];
getch();
}
else
{
printf("Fatal Error: Unknown character.\n");
exit(1);
}
}
} // getsym
//////////////////////////////////////////////////////////////////////
//
// f l a
// INT -- numerical constant // allocate storage in stack
// LIT -- numerical constant // set a constant on top of the stack
// LOD levelDiff data address
// LODI levelDiff --(addr at top of the stack)
// LODS -- --(absolute addr at top of the stack)
// LEA levelDiff data address // put the address of an var on stack top
// STO levelDiff target address
// STOI levelDiff --(addr at top - 1 of the stack, data at top)
// STOS -- --(absolute addr at top - 1 of the stack, data at top)
// CAL levelDiff procedure address
// CALS levelDiff address of cx of the procedure stored in stack
// JMP -- procedure address
// JPC -- procedure address // if stack[top]==0, jump to an address
// JND -- procedure address // if stack[top]==0, jump without top--
// JNDN -- procedure address // if stack[top]!=0, jump without top--
// RET -- total words that the pointer top need to move down
// OPR -- type of algebraic/logical instructions
// PRNT -- total words at stack top to be printed(if 0, print('\n'))
//
//////////////////////////////////////////////////////////////////////
// generates (assembles) an instruction.
void gen(int x, int y, int z)
{
if (cx > CXMAX)
{
printf("Fatal Error: Program too long.\n");
exit(1);
}
code[cx].f = x;
code[cx].l = y;
code[cx++].a = z;
} // gen
//////////////////////////////////////////////////////////////////////
// tests if error occurs and skips all symbols that do not belongs to s1 or s2.
void test(symset s1, symset s2, int n)
{
symset s;
if (! inset(sym, s1))
{
error(n);
s = uniteset(s1, s2);
while(! inset(sym, s))
getsym();
destroyset(s);
}
} // test
//////////////////////////////////////////////////////////////////////
int dx; // data allocation index // accumulated offset in current AR
void free_ptr(comtyp *ptr) // modified by nanahka 17-12-17
{
if (ptr)
{
while ( (ptr->k)--)
{
free_ptr(ptr[ptr->k + 1].ptr);
}
if (ptr->ptr)
{
free(ptr->ptr);
}
free(ptr);
}
}
//////////////////////////////////////////////////////////////////////
//TABLE: (table[0] for error declaration, correct entries start from table[1])
// name kind level address ptr
// const id enter(kind) --------value(stored in TABLE)--------- NULL
// var id enter(kind) level dx NULL
// pointer id enter(kind) level dx NULL/same as array(subtype array_pointer)
// procedure id enter(kind) level cx(set by block(), line752) 1. ptr[0].k is the number of its parameters,
// and ptr[1..ptr[0].k] are there types.
// 2. If the procedure is a parameter, then
// ptr->ptr[0].k == PMT_PROC and it uses CALS
// rather than CAL.
// 3. ptr->ptr[1].k is the type of its
// return value.
// 4. ptr->ptr[2].k is the total storage its
// parameters occupied.
// array id enter(kind) level dx ~ dx + ptr[0] - 1 ptr[1..ptr[0].k] are volumes
// of the dimensions
//*: Since all the entries created within a procedure will be released after the
// declaration of the procedure, except for the name of the procedure itself, all
// the entries in the table at a special moment are available for the statement
// to be analyzed.
//
//SUBTYPE: array_pointer
// only used in procedure, pointing to the interior of the array parameter. Creating
// an array_pointer doesn't allocate storage in the stack.
//
//*: a type is a binary pair of (kind, ptr), thus ptr must be a pointer to such pairs,
// so that in the ptr of a procedure the types of its parameters can be stored.
//////////////////////////////////////////////////////////////////////
// enter object(constant, variable or procedure) into table.
void enter(int kind)
{
int position(char* id, int tx_beg);
if (kind == ID_PROCEDURE && position(id, TABLE_BEGIN) || // added by nanahka 17-11-20
kind != ID_PROCEDURE && position(id, tx_b))
{
error(31); // Redeclaration of an identifier.
return;
}
mask* mk;
tx++;
strcpy(table[tx].name, id);
table[tx].kind = kind;
free_ptr(table[tx].ptr); // added by nanahka 17-12-15
switch (kind)
{
case ID_CONSTANT:
if (num > MAXADDRESS)
{
error(25); // The number is too great.
num = 0;
}
table[tx].value = num;
break;
case ID_VARIABLE:
case ID_POINTER: // modified by nanahka 17-12-15
mk = (mask*) &table[tx];
mk->level = level;
mk->address = dx++;
mk->ptr = ptr; // if VARIABLE / POINTER TO VARIABLE, ptr == NULL
ptr = 0;
break;
case ID_PROCEDURE: // modified by nanahka 17-12-15
mk = (mask*) &table[tx];
mk->level = level;
break;
case ID_ARRAY: // modified by nanahka 17-12-15
mk = (mask*) &table[tx];
mk->level = level;
mk->address = dx;
int i = ptr[0].k, s = ptr[1].k;
while (--i) {s *= ptr[ptr[0].k - i + 1].k;}
dx += s;
mk->ptr = ptr;
ptr = 0;
break;
case ID_LABEL: //added by lzp 17/12/16
table[tx].value = cx; //label pointing to an ins
break;
} // switch
} // enter
//////////////////////////////////////////////////////////////////////
// locates identifier in symbol table.
// language feature: Check if there is already a same var/const name within
// current block, or a procedure name in the whole TABLE. If NOT, return 0.
// This feature is to prevent wrong input of ids within compiling process,
// and support recursive calling of ancestor procedures.
//////////////////////////////////////////////////////////////////////
int position(char* id, int tx_beg) // modified by nanahka 17-11-20
{
int i;
i = tx + 1;
if (tx_beg)
{
strcpy(table[0].name, table[tx_beg].name);
}
strcpy(table[tx_beg].name, id);
while (strcmp(table[--i].name, id) != 0);
if (tx_beg)
{
strcpy(table[tx_beg].name, table[0].name);
}
return tx_beg == i ? i - tx_beg : i;
} // position
//////////////////////////////////////////////////////////////////////
int expression(symset ssys, int CONST_CHECK);
int createarray(symset ssys) // added by nanahka 17-12-16
{
int con_expr;
symset set;
char id_t[MAXIDLEN + 1];
strcpy(id_t, id);
int dim[MAXARYDIM + 1] = {};
do
{
getsym();
set = expandset(ssys, SYM_RSQUARE, SYM_NULL);
con_expr = expression(set, CONST_EXPR);
destroyset(set);
if (con_expr <= 0 || con_expr > MAXARYVOL)
{
error(36); // Volume of a dimension is out of range.
break;
}
if ( (++dim[0]) > MAXARYDIM)
{
error(35); // There are too many dimensions.
--dim[0];
break;
}
dim[dim[0]] = con_expr;
if (sym != SYM_RSQUARE)
{
error(34); // ']' expected. // if ']' lost, go finding the next '['
}
else
{
getsym();
}
}
while (sym == SYM_LSQUARE);
if (dim[0]) // modified by nanahka 17-11-13
{
ptr = (comtyp*)malloc( (dim[0] + 1) * sizeof(comtyp));
ptr[0].k = dim[0];
ptr[0].ptr = NULL;
int i = dim[0] + 1;
while (--i) // modified by nanahka 17-12-19
{
ptr[i].k = dim[i];
ptr[i].ptr = NULL;
}
strcpy(id, id_t);
}
return dim[0] ? TRUE : FALSE;
}
//////////////////////////////////////////////////////////////////////
void constdeclaration(symset ssys)
{
if (sym == SYM_IDENTIFIER)
{
getsym();
if (sym == SYM_EQU || sym == SYM_BECOMES)
{
if (sym == SYM_BECOMES)
error(1); // Found ':=' when expecting '='.
getsym();
num = expression(ssys, CONST_EXPR);
ptr = NULL; // modified by nanahka 17-12-19
enter(ID_CONSTANT);
}
else
{
error(3); // There must be an '=' to follow the identifier.
}
}
else
{
error(4); // There must be an identifier to follow 'const', 'var', or 'procedure'.
}
} // constdeclaration
//////////////////////////////////////////////////////////////////////
void vardeclaration(symset ssys)
{
if (sym == SYM_IDENTIFIER) // added & modified by nanahka 17-11-14
{
getsym();
if (sym == SYM_LSQUARE)
{ // array declaration
if (createarray(ssys)) // modified by nanahka 17-12-16
{
enter(ID_ARRAY);
}
}
else
{ // variable declaration
ptr = NULL; // added by nanahka 17-12-15
enter(ID_VARIABLE);
}
}
else
{
error(4); // There must be an identifier to follow 'const', 'var', or 'procedure'.
}
} // vardeclaration
//////////////////////////////////////////////////////////////////////
void subprocdeclaration(symset ssys) // added by nanahka 17-12-16
{
symset set, set1;
if (sym == SYM_LPAREN)
{
getsym();
int n = 0;
comtyp pmt[MAXFUNPMT + 1] = {};
set = createset(SYM_COMMA, SYM_RPAREN, SYM_NULL); // modified by nanahka 17-12-18
set1 = uniteset_mul(ssys, set, pmt_first_sys, 0);
while (inset(sym, pmt_first_sys)) // modified by nanahka 17-11-21
{
if ( (++n) > MAXFUNPMT)
{
error(39); // Too many parameters in a procedure.
--n;
break;
}
if (sym == SYM_AMPERSAND)
{ // argument passing by address_variable
getsym();
if (sym != SYM_IDENTIFIER)
{
error(15); // There must be an identifier to follow '&'.
--n; // added by nanahka 17-12-15
deleteset(set1, SYM_IDENTIFIER, SYM_NULL); // if there's syms between '&' and id, this is not a correct
test(set, set1, 40); // Missing ',' or ')'. // declaration, and this id should be jumped off
setinsert(set1, SYM_IDENTIFIER);
}
else
{
pmt[n].k = ID_POINTER;
pmt[n].ptr = NULL;
getsym();
}
}
else if (sym == SYM_IDENTIFIER) // modified by nanahka 17-12-18
{ // argument passing by value / (address_array)
getsym();
if (sym == SYM_LSQUARE)
{ // array declaration
if (createarray(set1))
{
pmt[n].k = ID_POINTER;
pmt[n].ptr = ptr;
ptr = NULL;
}
}
else if (inset(sym, set))
{ // variable declaration
pmt[n].k = ID_VARIABLE;
pmt[n].ptr = NULL;
}
else if (sym == SYM_LPAREN) // modified by nanahka 17-12-18
{
error(61); // 'void' or 'int' needed before a procedure declared.
}
if (pmt[n].k == ID_VOID) // did not set the value of pmt[n]
{
--n;
}
}
else // sym == SYM_VOID / SYM_INT // added by nanahka 17-12-18
{ // sub-procedure declaration
int rt = sym == SYM_VOID ? ID_VOID : ID_VARIABLE;
getsym();
if (sym != SYM_IDENTIFIER)
{
error(62); // There must be an identifier to follow 'void' or 'int'.
}
else
{
getsym();
}
pmt[n].k = ID_PROCEDURE;
subprocdeclaration(set1);
ptr->ptr[1].k = rt;
pmt[n].ptr = ptr;
ptr = NULL;
}
// clear the leftovers
test(set, set1, 19); // Incorrect symbol.
if (sym == SYM_COMMA)
{
getsym();
// ensure that when this iteration ends, next sym is correct
test(pmt_first_sys, set1, 19); // Incorrect symbol.
}
else if (sym == SYM_RPAREN)
{
break;
}
} // while
destroyset(set); // modified by nanahka 17-12-18
destroyset(set1);
if (sym == SYM_RPAREN)
{
getsym();
}
ptr = (comtyp*)malloc( (n + 1) * sizeof(comtyp));
ptr->ptr = (comtyp*)malloc(3 * sizeof(comtyp));
ptr->ptr[0].k = PMT_PROC;
ptr->k = n;
int sum_alloc = 0; // added by nanahka 17-12-18
while (n--)
{
ptr[n + 1].k = pmt[n + 1].k;
ptr[n + 1].ptr = pmt[n + 1].ptr;
sum_alloc += pmt[n + 1].k == ID_PROCEDURE ? 2 : 1;
}
ptr->ptr[2].k = sum_alloc;
}
else // procedure without parameters // added by nanahka 17-12-18
{
ptr = (comtyp*)malloc(sizeof(comtyp)); // modified by nanahka 17-12-15
ptr->ptr = (comtyp*)malloc(3 * sizeof(comtyp));
ptr->ptr[0].k = PMT_PROC;
ptr->ptr[2].k = 0;
ptr[0].k = 0;
}
}
//////////////////////////////////////////////////////////////////////
void listcode(int from, int to)
{
int i;
printf("\n");
for (i = from; i < to; i++)
{
printf("%5d %s\t%d\t%d\n", i, mnemonic[code[i].f], code[i].l, code[i].a);
}
printf("\n");
} // listcode
//////////////////////////////////////////////////////////////////////
void backpatch(int *list)
{
int *p = list + 1;
while (list[0]--) {code[*p++].a = cx;}
free(list);
}
//////////////////////////////////////////////////////////////////////
void mergelist(int *dst, int *src)
{
int *p = src + 1;
while (src[0]--) {dst[++dst[0]] = *p++;}
free(src);
}
//////////////////////////////////////////////////////////////////////
int countindex(int i, symset ssys)
{
symset set;
ptr = table[i].ptr;
int d = ptr->k; // d <- dimensions of the array
set = expandset(ssys, SYM_RSQUARE, SYM_NULL);
expression(set, UNCONST_EXPR);
if (sym != SYM_RSQUARE)
{
error(34); // ']' expected.
}
else
{
getsym();
}
--d;
while (sym == SYM_LSQUARE && d)
{
getsym(); // take '['
gen(LIT, 0, ptr[ptr[0].k - d + 1].k);
gen(OPR, 0, OPR_MUL);
expression(set, UNCONST_EXPR);
gen(OPR, 0, OPR_ADD);
if (sym != SYM_RSQUARE)
{
error(34); // ']' expected.
}
else
{
getsym();
}
--d;
}
destroyset(set);
return d ? FALSE : TRUE;
}
//////////////////////////////////////////////////////////////////////
int getarrayaddr(int i, symset ssys)
{
mask *mk = (mask*)&table[i];
if (sym != SYM_LSQUARE)
{
error(38); // '[' expected
mk = 0;
}
else
{
getsym();
if (countindex(i, ssys)) // modified by nanahka 17-12-19
{ // number of subscripts read == array dimensions
//gen(LIT, 0, sizeof(int));
//gen(OPR, 0, OPR_MUL);
}
else
{ // number of subscripts read < array dimensions
error(29); // Too few subscripts.
mk = 0;
}
}
if (!mk)
{ // discard the leftover parts of the subscripts
test(ssys, ssys, 19); // Incorrect symbol.
}
else if (sym == SYM_LSQUARE) // added by nanahka 17-11-15
{
test(ssys, ssys, 30); // Too many subscripts.
}
return mk ? TRUE : FALSE;
}
//////////////////////////////////////////////////////////////////////
int isSameCompType(comtyp *ptr1, comtyp *ptr2)
{
if (ptr1 && ptr2)
{
if (ptr1[0].k != ptr2[0].k)
{
return FALSE;
}
else
{
comtyp *p1 = ptr1->ptr; // modified by nanahka 17-12-17
comtyp *p2 = ptr2->ptr;
if (p1 && p2)
{
if (p1[1].k != p2[1].k)
{
return FALSE; // not have the same retval type
}
}
else if (p1 || p2)
{
return FALSE;
}
int n = ptr1[0].k;
while (n--)
{
if (ptr1[n + 1].k != ptr2[n + 1].k ||
!isSameCompType(ptr1[n + 1].ptr, ptr2[n + 1].ptr))
{
return FALSE;
}
}
return TRUE;
}
}
else if (!ptr1 && !ptr2)
{
return TRUE;
}
else
{
return FALSE;
}
}
//////////////////////////////////////////////////////////////////////
// this function must be called after the id is confirmed as a procedure
int callprocedure(int i, symset ssys)
{
symset set, set1;
mask* mk = (mask*)&table[i];
comtyp *p = table[i].ptr;
int n = p->k;
if (p->ptr[1].k == ID_VARIABLE) // retval placed at base - p->ptr[2].k - 1
{ // retval type is var
gen(INT, 0, 1);
}
if (sym == SYM_LPAREN)
{
getsym();
if (n)
{
if (sym == SYM_RPAREN)
{
error(42); // Too few parameters in a procedure.
}
else
{
set1 = uniteset(ssys, exp_first_sys);
setinsert_mul(set1, SYM_COMMA, SYM_RPAREN, SYM_NULL);
test(exp_first_sys, set1, 24); // The symbol can not be as the beginning of an expression.
destroyset(set1);
}
}
while (inset(sym, exp_first_sys) && n--)
{
int kind = p[p->k - n].k;
comtyp *pmt_ptr = p[p->k - n].ptr;
set = createset(SYM_COMMA, SYM_RPAREN, SYM_NULL);
set1 = uniteset_mul(ssys, set, exp_first_sys, 0);
if (kind == ID_VARIABLE)
{
expression(set1, UNCONST_EXPR);
}
else if (kind == ID_POINTER)
{
if (sym == SYM_IDENTIFIER)
{
if ((i = position(id, TABLE_BEGIN)) == 0)
{
error(11); // Undeclared identifier.
getsym();
}
else
{
getsym();
mask *mk = (mask*)&table[i];
if (pmt_ptr)
{ // array_pointer
if (mk->kind == ID_ARRAY && isSameCompType(mk->ptr, pmt_ptr))
{
gen(LEA, level - mk->level, mk->address);
}
else if (mk->kind == ID_POINTER && isSameCompType(mk->ptr, pmt_ptr))
{
gen(LOD, level - mk->level, mk->address);
}
else
{
error(41); // Non-array type/incorrect indices.
}
}
else
{ // variable pointer
if (mk->kind == ID_VARIABLE || mk->kind == ID_ARRAY)
{
gen(LEA, level - mk->level, mk->address);
}
else if (mk->kind == ID_POINTER)
{
gen(LOD, level - mk->level, mk->address);
}
else
{
error(54); // Incorrect type as an lvalue expression.
}
if (mk->kind == ID_ARRAY || (mk->kind == ID_POINTER && mk->ptr))
{ // pointer <- array[i][j]...
getarrayaddr(i, set1);
gen(OPR, 0, OPR_ADD);
}
}
} // if
}
else
{
error(53); // The symbol can not be as the beginning of an lvalue expression.
test(set, set1, 19); // Incorrect symbol;
}
if (pmt_ptr && sym == SYM_LSQUARE)
{
error(69); // Assigning non-array type to an array.
test(set, set1, 19); // Incorrect symbol;
}
}
else if (kind == ID_PROCEDURE)
{
if (sym == SYM_IDENTIFIER)
{
if ((i = position(id, TABLE_BEGIN)) == 0)
{
error(11); // Undeclared identifier.
getsym();
}
else
{
getsym();
mask *mk = (mask*)&table[i];
if (mk->kind == ID_PROCEDURE && isSameCompType(mk->ptr, pmt_ptr))
{
if (mk->ptr->ptr[0].k == NON_PMT_PROC)
{
gen(LEA, level - mk->level, 0); // LIT the StaticLink
gen(LIT, 0, mk->address); // LIT cx
}
else // PMT_PROC
{
gen(LOD, level - mk->level, mk->address - 1); // LIT the StaticLink
gen(LOD, level - mk->level, mk->address); // LIT cx
}
}
else
{
error(56); // Non-procedure type/incorrect parameter types.
}
} // if
}
else
{
error(55); // The symbol can not be as the beginning of a function call.
test(set, set1, 19); // Incorrect symbol;
}
if (sym == SYM_LPAREN)
{
error(70); // Assigning non-function type to a function.
test(set, set1, 19); // Incorrect symbol;
}
} // if
destroyset(set);
destroyset(set1);
if (sym == SYM_COMMA)
{
getsym();
set1 = uniteset(ssys, exp_first_sys);
setinsert(set1, SYM_RPAREN);
test(exp_first_sys, set1, 24); // The symbol can not be as the beginning of an expression.
destroyset(set1);
}
else if (sym == SYM_RPAREN)
{
if (n)
{
error(42); // Too few parameters in a procedure.
}
break;
}
else
{
error(40); // Missing ',' or ')'.
}
} // while
if (inset(sym, exp_first_sys))
{
error(39); // Too many parameters in a procedure.
}
if (sym == SYM_RPAREN)
{
getsym();
}
} // if
if (!n)
{
if (p->ptr[0].k == PMT_PROC)
{
gen(CALS, level - mk->level, mk->address);
}
else
{ // NON_PMT_PROC
gen(CAL, level - mk->level, mk->address);
}
}
return n ? FALSE : TRUE;
}
//////////////////////////////////////////////////////////////////////
int assignment(int i, symset ssys)
{
symset set, set1;
int CORRECT_ASSIGN = TRUE;
set = createset(SYM_BECOMES, SYM_NULL); // modified by nanahka 17-12-16
set1 = uniteset(ssys, set);
if (table[i].kind == ID_CONSTANT)
{
error(12); // Illegal assignment.
CORRECT_ASSIGN = FALSE;
}
else if (table[i].kind == ID_VARIABLE)