-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.c
201 lines (176 loc) · 4.38 KB
/
cell.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cell.h"
#include "m.h"
#include "stdlib.h"
#define CELL_ALLOC bmalloc
struct cell_t NA = {NIL, 0};
struct cell_t* cell_from_int(int i) {
struct cell_t* int_cell = CELL_ALLOC(sizeof(struct cell_t));
int_cell->tag = INTEGER;
int_cell->i = i;
return int_cell;
}
struct cell_t* cell_from_string(char* str) {
struct cell_t* str_cell = CELL_ALLOC(sizeof(struct cell_t));
str_cell->tag = STRING;
str_cell->s = CELL_ALLOC(sizeof(char)*strlen(str)+1);
strcpy(str_cell->s, str);
return str_cell;
}
struct cell_t* cell_call() {
struct cell_t* cell = CELL_ALLOC(sizeof(struct cell_t));
cell->tag = CALL;
cell->p = 0;
return cell;
}
struct cell_t* cell_from_bool(char bool) {
struct cell_t* cell = CELL_ALLOC(sizeof(struct cell_t));
cell->tag = BOOLEAN;
cell->i = 't' == bool;
return cell;
}
struct cell_t* cell_from_symbol(char* str, int len) {
if (str[0] == '"' && str[len-1] == '"')
return cell_from_string(str);
else if (is_numeric(str))
return cell_from_int(atoi(str));
else if (is_boolean(str))
return cell_from_bool(str[0]);
else if (0 == strcmp(str, "null"))
return &NA;
struct cell_t* cell = CELL_ALLOC(sizeof(struct cell_t));
cell->tag = SYM;
cell->s = malloc(sizeof(char)*strlen(str));
strcpy(cell->s, str);
return cell;
}
struct cell_t* cell_from_cons_cell(struct cons_cell_t* cons_cell) {
if (0 == cons_cell) return &NA;
struct cell_t* cell = CELL_ALLOC(sizeof(struct cell_t));
cell->p = cons_cell;
cell->tag = CONS_CELL;
return cell;
}
struct cell_t* cell_from_cons_cell2(struct cell_t* a, struct cell_t* d) {
if (0 == a) a = &NA;
if (0 == d) d = &NA;
struct cons_cell_t* cs = CELL_ALLOC(sizeof(struct cons_cell_t));
cs->car = a;
cs->cdr = d;
return cell_from_cons_cell(cs);
}
struct cell_t* cell_from_fn(struct cell_t* paramsandbody) {
struct cell_t* fn = CELL_ALLOC(sizeof(struct cell_t));
fn->tag = FN;
fn->p = paramsandbody;
return fn;
}
struct cell_t* CONS(struct cell_t* a, struct cell_t* b) {
struct cons_cell_t* cons_cell = CELL_ALLOC(sizeof(struct cons_cell_t));
cons_cell->car = a;
cons_cell->cdr = b;
return cell_from_cons_cell(cons_cell);
}
struct cell_t* CDR(struct cell_t* cell) {
if (&NA == cell) return &NA;
if (cell->tag != CONS_CELL) return &NA;
struct cons_cell_t* ptr = (struct cons_cell_t*)cell->p;
if (0 == ptr) {
fatal_error("NULL pointer CDRed");
return &NA;
}
return ptr->cdr;
}
struct cell_t* CAR(struct cell_t* cell) {
if (&NA == cell) return &NA;
if (cell->tag != CONS_CELL) return &NA;
struct cons_cell_t* ptr = (struct cons_cell_t*)cell->p;
if (0 == ptr) {
fatal_error("NULL pointer CARed");
return &NA;
}
return ptr->car;
}
struct cell_t* CDAR(struct cell_t* cell) {
return CDR(CAR(cell));
}
struct cell_t* CAAR(struct cell_t* cell) {
return CAR(CAR(cell));
}
struct cell_t* CADR(struct cell_t* cell) {
return CAR(CDR(cell));
}
struct cell_t* CADDR(struct cell_t* cell) {
return CAR(CDR(CDR(cell)));
}
int is_nil(struct cell_t* cell) {
return cell && cell->tag == NIL;
}
int is_sym(struct cell_t* cell) {
return cell && cell->tag == SYM;
}
int is_list(struct cell_t* cell) {
if (!cell) return 0;
while (cell != &NA) {
if (cell->tag != CONS_CELL) return 0;
cell = CDR(cell);
}
return 1;
}
void print_list(struct cell_t* cell) {
if (!cell) return;
printf("'(");
while (cell != &NA) {
print_cell(CAR(cell));
cell = CDR(cell);
}
printf(")");
}
void print_cell(struct cell_t* cell) {
if (0 == cell)
return;
if (is_list(cell)) {
print_list(cell);
return;
}
if (is_nil(cell)) {
printf("NIL ");
return;
}
switch (cell->tag) {
case NIL: printf("NIL "); break;
case INTEGER: printf("%d ", cell->i); break;
case STRING: printf("%s ", cell->s); break;
case CONS_CELL: printf("["); print_cell(CAR(cell)); printf(","); print_cell(CDR(cell)); printf("] "); break;
case SYM : printf("#%s ", cell->s); break;
case BOOLEAN : if (cell->i) printf("True"); else printf("False"); break;
case CALL : printf("CALL "); break;
case FN : printf("FN "); print_cell(cell->p); break;
}
}
void mark_cells(struct cell_t* c) {
if (&NA == c) return;
mark(c);
switch(c->tag) {
case INTEGER:
case BOOLEAN:
case STRING:
case SYM:
case NIL:
case CALL:
break;
case FN:
mark_cells(c->p);
break;
case CONS_CELL:
mark_cells(c->p);
mark_cells(CAR(c));
mark_cells(CDR(c));
}
}
void gc(struct cell_t* root) {
mark_cells(root);
sweep();
}