-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheme.c
287 lines (269 loc) · 7.33 KB
/
scheme.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
#include "scheme.h"
#include <stdio.h>
/* Instantiate a closure */
SCM SCM_close (SCM (*Cfunction)(void),
long arity, unsigned long size, ...) {
/* sizeof(SCM_unwrapped_closure) gives enough room to put the
* header, function pointer, arity, and one field. Each closure
* struct has its own set of free variable fields, so we need to
* make space for those as well (minus the one already there). See
* SCM_DefineClosure in scheme.h */
SCMref result = (SCMref) malloc(sizeof(struct SCM_unwrapped_closure) +
(size - 1)*sizeof(SCM));
unsigned long i;
va_list args;
if (result == (SCMref)NULL) SCM_error(SCM_ERR_CANT_ALLOC);
result->closure.header.tag = SCM_CLOSURE_TAG;
result->closure.behaviour = Cfunction;
result->closure.arity = arity;
va_start(args,size);
for (i = 0; i < size; i++) {
result->closure.environment[i] = va_arg(args,SCM);
}
va_end(args);
return SCM_Wrap(result);
}
/* Boxeses */
/* As the book code notes, it is kind of ridiculous to malloc a
* word. It would be better to allocate pools of boxes at a time,
* perhaps. */
SCM SCM_allocate_box(SCM v) {
SCM cell = (SCM) malloc(sizeof(struct SCM_box));
if (cell == (SCM)NULL) SCM_error(SCM_ERR_CANT_ALLOC);
cell->box.content = v;
return cell;
}
/* Pairs */
SCM SCM_cons (SCM a, SCM d) {
SCMref cell = (SCMref) malloc(sizeof(struct SCM_unwrapped_pair));
if (cell == (SCMref)NULL) SCM_error(SCM_ERR_CANT_ALLOC);
cell->pair.header.tag = SCM_PAIR_TAG;
cell->pair.cdr = d;
cell->pair.car = a;
return SCM_Wrap(cell);
}
SCM SCM_car (SCM cell) {
if (SCM_PairP(cell)) {
return SCM_Car(cell);
}
else return SCM_error(SCM_ERR_CAR);
}
SCM SCM_cdr (SCM cell) {
if (SCM_PairP(cell)) {
return SCM_Cdr(cell);
}
else return SCM_error(SCM_ERR_CDR);
}
SCM SCM_set_cdr (SCM cell, SCM value) {
if (SCM_PairP(cell)) {
SCM_Unwrap(cell)->pair.cdr = value;
return cell;
}
else return SCM_error(SCM_ERR_SET_CDR);
}
/* Just for fun, I'm going to take a different approach to the book
* here, and allocate a list in an array in one go. */
SCM SCM_list(unsigned long count, va_list arguments) {
if (count == 0) return SCM_nil;
struct SCM_unwrapped_pair *cells =
malloc(count * sizeof(struct SCM_unwrapped_pair));
unsigned long i;
for (i = 0; i < count; i++) {
struct SCM_unwrapped_pair *cell = &cells[i];
cell->header.tag = SCM_PAIR_TAG;
cell->car = va_arg(arguments,SCM);
if (i + 1 < count) {
cell->cdr = SCM_Wrap(&cells[i+1]);
}
else {
cell->cdr = SCM_nil;
}
}
return SCM_Wrap(cells);
}
/* For debug, and main output */
SCM SCM_prin(SCM x);
/* Prin a value in list position */
void SCM_prin_list(SCM x) {
if (SCM_FixnumP(x)) {
fprintf(stdout, " . %ld", SCM_Fixnum2int(x));
}
else {
switch (SCM_2tag(x)) {
case SCM_NULL_TAG: {
break;
}
case SCM_PAIR_TAG: {
fprintf(stdout, " ");
SCM_prin(SCM_Car(x));
SCM_prin_list(SCM_Cdr(x));
break;
}
default: {
fprintf(stdout, " . ");
SCM_prin(x);
break;
}
}
}
}
SCM SCM_prin(SCM x) {
if (SCM_FixnumP(x)) {
fprintf(stdout, "%ld", SCM_Fixnum2int(x));
}
else {
switch (SCM_2tag(x)) {
case SCM_NULL_TAG: {
fprintf(stdout, "()");
break;
}
case SCM_PAIR_TAG: {
fprintf(stdout, "(");
SCM_prin(SCM_Car(x));
SCM_prin_list(SCM_cdr(x));
fprintf(stdout, ")");
break;
}
case SCM_BOOLEAN_TAG: {
fprintf(stdout, "#%c", (SCM_EqP(x,SCM_true) ? 't' : 'f'));
break;
}
case SCM_UNDEFINED_TAG: {
fprintf(stdout, "#<undefined>");
break;
}
case SCM_SYMBOL_TAG: {
SCM str = SCM_Unwrap(x)->symbol.pname;
char *Cstring = SCM_Unwrap(str)->string.Cstring;
fprintf(stdout, "%s", Cstring);
break;
}
case SCM_STRING_TAG: {
char *Cstring = SCM_Unwrap(x)->string.Cstring;
fprintf(stdout,"\"%s\"", Cstring);
break;
}
case SCM_SUBR_TAG: {
fprintf(stdout, "#<procedure@%p>", (void *)(x));
break;
}
case SCM_CLOSURE_TAG: {
fprintf(stdout, "#<closure@%p>", (void *)(x));
break;
}
case SCM_ESCAPE_TAG: {
fprintf(stdout, "#<continuation@%p>", (void *)(x));
break;
}
default:
fprintf(stdout, "<something@%p>", (void *)(x));
break;
}
}
return (x);
}
SCM SCM_print(SCM x) {
SCM_prin(x);
printf("\n");
return (x);
}
SCM SCM_signal_error(unsigned long code, unsigned long line, char* file) {
fprintf(stdout, "Error %lu in %s:%lu\n", code, file, line);
exit(code);
}
/* Now for the big one, invoke. */
SCM SCM_invoke(SCM function, unsigned long number, ...) {
if (SCM_FixnumP(function)) {
return SCM_error(SCM_ERR_CANNOT_APPLY);
}
else {
switch (SCM_2tag(function)) {
case SCM_SUBR_TAG: {
SCM (*behaviour)(void) = (SCM_Unwrap(function)->subr).behaviour;
long arity = (SCM_Unwrap(function)->subr).arity;
SCM result;
if (arity >= 0) { // no varargs
if (arity != number) {
return SCM_error(SCM_ERR_WRONG_ARITY);
}
else {
if (arity == 0) {
result = behaviour();
}
else {
va_list args;
va_start(args, number);
/* ugly, but perhaps necessary */
SCM a0 = va_arg(args,SCM);
if (number == 1) {
result = ((SCM (*)(SCM))*behaviour)(a0);
}
else {
SCM a1 = va_arg(args,SCM);
if (number == 2) {
result = ((SCM (*)(SCM,SCM))*behaviour)(a0, a1);
}
else {
SCM a2 = va_arg(args,SCM);
if (number == 3) {
result = ((SCM (*)(SCM,SCM,SCM))*behaviour)(a0, a1, a2);
}
else {
return SCM_error(SCM_ERR_INTERNAL);
}
}
}
va_end(args);
}
return result;
}
}
else { /* varargs */
long min_arity = SCM_MinimalArity(arity);
if (number < min_arity) {
return SCM_error(SCM_ERR_WRONG_ARITY);
}
else {
va_list args;
SCM result;
va_start(args, number);
result = ((SCM (*)(unsigned long, va_list))
*behaviour)(number, args);
va_end(args);
return result;
}
}
} /* case SCM_SUBR_TAG */
case SCM_CLOSURE_TAG: {
SCM (*behaviour)(void) = (SCM_Unwrap(function)->closure).behaviour;
long arity = (SCM_Unwrap(function)->closure).arity;
SCM result;
va_list args;
va_start(args,number);
if (arity >= 0) {
if (arity != number) {
return SCM_error(SCM_ERR_WRONG_ARITY);
}
else {
result = ((SCM (*)(SCM,unsigned long, va_list)) *behaviour)
(function, number, args);
}
}
else { /* varargs */
long min_arity = SCM_MinimalArity(arity);
if (number < min_arity) {
return SCM_error(SCM_ERR_WRONG_ARITY);
}
else {
result = ((SCM (*)(SCM,unsigned long, va_list)) *behaviour)
(function, number, args);
}
}
va_end(args);
return result;
} /* case SCM_CLOSURE_TAG */
default:
return SCM_error(SCM_ERR_CANNOT_APPLY);
}
}
}