-
Notifications
You must be signed in to change notification settings - Fork 26
/
closure.c
197 lines (168 loc) · 4.25 KB
/
closure.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
/* closure.c -- operations on bindings, closures, lambdas, and thunks ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "gc.h"
/*
* Closure garbage collection support
*/
DefineTag(Closure, static);
extern Closure *mkclosure(Tree *tree, Binding *binding) {
gcdisable();
Ref(Closure *, closure, gcnew(Closure));
closure->tree = tree;
closure->binding = binding;
gcenable();
RefReturn(closure);
}
static void *ClosureCopy(void *op) {
void *np = gcnew(Closure);
memcpy(np, op, sizeof (Closure));
return np;
}
static size_t ClosureScan(void *p) {
Closure *closure = p;
closure->tree = forward(closure->tree);
closure->binding = forward(closure->binding);
return sizeof (Closure);
}
/* revtree -- destructively reverse a list stored in a tree */
static Tree *revtree(Tree *tree) {
Tree *prev, *next;
if (tree == NULL)
return NULL;
prev = NULL;
do {
assert(tree->kind == nList);
next = tree->u[1].p;
tree->u[1].p = prev;
prev = tree;
} while ((tree = next) != NULL);
return prev;
}
typedef struct Chain Chain;
struct Chain {
Closure *closure;
Chain *next;
};
static Chain *chain = NULL;
static Binding *extract(Tree *tree, Binding *bindings) {
assert(gcisblocked());
for (; tree != NULL; tree = tree->u[1].p) {
Tree *defn = tree->u[0].p;
assert(tree->kind == nList);
if (defn != NULL) {
List *list = NULL;
Tree *name = defn->u[0].p;
assert(name->kind == nWord || name->kind == nQword);
defn = revtree(defn->u[1].p);
for (; defn != NULL; defn = defn->u[1].p) {
Term *term;
Tree *word = defn->u[0].p;
NodeKind k = word->kind;
assert(defn->kind == nList);
assert(k == nWord || k == nQword || k == nPrim);
if (k == nPrim) {
char *prim = word->u[0].s;
if (streq(prim, "nestedbinding")) {
int i, count;
Chain *cp;
if (
(defn = defn->u[1].p) == NULL
|| defn->u[0].p->kind != nWord
|| (count = (atoi(defn->u[0].p->u[0].s))) < 0
) {
fail("$&parse", "improper use of $&nestedbinding");
NOTREACHED;
}
for (cp = chain, i = 0;; cp = cp->next, i++) {
if (cp == NULL) {
fail("$&parse", "bad count in $&nestedbinding: %d", count);
NOTREACHED;
}
if (i == count)
break;
}
term = mkterm(NULL, cp->closure);
} else {
fail("$&parse", "bad unquoted primitive in %%closure: $&%s", prim);
NOTREACHED;
}
} else
term = mkstr(word->u[0].s);
list = mklist(term, list);
}
bindings = mkbinding(name->u[0].s, list, bindings);
}
}
return bindings;
}
extern Closure *extractbindings(Tree *tree0) {
Chain me;
Tree *volatile tree = tree0;
Binding *volatile bindings = NULL;
gcdisable();
if (tree->kind == nList && tree->u[1].p == NULL)
tree = tree->u[0].p;
me.closure = mkclosure(NULL, NULL);
me.next = chain;
chain = &me;
ExceptionHandler
while (tree->kind == nClosure) {
bindings = extract(tree->u[0].p, bindings);
tree = tree->u[1].p;
if (tree == NULL)
fail("$&parse", "null body in %%closure");
if (tree->kind == nList && tree->u[1].p == NULL)
tree = tree->u[0].p;
}
CatchException (e)
chain = chain->next;
throw(e);
EndExceptionHandler
chain = chain->next;
Ref(Closure *, result, me.closure);
result->tree = tree;
result->binding = bindings;
gcenable();
RefReturn(result);
}
/*
* Binding garbage collection support
*/
DefineTag(Binding, static);
extern Binding *mkbinding(char *name, List *defn, Binding *next) {
assert(next == NULL || next->name != NULL);
validatevar(name);
gcdisable();
Ref(Binding *, binding, gcnew(Binding));
binding->name = name;
binding->defn = defn;
binding->next = next;
gcenable();
RefReturn(binding);
}
extern Binding *reversebindings(Binding *binding) {
if (binding == NULL)
return NULL;
else {
Binding *prev, *next;
prev = NULL;
do {
next = binding->next;
binding->next = prev;
prev = binding;
} while ((binding = next) != NULL);
return prev;
}
}
static void *BindingCopy(void *op) {
void *np = gcnew(Binding);
memcpy(np, op, sizeof (Binding));
return np;
}
static size_t BindingScan(void *p) {
Binding *binding = p;
binding->name = forward(binding->name);
binding->defn = forward(binding->defn);
binding->next = forward(binding->next);
return sizeof (Binding);
}