-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanutil.c
754 lines (685 loc) · 15 KB
/
scanutil.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
/*
module : scanutil.c
version : 1.10
date : 07/12/24
*/
/* File: Included file for scan utilities */
#define maxincludelevel 5
#define maxlinelength 132
#define linenumwidth 4
#define linenumspace " "
#define linenumsep " "
#define underliner "**** "
#define maxoutlinelength 60
#define messagelength 30
#define initial_alternative_radix 2
#if 0
#define maxchartab 1000
#define maxstringtab 100
#endif
typedef char identalfa[identlength + 1];
typedef char resalfa[reslength + 1];
#if 0
typedef char message[messagelength + 1];
typedef struct toops {
long symbols, types, strings, chars;
} toops;
#endif
typedef struct _REC_inputs {
FILE *fil;
identalfa nam;
long lastlinenumber;
} _REC_inputs;
typedef struct _REC_reswords {
resalfa alf;
symbol symb;
} _REC_reswords;
typedef struct _REC_stdidents {
identalfa alf;
standardident symb;
} _REC_stdidents;
static FILE *listing;
static _REC_inputs inputs[maxincludelevel];
static long includelevel, adjustment, writelisting;
static bool must_repeat_line;
static long scantimevariables['Z' + 1 - 'A'];
static long alternative_radix, linenumber;
static char line[maxlinelength + 1];
static int cc, ll;
static int chr;
static identalfa ident;
#if defined(MINPAS) || defined(MINJOY)
static standardident id;
#endif
#if 0
static resalfa res;
#endif
static char specials_repeat[maxrestab + 1];
static symbol sym;
static long num;
static _REC_reswords reswords[maxrestab + 1];
static int lastresword;
#if defined(MINPAS) || defined(MINJOY)
static _REC_stdidents stdidents[maxstdidenttab + 1];
#endif
static int laststdident;
#ifdef NETVER
static long trace = 1;
#endif
#if 0
static char resword_inverse[37];
static long stringtab[maxstringtab + 1];
static char chartab[maxchartab + 1];
static toops toop;
#endif
static long errorcount, outlinelength, statistics;
static clock_t start_clock, end_clock;
/* - - - - - MODULE ERROR - - - - - */
static void point_to_symbol(bool repeatline, FILE *f, char diag, char *mes)
{
char c;
int i, j;
if (repeatline) {
fprintf(f, "%*ld%s", linenumwidth, linenumber, linenumsep);
for (i = 0; i < ll; i++)
putc(line[i], f);
putc('\n', f);
}
fputs(underliner, f);
for (i = 0, j = cc - 2; i < j; i++)
if ((c = line[i]) < ' ')
putc(c, f);
else
putc(' ', f);
fprintf(f, "^\n");
fprintf(f, "%s-%c %-*.*s\n", errormark, diag, messagelength,
messagelength, mes);
if (diag == 'F')
fprintf(f, "execution aborted\n");
} /* point_to_symbol */
static void point(char diag, char *mes)
{
if (diag != 'I')
errorcount++;
if (includelevel > 0)
printf("INCLUDE file : \"%-*.*s\"\n", identlength, identlength,
inputs[includelevel - 1].nam);
point_to_symbol(true, stdout, diag, mes);
if (writelisting > 0) {
point_to_symbol(must_repeat_line, listing, diag, mes);
must_repeat_line = true;
}
if (diag == 'F')
exit(0);
} /* point */
/* - - - - - MODULE SCANNER - - - - - */
static void closelisting(void)
{
if (listing)
fclose(listing);
listing = 0;
}
/*
iniscanner - initialize global variables
*/
static void iniscanner(void)
{
start_clock = clock();
if ((listing = fopen(list_filename, "w")) == NULL) {
fprintf(stderr, "%s (not open for writing)\n", list_filename);
exit(0);
}
atexit(closelisting);
writelisting = 0;
chr = ' ';
linenumber = 0;
cc = 1;
ll = 1; /* to enable fatal message during initialisation */
memset(specials_repeat, 0, sizeof(specials_repeat)); /* def: no repeats */
includelevel = 0;
#if 0
/*
* Initial input file is stdin. This is not stored in the inputs table.
*/
inputs[0].fil = stdin;
strcpy(inputs[0].nam, "stdin");
inputs[0].lastlinenumber = 1;
#endif
adjustment = 0;
alternative_radix = initial_alternative_radix;
lastresword = 0;
laststdident = 0;
outlinelength = 0;
memset(scantimevariables, 0, sizeof(scantimevariables));
errorcount = 0;
must_repeat_line = false;
} /* iniscanner */
static void erw(char *str, symbol symb)
{
if (++lastresword > maxrestab)
point('F', "too many reserved words");
strncpy(reswords[lastresword].alf, str, reslength);
reswords[lastresword].alf[reslength] = 0;
reswords[lastresword].symb = symb;
} /* erw */
#if defined(MINPAS) || defined(MINJOY)
static void est(char *str, standardident symb)
{
if (++laststdident > maxstdidenttab)
point('F', "too many identifiers");
strncpy(stdidents[laststdident].alf, str, identlength);
stdidents[laststdident].alf[identlength] = 0;
stdidents[laststdident].symb = symb;
} /* est */
#endif
static void release(void)
{
int i;
for (i = 0; i < maxincludelevel; i++)
if (inputs[i].fil) {
fclose(inputs[i].fil);
inputs[i].fil = 0;
}
}
static void newfile(char *str, int flag)
{
static unsigned char init;
FILE *fp;
char *ptr;
if (!init) {
init = 1;
atexit(release);
}
if (!flag) {
if (!freopen(str, "r", stdin)) {
fprintf(stderr, "%s (not open for reading)\n", str);
exit(0);
}
adjustment = 0;
return;
}
/*
* str may contain a pathname. The pathname must be stripped, because
* identlength is not large enough to hold a pathname. The name is only
* used in error messages and does not need a pathname.
*/
if ((ptr = strrchr(str, '/')) != 0)
ptr++;
else
ptr = str;
strncpy(inputs[includelevel].nam, ptr, identlength);
inputs[includelevel].lastlinenumber = linenumber;
if ((fp = fopen(str, "r")) == NULL) {
/*
* If the include file does not contain a pathname yet, the pathname
* is prepended and the open is tried again. If that also fails, the
* program fails. Pathname itself is a global variable.
*/
if (ptr == str) {
flag = strlen(pathname) + strlen(str) + 1;
ptr = malloc(flag);
sprintf(ptr, "%s%s", pathname, str);
fp = fopen(ptr, "r");
free(ptr);
}
if (!fp) {
fprintf(stderr, "%s (not open for reading)\n", str);
exit(0);
}
}
inputs[includelevel].fil = fp;
adjustment = 1;
} /* newfile */
static void getsym(void);
#define dirlength 16
static void perhapslisting(void)
{
int i;
if (writelisting > 0) {
fprintf(listing, "%*ld%s", linenumwidth, linenumber, linenumsep);
for (i = 0; i < ll; i++)
putc(line[i], listing);
putc('\n', listing);
must_repeat_line = false;
fflush(listing);
}
}
static void getch(void)
{
FILE *f;
char *ptr;
if (cc == ll) {
if (adjustment != 0) {
if (adjustment == -1)
linenumber = inputs[includelevel - 1].lastlinenumber;
else
linenumber = 0;
includelevel += adjustment;
adjustment = 0;
}
linenumber++;
ll = 0;
cc = 0;
if (!includelevel) {
if (fgets(line, maxlinelength, stdin)) {
if ((ptr = strchr(line, '\n')) != 0)
*ptr = 0;
ll = strlen(line);
perhapslisting();
} else
exit(0);
} else {
f = inputs[includelevel - 1].fil;
if (fgets(line, maxlinelength, f)) {
if ((ptr = strchr(line, '\n')) != 0)
*ptr = 0;
ll = strlen(line);
perhapslisting();
} else {
fclose(f);
inputs[includelevel - 1].fil = NULL;
adjustment = -1;
}
}
line[ll++] = ' ';
} /* IF */
chr = line[cc++];
} /* getch */
static long value(void)
{
/* this is a LL(0) parser */
long result = 0;
do
getch();
while (chr <= ' ');
if (chr == '\'' || chr == '&' || isdigit(chr)) {
getsym();
result = num;
goto einde;
}
if (isupper(chr)) {
result = scantimevariables[chr - 'A'];
goto einde;
}
if (chr == '(') {
result = value();
while (chr <= ' ')
getch();
if (chr == ')')
getch();
else
point('E', "right parenthesis expected");
goto einde;
}
switch (chr) {
case '+':
result = value() + value();
break;
case '-':
result = value();
result -= value();
break;
case '*':
result = value() * value();
break;
case '/':
result = value();
result /= value();
break;
case '=':
result = value() == value();
break;
case '>':
result = value();
result = result > value();
break;
case '<':
result = value();
result = result < value();
break;
#if 0
case '?':
if (scanf("%ld", &result) != 1)
result = 0;
break;
#endif
default:
point('F', "illegal start of scan expr");
} /* CASE */
einde:
return result;
} /* value */
static void directive(void)
{
int i;
char c, dir[dirlength + 1];
getch();
i = 0;
do {
if (i < dirlength)
dir[i++] = chr;
getch();
} while (chr == '_' || isupper(chr));
dir[i] = 0;
if (!strcmp(dir, "IF")) {
if (value() < 1)
cc = ll; /* readln */
} else if (!strcmp(dir, "INCLUDE")) {
if (includelevel == maxincludelevel)
point('F', "too many include files");
while (chr <= ' ')
getch();
i = 0;
do {
if (i < identlength)
ident[i++] = chr;
getch();
} while (chr > ' ');
ident[i] = 0;
newfile(ident, 1);
} else if (!strcmp(dir, "PUT")) {
fflush(stdout);
for (i = cc - 1; i < ll; i++)
fputc(line[i], stderr);
fputc('\n', stderr);
cc = ll;
} else if (!strcmp(dir, "SET")) {
while (chr <= ' ')
getch();
if (!isupper(chr))
point('E', "\"A\" .. \"Z\" expected");
c = chr;
getch();
while (chr <= ' ')
getch();
if (chr != '=')
point('E', "\"=\" expected");
scantimevariables[c - 'A'] = value();
#if 0
} else if (!strcmp(dir, "TRACE")) {
trace = value();
#endif
} else if (!strcmp(dir, "LISTING")) {
i = writelisting;
writelisting = value();
if (!i)
perhapslisting();
} else if (!strcmp(dir, "STATISTICS"))
statistics = value();
else if (!strcmp(dir, "RADIX"))
alternative_radix = value();
else
point('F', "unknown directive");
getch();
} /* directive */
static void getsym(void)
{
#if 0
char c;
#endif
bool negated;
int i, j, k, index;
begin:
ident[index = 0] = 0; /* start with empty ident and index at 0 */
while (chr <= ' ')
getch();
switch (chr) {
case '\'':
getch();
if (chr == '\\')
num = value();
else {
num = chr;
getch();
}
#ifndef MINJOY
if (chr == '\'') /* no optional closing quote */
getch();
#endif
sym = charconst;
break;
#if 0
case '"':
if (toop.strings == maxstringtab)
point('F', "too many strings");
stringtab[num = ++toop.strings] = toop.chars + 1;
getch();
while (ch != '"') {
if ((c = ch) == '\\')
c = value();
else
getch();
if (++toop.chars > maxchartab)
point('F', "too many characters in strings");
chartab[toop.chars] = c;
} /* WHILE */
getch();
stringtab[num + 1] = toop.chars;
sym = stringconst;
/* FOR i := stringtab[num] TO stringtab[num+1] DO
write(chartab[i]) */
break;
#endif
case '(':
getch();
if (chr == '*') {
getch();
do {
while (chr != '*')
getch();
getch();
} while (chr != ')');
getch();
goto begin;
}
sym = leftparenthesis;
break;
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (chr != '-')
negated = false;
else {
getch();
if (!isdigit(chr)) {
ident[index++] = '-';
ident[index] = 0;
goto einde;
}
negated = true;
}
sym = numberconst;
num = 0;
do {
num = num * 10 + chr - '0';
getch();
} while (isdigit(chr));
if (negated)
num = -num;
break;
case '&': /* number in alternative radix */
sym = numberconst;
num = 0;
getch();
while (isdigit(chr) || isupper(chr)) {
if (isupper(chr))
chr += '9' - 'A' + 1;
if (chr >= alternative_radix + '0')
point('E', "exceeding alternative radix");
num = alternative_radix * num + chr - '0';
getch();
}
break;
case '%':
directive();
goto begin;
default:
if (chr == '_' || isalnum(chr))
goto einde;
if (chr > ' ') {
/*
* A special character has been read that may be a reserved word
* or it may be the start of an identifier. The table of reserved
* words is searched after reading each character.
*/
again:
if (index < identlength)
ident[index++] = chr;
getch();
ident[index] = 0;
i = 1;
j = lastresword;
do {
k = (i + j) / 2;
if (strcmp(ident, reswords[k].alf) <= 0)
j = k - 1;
if (strcmp(ident, reswords[k].alf) >= 0)
i = k + 1;
} while (i <= j);
if (i - 1 > j)
sym = reswords[k].symb;
else {
/*
* A reserved word was not recognized, but a special character
* was read. In that case, add the character to the word and
* try again to recognize a reserved word.
*/
if (strchr(specials_repeat, chr))
goto again;
/*
* No second special character was read. It is still possible
* to build an identifier that starts with one special char.
*/
einde:
if (chr == '_' || isalnum(chr)) {
do {
if (index < identlength)
ident[index++] = chr;
getch();
} while (chr == '_' || isalnum(chr));
}
ident[index] = 0;
sym = identifier;
}
}
break;
} /* CASE */
} /* getsym */
#ifndef MINJOY
static void check(symbol sy, symset ss, char *er)
{
if (sym == sy)
getsym();
else {
point('E', er);
if (P_inset(sym, ss))
getsym();
}
} /* check */
#if defined(MINPAS) || defined(DATBAS)
static void test(symset s1, symset s2, char *er)
{
symset set;
if (!P_inset(sym, s1)) {
point('E', er);
P_setunion(set, s1, s2);
if (!P_inset(sym, set)) {
do
getsym();
while (!P_inset(sym, set));
point('I', "skipped symbols to here");
}
}
} /* test */
#endif
#endif
/* - - - - - MODULE OUTPUT - - - - - */
#ifndef MINPAS
static void putch(int c)
{
putchar(c);
if (writelisting > 0) {
if (!outlinelength)
fprintf(listing, "%s%s", linenumspace, linenumsep);
putc(c, listing);
}
if (c == '\n')
outlinelength = 0;
else
outlinelength++;
}
static void writeline(void)
{
putchar('\n');
if (writelisting > 0)
putc('\n', listing);
outlinelength = 0;
}
static void writeident(char *str)
{
int i, length;
length = strlen(str);
if (outlinelength + length > maxoutlinelength)
writeline();
for (i = 0; i < length; i++)
putch(str[i]);
}
#ifdef DATBAS
static void writeresword(char *str)
{
int i, length;
for (length = reslength; length && str[length] <= ' '; length--)
;
if (outlinelength + length > maxoutlinelength)
writeline();
for (i = 0; i < length; i++)
putch(str[i]);
}
#endif
static void writenatural(intptr_t n)
{
if (n >= 10)
writenatural(n / 10);
putch(n % 10 + '0');
}
static void writeinteger(intptr_t i)
{
if (outlinelength + 12 > maxoutlinelength)
writeline();
if (i >= 0)
writenatural(i);
else {
putch('-');
writenatural(-i);
}
} /* writeinteger */
#endif
static void fin(FILE *f)
{
double lib;
if (errorcount > 0)
fprintf(f, "%ld error(s)\n", errorcount);
lib = end_clock = clock();
if ((lib -= start_clock) > 0) {
lib = lib * 1000 / CLOCKS_PER_SEC;
fprintf(f, "%.0f milliseconds CPU\n", lib);
}
}
static void finalise(void)
{
/* finalise */
fflush(stdout);
fin(stderr);
if (listing && writelisting > 0)
fin(listing);
/* finalise */
}
/* End Included file for scan utilities */