-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleruntime.c
1230 lines (1173 loc) · 29.4 KB
/
simpleruntime.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "number.c"
#include "intlist.c"
typedef struct VContext VContext; // map<int,Value*>
typedef struct Continuation Continuation;
typedef struct Value Value;
typedef enum{
EXP_NUM,
EXP_ABS,
EXP_NUMFUNC,
EXP_NUMFUNC1,
EXP_INTLIST,
EXP_SYSCALL, //open close read write getarg system
EXP_WHNF, // weak head normal form
EXP_REF,
EXP_APPLY
} ExpType;
typedef enum{
FUNC_ADD,
FUNC_SUB,
FUNC_MUL,
FUNC_DIV,
FUNC_MOD,
FUNC_GE,
FUNC_LE,
FUNC_NGE,
FUNC_NLE,
FUNC_EQ,
FUNC_NEQ,
FUNC_BINARY, //binary operations
FUNC_TOFLOAT,
FUNC_TOINT
} FuncType;
typedef enum{
SYS_EXIT,
SYS_OPEN,
SYS_CLOSE,
SYS_READ,
SYS_WRITE,
SYS_PEEK,
SYS_GETARG,
SYS_SYSTEM
} SyscallType;
typedef enum{
CONT_EVAL,
CONT_APPLY,
} ContType;
typedef enum{
VALUE_RESOLVED,
VALUE_EXCEPTION,
VALUE_RUNNING
} ValueType;
typedef union{
int int_val;
IntList * intlist_val;
} SyscallArg;
typedef struct VExp{
ExpType type;
FuncType funcType;
SyscallType syscallType;
union{
Number num_val; //EXP_NUM
struct VExp * abs_val; //EXP_ABS
int ref_val; //EXP_REF
struct{ //EXP_APPLY
struct VExp * ap_f;
struct VExp * ap_x;
};
struct{ //EXP_NUMFUNC
FuncType func_type;
};
struct{ //EXP_NUMFUNC1
FuncType func1_type;
Number func1_opa;
};
struct{ //EXP_INTLIST
IntList * intlist_val;
};
struct{ //EXP_SYSCALL
SyscallType sys_type;
int sys_nbind;
SyscallArg sys_arg1;
int sys_arg2;
Value * sys_cont;
};
struct VExp * pool_next;
};
int refcount;
} VExp;
const int syscall_arginfo[8][4]={
{1,0,-1,-1}, // SYS_EXIT 0 = int
{3,1, 0, 2}, //SYS_OPEN, 1 = intlist 2 = cont
{2,0, 2,-1}, //SYS_CLOSE
{2,0, 2,-1}, //SYS_READ,
{3,0, 0, 2}, //SYS_WRITE,
{2,0, 2,-1}, //SYS_PEEK
{1,2,-1,-1}, //SYS_GETARG,
{2,1, 2,-1} //SYS_SYSTEM
};
struct Value{
ValueType type;
union{
struct{ //VALUE_RESOLVED
VExp * exp;
VContext * context;
};
const char * message; //VALUE_EXCEPTION
struct{ //VALUE_RUNNING
VExp * r_exp;
VContext * r_context;
Continuation * r_cont;
};
Value * pool_next;
};
int refcount;
};
struct Continuation{
ContType type;
union{
struct{ //CONT_EVAL
VExp * eval_exp;
VContext * eval_context;
};
struct{ //CONT_APPLY
Value * ap_x; // unresolved
};
};
Continuation * cont;
};
typedef struct ValueStack{
union{
struct{
Value * val;
struct ValueStack * next;
};
struct ValueStack * pool_next;
};
}ValueStack;
struct VContext{
Value* val;
VContext * prev;
int refcount;
};
//===========================================================
Value * releaseValue(Value *);
VExp * releaseVExp(VExp *);
//===========================================================
VContext * allocateVContext(VContext * p){
static VContext * pool=NULL;
if (!p){
if (!pool){
VContext * p=(VContext *)malloc(sizeof(VContext)*1000);
for (int i=0;i<1000;i++){
p[i].prev=pool;
pool=p+i;
}
}
p=pool;
p->refcount=1;
pool=pool->prev;
return p;
}else{
p->prev=pool;
pool=p;
return NULL;
}
}
VContext * retainVContext(VContext * p){ // p: stolen returns: new
if (p){
p->refcount++;
}
return p;
}
VContext * releaseVContext(VContext * p){ // p: consumed
if (p){
p->refcount--;
if (p->refcount==0){
if (p->prev){
releaseVContext(p);
}
allocateVContext(p);
}
}
return NULL;
}
Value * lookUpRef(int ref,VContext * context){ // context: stolen returns: stolen
for (int i=0;i<ref;++i){
context = context->prev;
}
return context->val;
}
VContext * insertRef(Value * v,VContext * context){ // v: consumed context: consumed returns: new
VContext * ncontext=allocateVContext(NULL);
ncontext->val=v;
ncontext->prev=context;
return ncontext;
}
//===========================================================
Continuation * allocateContinuation(Continuation * p){
static Continuation * pool=NULL;
if (!p){
if (!pool){
Continuation * p=(Continuation *)malloc(sizeof(Continuation)*1000);
for (int i=0;i<1000;i++){
p[i].cont=pool;
pool=p+i;
}
}
p=pool;
pool=pool->cont;
return p;
}else{
p->cont=pool;
pool=p;
return NULL;
}
}
Continuation * releaseContinuationSingle(Continuation * p){
if (p){
if (p->type==CONT_EVAL){
releaseVExp(p->eval_exp);
releaseVContext(p->eval_context);
}else if (p->type==CONT_APPLY){
releaseValue(p->ap_x);
}
allocateContinuation(p);
}
return NULL;
}
Continuation * releaseContinuation(Continuation * p){
while (p){
Continuation * cont = p->cont;
releaseContinuationSingle(p);
p=cont;
}
return NULL;
}
//===========================================================
const char * funcNames(FuncType ft){
static const char * names[]={
"+",
"-",
"*",
"/",
"%",
">",
"<",
"<=",
">=",
"==",
"/=",
"",
"toFloat",
"toInt",
};
return names[ft];
}
const char * syscallNames(SyscallType st){
static const char * names[]={
"exit",
"openCmd",
"close",
"getCharF",
"putCharF",
"peekCharF",
"getArg",
"systemCmd"
};
return names[st];
}
void displayNumber(FILE * fout,Number a){
if (a.type==NUM_INT){
fprintf(fout,"%d",a.int_val);
}else{
fprintf(fout,"%f",a.double_val);
}
}
void displayExpRecursive(FILE * fout,VExp * e,int level){ // e : stolen
if (e->type==EXP_NUM) {
displayNumber(fout,e->num_val);
}else if (e->type==EXP_ABS){
fprintf(fout,"(\\v%d ",level);
displayExpRecursive(fout,e->abs_val,level+1);
fprintf(fout,")");
}else if (e->type==EXP_REF){
fprintf(fout,"v%d",level-1-e->ref_val);
}else if (e->type==EXP_APPLY){
fprintf(fout,"(");
displayExpRecursive(fout,e->ap_f,level);
fprintf(fout," ");
displayExpRecursive(fout,e->ap_x,level);
fprintf(fout,")");
}else if (e->type==EXP_NUMFUNC){
fprintf(fout,"%s",funcNames(e->func_type));
}else if (e->type==EXP_NUMFUNC1){
fprintf(fout,"(%s ",funcNames(e->func1_type));
displayNumber(fout,e->func1_opa);
fprintf(fout,")");
}else if (e->type==EXP_INTLIST){
fprintf(fout,"(makeIntList");
for (IntList * p=e->intlist_val;p;p=p->next){
fprintf(fout," %d",p->val);
}
fprintf(fout,")");
}else if (e->type==EXP_SYSCALL){
if (e->sys_nbind==0){
fprintf(fout,"%s",syscallNames(e->sys_type));
}else{
fprintf(fout,"(%s",syscallNames(e->sys_type));
int required=syscall_arginfo[e->sys_type][0];
if (e->sys_nbind>=1 && required>1){
int atype=syscall_arginfo[e->sys_type][1];
if (atype==0){
fprintf(fout," %d",e->sys_arg1.int_val);
}else{
fprintf(fout," (makeIntList");
for (IntList * p=e->sys_arg1.intlist_val;p;p=p->next){
fprintf(fout," %d",p->val);
}
fprintf(fout,")");
}
}
if (e->sys_nbind>=2 && required>2){
fprintf(fout," %d",e->sys_arg2);
}
if (e->sys_nbind>=1 && syscall_arginfo[e->sys_type][e->sys_nbind]==2){
fprintf(fout," ...");
}
fprintf(fout,")");
}
}
}
void displayExp(FILE * fout,VExp * e){ // e: stolen
displayExpRecursive(fout,e,0);
}
VExp * allocateVExp(VExp * p){
static VExp * pool=NULL;
if (!p){
if (!pool){
VExp * p=(VExp *)malloc(sizeof(VExp)*1000);
for (int i=0;i<1000;i++){
p[i].pool_next=pool;
pool=p+i;
}
}
p=pool;
p->refcount=1;
pool=pool->pool_next;
return p;
}else{
p->pool_next=pool;
pool=p;
return NULL;
}
}
VExp * retainVExp(VExp * p){ // p: stolen returns: new
if (p){
p->refcount++;
}
return p;
}
VExp * releaseVExp(VExp * p){ // p: consumed returns: NULL
if (p){
p->refcount--;
if (p->refcount==0){
if (p->type==EXP_ABS){
releaseVExp(p->abs_val);
}else if (p->type==EXP_APPLY){
releaseVExp(p->ap_f);
releaseVExp(p->ap_x);
}else if (p->type==EXP_INTLIST){
releaseIntList(p->intlist_val);
}else if (p->type==EXP_SYSCALL){
if (p->sys_nbind>=1 && syscall_arginfo[p->sys_type][1]==1){
releaseIntList(p->sys_arg1.intlist_val);
}
if (p->sys_nbind==syscall_arginfo[p->sys_type][0] && syscall_arginfo[p->sys_type][p->sys_nbind]==2){
releaseValue(p->sys_cont);
}
}
allocateVExp(p);
}
}
return NULL;
}
//===========================================================
Value * allocateValue(Value * p){
static Value * pool=NULL;
if (!p){
if (!pool){
Value * p=(Value *)malloc(sizeof(Value)*1000);
for (int i=0;i<1000;i++){
p[i].pool_next=pool;
pool=p+i;
}
}
p=pool;
p->refcount=1;
pool=pool->pool_next;
return p;
}else{
p->pool_next=pool;
pool=p;
return NULL;
}
}
Value * newResolvedValue(VExp * exp, VContext * context){ // exp : consumed context : consumed
Value * p = allocateValue(NULL);
p->type=VALUE_RESOLVED;
p->exp=exp;
p->context=context;
return p;
}
Value * newRunningValue(VExp * r_exp,VContext * r_context){ // r_exp : consumed r_context : consumed
Value * p = allocateValue(NULL);
p->type=VALUE_RUNNING;
p->r_exp=r_exp;
p->r_context=r_context;
p->r_cont=NULL;
return p;
}
Value * releaseValue(Value * p){ // p: consumed
if (p){
p->refcount--;
if (p->refcount==0){
if (p->type==VALUE_RESOLVED){
releaseVExp(p->exp);
releaseVContext(p->context);
} else if (p->type==VALUE_RUNNING){
releaseVExp(p->r_exp);
releaseVContext(p->r_context);
releaseContinuation(p->r_cont);
} else if (p->type==VALUE_EXCEPTION){
}
allocateValue(p);
}
}
return NULL;
}
Value * retainValue(Value * p){ // p: consumed returns: new
if (p){
p->refcount++;
}
return p;
}
//modifiers for Running Value
void pushEvalContinuation(Value * v,VExp * exp,VContext * context){ // v : stolen exp : consumed context : consumed
Continuation * p = allocateContinuation(NULL);
p->type=CONT_EVAL;
p->eval_exp=exp;
p->eval_context=context;
p->cont=v->r_cont;
v->r_cont=p;
}
void pushApplyContinuation(Value * v,Value * ap_x){ // v: stolen ap_x : consumed
Continuation * p = allocateContinuation(NULL);
p->type=CONT_APPLY;
p->ap_x=ap_x;
p->cont=v->r_cont;
v->r_cont=p;
}
void popContinuation(Value * v){ // v : stolen
Continuation * r=v->r_cont->cont;
releaseContinuationSingle(v->r_cont);
v->r_cont=r;
}
void clearNullContinuation(Value * v){ // v : stolen
VExp * exp=v->r_exp;
VContext * context=v->r_context;
// v->r_cont must be NULL
v->type=VALUE_RESOLVED;
v->exp=exp;
v->context=context;
}
void setRunningValue(Value * v,VExp * exp,VContext * context){ // v : stolen exp : consumed context : consumed
releaseVExp(v->r_exp);
v->r_exp=exp;
releaseVContext(v->r_context);
v->r_context=context;
}
void setExceptionValue(Value * v,const char * message){ // v : stolen
v->r_exp=releaseVExp(v->r_exp);
v->r_context=releaseVContext(v->r_context);
v->r_cont=releaseContinuation(v->r_cont);
v->type=VALUE_EXCEPTION;
v->message=message;
}
//===========================================================
ValueStack * allocateValueStack(ValueStack * p){
static ValueStack * pool=NULL;
if (!p){
if (!pool){
ValueStack * p=(ValueStack *)malloc(sizeof(ValueStack)*1000);
for (int i=0;i<1000;i++){
p[i].pool_next=pool;
pool=p+i;
}
}
p=pool;
pool=pool->pool_next;
return p;
}else{
p->pool_next=pool;
pool=p;
return NULL;
}
}
ValueStack * pushValueStack(Value * v,ValueStack * r){ // v : stolen
ValueStack * p = allocateValueStack(NULL);
p->next=r;
p->val=v;
return p;
}
ValueStack * popValueStack(ValueStack * p){
ValueStack * r=p->next;
allocateValueStack(p);
return r;
}
VExp * newVExpNum(Number num_val){ //returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_NUM;
p->num_val=num_val;
return p;
}
VExp * newVExpAbs(VExp * abs_val){ //abs_val: consumed returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_ABS;
p->abs_val=abs_val;
return p;
}
VExp * newVExpRef(int ref_val){ //returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_REF;
p->ref_val=ref_val;
return p;
}
VExp * newVExpApply(VExp * ap_f,VExp * ap_x){ // ap_f: consumed ap_x: consumed returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_APPLY;
p->ap_f=ap_f;
p->ap_x=ap_x;
return p;
}
VExp * newVExpNumfunc(FuncType func_type){ //returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_NUMFUNC;
p->func_type=func_type;
return p;
}
VExp * newVExpNumfunc1(FuncType func1_type,Number func1_opa){ //returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_NUMFUNC1;
p->func1_type=func1_type;
p->func1_opa=func1_opa;
return p;
}
VExp * newVExpIntList(IntList * intlist_val){ //intlist_val: consumed returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_INTLIST;
p->intlist_val=intlist_val;
return p;
}
VExp * newVExpSyscall0(SyscallType sys_type){//returns: new
VExp * p=allocateVExp(NULL);
p->type=EXP_SYSCALL;
p->sys_type=sys_type;
p->sys_nbind=0;
return p;
}
VExp * appendSyscallArg(VExp * exp,Value * x,const char ** error_message,Value ** to_resolve){ // exp: stolen x: stolen returns: new to_resolve[0]: stolen
error_message[0]=NULL;
to_resolve[0]=NULL;
int nrequired=syscall_arginfo[exp->sys_type][0];
if (nrequired<=exp->sys_nbind){
error_message[0]="too many arguments to syscall";
return NULL;
}else{
int argid=exp->sys_nbind;
int argtype=syscall_arginfo[exp->sys_type][1+argid];
if (argtype!=2){
if (x->type!=VALUE_RESOLVED && x->type!=VALUE_EXCEPTION){
to_resolve[0]=x;
return NULL;
}
}
if (argtype==0){
if (x->exp->type!=EXP_NUM || x->exp->num_val.type!=NUM_INT){
error_message[0]="cannot apply non-integer value to syscall";
return NULL;
}
}else if (argtype==1){
if (x->exp->type!=EXP_INTLIST){
error_message[0]="cannot apply non-intlist value to syscall";
return NULL;
}
}//else argtype=2
VExp * nexp=allocateVExp(NULL);
nexp->type=EXP_SYSCALL;
nexp->sys_type=exp->sys_type;
nexp->sys_nbind=exp->sys_nbind+1;
if (exp->sys_nbind>0){
int atype=syscall_arginfo[exp->sys_type][1+0];
if (atype==0){
nexp->sys_arg1.int_val=exp->sys_arg1.int_val;
}else if (atype==1){
nexp->sys_arg1.intlist_val=retainIntList(exp->sys_arg1.intlist_val);
}
}
if (exp->sys_nbind>1){
int atype=syscall_arginfo[exp->sys_type][1+1];
if (atype==0){
nexp->sys_arg2=exp->sys_arg2;
}
}
if (argtype==2){
nexp->sys_cont=retainValue(x);
}else{
if (exp->sys_nbind==0){
if (argtype==0){
nexp->sys_arg1.int_val=x->exp->num_val.int_val;
}else{ //argtype==1
nexp->sys_arg1.intlist_val=retainIntList(x->exp->intlist_val);
}
}else if (exp->sys_nbind==1){
if (argtype==0){
nexp->sys_arg2=x->exp->num_val.int_val;
}
}
}
return nexp;
}
}
VExp * constructBoolExp(bool v){ //returns : new
static VExp * p_true=NULL,*p_false=NULL;
if (v){
if (!p_true){
p_true=newVExpAbs(
newVExpAbs(
newVExpRef(1)
)
);
}
return retainVExp(p_true);
}else{
if (!p_false){
p_false=newVExpAbs(
newVExpAbs(
newVExpRef(0)
)
);
}
return retainVExp(p_false);
}
}
void incCounter(){
static long count=0,unit=1<<18;
count++;
if (!(count&(unit-1))){
fprintf(stderr,"count=%ld\n",count);
if ((unit<<4) <=count){
unit<<=1;
}
}
}
Value * resolveValue(Value * v){ // v : stolen returns: stolen
//hit count 146800640
//v->type must be VALUE_RUNNING
Continuation * cont=v->r_cont;
if (!cont){
//hit count 9437184
clearNullContinuation(v);
}else if (cont->type==CONT_EVAL){
//hit count 83886080
//v->r_exp and v->r_context should be NULL
VExp * exp=cont->eval_exp; // stolen
VContext * context=cont->eval_context; //stolen
if (exp->type<EXP_WHNF){
//hit count 2359296
setRunningValue(v,
retainVExp(exp),
retainVContext(context)
);
popContinuation(v);
}if (exp->type==EXP_REF){
//hit count 33554432
int ref=exp->ref_val;
Value * ref_val=lookUpRef(ref,context); // stolen
if (ref_val->type==VALUE_RESOLVED){
setRunningValue(v,
retainVExp(ref_val->exp),
retainVContext(ref_val->context)
);
popContinuation(v);
}else if(ref_val->type==VALUE_EXCEPTION){
setExceptionValue(v,ref_val->message);
}else{
//hit count 6815744
return ref_val;
}
}else if (exp->type==EXP_APPLY){
//hit count 46137344
VExp * f=retainVExp(exp->ap_f); //new
VContext * fcontext=retainVContext(context);
VExp * x=exp->ap_x; //stolen
VContext * xcontext=context;
Value * nvx;
if (x->type < EXP_WHNF){
//hit count 6815744
nvx = newResolvedValue(retainVExp(x),retainVContext(xcontext));
}else if(x->type == EXP_REF){
//hit count 29360128
nvx=retainValue(lookUpRef(x->ref_val,xcontext));
}else{
//hit count 10485760
nvx=newRunningValue(NULL,NULL);
pushEvalContinuation(nvx,
retainVExp(x),
retainVContext(xcontext)
);
}
setRunningValue(v,NULL,NULL);
popContinuation(v);
pushApplyContinuation(v,nvx);
pushEvalContinuation(v,f,fcontext);
}
}else if (cont->type==CONT_APPLY){
//hitcount 50331648
VExp * exp=v->r_exp; //stolen
VContext * context=v->r_context; //stolen
Value * x = cont->ap_x; //stolen
if (exp->type==EXP_NUM){
setExceptionValue(v,
"cannot use numeric value as function"
);
}else if (exp->type==EXP_ABS){
//hit count 37748736
VContext * ncontext=insertRef(
retainValue(x),
retainVContext(context)
); //new
VExp * nexp=retainVExp(exp->abs_val);
if (nexp->type < EXP_WHNF){
setRunningValue(v,nexp,ncontext);
popContinuation(v);
}else{
setRunningValue(v,NULL,NULL);
popContinuation(v);
pushEvalContinuation(v,nexp,ncontext);
}
}else if (exp->type==EXP_NUMFUNC){
//hit count 3932160
if (x->type!=VALUE_RESOLVED && x->type!=VALUE_EXCEPTION){
return x;
}
if (x->type==VALUE_EXCEPTION){
setExceptionValue(v,x->message);
}else if (x->exp->type!=EXP_NUM){
setExceptionValue(v,
"cannot apply built-in function on non-numeric value"
);
}else{
VExp * nexp=NULL;
if (exp->func_type<FUNC_BINARY){
nexp=newVExpNumfunc1(
exp->func_type,
x->exp->num_val
);
}else if (exp->func_type==FUNC_TOINT){
if (x->exp->num_val.type==NUM_INT){
nexp=retainVExp(x->exp);
}else{ // must be double
nexp=newVExpNum(
intNumber(floor(x->exp->num_val.double_val))
);
}
}else if (exp->func_type==FUNC_TOFLOAT){
if (x->exp->num_val.type==NUM_DOUBLE){
nexp=retainVExp(x->exp);
}else{ // must be int
nexp=newVExpNum(
doubleNumber((double)(x->exp->num_val.int_val))
);
}
} // cannot be else
//nexp : new
setRunningValue(v,
nexp,
NULL
);
popContinuation(v);
}
}else if (exp->type==EXP_NUMFUNC1){
//hit count 4718592
if (x->type!=VALUE_RESOLVED && x->type!=VALUE_EXCEPTION){
return x;
}
if (x->type==VALUE_EXCEPTION){
setExceptionValue(v,x->message);
}else if (x->exp->type!=EXP_NUM){
setExceptionValue(v,
"cannot apply built-in function on non-numeric value"
);
}else{
const char * exception_message=NULL;
Number result;
bool is_bool=false,result_bool;
if (exp->func1_type==FUNC_ADD){
result=addNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_SUB){
result=subNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_MUL){
result=mulNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_DIV){
result=divNumber(exp->func1_opa,x->exp->num_val,&exception_message);
}else if (exp->func1_type==FUNC_MOD){
result=modNumber(exp->func1_opa,x->exp->num_val,&exception_message);
}else{
is_bool=true;
if (exp->func1_type==FUNC_LE){
result_bool=leNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_GE){
result_bool=geNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_NLE){
result_bool=nleNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_NGE){
result_bool=ngeNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_EQ){
result_bool=eqNumber(exp->func1_opa,x->exp->num_val);
}else if (exp->func1_type==FUNC_NEQ){
result_bool=neqNumber(exp->func1_opa,x->exp->num_val);
}
}
if (exception_message!=NULL){
setExceptionValue(v,exception_message);
}else{
VExp * nexp=NULL;
if (is_bool){
nexp=constructBoolExp(result_bool);
}else{
nexp=newVExpNum(
result
);
}
//nexp : new
setRunningValue(v,
nexp,
NULL
);
popContinuation(v);
}
}
}else if (exp->type==EXP_INTLIST){
if (x->type!=VALUE_RESOLVED && x->type!=VALUE_EXCEPTION){
return x;
}
if (x->type==VALUE_EXCEPTION){
setExceptionValue(v,x->message);
}else if (x->exp->type!=EXP_NUM){
setExceptionValue(v,
"cannot apply built-in function on non-numeric value"
);
}else if (x->exp->num_val.type!=NUM_INT){
setExceptionValue(v,
"cannot append non-integer number to IntList"
);
}else{
VExp * nexp=newVExpIntList(
newIntList(
x->exp->num_val.int_val,
retainIntList(exp->intlist_val)
)
);
//printf("append %d %p -> %p\n",x->exp->num_val.int_val,exp->intlist_val,nexp->intlist_val);
//int cnt=0;
/*for (IntList *z=nexp->intlist_val;z;z=z->next){
printf("z %d %p\n",z->val,z->next);
cnt++;
if (cnt>=10){
puts("...");
break;
}
}*/
setRunningValue(v,nexp,NULL);
popContinuation(v);
}
}else if (exp->type==EXP_SYSCALL){
Value * to_resolve;
const char * error_message;
VExp * nexp=appendSyscallArg(exp,x,&error_message,&to_resolve); // new
if (!nexp){
if (error_message){
setExceptionValue(v,error_message);
}else if (to_resolve){
return to_resolve;
}
}else{
setRunningValue(v,nexp,NULL);
popContinuation(v);
}
}// else impossible
}
return NULL;
}
void resolveAllValue(Value * v){ // v : stolen
ValueStack * head=pushValueStack(v,NULL);
//head stores a list of stolen Value*
while (head){
if (head->val->type!=VALUE_RESOLVED && head->val->type!=VALUE_EXCEPTION){
Value * depends=resolveValue(head->val);
if (depends!=NULL && depends!=head->val){
head=pushValueStack(depends,head);
}
}else{
head=popValueStack(head);
}
}
}
int executeValue(Value * v,int argc,char ** args){ // v: consumed
int cur_argi=1;
char * cur_arg=NULL;
if (argc>1){
cur_arg=args[cur_argi];
}
FILE ** files;
int files_buflen;
files=(FILE**)malloc(sizeof(FILE*)*2);
files[0]=stdin;
files[1]=stdout;
files_buflen=2;
int exitcode=0;
while (true){
if (v->type!=VALUE_RESOLVED && v->type!=VALUE_EXCEPTION){
resolveAllValue(v);
}
if (v->type==VALUE_EXCEPTION){
printf("exception: %s\n",v->message);
exitcode=1;
break;
}
if (v->exp->type!=EXP_SYSCALL){
displayExp(stdout,v->exp);
puts("");
exitcode=0;
break;
}
int nrequired=syscall_arginfo[v->exp->sys_type][0];
if (nrequired>v->exp->sys_nbind){
printf("exception: insufficient arguments to syscall\n");
exitcode=1;