-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlispalone.c
429 lines (372 loc) · 10.4 KB
/
lispalone.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
// Lisp standalone implementation
// No external dependencies
#include <assert.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
/** Globals **/
short g_debug = 1;
/** Enumerations **/
enum FileDescriptors { Stdin, Stdout, Stderr };
enum CellTypes {
Nil, // nil type
Number, // value => CellValue
Symbol, // value => char*
String, // value => char*
List, // value ignored; see 'list' member
Lambda, // value ignored; see 'list' member, 'env' member
Proc // value => (Cell)(List args)
};
enum LSFlagsValues {
None = 0x00, //
Dynamic = 0x01, // if set, was allocated using dynamic memory (malloc, etc)
};
/** Forward declarations and typedefs **/
struct Cell_st;
struct List_st;
struct Env_st;
typedef struct Cell_st LSCell;
typedef struct List_st LSList;
typedef struct Map_st LSMap;
typedef struct Env_st LSEnv;
typedef unsigned LSCellType;
typedef long long LSCellValue;
typedef unsigned LSRefCounter;
typedef unsigned LSFlags;
typedef unsigned long long LSIndex;
LSCell sym_nil,
sym_true,
sym_false;
/** Structures **/
struct Cell_st {
LSCellType type;
LSCellValue value;
LSList *list;
LSEnv *env;
LSFlags flags;
};
void cell_construct(LSCell *cell, LSCellType type, LSCellValue value, LSList *list, LSEnv *env, LSFlags flags);
void cell_destruct (LSCell *cell);
LSCell *cell_new(LSCellType type, LSCellValue value, LSList *list, LSEnv *env);
LSCell *cell_dupe(LSCell *cell); // Duplicate and return pointer
void cell_copy(LSCell *source, LSCell *dest); // Copy from one to another
LSCell cell_index_or(LSCell cell, LSIndex index, LSCell defaultval);
LSIndex cell_strcmp(LSCell cell, const char *compare);
LSCell cell_head(LSCell cell);
LSList *cell_tail(LSCell cell);
struct List_st {
LSCell head;
LSList *tail;
LSFlags flags;
};
void list_construct(LSList *list, LSCell head, LSList *tail, LSFlags flags);
void list_destruct (LSList *list);
LSList *list_dupe(LSList *list);
LSList *list_new(LSCell head, LSList *tail);
LSCell list_index(LSList *list, LSIndex index);
LSCell list_index_or(LSList *list, LSIndex index, LSCell defaultval);
LSCell list_head(LSList *list);
LSList *list_tail(LSList *list);
void list_free(LSList *list) {
if(list->tail)
list_free(list->tail);
if(list->flags & Dynamic)
free(list);
}
int list_is_empty(LSList *list) { return list->head.type == Nil; }
void list_push_back(LSList *list, LSCell value);
struct Map_st {
LSList *keys;
LSList *values;
LSFlags flags;
};
void map_construct(LSMap *map, LSFlags flags);
void map_destruct(LSMap *map);
LSCell map_find(LSMap *map, char *name);
void map_set(LSMap *map, char *name, LSCell value);
void map_free(LSMap *map) {
if(map->keys && map->keys->flags & Dynamic)
list_free(map->keys);
if(map->values && map->values->flags & Dynamic)
list_free(map->values);
if(map->flags & Dynamic)
free(map);
}
struct Env_st {
LSEnv *outer;
LSMap members;
LSRefCounter refcount;
LSFlags flags;
};
void env_construct(LSEnv *env, LSEnv *outer, LSFlags flags);
void env_destruct (LSEnv *env);
LSEnv *env_new(LSEnv *outer, LSFlags flags);
LSEnv *env_new3(LSEnv *outer, LSList *names, LSList *values);
LSCell env_find(LSEnv *env, LSCell key);
void env_set (LSEnv *env, LSCell key, LSCell value);
void env_free (LSEnv *env);
void env_dereference (LSEnv *env);
/** Eval functions */
LSCell eval(LSCell x, LSEnv *env);
/** Cell implementation **/
void cell_construct(LSCell *cell, LSCellType type, LSCellValue value, LSList *list, LSEnv *env, LSFlags flags) {
cell->type = type;
cell->value = value;
cell->list = list;
cell->env = env;
cell->flags = flags;
}
void cell_destruct(LSCell *cell) {
if(cell->env != 0) {
env_dereference(cell->env);
}
}
LSCell *cell_new(LSCellType type, LSCellValue value, LSList *list, LSEnv *env) {
LSCell *cell;
if(!(cell = (LSCell*)malloc(sizeof(LSCell)))) return 0;
cell_construct(cell, type, value, list, env, Dynamic);
return cell;
}
LSCell *cell_dupe(LSCell *cell) {
return cell_new(cell->type, cell->value, cell->list, cell->env);
}
LSCell cell_index(LSCell cell, LSIndex index) {
return list_index(cell.list, index);
}
LSCell cell_index_or(LSCell cell, LSIndex index, LSCell defaultval) {
return list_index_or(cell.list, index, defaultval);
}
void cell_copy(LSCell *source, LSCell *dest) {
memcpy(dest, source, sizeof(LSCell));
}
LSIndex cell_strcmp(LSCell cell, const char *compare) {
if(cell.type == Symbol || cell.type == String)
return strcmp((char *)cell.value, compare);
return 1;
}
LSCell cell_head(LSCell cell) {
assert(cell.type == List || cell.type == Lambda);
return list_head(cell.list);
}
LSList *cell_tail(LSCell cell) {
assert(cell.type == List || cell.type == Lambda);
return list_tail(cell.list);
}
/** List implementation **/
void list_construct(LSList *list, LSCell head, LSList *tail, LSFlags flags) {
memcpy(&list->head, &head, sizeof(LSCell));
if((list->tail = tail)) {
list->tail = list_dupe(tail);
}
list->flags = flags;
}
LSList *list_new(LSCell head, LSList *tail) {
LSList *list;
if(!(list = (LSList*)malloc(sizeof(LSList)))) return 0;
list_construct(list, head, tail, Dynamic);
return list;
}
LSList *list_empty() {
LSList *list;
if(!(list = (LSList*)malloc(sizeof(LSList)))) return 0;
list->head.type = Nil;
list->tail = 0;
return list;
}
LSList *list_dupe(LSList *list) {
return list_new(list->head, list->tail);
}
LSCell list_index(LSList *list, LSIndex index) {
return list_index_or(list, index, sym_nil);
}
LSCell list_index_or(LSList *list, LSIndex index, LSCell defaultval) {
if(index == 0)
return list->head;
else if(list->tail == 0)
return defaultval;
return list_index_or(list->tail, index - 1, defaultval);
}
LSCell list_head(LSList *list) {
return list->head;
}
LSList *list_tail(LSList *list) {
return list->tail;
}
void list_push_back(LSList *list, LSCell value) {
LSList *it;
// find tail
it = list;
while(it->tail) it = it->tail;
// allocate new
it->tail = list_new(value, 0);
}
/** Environment implementation **/
void env_construct(LSEnv *env, LSEnv *outer, LSFlags flags) {
env->outer = outer;
map_construct(&env->members, flags);
env->refcount = 0;
env->flags = flags;
}
LSEnv *env_new(LSEnv *outer, LSFlags flags) {
LSEnv *env;
if(!(env = (LSEnv*)malloc(sizeof(LSEnv)))) return 0;
env_construct(env, outer, flags);
return env;
}
LSEnv *env_new3(LSEnv *outer, LSList *names, LSList *values) {
LSEnv *env;
LSList *it1, *it2;
if(!(env = (LSEnv*)malloc(sizeof(LSEnv)))) return 0;
it1 = names;
it2 = values;
while(it1 && it2 &&
it1->head.type != Nil && it2->head.type != Nil) {
env_set(env, it1->head, it2->head);
it1 = it1->tail;
it2 = it2->tail;
}
return env;
}
void env_set (LSEnv *env, LSCell key, LSCell value) {
LSCell nkey, nvalue;
map_set(&env->members, (char*)key.value, value);
}
LSCell env_find(LSEnv *env, LSCell key) {
return map_find(&env->members, (char*)key.value);
}
void env_dereference (LSEnv *env) {
assert(env->refcount > 0);
if(--env->refcount == 0)
if(env->flags & Dynamic)
env_free(env);
}
void env_free (LSEnv *env) {
assert(env->flags & Dynamic);
map_free(&env->members);
free(env);
}
/** Map implementation **/
void map_construct(LSMap *map, LSFlags flags) {
map->keys= list_new(sym_nil, 0);
map->values = list_new(sym_nil, 0);
map->flags = flags;
}
LSCell map_find(LSMap *map, char *name) {
LSCell result;
LSList *it1, *it2;
it1 = map->keys;
it2 = map->values;
assert(it1 != 0);
assert(it2 != 0);
while(it1 && it2) {
if(!cell_strcmp(it1->head, name))
return it2->head;
it1 = it1->tail;
it2 = it2->tail;
}
result.type = Nil;
return result;
}
void map_set(LSMap *map, char *name, LSCell value) {
}
/** Eval: exec an if statement **/
LSCell eval_if(LSCell x, LSEnv *env) {
LSCell test, conseq, alt;
test = cell_index(x, 1);
conseq = cell_index(x, 2);
alt = cell_index_or(x, 3, sym_nil);
if(cell_strcmp(eval(test, env), (char*)sym_false.value)) // not match false
return eval(conseq, env);
else
return eval(alt, env);
}
/** Eval: exec a begin statement **/
LSCell eval_begin(LSCell x, LSEnv *env) {
LSList *curr;
assert(env != 0);
assert(x.type == List);
assert(x.list != 0);
curr = x.list;
while(curr->tail) {
eval(curr->head, env);
curr = curr->tail;
}
return eval(curr->head, env);
}
LSCell proc_invoke(LSCell proc, LSList *args) {
return sym_nil;
}
LSCell eval_proc(LSCell proc, LSList *args, LSEnv *env) {
LSList *exps, *exp_it, *parms;
LSCell result, body;
LSEnv *new_env;
exps = list_empty();
exp_it = args;
while(exp_it && !list_is_empty(exp_it)) {
list_push_back(exps, eval(exp_it->head, env));
exp_it = exp_it->tail;
}
if(proc.type == Lambda) {
body = cell_index(proc, 2); // body
parms = cell_index(proc, 1).list; // parms
new_env = env_new3(proc.env, parms, exps);
result = eval(body, new_env);
} else if(proc.type == Proc) {
result = proc_invoke(proc, exps);
} else {
dprintf(Stderr, "not a function\n");
exit(5);
}
list_free(exps);
return result;
}
/** Eval loop implementation **/
LSCell eval(LSCell x, LSEnv *env) {
LSCell item0;
if(x.type == Symbol)
return env_find(env, x);
if(x.type == Number || x.type == String)
return x;
if(x.list == 0)
return sym_nil;
item0 = cell_index(x, 0);
if(item0.type == Symbol) {
if(!cell_strcmp(item0, "quote")) // (quote exp)
return cell_index(x, 1);
if(!cell_strcmp(item0, "if")) // (if test conseq [alt])
return eval_if(x, env);
if(!cell_strcmp(item0, "set!") || // (define var exp)
!cell_strcmp(item0, "define")) {
env_set(env, cell_index(x, 1), cell_index(x, 2));
return x;
}
if(!cell_strcmp(item0, "lambda")) { // (lambda (var*) exp)
x.env = env;
return x;
}
if(!cell_strcmp(item0, "begin")) { // (begin exp*)
return eval_begin(x, env);
}
}
// (proc exp*)
return eval_proc(eval(item0, env), cell_tail(x), env);
}
void setup() {
cell_construct(&sym_nil, Symbol, (LSCellValue)"nil", 0, 0, 0);
cell_construct(&sym_true, Symbol, (LSCellValue)"#t", 0, 0, 0);
cell_construct(&sym_false, Symbol, (LSCellValue)"#f", 0, 0, 0);
}
void test() {
}
int main (int argc, char **argv) {
setup();
test();
if(g_debug) {
dprintf(Stderr, "Sizes:\n");
dprintf(Stderr, "\tCell: %ld (%ldb)\n", sizeof(LSCell), sizeof(LSCell) / 8);
dprintf(Stderr, "\tList: %ld (%ldb)\n", sizeof(LSList), sizeof(LSList) / 8);
dprintf(Stderr, "\tMap : %ld (%ldb)\n", sizeof(LSMap), sizeof(LSMap) / 8);
dprintf(Stderr, "\tEnv : %ld (%ldb)\n", sizeof(LSEnv), sizeof(LSEnv) / 8);
}
return 0;
}