-
Notifications
You must be signed in to change notification settings - Fork 1
/
builtin.c
433 lines (387 loc) · 9.92 KB
/
builtin.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <base.h>
#include <runtime.h>
#include <builtin.h>
object *boolean(int bool) {
return bool ? &true_object : &false_object;
}
object *type_eq(object *a, char type) {
return boolean(a->type == type);
}
int true(object *a) {
return a->type == T_TRUE;
}
#define CHECK_TYPE(obj, thetype) do { if (obj->type != thetype) { \
FatalError("%s: Expected type %s, got %s", \
__func__, TYPE_NAME[thetype], obj_type_name(obj));}} while(0)
#define TYPE_CHECK_NUM2(a,b) do { if (!is_number(a) || !is_number(b)){ \
FatalError("%s unsupported for type %s and %s", \
__func__,obj_type_name(a), obj_type_name(b));}} while(0)
#define int_args(a,b) (is_int(a) && is_int(b))
/**
* Type Predicates
**/
object *__booleanp(object *a) {
return boolean(a->type == T_TRUE ||
a->type == T_FALSE);
}
object *__nullp(object *a) { return type_eq(a, T_NULL); }
object *__symbolp(object *a) { return type_eq(a, T_SYMBOL); }
object *__charp(object *a) { return type_eq(a, T_CHAR); }
object *__stringp(object *a) { return type_eq(a, T_STRING); }
object *__vectorp(object *a) { return type_eq(a, T_VECTOR); }
object *__pairp(object *a) { return type_eq(a, T_PAIR); }
object *__procedurep(object *a) { return type_eq(a, T_PROC); }
object *__numberp(object *a) {
return boolean(a->type == T_INT ||
a->type == T_REAL);
}
object *__atomp(object *a) {
switch (a->type) {
case T_NONE:
case T_TRUE:
case T_FALSE:
case T_NULL:
case T_SYMBOL:
case T_CHAR:
case T_STRING:
case T_INT:
case T_REAL:
return &true_object;
}
return &false_object;
}
object *__stringtosymbol(object *a) {
CHECK_TYPE(a, T_STRING);
object *result = new_object(T_SYMBOL);
obj_set_sym_val(result, a->val.str);
return result;
}
/**
* Display various kinds of objects
**/
object *__display(object *obj) {
switch (obj->type) {
case T_NONE:
printf("#No-value");
break;
case T_INT:
printf("%i", obj->val.int_);
break;
case T_STRING:
printf("%s", obj->val.str);
break;
case T_SYMBOL:
printf("%s", obj->val.sym);
break;
case T_CHAR:
// TODO Change this back and implement (write)
printf("#\%c", obj->val.chr);
break;
case T_TRUE:
printf("#t");
break;
case T_FALSE:
printf("#f");
break;
case T_PROC:
printf("#Procedure-(%s)", obj->val.proc.scm_name);
break;
case T_NULL:
printf("()");
break;
case T_PAIR:
{
object *head = obj;
printf("(");
while (true(__pairp(head->val.pair.cdr))) {
// list
__display(head->val.pair.car);
printf(" ");
head = head->val.pair.cdr;
}
if (true(__nullp(head->val.pair.cdr))) {
// empty list
__display(head->val.pair.car);
} else {
// cons cell
__display(head->val.pair.car);
printf(" . ");
__display(head->val.pair.cdr);
}
printf(")");
break;
}
default:
FatalError("display: Unsupported type: %i", obj->type);
}
return &none_object;
}
object *print(object *obj) {
__display(obj);
printf("\n");
return &none_object;
}
/**
* Numeric Functions
**/
int is_number(object *a) {
return true(__numberp(a));
}
int is_int(object *a) {
return a->type == T_INT;
}
int int_value(object *a) {
return a->val.int_;
}
double number_value(object *a) {
if (a->type == T_INT) {
return a->val.int_;
} else if (a->type == T_REAL) {
return a->val.real;
}
FatalError("Unsupported numerical type: %s", obj_type_name(a));
return 0; // avoid gcc error
}
object* __add(object* a, object* b){
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)){
object *res = new_object(T_INT);
res->val.int_ = int_value(a) + int_value(b);
return res;
}
object *res = new_object(T_REAL);
res->val.real = number_value(a) + number_value(b);;
return res;
}
object* __sub(object* a, object* b){
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
object *res = new_object(T_INT);
res->val.int_ = int_value(a) - int_value(b);
return res;
}
object *res = new_object(T_REAL);
res->val.real = number_value(a) - number_value(b);;
return res;
}
object* __mul(object *a, object *b){
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
object *res = new_object(T_INT);
res->val.int_ = int_value(a) * int_value(b);
return res;
}
object *res = new_object(T_REAL);
res->val.real = number_value(a) * number_value(b);;
return res;
}
object *__gt(object *a, object *b) {
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
return boolean(int_value(a) > int_value(b));
}
return boolean(number_value(a) > number_value(b));
}
object *__lt(object *a, object *b) {
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
return boolean(int_value(a) < int_value(b));
}
return boolean(number_value(a) < number_value(b));
}
object *__ge(object *a, object *b) {
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
return boolean(int_value(a) >= int_value(b));
}
return boolean(number_value(a) >= number_value(b));
}
object *__le(object *a, object *b) {
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
return boolean(int_value(a) <= int_value(b));
}
return boolean(number_value(a) <= number_value(b));
}
object *__eq(object *a, object *b) {
TYPE_CHECK_NUM2(a,b);
if (int_args(a,b)) {
return boolean(int_value(a) == int_value(b));
}
return boolean(number_value(a) == number_value(b));
}
/**
* List functions
**/
object *__cons(object *a, object *b) {
object *res = new_object(T_PAIR);
res->val.pair.car = a;
res->val.pair.cdr = b;
return res;
}
object *__car(object *a) {
CHECK_TYPE(a, T_PAIR);
return a->val.pair.car;
}
object *__cdr(object *a) {
CHECK_TYPE(a, T_PAIR);
return a->val.pair.cdr;
}
object *__length(object *a) {
int len = 0;
while (a != &null_object) {
if (!true(__pairp(a))) {
FatalError("Cannot get length of non pair.");
}
len++;
a = __cdr(a);
}
object *result = new_object(T_INT);
result->val.int_ = len;
return result;
}
/**
* R4RS Section 6.9 Control Features
**/
object *__apply(object *theproc, object *args) {
int arglen = number_value(__length(args));
object **arglist = NULL;
if (arglen) {
arglist = mallocz(sizeof(object *) * arglen);
int i = 0;
while (args != &null_object) {
arglist[i++] = __car(args);
args = __cdr(args);
}
}
// TODO env arg should be removed from call proc, the env
// is restored from the proc's closure.
object *result = call_proc(theproc, NULL, arglist, arglen);
free(arglist);
return result;
}
/* Implemented in prelude.
object *__not(object *a) {
if (a->type == T_FALSE) {
return &true_object;
}
return &false_object;
}
*/
/**
* R4RS 6.2 Equivalence predicates
**/
// TODO all these needed to be tested properly
object *__eqvp(object *a, object *b) {
if (a->type != b->type) {
// Objects must have same type
return &false_object;
}
switch (a->type) {
case T_TRUE:
case T_FALSE:
case T_NULL:
case T_NONE:
// In these cases equal type means objects are equal
return &true_object;
case T_INT:
return boolean(a->val.int_ == b->val.int_);
case T_CHAR:
return boolean(a->val.chr == b->val.chr);
case T_REAL:
return boolean(a->val.real == b->val.real);
case T_STRING:
return boolean(a->val.str == b->val.str);
case T_SYMBOL:
return boolean(strcasecmp(a->val.sym, b->val.sym) == 0);
case T_PAIR:
return boolean(a->val.pair.car == b->val.pair.car &&
a->val.pair.cdr == b->val.pair.cdr);
case T_PROC:
return boolean(a->val.proc.func == b->val.proc.func);
case T_VECTOR:
default:
FatalError("Unimplemented for type: %s", obj_type_name(a));
return &none_object; // avoid gcc error
}
}
object *__equalp(object *a, object *b) {
if (a->type != b->type) {
// Objects must have same type
return &false_object;
}
switch (a->type) {
case T_TRUE:
case T_FALSE:
case T_NULL:
case T_NONE:
case T_SYMBOL:
case T_INT:
case T_CHAR:
case T_REAL:
case T_PROC:
return __eqvp(a, b);
case T_STRING:
return boolean(strcmp(a->val.str, b->val.str) == 0);
case T_PAIR:
if (true(__equalp(__car(a), __car(b))) &&
true(__equalp(__cdr(a), __cdr(b)))) {
return &true_object;
}
return &false_object;
case T_VECTOR:
default:
FatalError("Unimplemented for type: %s", obj_type_name(a));
return &none_object; // avoid gcc error
}
}
object *__list(environ *env, object **args, int arglen) {
object *result = &null_object;
for (int i = arglen - 1; i >= 0; i--) {
result = __cons(args[i], result);
}
return result;
}
/**
* Setting up the global environment
**/
#define ADD(scm_name,func,arity) add_to_environment(env,#scm_name,new_builtin_proc(&func,arity))
void add_builtins_to_env(environ *env) {
ADD(boolean?, __booleanp, 1);
ADD(null?, __nullp, 1);
ADD(symbol?, __symbolp, 1);
ADD(char?, __charp, 1);
ADD(string?, __stringp, 1);
ADD(vector?, __vectorp, 1);
ADD(pair?, __pairp, 1);
ADD(procedure?, __procedurep, 1);
ADD(number?, __numberp, 1);
ADD(atom?, __atomp, 1);
ADD(string->symbol, __stringtosymbol, 1);
ADD(apply, __apply, 2);
//ADD(not, __not, 1);
ADD(eqv?, __eqvp, 2);
ADD(equal?, __equalp, 2);
ADD(display, __display, 1);
ADD(cons, __cons, 2);
ADD(car, __car, 1);
ADD(cdr, __cdr, 1);
// list is a slightly special case, it takes 0-N args, but there is no support
// in the language definition for (define (list . args) ...). So list is
// defined here although it is not using the builtin proc calling convention (which
// doesn't support varargs).
add_to_environment(env, "list", new_proc_object("list", &__list, 0, 1, NULL));
ADD(+, __add, 2);
ADD(-, __sub, 2);
ADD(*, __mul, 2);
ADD(>, __gt, 2);
ADD(<, __lt, 2);
ADD(>=, __ge, 2);
ADD(<=, __le, 2);
ADD(=, __eq, 2);
// Some test builtins
ADD(print, print, 1);
}