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
| ref reduce (/*tval T, ref P*/) {
S = nil;
int state=BURED;
tval f, t, pat;
ref p, sub;
Push(S,asDTA(ALLDONE));
loop: //full-reduce is BURED
if (Dbg>=DCycles) fprintf(stdout, "%s ",prstates[state-1000]);
switch (state) { // T is subject term, V is result
case BURED: //T is term
if (isNREF(T)||T==0) {
V = T;
state = PopDTA(S);
goto loop;
}
Push(S,ref(T)->car);
Push(S,asDTA(BUDONECDR));
T=ref(T)->cdr;
goto loop;
case BUDONECDR: //V is cdr, tos is car-to-do
T = Pop(S);
Push(S,V);
Push(S,asDTA(BUDONEBOTH));
state = BURED;
goto loop;
case BUDONEBOTH: //(V is car), tos is cdr
if (isREF(V)) {
T = Pop(S);
V = idx(new(V,T));
state = PopDTA(S);
goto loop;
}
f = V;
T = Pop(S);
state = TOPRED;
goto loop;
case TOPRED: //(f is fun,T = args)
if (Dbg>=DStepDump) pval(f,1);
p = P;
t = T;
state = FORRULES;
if (Dbg>=DSteps) Drulei=0;
goto loop;
case FORRULES: //(f,t,p, T, P)
if (p==nil) { // eor
state = BUILD;
goto loop;
}
if (Dbg>=DSteps) Drulei++;
if (ref(ref(p->car)->car)->car != f) {
p=ref(p->cdr);
goto loop;
}
sub = nil;
Push(S,asDTA(MATCHDONE));
state = MATCH;
pat = ref(ref(p->car)->car)->cdr;
goto loop;
case MATCH: //(t,pat,sub,p, T, P)
if (isREF(pat)&&pat!=0) { // compound
if (isNREF(t)) goto fail;
Push(S,ref(t)->cdr);
Push(S,ref(pat)->cdr);
Push(S,asDTA(MATCHDONECAR));
pat=ref(pat)->car;
t=ref(t)->car;
goto loop;
}
if (isVAR(pat)) {
sub = new(idx(new(pat,t)),idx(sub));
state = PopDTA(S);
goto loop;
}
if (pat==t) {
state = PopDTA(S);
goto loop;
}
//fallthrough intended
fail:
p=ref(p->cdr);
t = T;
do {state = PopDTA(S);
} while(state!=MATCHDONE);
state = FORRULES;
goto loop;
case MATCHDONECAR://(sub,p) tos is pat.cdr, 2nd is trm.cdr
pat = Pop(S);
t = Pop(S);
state = MATCH;
goto loop;
case MATCHDONE://(sub,p)
pat = ref(p->car)->cdr; //rhs
state = INST;
if (Dbg>=DSteps) Drewrcnt++;
if (Dbg>=DSteps) printf("Rule %d, Steps %d\n",Drulei,Drewrcnt);
goto loop;
case INST: //(pat,sub)
if (isREF(pat)&&pat!=0) {
Push(S,ref(pat)->cdr);
pat=ref(pat)->car;
Push(S,asDTA(INSTDONECAR));
goto loop;
}
if (isVAR(pat)) {// use p as tmp in sub
p = sub;
while (ref(p->car)->car!=pat) {
p = ref(p->cdr);
}
V = ref(p->car)->cdr;
state = PopDTA(S);
goto loop;
}
V = pat;
state = PopDTA(S);
goto loop;
case INSTDONECAR: // V is car
pat = Pop(S);
Push(S,V);
Push(S,asDTA(INSTDONEBOTH));
state = INST;
goto loop;
case INSTDONEBOTH: // V is cdr, ToS = car
t = Pop(S);
if (isFUN(t)) {
f = t;
T = V;
Push(S,pat);
Push(S,idx(sub));
Push(S,asDTA(INSTCONT));
state = TOPRED;
goto loop;
}
V = idx(new(t,V));
state = PopDTA(S);
goto loop;
case INSTCONT: {// V is car
sub = PopRef(S);
pat = Pop(S);
state = PopDTA(S);
goto loop;
}
case BUILD:
V = idx(new(f,T));
state = PopDTA(S);
goto loop;
case ALLDONE: //done
//res = Pop(S); if (S!=nil) {
error("Stack should be empty!");
}
return ref(V);
default: error("Bad state!");
}
}
|