-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimitives.c
373 lines (281 loc) · 8.12 KB
/
primitives.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
#include "primitives.h"
/* PRIMITIVE FUNCTIONS DEFINED */
/* primitive type-checking */
Obj isnumber_func(Obj obj) { return BOOLOBJ(GETTAG(obj) == NUM); }
oneArgFunc isnumber_ = isnumber_func;
Obj islist_func(Obj obj) { return BOOLOBJ(GETTAG(obj) == LIST); }
oneArgFunc islist_ = islist_func;
Obj isbool_func(Obj obj) { return BOOLOBJ(GETTAG(obj) == BOOL_); }
oneArgFunc isbool_ = isbool_func;
Obj issymbol_func(Obj obj) { return BOOLOBJ(GETTAG(obj) == NAME); }
oneArgFunc issymbol_ = issymbol_func;
// null? returns false for non-list objects
Obj null_func(Obj obj) {
bool isList = GETTAG(obj) == LIST;
if (!isList) return FALSEOBJ;
bool isNull = GETLIST(obj) == NULL;
return BOOLOBJ(isNull);
}
oneArgFunc null_ = null_func;
/* primitive list functions
(not to be confused with the list
macros found in objects.h!) */
/* car and cdr both do simple error-checking */
Obj car_func(Obj obj) {
if (!(GETTAG(obj) == LIST)) {
print_error_message(LIST, "car");
return ERROROBJ;
}
List* list = GETLIST(obj);
if (list == NULL) {
print_error_message(LIST, "car");
return ERROROBJ;
} else
return list->car;
}
Obj cdr_func(Obj obj) {
if (!(GETTAG(obj) == LIST)) {
print_error_message(LIST, "cdr");
return ERROROBJ;
}
List* list = GETLIST(obj);
// cdr of empty list is empty list
if (list == NULL)
return NULLOBJ;
else
return LISTOBJ(list->cdr);
}
Obj setcar_func(Obj obj, Obj carval) {
if (GETTAG(obj) != LIST) {
print_error_message(LIST, "set-car!");
return ERROROBJ;
}
List* list = GETLIST(obj);
if (list == NULL) {
print_error_message(LIST, "set-car!");
return ERROROBJ;
} else {
list->car = carval;
return LISTOBJ(list);
}
}
Obj setcdr_func(Obj obj, Obj cdrval) {
if (GETTAG(obj) != LIST || GETTAG(cdrval) != LIST) {
print_error_message(LIST, "set-cdr!");
return ERROROBJ;
}
List* list = GETLIST(obj);
if (list == NULL) {
print_error_message(LIST, "set-cdr!");
return ERROROBJ;
} else {
list->cdr = GETLIST(cdrval);
return LISTOBJ(list);
}
}
Obj cadr_func(Obj obj) { return car_func(cdr_func(obj)); }
Obj cddr_func(Obj obj) { return cdr_func(cdr_func(obj)); }
Obj cdadr_func(Obj obj) { return cdr_func(cadr_func(obj)); }
Obj caddr_func(Obj obj) { return car_func(cddr_func(obj)); }
Obj cdddr_func(Obj obj) { return cdr_func(cddr_func(obj)); }
Obj cadddr_func(Obj obj) { return car_func(cdddr_func(obj)); }
// is there a faster way to do this?
oneArgFunc car_ = car_func;
oneArgFunc cdr_ = cdr_func;
twoArgFunc setcar_ = setcar_func;
twoArgFunc setcdr_ = setcdr_func;
oneArgFunc cadr_ = cadr_func;
oneArgFunc cddr_ = cddr_func;
oneArgFunc cdadr_ = cdadr_func;
oneArgFunc caddr_ = caddr_func;
oneArgFunc cdddr_ = cdddr_func;
oneArgFunc cadddr_ = cadddr_func;
/* second object MUST BE list. if it isn't
a list, it will be coerced to a one-item list */
Obj cons_func(Obj car, Obj cdr) {
int isList = GETTAG(cdr) == LIST;
if (!isList) {
printf("%s\n", "Second arg not a list! Converting to one-item list...");
List* listcdr = makeList(cdr, NULL);
return LISTOBJ(makeList(car, listcdr));
} else
return LISTOBJ(makeList(car, GETLIST(cdr)));
}
twoArgFunc cons_ = cons_func;
/* primitive arithmetic */
Obj add_func(Obj a, Obj b) {
if (!are_both_nums(a, b)) {
print_error_message(NUM, ADD_NAME);
return ERROROBJ;
}
return NUMOBJ(GETNUM(a) + GETNUM(b));
}
Obj sub_func(Obj a, Obj b) {
if (!are_both_nums(a, b)) {
print_error_message(NUM, "-");
return ERROROBJ;
}
return NUMOBJ(GETNUM(a) - GETNUM(b));
}
Obj mul_func(Obj a, Obj b) {
if (!are_both_nums(a, b)) {
print_error_message(NUM, MUL_NAME);
return ERROROBJ;
}
return NUMOBJ(GETNUM(a) * GETNUM(b));
}
Obj div_func(Obj a, Obj b) { // floor division
if (!are_both_nums(a, b)) {
print_error_message(NUM, "/");
return ERROROBJ;
}
return NUMOBJ(GETNUM(a) / GETNUM(b));
}
Obj addone_func(Obj a) { return add_func(a, ONEOBJ); }
Obj subone_func(Obj a) { return sub_func(a, ONEOBJ); }
twoArgFunc add_ = add_func;
twoArgFunc sub_ = sub_func;
twoArgFunc mul_ = mul_func;
twoArgFunc div_ = div_func;
oneArgFunc addone_ = addone_func;
oneArgFunc subone_ = subone_func;
/* boolean functions */
// (not 0) and (not '()) return #t -- desirable?
Obj not_func(Obj expr) {
if (GETBOOL(expr) != 0)
return FALSEOBJ;
else
return TRUEOBJ;
}
Obj eq_func(Obj a, Obj b) {
if (!are_both_nums(a, b)) {
print_error_message(NUM, "=");
return ERROROBJ;
}
return BOOLOBJ(GETNUM(a) == GETNUM(b));
}
Obj lt_func(Obj a, Obj b) {
if (!are_both_nums(a, b)) {
print_error_message(NUM, "<");
return ERROROBJ;
}
return BOOLOBJ(GETNUM(a) < GETNUM(b));
}
Obj gt_func(Obj a, Obj b) {
if (!are_both_nums(a, b)) {
print_error_message(NUM, ">");
return ERROROBJ;
}
return BOOLOBJ(GETNUM(a) > GETNUM(b));
}
Obj iszero_func(Obj a) { return eq_func(a, ZEROOBJ); }
Obj isone_func(Obj a) { return eq_func(a, ONEOBJ); }
Obj geneq_func(Obj a, Obj b) {
Tag tag_a = GETTAG(a);
Tag tag_b = GETTAG(b);
if (tag_a != tag_b) return FALSEOBJ;
// else if a and be have the same type
switch (tag_a) {
case NUM:
return eq_func(a, b);
case NAME:
return BOOLOBJ(streq(GETNAME(a), GETNAME(b)));
case BOOL_:
return BOOLOBJ(GETBOOL(a) == GETBOOL(b));
case LIST:
// pointers to the same list
return BOOLOBJ(GETLIST(a) == GETLIST(b));
default:
return FALSEOBJ;
}
}
oneArgFunc not_ = not_func;
twoArgFunc eq_ = eq_func;
twoArgFunc lt_ = lt_func;
twoArgFunc gt_ = gt_func;
oneArgFunc iszero_ = iszero_func;
oneArgFunc isone_ = isone_func;
twoArgFunc geneq_ = geneq_func;
/* I/O */
Obj read_func(void) {
// printf("%s\n", "READ: ");
get_input();
if (isIrregular(code)) {
if (badSyntax(code)) printf("Bad syntax! Try again!\n");
read_func();
}
return parse(code);
}
nilArgFunc read_ = read_func;
Obj display_func(Obj obj) {
// printf("DISPLAY: ");
print_obj(obj);
if (INFO) getchar();
return obj;
}
oneArgFunc display_ = display_func;
Obj newline_func(void) {
printf("\n");
return DUMMYOBJ;
}
nilArgFunc newline_ = newline_func;
Obj error_func(void) {
printf("Error? Uh-oh!\n");
return ERROROBJ;
}
nilArgFunc error_ = error_func;
/* error-checking */
// error-checking helper
bool are_both_nums(Obj a, Obj b) {
return GETTAG(a) == NUM && GETTAG(b) == NUM;
}
void print_error_message(Tag tag, char* source) {
char* operation_type;
switch (tag) {
case NUM:
operation_type = "Arithmetic";
break;
case LIST:
operation_type = "List";
break;
default:
operation_type = "???";
break;
}
printf("%s operation error from *%s*! Oops!\n", operation_type, source);
}
/* primitive application */
/* if new primitives are added, applyPrimitive
can be extended in an obvious way */
Obj applyPrimitive(Obj func, Obj arglist) {
Prim prim = func.val.prim;
primFunc primfunc = prim.func;
argCount argcount = prim.count;
if (argcount == NIL) {
nilArgFunc nilfunc = primfunc.nil;
return nilfunc();
}
Obj arg1 = CAR(arglist);
if (argcount == ONE) {
oneArgFunc onefunc = primfunc.one;
return onefunc(arg1);
}
Obj arg2 = CADR(arglist);
if (argcount == TWO) {
twoArgFunc twofunc = primfunc.two;
return twofunc(arg1, arg2);
}
return DUMMYOBJ;
}
Obj apply_nil_func(Obj func, Obj arglist) {
return applyPrimitive(func, arglist);
}
twoArgFunc apply_nil_ = apply_nil_func;
Obj apply_one_func(Obj func, Obj arglist) {
return applyPrimitive(func, arglist);
}
twoArgFunc apply_one_ = apply_one_func;
Obj apply_two_func(Obj func, Obj arglist) {
return applyPrimitive(func, arglist);
}
twoArgFunc apply_two_ = apply_two_func;