-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.c
2295 lines (2094 loc) · 63.3 KB
/
interp.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
/*
module : interp.c
version : 1.1.1.2
date : 10/02/23
*/
/* FILE: interp.c */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "globals.h"
/* #define TRACING */
PUBLIC void printnode(); /* file utils.c */
PRIVATE void helpdetail_(); /* this file */
PRIVATE void make_manual(int latex);
PUBLIC char * opername();
#define ONEPARAM(NAME) \
if (stk == NULL) \
execerror("one parameter",NAME)
#define TWOPARAMS(NAME) \
if (stk == NULL || stk->next == NULL) \
execerror("two parameters",NAME)
#define THREEPARAMS(NAME) \
if (stk == NULL || stk->next == NULL \
|| stk->next->next == NULL) \
execerror("three parameters",NAME)
#define FOURPARAMS(NAME) \
if (stk == NULL || stk->next == NULL \
|| stk->next->next == NULL \
|| stk->next->next->next == NULL) \
execerror("four parameters",NAME)
#define FIVEPARAMS(NAME) \
if (stk == NULL || stk->next == NULL \
|| stk->next->next == NULL \
|| stk->next->next->next == NULL \
|| stk->next->next->next->next == NULL) \
execerror("five parameters",NAME)
#define ONEQUOTE(NAME) \
if (stk->op != LIST_) \
execerror("quotation as top parameter",NAME)
#define TWOQUOTES(NAME) \
ONEQUOTE(NAME); \
if (stk->next->op != LIST_) \
execerror("quotation as second parameter",NAME)
#define THREEQUOTES(NAME) \
TWOQUOTES(NAME); \
if (stk->next->next->op != LIST_) \
execerror("quotation as third parameter",NAME)
#define FOURQUOTES(NAME) \
THREEQUOTES(NAME); \
if (stk->next->next->next->op != LIST_) \
execerror("quotation as fourth parameter",NAME)
#define FIVEQUOTES(NAME) \
FOURQUOTES(NAME); \
if (stk->next->next->next->next->op != LIST_) \
execerror("quotation as fifth parameter",NAME)
#define SAME2TYPES(NAME) \
if (stk->op != stk->next->op) \
execerror("two parameters of the same type",NAME)
#define STRING(NAME) \
if (stk->op != STRING_) \
execerror("string",NAME)
#define INTEGER(NAME) \
if (stk->op != INTEGER_) \
execerror("integer",NAME)
#define INTEGERS2(NAME) \
if (stk->op != INTEGER_ || stk->next->op != INTEGER_) \
execerror("two integers",NAME)
#define NUMERICTYPE(NAME) \
if (stk->op != INTEGER_ && stk->op != CHAR_) \
execerror("numeric",NAME)
#define NUMERIC2(NAME) \
if (stk->next->op != INTEGER_ && stk->next->op != CHAR_) \
execerror("numeric second parameter",NAME)
#define CHECKZERO(NAME) \
if (stk->u.num == 0) \
execerror("non-zero operand",NAME)
#define LIST(NAME) \
if (stk->op != LIST_) \
execerror("list",NAME)
#define LIST2(NAME) \
if (stk->next->op != LIST_) \
execerror("list as second parameter",NAME)
#define USERDEF(NAME) \
if (stk->op != USR_) \
execerror("user defined symbol",NAME)
#define CHECKLIST(OPR,NAME) \
if (OPR != LIST_) \
execerror("internal list",NAME)
#define CHECKSETMEMBER(NODE,NAME) \
if ((NODE->op != INTEGER_ && NODE->op != CHAR_) || \
NODE->u.num >= SETSIZE) \
execerror("small numeric",NAME)
#define CHECKEMPTYSET(SET,NAME) \
if (SET == 0) \
execerror("non-empty set",NAME)
#define CHECKEMPTYSTRING(STRING,NAME) \
if (*STRING == '\0') \
execerror("non-empty string",NAME)
#define CHECKEMPTYLIST(LIST,NAME) \
if (LIST == NULL) \
execerror("non-empty list",NAME)
#define INDEXTOOLARGE(NAME) \
execerror("smaller index",NAME)
#define BADAGGREGATE(NAME) \
execerror("aggregate parameter",NAME)
#define BADDATA(NAME) \
do { execerror("different type",NAME); break; } while (0)
#define DMP dump->u.lis
#define DMP1 dump1->u.lis
#define DMP2 dump2->u.lis
#define DMP3 dump3->u.lis
#define DMP4 dump4->u.lis
#define DMP5 dump5->u.lis
#define SAVESTACK dump = newnode(LIST_,stk,dump)
#define SAVED1 DMP
#define SAVED2 DMP->next
#define SAVED3 DMP->next->next
#define SAVED4 DMP->next->next->next
#define SAVED5 DMP->next->next->next->next
#define SAVED6 DMP->next->next->next->next->next
#define POP(X) X = X->next
#define NULLARY(TYPE,VALUE) \
stk = newnode(TYPE,VALUE,stk)
#define UNARY(TYPE,VALUE) \
stk = newnode(TYPE,VALUE,stk->next)
#define BINARY(TYPE,VALUE) \
stk = newnode(TYPE,VALUE,stk->next->next)
#define TERNARY(TYPE,VALUE) \
stk = newnode(TYPE,VALUE,stk->next->next->next)
#define GETSTRING(NODE) \
( NODE->op == STRING_ ? NODE->u.str : \
(NODE->op == USR_ ? NODE->u.ent->name : \
opername(NODE->op) ) )
/* PUBLIC int clock(); */ /* file time.h */
PUBLIC Node *newnode(); /* file utils.c */
PUBLIC void memoryindex_();
PUBLIC void execerror(); /* file main.c */
PUBLIC void abortexecution_();
PUBLIC void getsym(); /* file scan.c */
PUBLIC void doinclude();
PUBLIC void readfactor(); /* file utils.c */
PUBLIC void writefactor();
PUBLIC void writeterm();
PUBLIC void quit_();
PUBLIC void gc_();
/* PUBLIC int malloc(); */ /* in the library */
/* PUBLIC void system(); */
/* - - - - O P E R A N D S - - - - */
#define PUSH(PROCEDURE,TYPE,VALUE) \
PRIVATE void PROCEDURE() \
{ NULLARY(TYPE,VALUE); }
PUSH(true_,BOOLEAN_,1) /* constants */
PUSH(false_,BOOLEAN_,0)
PUSH(setsize_,INTEGER_,SETSIZE)
PUSH(maxint_,INTEGER_,MAXINT)
PUSH(symtabmax_,INTEGER_,SYMTABMAX)
PUSH(memorymax_,INTEGER_,MEMORYMAX)
PUSH(stack_,LIST_,stk) /* variables */
PUSH(dump_,LIST_,dump)
PUSH(conts_,LIST_,newnode(LIST_,conts->u.lis->next,conts->next))
PUSH(symtabindex_,INTEGER_,LOC2INT(symtabindex))
/* this is now in utils.c
PUSH(memoryindex_,INTEGER_,MEM2INT(memoryindex))
*/
PUSH(echo_,INTEGER_,echoflag)
PUSH(autoput_,INTEGER_,autoput)
PUSH(clock_,INTEGER_,clock() - startclock)
/* - - - - - O P E R A T O R S - - - - - */
PRIVATE void id_()
{
/* do nothing */
}
PRIVATE void unstack_()
{
ONEPARAM("unstack");
LIST("unstack");
stk = stk->u.lis;
}
/*
PRIVATE void newstack_()
{
stk = NULL;
}
*/
/* - - - STACK - - - */
PRIVATE void name_()
{
ONEPARAM("name");
UNARY(STRING_, stk->op == USR_ ?
stk->u.ent->name : opername(stk->op));
}
PRIVATE void body_()
{
ONEPARAM("body");
USERDEF("body");
UNARY(LIST_,stk->u.ent->u.body);
}
PRIVATE void pop_()
{
ONEPARAM("pop");
POP(stk);
}
PRIVATE void swap_()
{
TWOPARAMS("swap");
SAVESTACK;
BINARY(SAVED1->op,SAVED1->u.num);
NULLARY(SAVED2->op,SAVED2->u.num);
POP(dump);
}
PRIVATE void rollup_()
{
THREEPARAMS("rollup");
SAVESTACK;
TERNARY(SAVED1->op,SAVED1->u.num);
NULLARY(SAVED3->op,SAVED3->u.num);
NULLARY(SAVED2->op,SAVED2->u.num);
POP(dump);
}
PRIVATE void rolldown_()
{
THREEPARAMS("rolldown");
SAVESTACK;
TERNARY(SAVED2->op,SAVED2->u.num);
NULLARY(SAVED1->op,SAVED1->u.num);
NULLARY(SAVED3->op,SAVED3->u.num);
POP(dump);
}
PRIVATE void rotate_()
{
THREEPARAMS("rotate");
SAVESTACK;
TERNARY(SAVED1->op,SAVED1->u.num);
NULLARY(SAVED2->op,SAVED2->u.num);
NULLARY(SAVED3->op,SAVED3->u.num);
POP(dump);
}
PRIVATE void dup_()
{
ONEPARAM("dup");
NULLARY(stk->op,stk->u.num);
}
/* - - - BOOLEAN - - - */
#define ANDORXOR(PROCEDURE,NAME,OPER1,OPER2) \
PRIVATE void PROCEDURE() \
{ TWOPARAMS(NAME); \
SAME2TYPES(NAME); \
switch (stk->next->op) \
{ case SET_: \
BINARY(SET_,stk->next->u.set OPER1 stk->u.set); \
return; \
case BOOLEAN_: case CHAR_: case INTEGER_: case LIST_: \
BINARY(BOOLEAN_,stk->next->u.num OPER2 stk->u.num); \
return; \
default: \
BADDATA(NAME); } }
ANDORXOR(and_,"and",&,&&)
ANDORXOR(or_,"or",|,||)
ANDORXOR(xor_,"xor",^,!=)
/* - - - INTEGER - - - */
PRIVATE void abs_()
{
ONEPARAM("abs");
INTEGER("abs");
if (stk->u.num < 0) UNARY(INTEGER_, - stk->u.num);
}
PRIVATE void sign_()
{
ONEPARAM("sign");
INTEGER("sign");
if (stk->u.num < 0) UNARY(INTEGER_,-1);
else if (stk->u.num > 0) UNARY(INTEGER_,1);
}
#define MULDIVREM(PROCEDURE,NAME,OPER,CHECK) \
PRIVATE void PROCEDURE() \
{ TWOPARAMS(NAME); \
INTEGERS2(NAME); \
CHECK; \
BINARY(INTEGER_,stk->next->u.num OPER stk->u.num); }
MULDIVREM(mul_,"*",*,)
MULDIVREM(rem_,"rem",%,CHECKZERO("rem"))
MULDIVREM(divide_,"/",/,CHECKZERO("/"))
/* - - - NUMERIC - - - */
#define PREDSUCC(PROCEDURE,NAME,OPER) \
PRIVATE void PROCEDURE() \
{ ONEPARAM(NAME); \
NUMERICTYPE(NAME); \
UNARY(stk->op,stk->u.num OPER 1); }
PREDSUCC(pred_,"pred",-)
PREDSUCC(succ_,"succ",+)
#define PLUSMINUS(PROCEDURE,NAME,OPER) \
PRIVATE void PROCEDURE() \
{ TWOPARAMS(NAME); \
INTEGER(NAME); \
NUMERIC2(NAME); \
BINARY(stk->next->op,stk->next->u.num OPER stk->u.num); }
PLUSMINUS(plus_,"+",+)
PLUSMINUS(minus_,"-",-)
#define MAXMIN(PROCEDURE,NAME,OPER) \
PRIVATE void PROCEDURE() \
{ TWOPARAMS(NAME); \
SAME2TYPES(NAME); \
NUMERICTYPE(NAME); \
BINARY(stk->op, \
stk->u.num OPER stk->next->u.num ? \
stk->next->u.num : stk->u.num); }
MAXMIN(max_,"max",<)
MAXMIN(min_,"min",>)
#define COMPREL(PROCEDURE,NAME,TYPE,OPR) \
PRIVATE void PROCEDURE() \
{ int comp = 0; \
TWOPARAMS(NAME); \
switch (stk->op) \
{ case BOOLEAN_: case CHAR_: case INTEGER_: \
comp = stk->next->u.num - stk->u.num OPR 0; \
break; \
case SET_: \
{ int i = 0; \
while ( i < SETSIZE && \
( (stk->next->u.set & 1 << i) == \
(stk->u.set & 1 << i) ) ) \
++i; \
if (i == SETSIZE) i = 0; else ++i; \
if (!(stk->u.set & 1 << i)) i = -i; \
comp = i OPR 0; \
break; } \
case LIST_: \
BADDATA(NAME); \
default: \
comp = strcmp(GETSTRING(stk->next), GETSTRING(stk)) \
OPR 0; \
break; } \
BINARY(TYPE,comp); }
COMPREL(eql_,"=",BOOLEAN_,==)
COMPREL(neql_,"!=",BOOLEAN_,!=)
COMPREL(less_,"<",BOOLEAN_,<)
COMPREL(leql_,"<=",BOOLEAN_,<=)
COMPREL(greater_,">",BOOLEAN_,>)
COMPREL(geql_,">=",BOOLEAN_,>=)
COMPREL(compare_,"compare",INTEGER_,+)
/* - - - AGGREGATES - - - */
PRIVATE void first_()
{
ONEPARAM("first");
switch (stk->op)
{ case LIST_:
CHECKEMPTYLIST(stk->u.lis,"first");
UNARY(stk->u.lis->op,stk->u.lis->u.num);
return;
case STRING_:
CHECKEMPTYSTRING(stk->u.str,"first");
UNARY(CHAR_,*(stk->u.str));
return;
case SET_:
{ int i = 0;
CHECKEMPTYSET(stk->u.set,"first");
while (!(stk->u.set & (1 << i))) i++;
UNARY(INTEGER_,i);
return; }
default:
BADAGGREGATE("first"); }
}
PRIVATE void rest_()
{
ONEPARAM("rest");
switch (stk->op)
{ case SET_:
{ int i = 0;
CHECKEMPTYSET(stk->u.set,"rest");
while (!(stk->u.set & (1 << i))) i++;
UNARY(SET_,stk->u.set & ~(1 << i));
break; }
case STRING_:
{ char *s = stk->u.str;
CHECKEMPTYSTRING(s,"rest");
UNARY(STRING_, ++s);
break; }
case LIST_:
CHECKEMPTYLIST(stk->u.lis,"rest");
UNARY(LIST_,stk->u.lis->next);
return;
default:
BADAGGREGATE("rest"); }
}
PRIVATE void uncons_()
{
ONEPARAM("uncons");
switch (stk->op)
{ case SET_:
{ int i = 0; long set = stk->u.set;
CHECKEMPTYSET(set,"uncons");
while (!(set & (1 << i))) i++;
UNARY(INTEGER_,i);
NULLARY(SET_,set & ~(1 << i));
break; }
case STRING_:
{ char *s = stk->u.str;
CHECKEMPTYSTRING(s,"uncons");
UNARY(CHAR_,*s);
NULLARY(STRING_,++s);
break; }
case LIST_:
SAVESTACK;
CHECKEMPTYLIST(SAVED1->u.lis,"uncons");
UNARY(SAVED1->u.lis->op,SAVED1->u.lis->u.num);
NULLARY(LIST_,SAVED1->u.lis->next);
POP(dump);
return;
default:
BADAGGREGATE("uncons"); }
}
PRIVATE void unswons_()
{
ONEPARAM("unswons");
switch (stk->op)
{ case SET_:
{ int i = 0; long set = stk->u.set;
CHECKEMPTYSET(set,"unswons");
while (!(set & (1 << i))) i++;
UNARY(SET_,set & ~(1 << i));
NULLARY(INTEGER_,i);
break; }
case STRING_:
{ char *s = stk->u.str;
CHECKEMPTYSTRING(s,"unswons");
UNARY(STRING_,++s);
NULLARY(CHAR_,*(--s));
break; }
case LIST_:
SAVESTACK;
CHECKEMPTYLIST(SAVED1->u.lis,"unswons");
UNARY(LIST_,SAVED1->u.lis->next);
NULLARY(SAVED1->u.lis->op,SAVED1->u.lis->u.num);
POP(dump);
return;
default:
BADAGGREGATE("unswons"); }
}
PRIVATE int equal_aux(); /* forward */
PRIVATE int equal_list_aux(n1,n2)
Node *n1, *n2;
{
if (n1 == NULL && n2 == NULL) return 1;
if (n1 == NULL || n2 == NULL) return 0;
if (equal_aux(n1,n2))
return equal_list_aux(n1->next,n2->next);
else return 0;
}
PRIVATE int equal_aux(n1,n2)
Node *n1, *n2;
{
if (n1 == NULL && n2 == NULL) return 1;
if (n1 == NULL || n2 == NULL) return 0;
switch (n1->op)
{ case BOOLEAN_: case CHAR_: case INTEGER_:
if (n2->op != BOOLEAN_ && n2->op != CHAR_ &&
n2->op != INTEGER_)
return 0;
return n1->u.num == n2->u.num;
case SET_ :
if (n2->op != SET_) return 0;
return n1->u.num == n2->u.num;
case LIST_ :
if (n2->op != LIST_) return 0;
return equal_list_aux(n1->u.lis,n2->u.lis);
default:
return strcmp(GETSTRING(n1),GETSTRING(n2)) == 0; }
}
PRIVATE void equal_()
{
TWOPARAMS("equal");
BINARY(BOOLEAN_,equal_aux(stk,stk->next));
}
#define INHAS(PROCEDURE,NAME,AGGR,ELEM) \
PRIVATE void PROCEDURE() \
{ int found = 0; \
TWOPARAMS(NAME); \
switch (AGGR->op) \
{ case SET_: \
found = ((AGGR->u.set) & (1 << ELEM->u.num)) > 0; \
break; \
case STRING_: \
{ char *s; \
for (s = AGGR->u.str; \
*s != '\0' && *s != ELEM->u.num; \
s++); \
found = *s != '\0'; \
break; } \
case LIST_: \
{ Node *n = AGGR->u.lis; \
while (n != NULL && n->u.num != ELEM->u.num) \
n = n->next; \
found = n != NULL; \
break; } \
default: \
BADAGGREGATE(NAME); } \
BINARY(BOOLEAN_,found); \
}
INHAS(in_,"in",stk,stk->next)
INHAS(has_,"has",stk->next,stk)
#define OF_AT(PROCEDURE,NAME,AGGR,INDEX) \
PRIVATE void PROCEDURE() \
{ TWOPARAMS(NAME); \
switch (AGGR->op) \
{ case SET_: \
{ int i; int indx = INDEX->u.num; \
CHECKEMPTYSET(AGGR->u.set,NAME); \
for (i = 0; i < SETSIZE; i++) \
{ if (AGGR->u.set & (1 << i)) \
{ if (indx == 0) \
{BINARY(INTEGER_,i); return;} \
indx--; } } \
INDEXTOOLARGE(NAME); \
return; } \
case STRING_: \
if (strlen(AGGR->u.str) < (size_t)INDEX->u.num) \
INDEXTOOLARGE(NAME); \
BINARY(CHAR_,AGGR->u.str[INDEX->u.num]); \
return; \
case LIST_: \
{ Node *n = AGGR->u.lis; int i = INDEX->u.num; \
CHECKEMPTYLIST(n,NAME); \
while (i > 0) \
{ if (n->next == NULL) \
INDEXTOOLARGE(NAME); \
n = n->next; i--; } \
BINARY(n->op,n->u.num); \
return; } \
default: \
BADAGGREGATE(NAME); } \
}
OF_AT(of_,"of",stk,stk->next)
OF_AT(at_,"at",stk->next,stk)
PRIVATE void choice_()
{
THREEPARAMS("choice");
if (stk->next->next->u.num)
stk = newnode(stk->next->op,stk->next->u.num,
stk->next->next->next);
else stk = newnode(stk->op,stk->u.num,
stk->next->next->next);
}
PRIVATE void opcase_()
{
Node *n;
ONEPARAM("opcase");
LIST("opcase");
n = stk->u.lis;
CHECKEMPTYLIST(n,"opcase");
while ( n->next != NULL &&
n->op == LIST_ &&
n->u.lis->op != stk->next->op )
n = n->next;
CHECKLIST(n->op,"opcase");
UNARY(LIST_,
n->next != NULL ? n->u.lis->next : n->u.lis);
}
#define CONS_SWONS(PROCEDURE,NAME,AGGR,ELEM) \
PRIVATE void PROCEDURE() \
{ TWOPARAMS(NAME); \
Node *temp; \
switch (AGGR->op) \
{ case LIST_: \
temp = newnode(ELEM->op, ELEM->u.num, AGGR->u.lis); \
BINARY(LIST_,temp); \
break; \
case SET_: \
CHECKSETMEMBER(ELEM,NAME); \
BINARY(SET_,AGGR->u.set | (1 << ELEM->u.num)); \
break; \
case STRING_: \
{ char *s; \
s = (char *) malloc(strlen(AGGR->u.str) + 2); \
s[0] = ELEM->u.num; \
strcpy(++s,AGGR->u.str); \
BINARY(STRING_,--s); \
break; } \
default: \
BADAGGREGATE(NAME); } \
}
CONS_SWONS(cons_,"cons",stk,stk->next)
CONS_SWONS(swons_,"swons",stk->next,stk)
PRIVATE void drop_()
{ int n = stk->u.num;
TWOPARAMS("drop");
switch (stk->next->op)
{ case SET_:
{ int i; int result = 0;
for (i = 0; i < SETSIZE; i++)
if (stk->next->u.set & (1 << i))
{ if (n < 1) result = result | (1 << i);
else n--; }
BINARY(SET_,result);
return; }
case STRING_:
{ char *result = stk->next->u.str;
while (n-- > 0 && *result != '\0') ++result;
BINARY(STRING_,result);
return; }
case LIST_:
{ Node *result = stk->next->u.lis;
while (n-- > 0 && result != NULL) result = result->next;
BINARY(LIST_,result);
return; }
default:
BADAGGREGATE("drop"); }
}
PRIVATE void take_()
{ int n = stk->u.num;
TWOPARAMS("take");
switch (stk->next->op)
{ case SET_:
{ int i; int result = 0;
for (i = 0; i < SETSIZE; i++)
if (stk->next->u.set & (1 << i))
{ if (n > 0)
{ --n; result = result | (1 << i); }
else break; }
BINARY(SET_,result);
return; }
case STRING_:
{ int i; char *old, *p, *result;
i = stk->u.num;
old = stk->next->u.str;
p = result = (char *) malloc(strlen(old) - i + 1);
while (i-- > 0) *p++ = *old++;
BINARY(STRING_,result);
return; }
case LIST_:
{ int i = stk->u.num;
if (i < 1) { BINARY(LIST_,NULL); return; }
dump1 = newnode(LIST_,stk->next->u.lis,dump1);/* old */
dump2 = newnode(LIST_,NULL, dump2); /* head */
dump3 = newnode(LIST_,NULL, dump3); /* last */
while (DMP1 != NULL && i-- > 0)
{ if (DMP2 == NULL) /* first */
{ DMP2 = newnode(DMP1->op,DMP1->u.num,NULL);
DMP3 = DMP2; }
else /* further */
{ Node *temp = newnode(DMP1->op,DMP1->u.num,NULL);
DMP3->next = temp;
DMP3 = DMP3->next; }
DMP1 = DMP1->next; }
DMP3->next = NULL;
BINARY(LIST_,DMP2);
POP(dump1); POP(dump2); POP(dump3);
return; }
default:
BADAGGREGATE("take"); }
}
PRIVATE void concat_()
{
TWOPARAMS("concat");
switch (stk->op)
{ case SET_:
BINARY(SET_,stk->next->u.set | stk->u.set);
return;
case STRING_:
{ char *s, *p;
s = p = (char *)malloc(strlen(stk->next->u.str) +
strlen(stk->u.str) + 1);
while ((*p++ = *(stk->next->u.str)++) != '\0');
--p; /* don't want terminating null */
while ((*p++ = *(stk->u.str)++) != '\0');
BINARY(STRING_,s);
return; }
case LIST_:
if (stk->next->u.lis == NULL)
{ BINARY(LIST_,stk->u.lis); return; }
dump1 = newnode(LIST_,stk->next->u.lis,dump1);/* old */
dump2 = newnode(LIST_,NULL,dump2); /* head */
dump3 = newnode(LIST_,NULL,dump3); /* last */
while (DMP1 != NULL)
{ if (DMP2 == NULL) /* first */
{ DMP2 =
newnode(DMP1->op,
DMP1->u.num,NULL);
DMP3 = DMP2; }
else /* further */
{ Node *temp =
newnode(DMP1->op,
DMP1->u.num,NULL);
DMP3->next = temp;
DMP3 = DMP3->next; };
DMP1 = DMP1->next; }
DMP3->next = stk->u.lis;
BINARY(LIST_,DMP2);
POP(dump1);
POP(dump2);
POP(dump3);
return;
default:
BADAGGREGATE("concat"); };
}
PRIVATE void null_()
{
ONEPARAM("null");
UNARY(BOOLEAN_,
stk->op == STRING_ ? *(stk->u.str) == '\0' : ! stk->u.num);
/*
switch (stk->op)
{ case STRING_:
UNARY(BOOLEAN_, *(stk->u.str) == '\0');
break;
default:
UNARY(BOOLEAN_, ! stk->u.num); }
*/
}
PRIVATE void nullval_()
{
ONEPARAM("nullval");
if (stk->op == STRING_)
UNARY(STRING_,""); else
UNARY(stk->op,0);
}
PRIVATE void not_()
{
ONEPARAM("not");
switch (stk->op)
{ case SET_:
UNARY(SET_,~ stk->u.set);
break;
case STRING_:
UNARY(BOOLEAN_, *(stk->u.str) != '\0');
break;
case BOOLEAN_: case CHAR_: case INTEGER_: case LIST_:
UNARY(BOOLEAN_, ! stk->u.num);
break;
default:
BADDATA("not"); }
}
PRIVATE void size_()
{
int siz = 0;
ONEPARAM("size");
switch (stk->op)
{ case SET_:
{ int i;
for (i = 0; i < SETSIZE; i++)
if (stk->u.set & (1 << i)) siz++;
break; }
case STRING_:
siz = strlen(stk->u.str);
break;
case LIST_:
{ Node *e = stk->u.lis;
while (e != NULL) {e = e->next; siz++;};
break; }
default :
BADDATA("size"); }
UNARY(INTEGER_,siz);
}
PRIVATE void small_()
{
int sml = 0;
ONEPARAM("small");
switch (stk->op)
{ case BOOLEAN_: case INTEGER_:
sml = stk->u.num < 2;
break;
case SET_:
if (stk->u.set == 0) sml = 1; else
{ int i = 0;
while (!(stk->u.set & (1 << i))) i++;
D( printf("small: first member found is %d\n",i); )
sml = (stk->u.set & ~(1 << i)) == 0; }
break;
case STRING_:
sml = stk->u.str[0] == '\0' || stk->u.str[1] == '\0';
break;
case LIST_:
sml = stk->u.lis == NULL || stk->u.lis->next == NULL;
break;
default:
BADDATA("small"); }
UNARY(BOOLEAN_,sml);
}
#define TYPE(PROCEDURE,NAME,REL,TYP) \
PRIVATE void PROCEDURE() \
{ ONEPARAM(NAME); \
UNARY(BOOLEAN_,stk->op REL TYP); }
TYPE(integer_,"integer",==,INTEGER_)
TYPE(char_,"char",==,CHAR_)
TYPE(logical_,"logical",==,BOOLEAN_)
TYPE(string_,"string",==,STRING_)
TYPE(set_,"set",==,SET_)
TYPE(list_,"list",==,LIST_)
TYPE(leaf_,"leaf",!=,LIST_)
TYPE(user_,"user",==,USR_)
#define USETOP(PROCEDURE,NAME,TYPE,BODY) \
PRIVATE void PROCEDURE() \
{ ONEPARAM(NAME); TYPE(NAME); BODY; POP(stk); }
USETOP( put_,"put",ONEPARAM, writefactor(stk);printf(" "))
USETOP( putch_,"putch",NUMERICTYPE, printf("%c", (char) stk->u.num) )
USETOP( setecho_,"setecho",NUMERICTYPE, echoflag = stk->u.num )
USETOP( setautoput_,"setautoput",NUMERICTYPE, autoput = stk->u.num )
USETOP( settracegc_,"settracegc",NUMERICTYPE, tracegc = stk->u.num )
USETOP( include_,"include",STRING, doinclude(stk->u.str) )
USETOP( system_,"system",STRING, system(stk->u.str) )
PRIVATE void get_()
{
getsym();
readfactor();
}
PUBLIC void dummy_()
{
/* never called */
}
#define HELP(PROCEDURE,REL) \
PRIVATE void PROCEDURE() \
{ Entry *i = symtabindex; \
while (i != symtab) \
if ((--i)->name[0] REL '_') \
printf("%s ",i->name); \
printf("\n"); }
HELP(help1_,!=)
HELP(h_help1_,==)
/* - - - - - C O M B I N A T O R S - - - - - */
#ifdef TRACING
PRIVATE void writestack(Node *n)
{
if (n) {
writestack(n->next);
if (n->next)
putchar(' ');
writefactor(n);
}
}
#endif
PUBLIC void exeterm(n)
Node *n;
{
Node *stepper;
start:
if (n == NULL) return;
conts = newnode(LIST_,n,conts);
while (conts->u.lis != NULL)
{
if (tracegc > 5)
{ printf("exeterm1: %ld ",(long)conts->u.lis);
printnode(conts->u.lis); }
stepper = conts->u.lis;
#ifdef TRACING
writestack(stk);
printf(" : ");
writeterm(stepper);
putchar('\n');
#endif
conts->u.lis = conts->u.lis->next;
switch (stepper->op)
{ case BOOLEAN_: case CHAR_: case INTEGER_:
case SET_: case STRING_: case LIST_:
NULLARY(stepper->op,stepper->u.num); break;
case USR_:
if (stepper->u.ent->u.body == NULL)
execerror("definition", stepper->u.ent->name);
if (stepper->next == NULL)
{ POP(conts);
n = stepper->u.ent->u.body;
goto start; }
else exeterm(stepper->u.ent->u.body );
break;
case COPIED_: case ILLEGAL_:
printf("exeterm: attempting to execute bad node\n");
printnode(stepper);
break;
default:
D( printf("trying to do "); )
D( writefactor(dump1); )
(*(stepper->u.proc))(); break; }
if (tracegc > 5)
{ printf("exeterm2: %ld ",(long)stepper);
printnode(stepper); }
/*
stepper = stepper->next; }
*/
}
POP(conts);
D( printf("after execution, stk is:\n"); )
D( writeterm(stk); )
D( printf("\n"); )
}
PRIVATE void x_()
{
ONEPARAM("x");
ONEQUOTE("x");
exeterm(stk->u.lis);
}
PRIVATE void i_()
{
ONEPARAM("i");
ONEQUOTE("i");
SAVESTACK;
POP(stk);
exeterm(SAVED1->u.lis);
POP(dump);
}
PRIVATE void dip_()
{
TWOPARAMS("dip");
ONEQUOTE("dip");
SAVESTACK;
stk = stk->next->next;
exeterm(SAVED1->u.lis);
NULLARY(SAVED2->op,SAVED2->u.num);
POP(dump);
}
#define DIPPED(PROCEDURE,NAME,PARAMCOUNT,ARGUMENT) \
PRIVATE void PROCEDURE() \
{ PARAMCOUNT(NAME); \
SAVESTACK; \
POP(stk); \
ARGUMENT(); \
NULLARY(SAVED1->op,SAVED1->u.num); \
POP(dump); \
}
DIPPED(popd_,"popd",TWOPARAMS,pop_)
DIPPED(dupd_,"dupd",TWOPARAMS,dup_)
DIPPED(swapd_,"swapd",THREEPARAMS,swap_)
DIPPED(rolldownd_,"rolldownd",FOURPARAMS,rolldown_)
DIPPED(rollupd_,"rollupd",FOURPARAMS,rollup_)
DIPPED(rotated_,"rotated",FOURPARAMS,rotate_)
#define N_ARY(PROCEDURE,NAME,PARAMCOUNT,TOP) \
PRIVATE void PROCEDURE() \
{ PARAMCOUNT(NAME); \
ONEQUOTE(NAME); \
SAVESTACK; \
POP(stk); \
exeterm(SAVED1->u.lis); \
stk = newnode(stk->op,stk->u.num,TOP); \
POP(dump); \
}
N_ARY(nullary_,"nullary",ONEPARAM,SAVED2)
N_ARY(unary_,"unary",TWOPARAMS,SAVED3)
N_ARY(binary_,"binary",THREEPARAMS,SAVED4)
N_ARY(ternary_,"ternary",FOURPARAMS,SAVED5)
/*
PRIVATE void nullary_()
{
ONEPARAM("nullary");
SAVESTACK;
POP(stk);
exeterm(SAVED1->u.lis);
stk->next = SAVED2;
POP(dump);
}
*/
PRIVATE void times_()
{
int i,n;
TWOPARAMS("times");
ONEQUOTE("times");
SAVESTACK;
stk = stk->next->next;
n = SAVED2->u.num;
for (i = 1; i <= n; i++)
exeterm(SAVED1->u.lis);
POP(dump);