-
Notifications
You must be signed in to change notification settings - Fork 8
/
reduce.c
2395 lines (2178 loc) · 70.6 KB
/
reduce.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
/* MIRANDA REDUCE */
/* new SK reduction machine - installed Oct 86 */
/**************************************************************************
* Copyright (C) Research Software Limited 1985-90. All rights reserved. *
* The Miranda system is distributed as free software under the terms in *
* the file "COPYING" which is included in the distribution. *
* *
* Revised to C11 standard and made 64bit compatible, January 2020 *
*------------------------------------------------------------------------*/
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <wchar.h>
struct stat buf; /* used only by code for FILEMODE, FILESTAT in reduce */
#include "data.h"
#include "big.h"
#include "lex.h"
extern int debug, UTF8, UTF8OUT;
#define FST HD
#define SND TL
#define BSDCLOCK
/* POSIX clock wraps around after c. 72 mins */
#ifdef RYU
char* d2s(double);
word d2s_buffered(double, char*);
#endif
double fa,fb;
long long cycles=0;
word stdinuse=0;
/* int lasthead=0; // DEBUG */
static void apfile(word);
static void closefile(word);
static void div_error(void);
static void fn_error(char *);
static void getenv_error(char *);
static word g_residue(word);
static void lexfail(word);
static word lexstate(word);
static int memclass(int,word);
static word numplus(word,word);
static void outf(word);
static word piperrmess(word);
static void print(word);
static word reduce(word);
static void stdin_error(int);
static void subs_error(void);
static void int_error(char *);
#define constr_tag(x) hd[x]
#define idconstr_tag(x) hd[id_val(x)]
#define constr_name(x) (tag[tl[x]]==ID?get_id(tl[x]):get_id(pn_val(tl[x])))
#define suppressed(x) (tag[tl[x]]==STRCONS&&tag[pn_val(tl[x])]!=ID)
/* suppressed constructor */
#define isodigit(x) ('0'<=(x) && (x)<='7')
#define sign(x) (x)
#define fsign(x) ((d=(x))<0?-1:d>0)
/* ### */ /* functions marked ### contain possibly recursive calls
to reduce - fix later */
int compare(a,b) /* returns -1, 0, 1 as a is less than equal to or greater than
b in the ordering imposed on all data types by the miranda
language -- a and b already reduced */
/* used by MATCH, EQ, NEQ, GR, GRE */
word a,b;
{ double d;
L: switch(tag[a])
{ case DOUBLE:
if(tag[b]==DOUBLE)return(fsign(get_dbl(a)-get_dbl(b)));
else return(fsign(get_dbl(a)-bigtodbl(b)));
case INT:
if(tag[b]==INT)return(bigcmp(a,b));
else return(fsign(bigtodbl(a)-get_dbl(b)));
case UNICODE: return sign(get_char(a)-get_char(b));
case ATOM:
if(tag[b]==UNICODE) return sign(get_char(a)-get_char(b));
if((S<=a&&a<=ERROR)||(S<=b&&b<=ERROR))
fn_error("attempt to compare functions");
/* what about constructors - FIX LATER */
if(tag[b]==ATOM)return(sign(a-b)); /* order of declaration */
else return(-1); /* atomic object always less than non-atomic */
case CONSTRUCTOR:
if(tag[b]==CONSTRUCTOR)
return(sign(constr_tag(a)-constr_tag(b))); /*order of declaration*/
else return(-1); /* atom less than non-atom */
case CONS: case AP:
if(tag[a]==tag[b])
{ word temp;
hd[a]=reduce(hd[a]);
hd[b]=reduce(hd[b]);
if((temp=compare(hd[a],hd[b]))!=0)return(temp);
a=tl[a]=reduce(tl[a]);
b=tl[b]=reduce(tl[b]);
goto L; }
else if(S<=b&&b<=ERROR)fn_error("attempt to compare functions");
else return(1); /* non-atom greater than atom */
default: fprintf(stderr,"\nghastly error in compare\n");
}
return(0);
}
void force(x) /* ensures that x is evaluated "all the way" */
word x; /* x is already reduced */ /* ### */
{ word h;
switch(tag[x])
{ case AP: h=hd[x];
while(tag[h]==AP)h=hd[h];
if(S<=h&&h<=ERROR)return; /* don't go inside functions */
/* what about unsaturated constructors? fix later */
while(tag[x]==AP)
{ tl[x]=reduce(tl[x]);
force(tl[x]);
x=hd[x]; }
return;
case CONS: while(tag[x]==CONS)
{ hd[x]=reduce(hd[x]);
force(hd[x]);
x=tl[x]=reduce(tl[x]); }
}
return;
}
word head(x) /* finds the function part of x */
word x;
{ while(tag[x]==AP)x= hd[x];
return(x);
}
extern char linebuf[]; /* used as workspace in various places */
/* ### */ /* opposite is str_conv - see lex.c */
char *getstring(x,cmd) /* collect Miranda string - x is already reduced */
word x;
char *cmd; /* context, for error message */
{ word x1=x,n=0;
char *p=linebuf;
while(tag[x]==CONS&&n<BUFSIZE)
n++, hd[x] = reduce(hd[x]), x=tl[x]=reduce(tl[x]);
x=x1;
while(tag[x]==CONS&&n--)
*p++ = hd[x], x=tl[x];
*p++ ='\0';
if(p-linebuf>BUFSIZE)
{ if(cmd)fprintf(stderr,
"\n%s, argument string too long (limit=%d chars): %s...\n",
cmd,BUFSIZE,linebuf),
outstats(),
exit(1);
else return(linebuf); /* see G_CLOSE */ }
return(linebuf); /* very inefficient to keep doing this for filenames etc.
CANNOT WE SUPPORT A PACKED REPRESENTATION OF STRINGS? */
} /* call keep(linebuf) if you want to save the string */
FILE *s_out=NULL; /* destination of current output message */
/* initialised in main() */
#define Stdout 0
#define Stderr 1
#define Tofile 2
#define Closefile 3
#define Appendfile 4
#define System 5
#define Exit 6
#define Stdoutb 7
#define Tofileb 8
#define Appendfileb 9
/* order of declaration of constructors of these names in sys_message */
/* ### */
void output(e) /* "output" is called by YACC (see rules.y) to print the
value of an expression - output then calls "reduce" - so the
whole reduction process is driven by the need to print */
/* the value of the whole expression is a list of `messages' */
word e;
{
extern word *cstack;
cstack = &e; /* don't follow C stack below this in gc */
L:e= reduce(e);
while(tag[e]==CONS)
{
hd[e]= reduce(hd[e]);
switch(constr_tag(head(hd[e])))
{ case Stdout: print(tl[hd[e]]);
break;
case Stdoutb: UTF8OUT=0;
print(tl[hd[e]]);
UTF8OUT=UTF8;
break;
case Stderr: s_out=stderr; print(tl[hd[e]]); s_out=stdout;
break;
case Tofile: outf(hd[e]);
break;
case Tofileb: UTF8OUT=0;
outf(hd[e]);
UTF8OUT=UTF8;
break;
case Closefile: closefile(tl[hd[e]]=reduce(tl[hd[e]]));
break;
case Appendfile: apfile(tl[hd[e]]=reduce(tl[hd[e]]));
break;
case Appendfileb: UTF8OUT=0;
apfile(tl[hd[e]]=reduce(tl[hd[e]]));
UTF8OUT=UTF8;
break;
case System: system(getstring(tl[hd[e]]=reduce(tl[hd[e]]),"System"));
break;
case Exit: { word n=reduce(tl[hd[e]]);
if(tag[n]==INT)n=digit0(n);
else int_error("Exit");
outstats(); exit(n); }
default: fprintf(stderr,"\n<impossible event in output list: ");
out(stderr,hd[e]);
fprintf(stderr,">\n"); }
e= tl[e]= reduce(tl[e]);
}
if(e==NIL)return;
fprintf(stderr,"\nimpossible event in output\n"),
putc('<',stderr),out(stderr,e),fprintf(stderr,">\n");
exit(1);
}
/* ### */
void print(e) /* evaluate list of chars and send to s_out */
word e;
{ e= reduce(e);
while(tag[e]==CONS && is_char(hd[e]=reduce(hd[e])))
{ unsigned c=get_char(hd[e]);
if(UTF8)fputwc(c,s_out); else
if(c<256) putc(c,s_out);
else fprintf(stderr,"\n warning: non Latin1 char \%x in print, ignored\n",c);
e= tl[e]= reduce(tl[e]); }
if(e==NIL)return;
fprintf(stderr,"\nimpossible event in print\n"),
putc('<',stderr),out(stderr,e),fprintf(stderr,">\n"),
exit(1);
}
word outfilq=NIL; /* list of opened-for-output files */
/* note that this will be automatically reset to NIL and all files on it
closed at end of expression evaluation, because of the fork-exit structure */
/* ### */
void outf(e) /* e is of the form (Tofile f x) */
word e;
{ word p=outfilq; /* have we already opened this file for output? */
char *f=getstring(tl[hd[e]]=reduce(tl[hd[e]]),"Tofile");
while(p!=NIL && strcmp((char *)hd[hd[p]],f)!=0)p=tl[p];
if(p==NIL) /* new output file */
{ s_out= fopen(f,"w");
if(s_out==NULL)
{ fprintf(stderr,"\nTofile: cannot write to \"%s\"\n",f);
s_out=stdout;
return;
/* outstats(); exit(1); // release one policy */
}
if(isatty(fileno(s_out)))setbuf(s_out,NULL); /*for unbuffered tty output*/
outfilq= cons(datapair(keep(f),s_out),outfilq); }
else s_out= (FILE *)tl[hd[p]];
print(tl[e]);
s_out= stdout;
}
void apfile(f) /* open file of name f for appending and add to outfilq */
word f;
{ word p=outfilq; /* is it already open? */
char *fil=getstring(f,"Appendfile");
while(p!=NIL && strcmp((char *)hd[hd[p]],fil)!=0)p=tl[p];
if(p==NIL) /* no, so open in append mode */
{ FILE *s=fopen(fil,"a");
if(s==NULL)
fprintf(stderr,"\nAppendfile: cannot write to \"%s\"\n",fil);
else outfilq= cons(datapair(keep(fil),s),outfilq);
}
/* if already there do nothing */
}
void closefile(f) /* remove file of name "f" from outfilq and close stream */
word f;
{ word *p= &outfilq; /* is this file open for output? */
char *fil=getstring(f,"Closefile");
while(*p!=NIL && strcmp((char *)hd[hd[*p]],fil)!=0)p= &tl[*p];
if(*p!=NIL) /* yes */
{ fclose((FILE *)tl[hd[*p]]);
*p=tl[*p]; /* remove link from outfilq */}
/* otherwise ignore closefile request (harmless??) */
}
static word errtrap=0; /* to prevent error cycles - see ERROR below */
word waiting=NIL;
/* list of terminated child processes with exit_status - see Exec/EXEC */
/* pointer-reversing SK reduction machine - based on code written Sep 83 */
#define READY(x) (x)
#define RESTORE(x)
/* in this machine the above two are no-ops, alternate definitions are, eg
#define READY(x) (x+1)
#define RESTORE(x) x--
(if using this method each strict comb needs next opcode unallocated)
see comment before "ready" switch */
#define mktlptr(x) x |= tlptrbit
#define mk1tlptr x |= tlptrbits
#define mknormal(x) x &= ~tlptrbits
#define abnormal(x) ((x)<0)
/* covers x is tlptr and x==BACKSTOP */
/* control abstractions */
#define setcell(t,a,b) tag[e]=t,hd[e]=a,tl[e]=b
#define DOWNLEFT hold=s, s=e, e=hd[e], hd[s]=hold
#define DOWNRIGHT hold=hd[s], hd[s]=e, e=tl[s], tl[s]=hold, mktlptr(s)
#define downright if(abnormal(s))goto DONE; DOWNRIGHT
#define UPLEFT hold=s, s=hd[s], hd[hold]=e, e=hold
#define upleft if(abnormal(s))goto DONE; UPLEFT
#define GETARG(a) UPLEFT, a=tl[e]
#define getarg(a) upleft; a=tl[e]
#define UPRIGHT mknormal(s), hold=tl[s], tl[s]=e, e=hd[s], hd[s]=hold
#define lastarg tl[e]
word reds=0;
/* IMPORTANT WARNING - the macro's
`downright;' `upleft;' `getarg;'
MUST BE ENCLOSED IN BRACES when they occur as the body of a control
structure (if, while etc.) */
#define simpl(r) hd[e]=I, e=tl[e]=r
#ifdef DEBUG
word maxrdepth=0,rdepth=0;
#endif
#define fails(x) (x==NIL)
#define FAILURE NIL
/* used by grammar combinators */
/* reduce e to hnf, note that a function in hnf will have head h with
S<=h<=ERROR all combinators lie in this range see combs.h */
word reduce(e)
word e;
{ word s=BACKSTOP,hold,arg1,arg2,arg3;
#ifdef DEBUG
if(++rdepth>maxrdepth)maxrdepth=rdepth;
if(debug&02)
printf("reducing: "),out(stdout,e),putchar('\n');
#endif
NEXTREDEX:
while(!abnormal(e)&&tag[e]==AP)DOWNLEFT;
#ifdef HISTO
histo(e);
#endif
#ifdef DEBUG
if(debug&02)
{ printf("head= ");
if(e==BACKSTOP)printf("BACKSTOP");
else out(stdout,e);
putchar('\n'); }
#endif
OPDECODE:
/*lasthead=e; // DEBUG */
cycles++;
switch(e)
{
case S: /* S f g x => f x(g x) */
getarg(arg1);
getarg(arg2);
upleft;
hd[e]=ap(arg1,lastarg); tl[e]=ap(arg2,lastarg);
DOWNLEFT;
DOWNLEFT;
goto NEXTREDEX;
case B: /* B f g x => f(g z) */
getarg(arg1);
getarg(arg2);
upleft;
hd[e]=arg1; tl[e]=ap(arg2,lastarg);
DOWNLEFT;
goto NEXTREDEX;
case CB: /* CB f g x => g(f z) */
getarg(arg1);
getarg(arg2);
upleft;
hd[e]=arg2; tl[e]=ap(arg1,lastarg);
DOWNLEFT;
goto NEXTREDEX;
case C: /* C f g x => f x g */
getarg(arg1);
getarg(arg2);
upleft;
hd[e]=ap(arg1,lastarg); tl[e]=arg2;
DOWNLEFT;
DOWNLEFT;
goto NEXTREDEX;
case Y: /* Y h => self where self=(h self) */
upleft;
hd[e]=tl[e]; tl[e]=e;
DOWNLEFT;
goto NEXTREDEX;
L_K:
case K: /* K x y => x */
getarg(arg1);
upleft;
hd[e]=I; e=tl[e]=arg1;
goto NEXTREDEX; /* could make eager in first arg */
L_KI:
case KI: /* KI x y => y */
upleft; /* lose first arg */
upleft;
hd[e]=I; e=lastarg; /* ?? */
goto NEXTREDEX; /* could make eager in 2nd arg */
case S1: /* S1 k f g x => k(f x)(g x) */
getarg(arg1);
getarg(arg2);
getarg(arg3);
upleft;
hd[e]=ap(arg2,lastarg);
hd[e]=ap(arg1,hd[e]);
tl[e]=ap(arg3,lastarg);
DOWNLEFT;
DOWNLEFT;
goto NEXTREDEX;
case B1: /* B1 k f g x => k(f(g x)) */
getarg(arg1); /* Mark Scheevel's new B1 */
getarg(arg2);
getarg(arg3);
upleft;
hd[e]=arg1;
tl[e]=ap(arg3,lastarg);
tl[e]=ap(arg2,tl[e]);
DOWNLEFT;
goto NEXTREDEX;
case C1: /* C1 k f g x => k(f x)g */
getarg(arg1);
getarg(arg2);
getarg(arg3);
upleft;
hd[e]=ap(arg2,lastarg);
hd[e]=ap(arg1,hd[e]);
tl[e]=arg3;
DOWNLEFT;
goto NEXTREDEX;
case S_p: /* S_p f g x => (f x) : (g x) */
getarg(arg1);
getarg(arg2);
upleft;
setcell(CONS,ap(arg1,lastarg),ap(arg2,lastarg));
goto DONE;
case B_p: /* B_p f g x => f : (g x) */
getarg(arg1);
getarg(arg2);
upleft;
setcell(CONS,arg1,ap(arg2,lastarg));
goto DONE;
case C_p: /* C_p f g x => (f x) : g */
getarg(arg1);
getarg(arg2);
upleft;
setcell(CONS,ap(arg1,lastarg),arg2);
goto DONE;
case ITERATE: /* ITERATE f x => x:ITERATE f (f x) */
getarg(arg1);
upleft;
hold=ap(hd[e],ap(arg1,lastarg));
setcell(CONS,lastarg,hold);
goto DONE;
case ITERATE1: /* ITERATE1 f x => [], x=FAIL
=> x:ITERATE1 f (f x), otherwise */
getarg(arg1);
upleft;
if((lastarg=reduce(lastarg))==FAIL) /* ### */
{ hd[e]=I; e=tl[e]=NIL; }
else
{ hold=ap(hd[e],ap(arg1,lastarg));
setcell(CONS,lastarg,hold); }
goto DONE;
case G_RULE:
case P: /* P x y => x:y */
getarg(arg1);
upleft;
setcell(CONS,arg1,lastarg);
goto DONE;
case U: /* U f x => f (HD x) (TL x)
non-strict uncurry */
getarg(arg1);
upleft;
hd[e]=ap(arg1,ap(HD,lastarg));
tl[e]=ap(TL,lastarg);
DOWNLEFT;
DOWNLEFT;
goto NEXTREDEX;
case Uf: /* Uf f x => f (BODY x) (LAST x)
version of non-strict U for
arbitrary constructors */
getarg(arg1);
upleft;
if(tag[head(lastarg)]==CONSTRUCTOR) /* be eager if safe */
hd[e]=ap(arg1,hd[lastarg]),
tl[e]=tl[lastarg];
else
hd[e]=ap(arg1,ap(BODY,lastarg)),
tl[e]=ap(LAST,lastarg);
DOWNLEFT;
DOWNLEFT;
goto NEXTREDEX;
case ATLEAST: /* ATLEAST k f x => f(x-k), isnat x & x>=k
=> FAIL, otherwise */
/* for matching n+k patterns */
getarg(arg1);
getarg(arg2);
upleft;
lastarg= reduce(lastarg); /* ### */
if(tag[lastarg]==INT)
{ hold = bigsub(lastarg,arg1);
if(poz(hold))hd[e]=arg2,tl[e]=hold;
else hd[e]=I,e=tl[e]=FAIL; }
else hd[e]=I,e=tl[e]=FAIL;
goto NEXTREDEX;
case U_: /* U_ f (a:b) => f a b
U_ f other => FAIL
U_ is a strict version of U(see above) */
getarg(arg1);
upleft;
lastarg= reduce(lastarg); /* ### */
if(lastarg==NIL)
{ hd[e]=I;
e=tl[e]=FAIL;
goto NEXTREDEX; }
hd[e]=ap(arg1,hd[lastarg]);
tl[e]=tl[lastarg];
goto NEXTREDEX;
case Ug: /* Ug k f (k x1 ... xn) => f x1 ... xn, n>=0
Ug k f other => FAIL
Ug is a strict version of U for arbitrary constructor k */
getarg(arg1);
getarg(arg2);
upleft;
lastarg= reduce(lastarg); /* ### */
if(constr_tag(arg1)!=constr_tag(head(lastarg)))
{ hd[e]=I;
e=tl[e]=FAIL;
goto NEXTREDEX; }
if(tag[lastarg]==CONSTRUCTOR) /* case n=0 */
{ hd[e]=I; e=tl[e]=arg2; goto NEXTREDEX; }
hd[e]=hd[lastarg];
tl[e]=tl[lastarg];
while(tag[hd[e]]!=CONSTRUCTOR)
/* go back to head of arg3, copying spine */
{ hd[e]=ap(hd[hd[e]],tl[hd[e]]);
DOWNLEFT; }
hd[e]=arg2; /* replace k with f */
goto NEXTREDEX;
case MATCH: /* MATCH a f a => f
MATCH a f b => FAIL */
upleft;
arg1=lastarg=reduce(lastarg); /* ### */
/* note that MATCH evaluates arg1, usually needless, could have second
version - MATCHEQ, say */
getarg(arg2);
upleft;
lastarg=reduce(lastarg); /* ### */
hd[e]=I;
e=tl[e]=compare(arg1,lastarg)?FAIL:arg2;
goto NEXTREDEX;
case MATCHINT: /* same but 1st arg is integer literal */
getarg(arg1);
getarg(arg2);
upleft;
lastarg=reduce(lastarg); /* ### */
hd[e]=I;
e=tl[e]=(tag[lastarg]!=INT||bigcmp(arg1,lastarg))?FAIL:arg2;
/* note no coercion from INT to DOUBLE here */
goto NEXTREDEX;
case GENSEQ: /* GENSEQ (i,NIL) a => a:GENSEQ (i,NIL) (a+i)
GENSEQ (i,b) a => [], a>b=sign
=> a:GENSEQ (i,b) (a+i), otherwise
where
sign = 1, i>=0
= -1, otherwise */
GETARG(arg1);
UPLEFT;
if(tl[arg1]!=NIL&&
(tag[arg1]==AP?compare(lastarg,tl[arg1]):compare(tl[arg1],lastarg))>0)
hd[e]=I, e=tl[e]=NIL;
else hold=ap(hd[e],numplus(lastarg,hd[arg1])),
setcell(CONS,lastarg,hold);
goto DONE;
/* efficiency hack - tag of arg1 encodes sign of step */
case MAP: /* MAP f [] => []
MAP f (a:x) => f a : MAP f x */
getarg(arg1);
upleft;
lastarg=reduce(lastarg); /* ### */
if(lastarg==NIL)
hd[e]=I, e=tl[e]=NIL;
else hold=ap(hd[e],tl[lastarg]),
setcell(CONS,ap(arg1,hd[lastarg]),hold);
goto DONE;
case FLATMAP: /* funny version of map for compiling zf exps
FLATMAP f [] => []
FLATMAP f (a:x) => FLATMAP f x, f a=FAIL
=> f a ++ FLATMAP f x
(FLATMAP was formerly called MAP1) */
getarg(arg1);
getarg(arg2);
L1:arg2=reduce(arg2); /* ### */
if(arg2==NIL)
{ hd[e]=I;
e=tl[e]=NIL;
goto DONE; }
hold=reduce(hold=ap(arg1,hd[arg2]));
if(hold==FAIL||hold==NIL){ arg2=tl[arg2]; goto L1; }
tl[e]=ap(hd[e],tl[arg2]);
hd[e]=ap(APPEND,hold);
goto NEXTREDEX;
case FILTER: /* FILTER f [] => []
FILTER f (a:x) => a : FILTER f x, f a
=> FILTER f x, otherwise */
getarg(arg1);
upleft;
lastarg=reduce(lastarg); /* ### */
while(lastarg!=NIL&&reduce(ap(arg1,hd[lastarg]))==False) /* ### */
lastarg=reduce(tl[lastarg]); /* ### */
if(lastarg==NIL)
hd[e]=I, e=tl[e]=NIL;
else hold=ap(hd[e],tl[lastarg]),
setcell(CONS,hd[lastarg],hold);
goto DONE;
case LIST_LAST: /* LIST_LAST x => x!(#x-1) */
upleft;
if((lastarg=reduce(lastarg))==NIL)fn_error("last []"); /* ### */
while((tl[lastarg]=reduce(tl[lastarg]))!=NIL) /* ### */
lastarg=tl[lastarg];
hd[e]=I; e=tl[e]=hd[lastarg];
goto NEXTREDEX;
case LENGTH: /* takes length of a list */
upleft;
{ long long n=0; /* problem - may be followed by gc */
/* cannot make static because of ### below */
while((lastarg=reduce(lastarg))!=NIL) /* ### */
lastarg=tl[lastarg],n++;
simpl(sto_int(n)); }
goto DONE;
case DROP:
getarg(arg1);
upleft;
arg1=tl[hd[e]]=reduce(tl[hd[e]]); /* ### */
if(tag[arg1]!=INT)int_error("drop");
{ long long n=get_int(arg1);
while(n-- >0)
if((lastarg=reduce(lastarg))==NIL) /* ### */
{ simpl(NIL); goto DONE; }
else lastarg=tl[lastarg]; }
simpl(lastarg);
goto NEXTREDEX;
case SUBSCRIPT: /* SUBSCRIPT i x => x!i */
upleft;
upleft;
arg1=tl[hd[e]]=reduce(tl[hd[e]]); /* ### */
lastarg=reduce(lastarg); /* ### */
if(lastarg==NIL)subs_error();
{ long long indx;
if(tag[arg1]==ATOM)indx=arg1;/* small indexes represented directly */
else if(tag[arg1]==INT)indx=get_int(arg1);
else int_error("!");
/* problem, indx may be followed by gc
- cannot make static, because of ### below */
if(indx<0)subs_error();
while(indx)
{ lastarg= tl[lastarg]= reduce(tl[lastarg]); /* ### */
if(lastarg==NIL)subs_error();
indx--; }
hd[e]= I;
e=tl[e]=hd[lastarg]; /* could be eager in tl[e] */
goto NEXTREDEX; }
case FOLDL1: /* FOLDL1 op (a:x) => FOLDL op a x */
getarg(arg1);
upleft;
if((lastarg=reduce(lastarg))!=NIL) /* ### */
{ hd[e]=ap2(FOLDL,arg1,hd[lastarg]);
tl[e]=tl[lastarg];
goto NEXTREDEX; }
else fn_error("foldl1 applied to []");
case FOLDL: /* FOLDL op r [] => r
FOLDL op r (a:x) => FOLDL op (op r a)^ x
^ (FOLDL op) is made strict in 1st param */
getarg(arg1);
getarg(arg2);
upleft;
while((lastarg=reduce(lastarg))!=NIL) /* ### */
arg2=reduce(ap2(arg1,arg2,hd[lastarg])), /* ^ ### */
lastarg=tl[lastarg];
hd[e]=I, e=tl[e]=arg2;
goto NEXTREDEX;
case FOLDR: /* FOLDR op r [] => r
FOLDR op r (a:x) => op a (FOLDR op r x) */
getarg(arg1);
getarg(arg2);
upleft;
lastarg=reduce(lastarg); /* ### */
if(lastarg==NIL)
hd[e]=I, e=tl[e]=arg2;
else hold=ap(hd[e],tl[lastarg]),
hd[e]=ap(arg1,hd[lastarg]), tl[e]=hold;
goto NEXTREDEX;
L_READBIN:
case READBIN: /* READBIN streamptr => nextchar : READBIN streamptr
if end of file, READBIN file => NIL
READBIN does no UTF-8 conversion */
UPLEFT; /* gc insecurity - arg is not a heap object */
if(lastarg==0) /* special case created by $:- */
{ if(stdinuse=='-')stdin_error(':');
if(stdinuse)
{ hd[e]=I; e=tl[e]=NIL; goto DONE; }
stdinuse=':';
tl[e]=(word)stdin; }
hold= getc((FILE *)lastarg);
if(hold==EOF)
{ fclose((FILE *)lastarg);
hd[e]=I;
e=tl[e]= NIL;
goto DONE; }
setcell(CONS,hold,ap(READBIN,lastarg));
goto DONE;
L_READ:
case READ: /* READ streamptr => nextchar : READ streamptr
if end of file, READ file => NIL
does UTF-8 conversion where appropriate */
UPLEFT; /* gc insecurity - arg is not a heap object */
if(lastarg==0) /* special case created by $- */
{ if(stdinuse==':')stdin_error('-');
if(stdinuse)
{ hd[e]=I; e=tl[e]=NIL; goto DONE; }
stdinuse='-';
tl[e]=(word)stdin; }
hold=UTF8?sto_char(fgetwc((FILE *)lastarg)):getc((FILE *)lastarg);
if(hold==EOF)
{ fclose((FILE *)lastarg);
hd[e]=I;
e=tl[e]= NIL;
goto DONE; }
setcell(CONS,hold,ap(READ,lastarg));
goto DONE;
L_READVALS:
case READVALS: /* READVALS (t:fil) f => [], EOF from FILE *f
=> val : READVALS t f, otherwise
where val is obtained by parsing lines of
f, and taking next legal expr of type t */
GETARG(arg1);
upleft;
hold=parseline(hd[arg1],(FILE *)lastarg,tl[arg1]);
if(hold==EOF)
{ fclose((FILE *)lastarg);
hd[e]=I;
e=tl[e]= NIL;
goto DONE; }
arg2=ap(hd[e],lastarg);
setcell(CONS,hold,arg2);
goto DONE;
case BADCASE: /* BADCASE cons(oldn,here_info) => BOTTOM */
UPLEFT;
{ word subject= hd[lastarg];
/* either datapair(oldn,0) or 0 */
fprintf(stderr,"\nprogram error: missing case in definition");
if(subject) /* cannot do patterns - FIX LATER */
fprintf(stderr," of %s",(char *)hd[subject]);
putc('\n',stderr);
out_here(stderr,tl[lastarg],1);
/* if(nargs>1)
{ int i=2;
fprintf(stderr,"arg%s = ",nargs>2?"s":"");
while(i<=nargs)out(stderr,tl[stackp[-(i++)]]),putc(' ',stderr);
putc('\n',stderr); } // fix later */
}
outstats();
exit(1);
case GETARGS: /* GETARGS 0 => argv ||`$*' = command line args */
UPLEFT;
simpl(conv_args());
goto DONE;
case CONFERROR: /* CONFERROR error_info => BOTTOM */
/* if(nargs<1)fprintf(stderr,"\nimpossible event in reduce\n"),
exit(1); */
UPLEFT;
fprintf(stderr,"\nprogram error: lhs of definition doesn't match rhs");
/*fprintf(stderr," OF ");
out_formal1(stderr,hd[lastarg]); // omit - names may have been aliased */
putc('\n',stderr);
out_here(stderr,tl[lastarg],1);
outstats();
exit(1);
case ERROR: /* ERROR error_info => BOTTOM */
upleft;
if(errtrap)fprintf(stderr,"\n(repeated error)\n");
else { errtrap=1;
fprintf(stderr,"\nprogram error: ");
s_out=stderr;
print(lastarg); /* ### */
putc('\n',stderr); }
outstats();
exit(1);
case WAIT: /* WAIT pid => <exit_status of child process pid> */
UPLEFT;
{ word *w= &waiting; /* list of terminated pid's and their exit statuses */
while(*w!=NIL&&hd[*w]!=lastarg)w= &tl[tl[*w]];
if(*w!=NIL)hold=hd[tl[*w]],
*w=tl[tl[*w]]; /* remove entry */
else { int status;
while((hold=wait(&status))!=lastarg&&hold!= -1)
waiting=cons(hold,cons(WEXITSTATUS(status),waiting));
if(hold!= -1)hold=WEXITSTATUS(status); }}
simpl(stosmallint(hold));
goto DONE;
L_I:
/* case MONOP: (all strict monadic operators share this code) */
case I: /* we treat I as strict to avoid I-chains (MOD1) */
case SEQ:
case FORCE:
case HD:
case TL:
case BODY:
case LAST:
case EXEC:
case FILEMODE:
case FILESTAT:
case GETENV:
case INTEGER:
case NUMVAL:
case TAKE:
case STARTREAD:
case STARTREADBIN:
case NB_STARTREAD:
case COND:
case APPEND:
case AND:
case OR:
case NOT:
case NEG:
case CODE:
case DECODE:
case SHOWNUM:
case SHOWHEX:
case SHOWOCT:
case ARCTAN_FN: /* ...FN are strict functions of one numeric arg */
case EXP_FN:
case ENTIER_FN:
case LOG_FN:
case LOG10_FN:
case SIN_FN:
case COS_FN:
case SQRT_FN:
downright; /* subtask -- reduce arg */
goto NEXTREDEX;
case TRY: /* TRY f g x => TRY(f x)(g x) */
getarg(arg1);
getarg(arg2);
while(!abnormal(s))
{ UPLEFT;
hd[e]=ap(TRY,arg1=ap(arg1,lastarg));
arg2=tl[e]=ap(arg2,lastarg); }
DOWNLEFT;
/* DOWNLEFT; DOWNRIGHT; equivalent to:*/
hold=s,s=e,e=tl[e],tl[s]=hold,mktlptr(s); /* now be strict in arg1 */
goto NEXTREDEX;
case FAIL: /* FAIL x => FAIL */
while(!abnormal(s))hold=s,s=hd[s],hd[hold]=FAIL,tl[hold]=0;
goto DONE;
/* case DIOP: (all strict diadic operators share this code) */
case ZIP:
case STEP:
case EQ:
case NEQ:
case PLUS:
case MINUS:
case TIMES:
case INTDIV:
case FDIV:
case MOD:
case GRE:
case GR:
case POWER:
case SHOWSCALED:
case SHOWFLOAT:
case MERGE:
upleft;
downright; /* first subtask -- reduce arg2 */
goto NEXTREDEX;
case Ush: /* strict in three args */
case STEPUNTIL:
upleft;
upleft;
downright;
goto NEXTREDEX; /* first subtask -- reduce arg3 */
case Ush1: /* non-strict version of Ush */
/* Ush1 (k f1...fn) p stuff
=> "k"++' ':f1 x1 ...++' ':fn xn, p='\0'
=> "(k"++' ':f1 x1 ...++' ':fn xn++")", p='\1'
where xi = LAST(BODY^(n-i) stuff) */
getarg(arg1);
arg1=reduce(arg1); /* ### */
getarg(arg2);
arg2=reduce(arg2); /* ### */
getarg(arg3);
if(tag[arg1]==CONSTRUCTOR) /* don't parenthesise atom */
{ hd[e]=I;
if(suppressed(arg1))
e=tl[e]=str_conv("<unprintable>");
else e=tl[e]=str_conv(constr_name(arg1));
goto DONE; }
hold=arg2?cons(')',NIL):NIL;
while(tag[arg1]!=CONSTRUCTOR)
hold=cons(' ',ap2(APPEND,ap(tl[arg1],ap(LAST,arg3)),hold)),
arg1=hd[arg1],arg3=ap(BODY,arg3);
if(suppressed(arg1))
{ hd[e]=I; e=tl[e]=str_conv("<unprintable>"); goto DONE; }
hold=ap2(APPEND,str_conv(constr_name(arg1)),hold);
if(arg2)
{ setcell(CONS,'(',hold); goto DONE; }
else { hd[e]=I; e=tl[e]=hold; goto NEXTREDEX; }
case MKSTRICT: /* MKSTRICT k f x1 ... xk => f x1 ... xk, xk~=BOT */
GETARG(arg1);
getarg(arg2);
{ word i=arg1;
while(i--) { upleft; } }
lastarg=reduce(lastarg); /* ### */
while(--arg1) /* go back towards head, copying spine */
{ hd[e]=ap(hd[hd[e]],tl[hd[e]]);
DOWNLEFT;}
hd[e]=arg2; /* overwrite (MKSTRICT k f) with f */
goto NEXTREDEX;
case G_ERROR: /* G_ERROR f g toks = (g residue):[], fails(f toks)
= f toks, otherwise */
GETARG(arg1);
GETARG(arg2);
upleft;
hold=ap(arg1,lastarg);
hold=reduce(hold); /* ### */
if(!fails(hold))
{ hd[e]=I; e=tl[e]=hold; goto DONE; }
hold=g_residue(lastarg);
setcell(CONS,ap(arg2,hold),NIL);
goto DONE;
case G_ALT: /* G_ALT f g toks = f toks, !fails(f toks)
= g toks, otherwise */
GETARG(arg1);