/
value.c
343 lines (321 loc) · 9.09 KB
/
value.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
#include "env.h"
#include "value.h"
#include "list.h"
#include "util.h"
#include "idMap.h"
#include "closure.h"
#include "ast.h"
#include "mem.h"
int newIntValueC = 0, newStringValueC = 0, newClosureValueC = 0,
newEnvValueC = 0, newListValueC = 0, newBuiltinFunC = 0;
int freeIntValueC = 0, freeStringValueC = 0, freeClosureValueC = 0,
freeEnvValueC = 0, freeListValueC = 0, freeBuiltinFunC = 0;
Value *newNoneValue() {
static Value *res = 0;
if (res) {
return res;
} else {
res = allocValue();
res->type = NONE_VALUE_TYPE;
res->data = 0;
res->mark = UNMARKED; // none is alwaus refed by global env's parent, so it
// wil never be GCed.
return res;
}
}
Value *newIntValue(long x) {
newIntValueC++;
Value *res = allocValue();
res->type = INT_VALUE_TYPE;
res->data = (void *)x;
res->mark = UNMARKED;
return res;
}
Value *newStringValue(char *s) {
newStringValueC++;
Value *res = allocValue();
res->type = STRING_VALUE_TYPE;
res->data = (void *)s;
res->mark = UNMARKED;
return res;
}
Value *newListValue(List *list) {
newListValueC++;
Value *res = allocValue();
res->type = LIST_VALUE_TYPE;
res->data = list;
res->mark = UNMARKED;
return res;
}
Value *newClosureValue(Node *t, Env *e) {
newClosureValueC++;
Value *res = allocValue();
res->type = CLOSURE_VALUE_TYPE;
res->data = newClosure(t, e->envValue);
res->mark = UNMARKED;
return res;
}
Value *newBuiltinFun(BuiltinFun f) {
newBuiltinFunC++;
Value *res = allocValue();
res->type = BUILTIN_FUN_VALUE_TYPE;
res->data = f;
res->mark = UNMARKED;
return res;
}
Value *newEnvValue(Env *parent) {
newEnvValueC++;
Value *res = allocValue();
res->type = ENV_VALUE_TYPE;
res->data = newEnv(parent ? parent->envValue : newNoneValue(), res);
res->mark = UNMARKED;
envPutLocal(res->data, getIntId("this"), res);
return res;
}
void freeValue(Value *v) {
#ifdef DEBUG_GC
printf("freeing %s @ %p\n", valueToString(v), v);
#endif
switch (v->type) {
case CLOSURE_VALUE_TYPE:
freeClosure((Closure *)v->data);
freeClosureValueC++;
break;
case ENV_VALUE_TYPE:
freeEnv((Env *)v->data);
freeEnvValueC++;
break;
case LIST_VALUE_TYPE:
freeList((List *)v->data);
freeListValueC++;
break;
case STRING_VALUE_TYPE:
tlFree((char *)v->data);
freeStringValueC++;
break;
case INT_VALUE_TYPE: {
freeIntValueC++;
break;
}
case BUILTIN_FUN_VALUE_TYPE:
freeBuiltinFunC++;
break;
case NONE_VALUE_TYPE:
return; // none is never freed
default:
error("unkonwn value type passed to freeValue: %d\n", v->type);
}
v->type = NONE_VALUE_TYPE;
}
static int mySnprintf(char *s, int n, char *format, ...) {
va_list ap;
va_start(ap, format);
if (n > 0) {
return vsnprintf(s, n, format, ap);
} else {
return vsnprintf(0, 0, format, ap);
}
va_end(ap);
}
static int valueToStringInternal(Value *v, char *s, int n) {
Node *t;
int i;
switch (v->type) {
case BUILTIN_FUN_VALUE_TYPE:
return mySnprintf(s, n, "<builtin-function@%p>", v->data);
case NONE_VALUE_TYPE:
return mySnprintf(s, n, "none");
case INT_VALUE_TYPE:
return mySnprintf(s, n, "%d", v->data);
case CLOSURE_VALUE_TYPE: {
int len = 0;
Node *f = ((Closure *)v->data)->f;
len += mySnprintf(s + len, n - len, "fun %s(",
getStrId((long)chld(f, 0)->data));
t = chld(f, 1);
for (i = 0; i < chldNum(t); i++) {
if (i) {
len += mySnprintf(s + len, n - len, ", ");
}
len += mySnprintf(s + len, n - len, "%s",
getStrId((long)chld(t, i)->data));
}
len += mySnprintf(s + len, n - len, ")");
return len;
}
case LIST_VALUE_TYPE: {
int len = 0;
List *l = v->data;
len += mySnprintf(s + len, n - len, "[");
for (i = 0; i < listSize(l); i++) {
if (i) {
len += mySnprintf(s + len, n - len, ", ");
}
len += valueToStringInternal(listGet(l, i), s + len, n - len);
}
len += mySnprintf(s + len, n - len, "]");
return len;
}
case STRING_VALUE_TYPE: {
return mySnprintf(s, n, "%s", v->data);
}
case ENV_VALUE_TYPE: {
List *keys = envGetAllIds(v->data);
int len = 0;
len += mySnprintf(s, n, "env parent->%p { ", getEnvFromValue(v)->parent);
int keyN = listSize(keys);
for (i = 0; i < keyN; i++) {
long key = (long)listGet(keys, i);
len += mySnprintf(s + len, n - len, "%s->%p ", getStrId(key),
envGet(v->data, key));
}
len += mySnprintf(s + len, n - len, "}");
freeList(keys);
return len;
}
default:
error("cannot print unknown value type: %d at %p\n", v->type, v);
}
}
char *valueToString(Value *v) {
int l = valueToStringInternal(v, 0, 0) + 1;
char *s = (char *)tlMalloc(l);
valueToStringInternal(v, s, l);
return s;
}
int valueEquals(Value *v1, Value *v2) {
if (v1 == v2) return 1;
if (v1->type != v2->type) return 0;
switch (v1->type) {
case INT_VALUE_TYPE:
case ENV_VALUE_TYPE:
case BUILTIN_FUN_VALUE_TYPE:
case CLOSURE_VALUE_TYPE:
return v1->data == v2->data;
case LIST_VALUE_TYPE: {
List *l1 = v1->data;
List *l2 = v2->data;
int len = listSize(l1);
int i;
if (len != listSize(l2)) return 0;
for (i = 0; i < len; i++)
if (!valueEquals(listGet(l1, i), listGet(l2, i))) return 0;
return 1;
}
case STRING_VALUE_TYPE:
return strcmp((char *)v1->data, (char *)v2->data) == 0;
case NONE_VALUE_TYPE:
return 1; // singleton, same type means equal
default:
error("unknown value type passed to valueEquals: %d\n", v1->type);
}
}
Value *valueSub(Value *v1, Value *v2) {
if (v1->type != INT_VALUE_TYPE || v2->type != INT_VALUE_TYPE)
error("- operator only works for int");
return newIntValue((long)v1->data - (long)v2->data);
}
Value *valueMul(Value *v1, Value *v2) {
if (v1->type != INT_VALUE_TYPE || v2->type != INT_VALUE_TYPE)
error("* operator only works for int");
return newIntValue((unsigned long)v1->data * (unsigned long)v2->data);
}
Value *valueDiv(Value *v1, Value *v2) {
if (v1->type != INT_VALUE_TYPE || v2->type != INT_VALUE_TYPE)
error("/ operator only works for int");
if (!v2->data) error("denominator cannot be zero\n");
return newIntValue((long)v1->data / (long)v2->data);
}
Value *valueMod(Value *v1, Value *v2) {
if (v1->type != INT_VALUE_TYPE || v2->type != INT_VALUE_TYPE)
error("%% operator only works for int");
if (!v2->data) error("denominator cannot be zero\n");
return newIntValue((long)v1->data % (long)v2->data);
}
Value *valueAdd(Value *v1, Value *v2) {
switch (v1->type) {
case INT_VALUE_TYPE:
switch (v2->type) {
case INT_VALUE_TYPE:
return newIntValue((long)v1->data + (long)v2->data);
default:
error("int can only add to int\n");
}
case LIST_VALUE_TYPE: {
List *res = listCopy(v1->data);
int i;
assert(v2->type == LIST_VALUE_TYPE);
List *l = v2->data;
for (i = 0; i < listSize(l); i++) {
listPush(res, listGet(l, i));
}
return newListValue(res);
}
case STRING_VALUE_TYPE: {
if (v2->type == STRING_VALUE_TYPE) {
char *s1 = (char *)v1->data, *s2 = (char *)v2->data;
char *res = (char *)tlMalloc(strlen(s1) + strlen(s2) + 1);
*res = 0;
strcat(res, s1);
strcat(res, s2);
return newStringValue(res);
} else {
error("can only add string to string\n");
}
}
case CLOSURE_VALUE_TYPE:
error("Closure value cannot add to anything\n");
case ENV_VALUE_TYPE:
error("Environment value cannot add to anything\n");
default:
error("unknown value type passed to valueAdd: %d\n", v1->type);
}
return 0; // never reach here
}
int valueCmp(Value *v1, Value *v2) {
if (v1->type != v2->type) {
return -1; // comparing different type
}
if (v1 == v2) {
return 0;
}
switch (v1->type) {
case LIST_VALUE_TYPE: {
List *l1 = v1->data, *l2 = v2->data;
int i, n1 = listSize(l1), n2 = listSize(l2);
for (i = 0; i < n1 && i < n2; i++) {
int res = valueCmp(listGet(l1, i), listGet(l2, i));
if (res) return res;
}
if (n1 == n2)
return 0;
else if (n1 < n2)
return -1;
else
return 1;
}
case STRING_VALUE_TYPE:
return strcmp(v1->data, v2->data);
case CLOSURE_VALUE_TYPE:
case ENV_VALUE_TYPE:
case BUILTIN_FUN_VALUE_TYPE:
case INT_VALUE_TYPE:
return (long)(v1->data) - (long)(v2->data);
case NONE_VALUE_TYPE:
return 0;
default:
error("cannot compare unknown type: %d\n", v1->type);
}
}
long getIntFromValue(Value *intValue) {
assert(intValue->type == INT_VALUE_TYPE);
return (long)intValue->data;
}
Env *getEnvFromValue(Value *v) {
assert(v->type == ENV_VALUE_TYPE);
return (Env *)v->data;
}
List *getListFromValue(Value *v) {
assert(v->type == LIST_VALUE_TYPE);
return (List *)v->data;
}