-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprim.c
188 lines (164 loc) · 4.37 KB
/
prim.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
#include <stdio.h>
#include "lilscheme.h"
Handle CallPrimitive(Handle prim, Handle argv) {
// TODO: check arity
PRIMITIVE *pr = DATA_AREA(PRIMITIVE, prim);
PRIMPTR proc = pr->procedure;
return (*proc)(argv);
}
Handle CreatePrimitive(PRIMPTR proc, int arity) {
Handle prim = CreateObject(TYPE_PRIMITIVE, 0);
PRIMITIVE *pr = DATA_AREA(PRIMITIVE, prim);
pr->procedure = proc;
pr->arguments = arity;
return prim;
}
Handle prim_PLUS(Handle argv) {
// TODO: handle floats
int sum = 0;
FOR_IN_VECTOR(i, argv) {
Handle x = VectorRef(argv, i);
Typecheck(x, TYPE_INT);
sum += UnboxInteger(x);
}
return CreateInteger(sum);
}
Handle prim_MINUS(Handle argv) {
// TODO: handle floats
Handle first = VectorRef(argv, 0);
Typecheck(first, TYPE_INT);
int a = UnboxInteger(first);
int len = VectorLength(argv);
for (int i = 1; i < len; i++) {
Handle x = VectorRef(argv, i);
Typecheck(x, TYPE_INT);
a -= UnboxInteger(x);
}
return CreateInteger(a);
}
Handle prim_TIMES(Handle argv) {
// TODO: handle floats
int product = 1;
FOR_IN_VECTOR(i, argv) {
Handle x = VectorRef(argv, i);
Typecheck(x, TYPE_INT);
product *= UnboxInteger(x);
}
return CreateInteger(product);
}
Handle prim_EQUAL(Handle argv) {
Handle first = VectorRef(argv, 0);
TypecheckNumeric(first);
int len = VectorLength(argv);
for (int i = 1; i < len; i++) {
Handle x = VectorRef(argv, i);
TypecheckNumeric(x);
if (CompareNumbers(first, x) != 0) {
return LISP_FALSE;
}
}
return LISP_TRUE;
}
Handle prim_cons(Handle argv) {
Handle a = VectorRef(argv, 0);
Handle b = VectorRef(argv, 1);
return CreateCons(a, b);
}
Handle prim_car(Handle argv) {
Handle pair = VectorRef(argv, 0);
return Car(pair);
}
Handle prim_cdr(Handle argv) {
Handle pair = VectorRef(argv, 0);
return Cdr(pair);
}
Handle prim_set_car(Handle argv) {
Handle pair = VectorRef(argv, 0);
Handle obj = VectorRef(argv, 1);
SetCar(pair, obj);
return pair;
}
Handle prim_set_cdr(Handle argv) {
Handle pair = VectorRef(argv, 0);
Handle obj = VectorRef(argv, 1);
SetCdr(pair, obj);
return pair;
}
// eq? compares object identity; it returns true iff "both" arguments are the
// same object, which is true iff "they" have the same handle.
Handle prim_eqp(Handle argv) {
Handle first = VectorRef(argv, 0);
Handle second = VectorRef(argv, 1);
return LISP_BOOLEAN(first == second);
}
// eqv? compares the identity of compound objects like conses and vectors and the
// value of atomic objects. In practice, this means that eqv? is the same as eq?
// for non-numeric objects and is the same as = for numeric objects.
Handle prim_eqvp(Handle argv) {
Handle first = VectorRef(argv, 0);
Handle second = VectorRef(argv, 1);
return LISP_BOOLEAN(Equivalent(first, second));
}
// TODO: ports
Handle prim_display(Handle argv) {
Handle o = VectorRef(argv, 0);
DisplayObject(o, stdout);
putchar('\n');
return nil;
}
Handle prim_type_of(Handle argv) {
Handle o = VectorRef(argv, 0);
switch (TYPEOF(o)) {
case TYPE_NIL:
return SYM(nil);
case TYPE_INT:
return SYM(integer);
case TYPE_FLOAT:
return SYM(float);
case TYPE_CONS:
return SYM(pair);
case TYPE_VECTOR:
return SYM(vector);
case TYPE_BYTEVECTOR:
return SYM(bytevector);
case TYPE_FUNCTION:
return SYM(procedure);
case TYPE_PRIMITIVE:
return SYM(primitive-procedure);
case TYPE_CONTEXT:
return SYM(continuation);
default:
panic("forgot a type in the type-of primitive");
return SYM(UNKNOWN-TYPE);
}
}
struct PrimTableEntry {
char *name;
PRIMPTR proc;
int arity;
};
static struct PrimTableEntry primTable[] = {
{"+", prim_PLUS, -1},
{"-", prim_MINUS, -1},
{"*", prim_TIMES, -2},
{"=", prim_EQUAL, -2},
{"eq?", prim_eqp, 2},
{"eqv?", prim_eqvp, 2},
{"cons", prim_cons, 2},
{"car", prim_car, 1},
{"cdr", prim_car, 1},
{"set-car!", prim_set_car, 2},
{"set-cdr!", prim_set_cdr, 2},
{"display", prim_display, 1},
{"type-of", prim_type_of, 1},
{NULL, NULL, 0}
};
void ConstructPrimitives() {
int idx = 0;
while (primTable[idx].name != NULL) {
Handle key = CreateSymbol(primTable[idx].name);
Handle prim = CreatePrimitive(primTable[idx].proc, primTable[idx].arity);
globals = AlistSet(globals, key, prim);
idx++;
}
}