-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzctoken.c
1774 lines (1597 loc) · 50 KB
/
zctoken.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
/* -*- Mode: C; Package: (CParse C); Tab-width: 5 -*-
* File: ZCTOKEN.C
*
* This code has been placed in the public domain.
*
* This file contains the hand-coded tokenizer.
* Design inspired by Jason T. Linhart.
*/
#include "ytab.h"
/* ---------------- Character type table ---------------- */
/* We use slightly different tables #include <ctype.h> */
#define _U 0001
#define _L 0002
#define _N 0004
#define _S 0010
#define _P 0020
#define _C 0040
#define _X 0100
#define _SYM 0200 /* _ and $ */
#define _O 0400 /* octal digits */
#define isalpha(c) ((ctype+1)[c]&(_U|_L))
#define isupper(c) ((ctype+1)[c]&_U)
#define islower(c) ((ctype+1)[c]&_L)
#define isdigit(c) ((ctype+1)[c]&_N)
#define isxdigit(c) ((ctype+1)[c]&(_N|_X))
#define isodigit(c) ((ctype+1)[c]&_O)
#define isspace(c) ((ctype+1)[c]&_S)
#define ispunct(c) ((ctype+1)[c]&_P)
#define isalnum(c) ((ctype+1)[c]&(_U|_L|_N))
#define issym(c) ((ctype+1)[c]&(_U|_L|_N|_SYM))
#define isprint(c) ((ctype+1)[c]&(_P|_U|_L|_N))
#define iscntrl(c) ((ctype+1)[c]&_C)
#define isascii(c) ((unsigned)(c)<=0177)
#define toascii(c) ((c)&0177)
#define _toupper(c) ((c)-'a'+'A')
#define _tolower(c) ((c)-'A'+'a')
unsigned short ctype[257] = {
/* -1 (EOF) */ 0,
/* 00 */ 0020, 0020, 0020, 0020,
/* 04 */ 0020, 0020, 0020, 0020,
/* 010 */ 0020, 0020, 0020, 0020,
/* 014 */ 0020, 0020, 0020, 0020,
/* 020 */ 0020, 0020, 0020, 0020,
/* 024 */ 0020, 0020, 0020, 0020,
/* 030 */ 0020, 0020, 0020, 0020,
/* 034 */ 0020, 0020, 0020, 0020,
/* ' ' */ 0010, 0020, 0020, 0020,
/* '$' */ 0220, 0020, 0020, 0020,
/* '(' */ 0020, 0020, 0020, 0020,
/* ',' */ 0020, 0020, 0020, 0020,
/* '0' */ 0404, 0404, 0404, 0404,
/* '4' */ 0404, 0404, 0404, 0404,
/* '8' */ 0004, 0004, 0020, 0020,
/* '<' */ 0020, 0020, 0020, 0020,
/* '@' */ 0020, 0101, 0101, 0101,
/* 'D' */ 0101, 0101, 0101, 0001,
/* 'H' */ 0001, 0001, 0001, 0001,
/* 'L' */ 0001, 0001, 0001, 0001,
/* 'P' */ 0001, 0001, 0001, 0001,
/* 'T' */ 0001, 0001, 0001, 0001,
/* 'X' */ 0001, 0001, 0001, 0020,
/* '\' */ 0020, 0020, 0020, 0220,
/* '`' */ 0020, 0102, 0102, 0102,
/* 'd' */ 0102, 0102, 0102, 0002,
/* 'h' */ 0002, 0002, 0002, 0002,
/* 'l' */ 0002, 0002, 0002, 0002,
/* 'p' */ 0002, 0002, 0002, 0002,
/* 't' */ 0002, 0002, 0002, 0002,
/* 'x' */ 0002, 0002, 0002, 0020,
/* '|' */ 0020, 0020, 0020, 0040,
/* 0200 */ 0040, 0040, 0040, 0040,
/* 0204 */ 0040, 0040, 0040, 0040,
/* 0210 */ 0010, 0010, 0010, 0040,
/* 0214 */ 0010, 0010, 0040, 0040,
/* 0220 */ 0040, 0040, 0040, 0040,
/* 0224 */ 0040, 0040, 0040, 0040,
/* 0230 */ 0040, 0040, 0040, 0040,
/* 0234 */ 0040, 0040, 0040, 0040,
/* identically 0040 from here on out */
/* 0240 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0250 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0260 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0270 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0300 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0310 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0320 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0330 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0340 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0350 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0360 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040,
/* 0370 */ 0040, 0040, 0040, 0040, 0040, 0040, 0040, 0040
};
/* Dispatch table values */
#define DALPHA 1 /* A-Z a-z _ $ */
#define DDIGIT 2 /* 0-9 */
#define DOPER 3 /* ~ ( ) $ : , ? [ ] { } ; */
#define DDOPER 4 /* ! # % & * + - < = > ^ | */
#define DQUOTE 5 /* " ' */
#define DWHITE 6 /* <Space> <Tab> <Return> <Page> */
#define DSLASH 7 /* / */
#define DDOT 8 /* . */
#define DATSIGN 9 /* @ */
#define DEOF 127
/* !"#$%&'()*+,-./0123456789:;<=>?
* @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
* `abcdefghijklmnopqrstuvwxyz{|}~
*/
/* Dispatch table: tells what kind of token this char may start */
char dispatch[256] = {
/* 00 */ DEOF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* !"#$%&' */ DWHITE, DDOPER, DQUOTE, DDOPER, DALPHA, DDOPER, DDOPER, DQUOTE,
/* ()*+,-./ */ DOPER, DOPER, DDOPER, DDOPER, DOPER, DDOPER, DDOT, DSLASH,
/* 01234567 */ DDIGIT, DDIGIT, DDIGIT, DDIGIT, DDIGIT, DDIGIT, DDIGIT, DDIGIT,
/* 89:;<=>? */ DDIGIT, DDIGIT, DOPER, DOPER, DDOPER, DDOPER, DDOPER, DOPER,
/* @ABCDEFG */ DATSIGN, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA,
/* HIJKLMNO */ DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA,
/* PQRSTUVW */ DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA,
/* XYZ[\]^_ */ DALPHA, DALPHA, DALPHA, DOPER, 0, DOPER, DDOPER, DALPHA,
/* `abcdefg */ 0, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA,
/* hijklmno */ DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA,
/* pqrstuvw */ DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA, DALPHA,
/* xyz{|}~‡ */ DALPHA, DALPHA, DALPHA, DOPER, DDOPER, DOPER, DOPER, 0,
/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, DWHITE, 0, 0, DWHITE, DWHITE, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/* Single-char table: values are lexemes (from ytab.h) */
unsigned short single_lex[128] = {
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* !"#$%&' */ 0, NOT, 0, MACTOKSTR, 0, DIVMOD, AND_ADDRESS, 0,
/* ()*+,-./ */ LPAREN, RPAREN, MUL_PTR, PLUSMINUS, COMMA, PLUSMINUS, ELEMENT, DIVMOD,
/* 01234567 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 89:;<=>? */ 0, 0, COLON, SEMI, COMPARISON, ASSIGN, COMPARISON, QMARK,
/* @ABCDEFG */ 0, 0, 0, 0, 0, 0, 0, 0,
/* HIJKLMNO */ 0, 0, 0, 0, 0, 0, 0, 0,
/* PQRSTUVW */ 0, 0, 0, 0, 0, 0, 0, 0,
/* XYZ[\]^_ */ 0, 0, 0, LBRACKET, 0, RBRACKET, BIT_XOR, 0,
/* `abcdefg */ 0, 0, 0, 0, 0, 0, 0, 0,
/* hijklmno */ 0, 0, 0, 0, 0, 0, 0, 0,
/* pqrstuvw */ 0, 0, 0, 0, 0, 0, 0, 0,
/* xyz{|}~ */ 0, 0, 0, LBRACE, BIT_OR, RBRACE, BIT_NOT, 0 };
/* Single-char yylvals */
lispval single_lval[128] = {
/* 00 */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* 08 */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* 10 */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* 18 */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* !"#$%&' */ @'(), @'c:!, @'(), @'(), @'(), @'c:%, @'c:&, @'(),
/* ()*+,-./ */ @'(), @'(), @'c:*, @'c:+, @'(), @'c:-, @'c:\., @'c:/,
/* 01234567 */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* 89:;<=>? */ @'(), @'(), @'(), @'(), @'c:<, @'c:=, @'c:>, @'(),
/* @ABCDEFG */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* HIJKLMNO */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* PQRSTUVW */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* XYZ[\]^_ */ @'(), @'(), @'(), @'(), @'(), @'(), @'c:^, @'(),
/* `abcdefg */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* hijklmno */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* pqrstuvw */ @'(), @'(), @'(), @'(), @'(), @'(), @'(), @'(),
/* xyz{|}~ */ @'(), @'(), @'(), @'(), @'c:\|, @'(), @'c:~, @'() };
/* Double-char table: values are lexemes (from ytab.h) */
unsigned short double_lex[128] = {
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* !"#$%&' */ 0, 0, 0, MACTOKCAT, 0, 0, AND, 0,
/* ()*+,-./ */ 0, 0, 0, INCREMENT, 0, INCREMENT, 0, 0,
/* 01234567 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 89:;<=>? */ 0, 0, 0, 0, SHIFT, EQUALITY, SHIFT, 0,
/* @ABCDEFG */ 0, 0, 0, 0, 0, 0, 0, 0,
/* HIJKLMNO */ 0, 0, 0, 0, 0, 0, 0, 0,
/* PQRSTUVW */ 0, 0, 0, 0, 0, 0, 0, 0,
/* XYZ[\]^_ */ 0, 0, 0, 0, 0, 0, 0, 0,
/* `abcdefg */ 0, 0, 0, 0, 0, 0, 0, 0,
/* hijklmno */ 0, 0, 0, 0, 0, 0, 0, 0,
/* pqrstuvw */ 0, 0, 0, 0, 0, 0, 0, 0,
/* xyz{|}~ */ 0, 0, 0, 0, OR, 0, 0, 0 };
/* Double-char yylvals */
lispval double_lval[128] = {
/* 00 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 08 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 10 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 18 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* !"#$%&' */ @(), @(), @(), @(), @(), @(), @'c:&&, @(),
/* ()*+,-./ */ @(), @(), @(), @'c:++, @(), @'c:--, @(), @(),
/* 01234567 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 89:;<=>? */ @(), @(), @(), @(), @'c:<<, @'c:==, @'c:>>, @(),
/* @ABCDEFG */ @(), @(), @(), @(), @(), @(), @(), @(),
/* HIJKLMNO */ @(), @(), @(), @(), @(), @(), @(), @(),
/* PQRSTUVW */ @(), @(), @(), @(), @(), @(), @(), @(),
/* XYZ[\]^_ */ @(), @(), @(), @(), @(), @(), @(), @(),
/* `abcdefg */ @(), @(), @(), @(), @(), @(), @(), @(),
/* hijklmno */ @(), @(), @(), @(), @(), @(), @(), @(),
/* pqrstuvw */ @(), @(), @(), @(), @(), @(), @(), @(),
/* xyz{|}~ */ @(), @(), @(), @(), @'c:\|\|, @(), @(), @() };
/* Assign table (char=): values are lexemes */
unsigned short assign_lex[128] = {
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* !"#$%&' */ 0, EQUALITY, 0, 0, 0, OP_ASSIGN, OP_ASSIGN, 0,
/* ()*+,-./ */ 0, 0, OP_ASSIGN, OP_ASSIGN, 0, OP_ASSIGN, 0, OP_ASSIGN,
/* 01234567 */ 0, 0, 0, 0, 0, 0, 0, 0,
/* 89:;<=>? */ 0, 0, 0, 0, COMPARISON, 0, COMPARISON, 0,
/* @ABCDEFG */ 0, 0, 0, 0, 0, 0, 0, 0,
/* HIJKLMNO */ 0, 0, 0, 0, 0, 0, 0, 0,
/* PQRSTUVW */ 0, 0, 0, 0, 0, 0, 0, 0,
/* XYZ[\]^_ */ 0, 0, 0, 0, 0, 0, OP_ASSIGN, 0,
/* `abcdefg */ 0, 0, 0, 0, 0, 0, 0, 0,
/* hijklmno */ 0, 0, 0, 0, 0, 0, 0, 0,
/* pqrstuvw */ 0, 0, 0, 0, 0, 0, 0, 0,
/* xyz{|}~ */ 0, 0, 0, 0, OP_ASSIGN, 0, 0, 0 };
/* Assign yylvals */
lispval assign_lval[128] = {
/* 00 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 08 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 10 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 18 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* !"#$%&' */ @(), @'c:!=, @(), @(), @(), @'c:%=, @'c:&=, @(),
/* ()*+,-./ */ @(), @(), @'c:*=, @'c:+=, @(), @'c:-=, @(), @'c:/=,
/* 01234567 */ @(), @(), @(), @(), @(), @(), @(), @(),
/* 89:;<=>? */ @(), @(), @(), @(), @'c:<=, @(), @'c:>=, @(),
/* @ABCDEFG */ @(), @(), @(), @(), @(), @(), @(), @(),
/* HIJKLMNO */ @(), @(), @(), @(), @(), @(), @(), @(),
/* PQRSTUVW */ @(), @(), @(), @(), @(), @(), @(), @(),
/* XYZ[\]^_ */ @(), @(), @(), @(), @(), @(), @'c:^=, @(),
/* `abcdefg */ @(), @(), @(), @(), @(), @(), @(), @(),
/* hijklmno */ @(), @(), @(), @(), @(), @(), @(), @(),
/* pqrstuvw */ @(), @(), @(), @(), @(), @(), @(), @(),
/* xyz{|}~ */ @(), @(), @(), @(), @'c:\|=, @(), @(), @() };
#define EOF 0 /* Note: not -1 */
#define NUL '\0'
#define TRUE ((char) 1)
#define FALSE ((char) 0)
#define NULL ((char *) 0)
typedef char FLAG;
#define ENDIF 16384
#define TOKBUFSIZE 4096
#define SAVBUFSIZE 6144
/* Easy access to Lisp primitives */
#define car(l) GL$CAR(l)
#define cdr(l) GL$CDR(l)
#define cadr(l) GL$CADR(l)
#define cdar(l) GL$CDAR(l)
#define caar(l) GL$CAAR(l)
#define cddr(l) GL$CDDR(l)
#define cadar(l) GL$CADAR(l)
#define caddr(l) GL$CADDR(l)
#define cdddr(l) GL$CDDDR(l)
#define cadddr(l) GL$CADDDR(l)
#define push(x,l) GL$PUSH(x,l)
#define pop(l) GL$POP(l)
#define cons(a,d) GL$CONS(a,d)
#define rplaca(l,x) GL$RPLACA(l,x)
#define rplacd(l,x) GL$RPLACD(l,x)
#define nreverse(l) GL$NREVERSE(l)
#define list GL$LIST
/* This struct holds all the internal variables of the lexer (for re-entrancy). */
struct lexerstate {
char curchar; /* current input character */
char *bufptr; /* where to get next character */
lispval yylval; /* token being returned */
lispval (*cpstream)(); /* c-parser stream we're reading from */
char *tokbuf; /* current token buffer */
char *savbuf, *savbufp; /* text-save buffer with end pointer */
char *lastbufptr; /* value of bufptr when text-saving started */
lispval ifstack; /* stack of active #if*s */
int ifstate; /* see below */
int macrostate; /* see below */
lispval macro, /* macro being expanded */
macparams, /* parameters thereof */
macargs; /* arguments thereto */
FLAG macargstr, /* is next macro arg to be stringified? */
mactokcat; /* is next macro token 2B concatenated? */
char *macexpstart; /* bufptr to beginning of macro exp */
};
/* Possible values for ifstate in the above: */
#define IFNONE 0 /* no #if in progress */
#define IFSTART 1 /* #if (or #elif) encountered */
#define IFPARSE 2 /* #if's expression being parsed */
#define IFDEFINED 3 /* in scope of 'defined' operator */
/* Possible values for macrostate in the above: */
#define MNONE 0 /* no macro processing going on */
#define MDEFINING 1 /* macro definition being read */
#define MARGGING 2 /* macro args (or params) being gathered */
#define MEXPANDING 3 /* macro being expanded */
/* Routines in this file (in order) (indented so as not to fire sectionizer) */
struct lexerstate *LNew();
void LReset();
lispval Lyyval();
char LTyi();
int LCurLine();
FLAG LCommentsNest();
int LToken();
FLAG LComment();
int LSymbol(), LSymbol1();
lispval LInt(), LFloat(), LString(), LChar();
int LPreProc();
void LDefine(), LUndef(), LIfdef(), LElse(), LEndif(), LInclude();
int LLisp();
lispval LCondP(), LCondCheck(), LGetSym();
void LSkipToEOL(), LMacArgs(), LMacExpand(), LMacroSymbol();
lispval LMacArg();
void LScanExpansion(), LSaveText();
char LNextLine();
void LSetBuf();
int LBufIndex();
/* The following are copied from ZCCLIB.C, to reduce compilation dependencies. */
static char *strcpy(), *memcpy();
static int strlen(), strcmp(), strncmp();
static lispval str2lisp();
static int toupper(), tolower();
/* Creates a new instance of the lexer and returns it. */
struct lexerstate *
LNew ()
{
struct lexerstate *lstate;
char *tokbuf;
int tokbufsize = TOKBUFSIZE; /* for Lisp inclusion */
lstate = (struct lexerstate *) calloc (1, sizeof (struct lexerstate));
lstate->tokbuf = tokbuf = (char *) calloc (TOKBUFSIZE, 1);
#lisp
(gl:setf (zeta-c:zcprim>array-freelist-link |tokbuf.array|)
(gl:make-array |tokbufsize| :type gl:art-string :fill-pointer 0
:displaced-to |tokbuf.array|
:displaced-index-offset 0))
#endlisp
lstate->savbuf = (char *) calloc (SAVBUFSIZE, 1);
/* Only the array part of the returned pointer is kept -- index must be 0! */
return lstate;
}
/* Returns a token in a form Lisp will like (i.e. as an ART-STRING).
* tokstart, tokend delimit the token; both must point into lstate->tokbuf
* as created by LNew. Note that only one of these can be in use at a time
* for any given instance of the lexer. */
lispval
LLispTok (tokstart, tokend)
char *tokstart, *tokend;
{
lispval lisptok;
int length = tokend - tokstart;
lisptok = @(zeta-c:zcprim>array-freelist-link |tokstart.array|);
#lisp
(gl:setf #+3600 (si:array-index-offset-field |lisptok|)
#-3600 (gl:%p-contents-offset |lisptok| 3)
|tokstart.index|)
(gl:setf (gl:fill-pointer |lisptok|) |length|)
#endlisp
return lisptok;
}
/* Initializes a lexer instance. */
LInit (lstate, cpstream)
struct lexerstate *lstate;
lispval (*cpstream)();
{
lstate->bufptr = lstate->savbufp = NULL;
lstate->cpstream = cpstream;
lstate->curchar = NUL;
lstate->ifstack = @GL:NIL;
lstate->ifstate = IFNONE;
lstate->macrostate = MNONE;
lstate->macro = @GL:NIL;
}
/* Resets the state of the lexer. This is used for interactive input,
* in which aborts and the like can leave the state inconsistent. */
void
LReset (lstate)
struct lexerstate *lstate;
{
lstate->curchar = NUL;
lstate->bufptr = lstate->savbufp = NULL;
lstate->ifstack = @GL:NIL;
lstate->ifstate = IFNONE;
lstate->macrostate = MNONE;
lstate->macro = @GL:NIL;
}
/* Returns lstate->yylval (to be called from outside). */
lispval
Lyylval (lstate)
struct lexerstate *lstate;
{
return lstate->yylval;
}
/* Macros for dealing with the input buffering scheme.
* They assume the following local declarations:
* struct lexerstate *lstate;
* char c, *cptr;
* char *token = lstate->tokbuf; (only if NEXT is called)
*/
/* LOADSTATE sets things up on function entry, reading the first char.
* lstate->bufptr == NULL case happens only before first char of a file is read.
* lstate->curchar == NUL case happens only after a HALFSKIP or at EOF. */
#define LOADSTATE ( c = (cptr = lstate->bufptr) ? ((c = lstate->curchar) ? c : LNextLine (lstate, &cptr)) : (cptr = "", '\n') )
/* SAVESTATE puts things back before returning. Be sure you call it on every
* path out of the function! Note, if you SAVESTATE but don't return, you don't
* have to LOADSTATE; just continue. */
#define SAVESTATE ( lstate->curchar = c, lstate->bufptr = cptr )
/* SKIP reads the next character into "c". */
#define SKIP ( (c = *cptr++) ? c : (c = LNextLine (lstate, &cptr)) )
/* HALFSKIP advances the input pointer without actually forcing the next char
* to be read. Use it only right before a SAVESTATE. */
#define HALFSKIP ( c = *cptr++ )
/* NEXT appends the current char to token, then reads the next char. */
#define NEXT ( *token++ = c, SKIP )
/* Reads a character from the input. Everything else in this file
* uses the macros above, but this is available for calling from
* outside. */
char
LTyi (lstate)
struct lexerstate *lstate;
{
char c, *cptr, t;
LOADSTATE;
t = c;
SKIP;
SAVESTATE;
return t;
}
/* "Unreads" a character. This is only to be called from outside. */
void
LUnTyi (lstate, unc)
struct lexerstate *lstate;
char unc;
{
char c, *cptr;
LOADSTATE;
--cptr;
c = unc;
/* if (c != cptr[-1])
LError (lstate->cpstream, 0,
@"Internal error: char being untyi-ed doesn't match buffer");*/
SAVESTATE;
}
/* What file are we reading from? (Returns a pathname object.) */
lispval
LCurFile (lstate)
struct lexerstate *lstate;
{
return lstate->cpstream (@:current-file);
}
/* What line are we reading on? */
int
LCurLine (lstate)
struct lexerstate *lstate;
{
return (int) (lstate->cpstream (@:current-line));
}
/* Do comments nest here? */
FLAG
LCommentsNest (lstate)
struct lexerstate *lstate;
{
return (lstate->cpstream (@:comments-nest-p) ? TRUE : FALSE);
}
/* Are we at the end of the current bufferful of input?
(Used in interactive mode only.) */
FLAG
LAtEnd (lstate)
struct lexerstate *lstate;
{
return lstate->curchar == NUL;
}
/* Reads the next token. Returns the lexeme for yyparse;
* sets lstate->yylval to the token object.
* `skipping', if TRUE, says that we're reading inside the scope of a failed
* #conditional; we have to tokenize but don't need the tokens.
*/
int
LToken (lstate)
struct lexerstate *lstate;
{
char c, *cptr, *token, t, ch, *tsavbufp, *tcptr;
int l, i, GL$LENGTH();
FLAG painted, floatp, hexp, shortp, longp, unsignedp;
lispval skipping; /* fast boolean */
lispval LFloat(), LInt(), LString(), LChar();
if (lstate->ifstate == IFSTART) { /* a #if or #elif encountered */
lstate->ifstate = IFPARSE;
return SHARPIF;
}
LOADSTATE;
for (;;) {
floatp = FALSE; /* slighly bogus indentation, sorry */
token = lstate->tokbuf;
switch (dispatch[c]) {
case DEOF: /* end of "file" (input source) */
if (lstate->macrostate != MNONE) {
SAVESTATE;
return ENDOFSTREAM;
}
/* If there are any active #if*s, warn user */
for (; lstate->ifstack && caddr(car(lstate->ifstack)) == LCurFile (lstate);
lstate->ifstack = cdr(lstate->ifstack))
LWarnNoCtx (@"Missing #endif to ~A on line ~A",
car(cdar(lstate->ifstack)),
cadr(cdar(lstate->ifstack)));
/* If there's no input left after popping, return. */
if (!lstate->cpstream (@:pop-input-source)) {
SAVESTATE;
return ENDOFSTREAM;
}
LOADSTATE; /* will load new stuff since source popped */
break;
case DWHITE:
if (c == '\n' && lstate->macrostate == MDEFINING) {
SAVESTATE;
return ENDOFSTREAM;
}
if (c == '\n' && lstate->ifstate == IFPARSE) {
SAVESTATE;
return SHARPIF;
}
t = c;
SKIP;
if (t == '\n') {
lstate->macexpstart = cptr - 1; /* previous line now lost */
if (c == '#') {
SKIP; SAVESTATE;
if (l = LPreProc (lstate)) return l; /* #lisp uses this */
skipping = !LCondP (lstate); /* cache */
LOADSTATE;
}
}
else {
SAVESTATE;
LSaveText (lstate);
tsavbufp = lstate->savbufp; /* condense grayspace */
while (c == ' ' || c == '\t') SKIP;
if (tsavbufp) {
if (lstate->macrostate == MEXPANDING && lstate->mactokcat)
--tsavbufp;
else tsavbufp[-1] = ' ';
lstate->savbufp = tsavbufp;
lstate->lastbufptr = cptr - 1;
}
}
break;
case DALPHA: /* identifier */
tcptr = cptr - 1; /* save place for macro-exp code below */
while (issym(NEXT));
*token = NUL;
if (c == '\5') { /* is token "painted"? */
SKIP;
painted = TRUE;
SAVESTATE;
LSaveText (lstate); /* delete paint from save buffer */
--lstate->savbufp; /* works even if null */
}
else painted = FALSE;
if (skipping) break;
SAVESTATE;
if (lstate->macrostate == MNONE) { /* unless processing macro ... */
if (l = LSymbol (lstate, token, painted)) return l;
/* else symbol is a macro name to be expanded */
lstate->macexpstart = tcptr;
LMacExpand (lstate);
LOADSTATE;
}
else if (lstate->macrostate == MDEFINING) {
LSymbol (lstate, token, TRUE); /* get name into yylval */
if (lstate->macro == lstate->yylval) /* if token is macro, */
*lstate->savbufp++ = '\5'; /* "paint" it */
}
else if (lstate->macrostate == MEXPANDING) {
LMacroSymbol (lstate);
LOADSTATE;
}
else return SYMBOL;
break;
case DDOPER:
/* These chars can be either doubled or followed by =, or maybe both */
t = c;
SKIP;
if (c == t && (l = double_lex[t])) { /* XX */
SKIP;
if (l == SHIFT && c == '=') { /* <<= or >>= */
SKIP;
if (skipping) break;
SAVESTATE;
lstate->yylval = (t == '<') ? @'c:<<= : @'c:>>= ;
return OP_ASSIGN;
}
else {
if (skipping) break;
SAVESTATE;
lstate->yylval = double_lval[t];
return l;
}
}
else if (c == '=' && (l = assign_lex[t])) { /* X= */
SKIP;
if (skipping) break;
SAVESTATE;
lstate->yylval = assign_lval[t];
return l;
}
else if (t == '-' && c == '>') { /* -> */
SKIP;
if (skipping) break;
SAVESTATE;
lstate->yylval = @'c:-> ;
return ELEMENT;
}
else { /* X */
if (skipping) break;
SAVESTATE;
lstate->yylval = single_lval[t];
return single_lex[t];
}
break;
case DOPER:
t = c;
if (skipping) {
SKIP;
break;
}
HALFSKIP; /* for C listener: don't force read of next char */
SAVESTATE; /* this one might have been ';' or '}' */
lstate->yylval = single_lval[t];
return single_lex[t];
case DSLASH:
SAVESTATE;
LSaveText (lstate);
tsavbufp = lstate->savbufp;
SKIP;
if (c != '*') {
if (c == '=') {
SKIP;
if (skipping) break;
SAVESTATE;
lstate->yylval = @'c:/= ;
return OP_ASSIGN;
}
if (skipping) break;
SAVESTATE;
lstate->yylval = @'c:/ ;
return DIVMOD;
}
SKIP;
SAVESTATE;
{ int nullp = LComment (lstate);
if (tsavbufp) { /* comment => #\Space */
*tsavbufp++ = ' ';
if (nullp && lstate->macrostate == MDEFINING) {
strcpy (tsavbufp, "## "); /* null comment was */
tsavbufp += 3; /* old-style token concat */
}
lstate->savbufp = tsavbufp;
lstate->lastbufptr = lstate->bufptr - 1;
}
}
LOADSTATE;
break;
case DDOT:
if (NEXT == '.') {
if (SKIP != '.') {
SAVESTATE;
LError (lstate->cpstream, -1, @"Illegal token `..'");
}
if (skipping) break;
SAVESTATE;
lstate->yylval = @'c:|...|;
return ELLIPSIS;
}
else if (!isdigit(c)) {
if (skipping) break;
SAVESTATE;
lstate->yylval = @'c:\.;
return ELEMENT;
}
floatp = TRUE; /* and fall thru */
case DDIGIT:
hexp = shortp = longp = unsignedp = FALSE;
if (!floatp && c == '0' && toupper(NEXT) == 'X') {
SKIP;
hexp = TRUE;
while (isxdigit(NEXT));
}
else while (isdigit(c)) NEXT;
if (!hexp && c == '.') {
floatp = TRUE;
while (isdigit(NEXT));
}
if (!hexp && toupper(c) == 'E') {
floatp = TRUE;
NEXT;
if (c == '+' || c == '-') NEXT;
while (isdigit(NEXT));
}
if (floatp) {
if (toupper(c) == 'F') { SKIP; shortp = TRUE; }
else if (toupper(c) == 'L') { SKIP; longp = TRUE; }
}
else for (;;) {
if (toupper(c) == 'U') { SKIP; unsignedp = TRUE; }
else if (toupper(c) == 'L') { SKIP; longp = TRUE; }
else break;
}
/* Only now do we actually do the conversions */
if (skipping) break;
SAVESTATE;
lstate->yylval = floatp
? LFloat (lstate->tokbuf, token, shortp, longp)
: LInt (lstate, lstate->tokbuf, token, hexp, unsignedp, longp);
return NUMBER;
case DQUOTE:
t = c;
SKIP;
while (c != t) {
if (c == '\\') {
SKIP;
if (isodigit(c)) {
for (i = 0, ch = 0; i < 3 && isodigit(c); ++i, SKIP)
ch = 8*ch + c - '0';
*token++ = ch;
}
else if (c == 'x') {
for (i = 0, ch = 0; i < 2 && isxdigit(c); ++i, SKIP)
ch = 16*ch + (c <= '9') ? c - '0'
: (c | 0x20) - 'a' + 10;
*token++ = ch;
}
else {
switch (c) {
case 'r': *token++ = '\r'; break;
case 'n': *token++ = '\n'; break;
case 't': *token++ = '\t'; break;
case 'b': *token++ = '\b'; break;
case 'f': *token++ = '\f'; break;
case 'v': *token++ = '\v'; break;
default: *token++ = c; break;
}
SKIP;
}
}
else if (c == '\n') {
SAVESTATE;
LError (lstate->cpstream, 0, @"Missing closing ~C", t);
break;
}
else NEXT;
}
if (c != '\n') SKIP;
if (skipping) break;
SAVESTATE;
if (t == '"') lstate->yylval = LString (lstate->tokbuf, token);
else lstate->yylval = LChar (lstate, lstate->tokbuf, token);
return t == '"' ? STRING : CHARCONST;
case DATSIGN: /* Lisp form inclusion */
SKIP;
SAVESTATE;
lstate->yylval = list(@'c:\#LISP, @'c:((|lispval|)),
lstate->cpstream (@:readlisp));
LOADSTATE;
if (!skipping) return SYMBOL;
break;
default:
SAVESTATE;
LError (lstate->cpstream, 0, @"Illegal character '~C'", c);
break;
}
}
}
/* Finds the end of a comment; returns TRUE if the comment was null.
* Expects the initial '/' and '*' to be already read.
*/
FLAG
LComment(lstate)
struct lexerstate *lstate;
{
char *cptr, oc;
int c, nchars, startline, commentsnest;
LOADSTATE;
startline = LCurLine (lstate);
commentsnest = LCommentsNest (lstate);
oc = NUL;
nchars = -1;
while (oc != '*' || c != '/') {
if (c == EOF) {
SAVESTATE;
LError (lstate->cpstream, 0, @"Input stream ended in the middle of a comment, which started on line ~D", startline);
}
if (commentsnest && oc == '/' && c == '*') {
SAVESTATE;
LComment(lstate);
LOADSTATE;
}
oc = c;
SKIP;
++nchars;
}
SKIP;
SAVESTATE;
return nchars == 0;
}
/* Converts a symbol. Returns its lexeme, or 0 for a macro reference. */
int
LSymbol (lstate, tokend, nomacro)
struct lexerstate *lstate;
char *tokend;
FLAG nomacro;
{
return (int) lstate->cpstream(@:process-symbol, lstate->ifstate, nomacro,
LLispTok (lstate->tokbuf, tokend));
}
/* Called by (:method c-parser :process-symbol) (called above).
* lexeme 0 means this symbol is a macro reference. */
int
LSymbol1 (lstate, lexeme, symbol, macparams, macdef)
struct lexerstate *lstate;
lispval symbol;
int lexeme;
optarg lispval macparams, macdef;
{
if (lexeme) lstate->yylval = symbol;
else {
lstate->macro = symbol;
lstate->macparams = macparams;
lstate->yylval = macdef;
}
if (lexeme == DEFINED) lstate->ifstate = IFDEFINED;
else if (lstate->ifstate == IFDEFINED) lstate->ifstate = IFPARSE;
return lexeme;
}
/* Converts an integer token; hexp, unsignedp, and longp tell if there was
* a leading "0x", a trailing "u", and/or a trailing "l"; these have been
* deleted from the token string. */
lispval
LInt (lstate, tokstart, tokend, hexp, unsignedp, longp)
struct lexerstate *lstate;
char *tokstart, *tokend;
FLAG hexp, unsignedp, longp;
{
int toklen = tokend - tokstart;
lispval type = unsignedp ? (longp ? @(zeta-c:zctype>unsigned-long)
: @(zeta-c:zctype>unsigned))
: (longp ? @(zeta-c:zctype>long) : @GL:NIL);
char *ttok;
lispval lisptok, value;
int base = hexp ? 16 : *tokstart == '0' ? 8 : 10;
if (base == 8) for (ttok = tokstart; ttok < tokend; ++ttok)
if (*ttok > '7')
LError (lstate->cpstream, ttok - tokend,
@"Digit '~C' not allowed in octal number.", *ttok);
lisptok = LLispTok (tokstart, tokend);
value = @(si:xr-read-fixnum-internal |lisptok| 0 (gl:string-length |lisptok|)
|base|);
return type ? @`(c:quote+ ,|value| ,|type|) : value;
}
/* Converts a floating-point token; shortp and longp tell if there was a
* trailing "f" or "l" (but in fact "l" is ignored, as ZETA-C does not have
* a separate long double type); these have been deleted from the token string. */
lispval
LFloat (tokstart, tokend, shortp, longp)
char *tokstart, *tokend;
FLAG shortp, longp;
{
lispval lisptok = LLispTok (tokstart, tokend);
GL$IGNORE(longp); /* long double = double */
if (shortp) return @(si:xr-read-flonum |lisptok| #+3600 :single-float
#-3600 :small-flonum);
else return @(si:xr-read-flonum |lisptok| #+3600 :double-float
#-3600 gl:nil);
}
/* Converts a string. The quotes have been deleted, and escape sequences
* processed. */
lispval
LString (tokstart, tokend)
char *tokstart, *tokend;
{
int toklen = tokend - tokstart, i, c;
lispval string;
string = @(gl:make-array |toklen| :type gl:art-string);
for (i = 0; i < toklen; ++i) {
c = tokstart[i];
@(gl:aset (zeta-c:code-char |c|) |string| |i|);
}
return @`(c:string+ ,|string|);
}
/* Converts a character constant. The single quotes have been deleted, and escape
* sequences processed. ZETA-C supports multicharacter constants ala 4.2bsd: the
* first character goes in the high-order position (backwards from a string).
* There is no length limit (!). */
lispval
LChar (lstate, tokstart, tokend)
struct lexerstate *lstate;
char *tokstart, *tokend;
{
int toklen = tokend - tokstart;
int value = 0, i;
lispval type = toklen < 2 ? @(zeta-c:zctype>char)
: toklen == 2 ? @(zeta-c:zctype>unsigned-short)
: toklen <= 4
? @(zeta-c:zctype>unsigned)
: @(zeta-c:zctype>unsigned-long);
if (toklen < 1)
LError (lstate->cpstream, 0,
@"Char constant must have at least one char");
/* 4bsd-style multicharacter constants. These are "backwards" relative
* to strings: first character goes in high-order spot. */
for (i = 0; i < toklen; ++i) value = (value << 8) + tokstart[i];
return @`(c:quote+ ,|value| ,|type|);
}
/* Used often by preprocessor routines */
#define SKIPGRAY while (c == ' ' || c == '\t') SKIP; /* skip grayspace */
#define ENDCHECK { if (c == '\n') { SAVESTATE; LEOLErr(lstate); } else if (c == 0) { SAVESTATE; LEOFErr(lstate); } }
LEOLErr (lstate)
struct lexerstate *lstate;
{
LError (lstate->cpstream, 0, @"Unexpected end of line");
}