48 |
{ |
{ |
49 |
int i; |
int i; |
50 |
|
|
51 |
env->gc_limit= 20; |
env->gc_limit= 400000; |
52 |
env->gc_count= 0; |
env->gc_count= 0; |
53 |
env->gc_ref= NULL; |
env->gc_ref= NULL; |
|
env->gc_protect= NULL; |
|
54 |
|
|
55 |
env->head= NULL; |
env->head= NULL; |
56 |
for(i= 0; i<HASHTBLSIZE; i++) |
for(i= 0; i<HASHTBLSIZE; i++) |
80 |
|
|
81 |
env->head= env->head->next; /* Remove the top stack item */ |
env->head= env->head->next; /* Remove the top stack item */ |
82 |
free(temp); /* Free the old top stack item */ |
free(temp); /* Free the old top stack item */ |
|
|
|
|
env->gc_limit--; |
|
|
gc_init(env); |
|
83 |
} |
} |
84 |
|
|
85 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
123 |
nitem->next= env->gc_ref; |
nitem->next= env->gc_ref; |
124 |
env->gc_ref= nitem; |
env->gc_ref= nitem; |
125 |
|
|
126 |
env->gc_count++; |
env->gc_count += sizeof(value); |
127 |
|
nval->gc.flag.mark= 0; |
128 |
protect(env, nval); |
nval->gc.flag.protect= 0; |
|
gc_init(env); |
|
|
unprotect(env); |
|
129 |
|
|
130 |
return nval; |
return nval; |
131 |
} |
} |
132 |
|
|
133 |
/* Mark values recursively. |
/* Mark values recursively. |
134 |
Marked values are not collected by the GC. */ |
Marked values are not collected by the GC. */ |
135 |
void gc_mark(value *val) |
inline void gc_mark(value *val) |
136 |
{ |
{ |
137 |
stackitem *iterator; |
stackitem *iterator; |
138 |
|
|
139 |
if(val==NULL || val->gc_garb==0) |
if(val->gc.flag.mark) |
140 |
return; |
return; |
141 |
|
|
142 |
val->gc_garb= 0; |
val->gc.flag.mark= 1; |
143 |
|
|
144 |
if(val->type==list) { |
if(val->type==list) { |
145 |
iterator= val->content.ptr; |
iterator= val->content.ptr; |
151 |
} |
} |
152 |
} |
} |
153 |
|
|
154 |
|
inline void gc_maybe(environment *env) |
155 |
|
{ |
156 |
|
if(env->gc_count < env->gc_limit) |
157 |
|
return; |
158 |
|
else |
159 |
|
return gc_init(env); |
160 |
|
} |
161 |
|
|
162 |
/* Start GC */ |
/* Start GC */ |
163 |
extern void gc_init(environment *env) |
extern void gc_init(environment *env) |
164 |
{ |
{ |
166 |
symbol *tsymb; |
symbol *tsymb; |
167 |
int i; |
int i; |
168 |
|
|
169 |
if(env->gc_count < env->gc_limit) |
if(env->interactive){ |
170 |
return; |
printf("Garbage collecting."); |
|
|
|
|
/* Garb by default */ |
|
|
iterator= env->gc_ref; |
|
|
while(iterator!=NULL) { |
|
|
iterator->item->gc_garb= 1; |
|
|
iterator= iterator->next; |
|
|
} |
|
|
|
|
|
/* Mark protected values */ |
|
|
iterator= env->gc_protect; |
|
|
while(iterator!=NULL) { |
|
|
gc_mark(iterator->item); |
|
|
iterator= iterator->next; |
|
171 |
} |
} |
172 |
|
|
173 |
/* Mark values on stack */ |
/* Mark values on stack */ |
177 |
iterator= iterator->next; |
iterator= iterator->next; |
178 |
} |
} |
179 |
|
|
180 |
|
if(env->interactive){ |
181 |
|
printf("."); |
182 |
|
} |
183 |
|
|
184 |
/* Mark values in hashtable */ |
/* Mark values in hashtable */ |
185 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
186 |
tsymb= env->symbols[i]; |
tsymb= env->symbols[i]; |
187 |
while(tsymb!=NULL) { |
while(tsymb!=NULL) { |
188 |
gc_mark(tsymb->val); |
if (tsymb->val != NULL) |
189 |
|
gc_mark(tsymb->val); |
190 |
tsymb= tsymb->next; |
tsymb= tsymb->next; |
191 |
} |
} |
192 |
} |
} |
193 |
|
|
194 |
|
if(env->interactive){ |
195 |
|
printf("."); |
196 |
|
} |
197 |
|
|
198 |
env->gc_count= 0; |
env->gc_count= 0; |
199 |
|
|
200 |
while(env->gc_ref!=NULL) { /* Sweep unused values */ |
while(env->gc_ref!=NULL) { /* Sweep unused values */ |
201 |
|
|
202 |
if(env->gc_ref->item->gc_garb) { |
if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ |
203 |
|
|
204 |
switch(env->gc_ref->item->type) { /* Remove content */ |
switch(env->gc_ref->item->type) { /* Remove content */ |
205 |
case string: |
case string: |
211 |
env->gc_ref->item->content.ptr= titem->next; |
env->gc_ref->item->content.ptr= titem->next; |
212 |
free(titem); |
free(titem); |
213 |
} |
} |
|
break; |
|
214 |
default: |
default: |
|
break; |
|
215 |
} |
} |
216 |
free(env->gc_ref->item); /* Remove from gc_ref */ |
free(env->gc_ref->item); /* Remove from gc_ref */ |
217 |
titem= env->gc_ref->next; |
titem= env->gc_ref->next; |
218 |
free(env->gc_ref); /* Remove value */ |
free(env->gc_ref); /* Remove value */ |
219 |
env->gc_ref= titem; |
env->gc_ref= titem; |
220 |
} else { /* Keep values */ |
continue; |
221 |
titem= env->gc_ref->next; |
} else { |
222 |
env->gc_ref->next= new_head; |
env->gc_count += sizeof(value); |
223 |
new_head= env->gc_ref; |
if(env->gc_ref->item->type == string) |
224 |
env->gc_ref= titem; |
env->gc_count += strlen(env->gc_ref->item->content.ptr); |
|
env->gc_count++; |
|
225 |
} |
} |
226 |
|
|
227 |
|
/* Keep values */ |
228 |
|
titem= env->gc_ref->next; |
229 |
|
env->gc_ref->next= new_head; |
230 |
|
new_head= env->gc_ref; |
231 |
|
new_head->item->gc.flag.mark= 0; |
232 |
|
env->gc_ref= titem; |
233 |
} |
} |
234 |
|
|
235 |
env->gc_limit= env->gc_count*2; |
if (env->gc_limit < env->gc_count*2) |
236 |
|
env->gc_limit= env->gc_count*2; |
237 |
|
|
238 |
env->gc_ref= new_head; |
env->gc_ref= new_head; |
239 |
|
|
240 |
|
if(env->interactive){ |
241 |
|
printf("done\n"); |
242 |
|
} |
243 |
|
|
244 |
} |
} |
245 |
|
|
246 |
/* Protect values from GC */ |
/* Protect values from GC */ |
247 |
void protect(environment *env, value *val) |
void protect(value *val) |
248 |
{ |
{ |
249 |
stackitem *new_item= malloc(sizeof(stackitem)); |
stackitem *iterator; |
250 |
new_item->item= val; |
|
251 |
new_item->next= env->gc_protect; |
if(val->gc.flag.protect) |
252 |
env->gc_protect= new_item; |
return; |
253 |
|
|
254 |
|
val->gc.flag.protect= 1; |
255 |
|
|
256 |
|
if(val->type==list) { |
257 |
|
iterator= val->content.ptr; |
258 |
|
|
259 |
|
while(iterator!=NULL) { |
260 |
|
protect(iterator->item); |
261 |
|
iterator= iterator->next; |
262 |
|
} |
263 |
|
} |
264 |
} |
} |
265 |
|
|
266 |
/* Unprotect values from GC */ |
/* Unprotect values from GC */ |
267 |
void unprotect(environment *env) |
void unprotect(value *val) |
268 |
{ |
{ |
269 |
stackitem *temp= env->gc_protect; |
stackitem *iterator; |
270 |
env->gc_protect= env->gc_protect->next; |
|
271 |
free(temp); |
if(!(val->gc.flag.protect)) |
272 |
|
return; |
273 |
|
|
274 |
|
val->gc.flag.protect= 0; |
275 |
|
|
276 |
|
if(val->type==list) { |
277 |
|
iterator= val->content.ptr; |
278 |
|
|
279 |
|
while(iterator!=NULL) { |
280 |
|
unprotect(iterator->item); |
281 |
|
iterator= iterator->next; |
282 |
|
} |
283 |
|
} |
284 |
} |
} |
285 |
|
|
286 |
/* Push a value onto the stack */ |
/* Push a value onto the stack */ |
318 |
void push_cstring(environment *env, const char *in_string) |
void push_cstring(environment *env, const char *in_string) |
319 |
{ |
{ |
320 |
value *new_value= new_val(env); |
value *new_value= new_val(env); |
321 |
|
int length= strlen(in_string)+1; |
322 |
|
|
323 |
new_value->content.ptr= malloc(strlen(in_string)+1); |
new_value->content.ptr= malloc(length); |
324 |
|
env->gc_count += length; |
325 |
strcpy(new_value->content.ptr, in_string); |
strcpy(new_value->content.ptr, in_string); |
326 |
new_value->type= string; |
new_value->type= string; |
327 |
|
|
388 |
char *mangled; /* Mangled function name */ |
char *mangled; /* Mangled function name */ |
389 |
|
|
390 |
new_value= new_val(env); |
new_value= new_val(env); |
391 |
protect(env, new_value); |
protect(new_value); |
392 |
new_fvalue= new_val(env); |
new_fvalue= new_val(env); |
393 |
protect(env, new_fvalue); |
protect(new_fvalue); |
394 |
|
|
395 |
/* The new value is a symbol */ |
/* The new value is a symbol */ |
396 |
new_value->type= symb; |
new_value->type= symb; |
436 |
} |
} |
437 |
|
|
438 |
push_val(env, new_value); |
push_val(env, new_value); |
439 |
unprotect(env); unprotect(env); |
unprotect(new_value); unprotect(new_fvalue); |
440 |
} |
} |
441 |
|
|
442 |
/* Print newline. */ |
/* Print newline. */ |
629 |
env->err=3; |
env->err=3; |
630 |
return; |
return; |
631 |
} |
} |
632 |
protect(env, val); |
protect(val); |
633 |
toss(env); /* toss the symbol */ |
toss(env); /* toss the symbol */ |
634 |
if(env->err) return; |
if(env->err) return; |
635 |
push_val(env, val); /* Return its bound value */ |
push_val(env, val); /* Return its bound value */ |
636 |
unprotect(env); |
unprotect(val); |
637 |
} |
} |
638 |
|
|
639 |
/* If the top element is a symbol, determine if it's bound to a |
/* If the top element is a symbol, determine if it's bound to a |
647 |
|
|
648 |
eval_start: |
eval_start: |
649 |
|
|
650 |
|
gc_maybe(env); |
651 |
|
|
652 |
if(env->head==NULL) { |
if(env->head==NULL) { |
653 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
654 |
env->err=1; |
env->err=1; |
675 |
/* If it's a list */ |
/* If it's a list */ |
676 |
case list: |
case list: |
677 |
temp_val= env->head->item; |
temp_val= env->head->item; |
678 |
protect(env, temp_val); |
protect(temp_val); |
679 |
|
|
680 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
681 |
iterator= (stackitem*)temp_val->content.ptr; |
iterator= (stackitem*)temp_val->content.ptr; |
684 |
push_val(env, iterator->item); |
push_val(env, iterator->item); |
685 |
|
|
686 |
if(env->head->item->type==symb |
if(env->head->item->type==symb |
687 |
&& strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { |
&& (((symbol*)(env->head->item->content.ptr))->id[0] == ';')) { |
688 |
toss(env); |
toss(env); |
689 |
if(env->err) return; |
if(env->err) return; |
690 |
|
|
696 |
} |
} |
697 |
iterator= iterator->next; |
iterator= iterator->next; |
698 |
} |
} |
699 |
unprotect(env); |
unprotect(temp_val); |
700 |
return; |
return; |
701 |
|
|
702 |
default: |
default: |
740 |
|
|
741 |
iterator= env->head; |
iterator= env->head; |
742 |
pack= new_val(env); |
pack= new_val(env); |
743 |
protect(env, pack); |
protect(pack); |
744 |
|
|
745 |
if(iterator==NULL |
if(iterator==NULL |
746 |
|| (iterator->item->type==symb |
|| (iterator->item->type==symb |
771 |
push_val(env, pack); |
push_val(env, pack); |
772 |
rev(env); |
rev(env); |
773 |
|
|
774 |
unprotect(env); |
unprotect(pack); |
775 |
} |
} |
776 |
|
|
777 |
/* Relocate elements of the list on the stack. */ |
/* Relocate elements of the list on the stack. */ |
905 |
} |
} |
906 |
|
|
907 |
env->gc_limit= 0; |
env->gc_limit= 0; |
908 |
gc_init(env); |
gc_maybe(env); |
909 |
|
|
910 |
if(env->free_string!=NULL) |
if(env->free_string!=NULL) |
911 |
free(env->free_string); |
free(env->free_string); |
1044 |
toss(&myenv); /* No error check in main */ |
toss(&myenv); /* No error check in main */ |
1045 |
eval(&myenv); |
eval(&myenv); |
1046 |
} |
} |
1047 |
gc_init(&myenv); |
gc_maybe(&myenv); |
1048 |
} |
} |
1049 |
quit(&myenv); |
quit(&myenv); |
1050 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
1069 |
&& env->head->next->item->type==string) { |
&& env->head->next->item->type==string) { |
1070 |
a_val= env->head->item; |
a_val= env->head->item; |
1071 |
b_val= env->head->next->item; |
b_val= env->head->next->item; |
1072 |
protect(env, a_val); protect(env, b_val); |
protect(a_val); protect(b_val); |
1073 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1074 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1075 |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; |
1077 |
strcpy(new_string, b_val->content.ptr); |
strcpy(new_string, b_val->content.ptr); |
1078 |
strcat(new_string, a_val->content.ptr); |
strcat(new_string, a_val->content.ptr); |
1079 |
push_cstring(env, new_string); |
push_cstring(env, new_string); |
1080 |
unprotect(env); unprotect(env); |
unprotect(a_val); unprotect(b_val); |
1081 |
free(new_string); |
free(new_string); |
1082 |
|
|
1083 |
return; |
return; |
1278 |
stackitem *old_item, *new_item, *prev_item; |
stackitem *old_item, *new_item, *prev_item; |
1279 |
value *new_value; |
value *new_value; |
1280 |
|
|
1281 |
protect(env, old_value); |
protect(old_value); |
1282 |
new_value= new_val(env); |
new_value= new_val(env); |
1283 |
protect(env, new_value); |
protect(new_value); |
1284 |
new_value->type= old_value->type; |
new_value->type= old_value->type; |
1285 |
|
|
1286 |
switch(old_value->type){ |
switch(old_value->type){ |
1315 |
break; |
break; |
1316 |
} |
} |
1317 |
|
|
1318 |
unprotect(env); unprotect(env); |
unprotect(old_value); unprotect(new_value); |
1319 |
|
|
1320 |
return new_value; |
return new_value; |
1321 |
} |
} |
1411 |
} |
} |
1412 |
|
|
1413 |
loop= env->head->item; |
loop= env->head->item; |
1414 |
protect(env, loop); |
protect(loop); |
1415 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1416 |
|
|
1417 |
test= env->head->item; |
test= env->head->item; |
1418 |
protect(env, test); |
protect(test); |
1419 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1420 |
|
|
1421 |
do { |
do { |
1440 |
|
|
1441 |
} while(truth); |
} while(truth); |
1442 |
|
|
1443 |
unprotect(env); unprotect(env); |
unprotect(loop); unprotect(test); |
1444 |
} |
} |
1445 |
|
|
1446 |
|
|
1465 |
} |
} |
1466 |
|
|
1467 |
loop= env->head->item; |
loop= env->head->item; |
1468 |
protect(env, loop); |
protect(loop); |
1469 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1470 |
|
|
1471 |
foo2= env->head->item->content.i; |
foo2= env->head->item->content.i; |
1489 |
foo1--; |
foo1--; |
1490 |
} |
} |
1491 |
} |
} |
1492 |
unprotect(env); |
unprotect(loop); |
1493 |
} |
} |
1494 |
|
|
1495 |
/* Variant of for-loop */ |
/* Variant of for-loop */ |
1511 |
} |
} |
1512 |
|
|
1513 |
loop= env->head->item; |
loop= env->head->item; |
1514 |
protect(env, loop); |
protect(loop); |
1515 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1516 |
|
|
1517 |
foo= env->head->item; |
foo= env->head->item; |
1518 |
protect(env, foo); |
protect(foo); |
1519 |
toss(env); if(env->err) return; |
toss(env); if(env->err) return; |
1520 |
|
|
1521 |
iterator= foo->content.ptr; |
iterator= foo->content.ptr; |
1526 |
eval(env); if(env->err) return; |
eval(env); if(env->err) return; |
1527 |
iterator= iterator->next; |
iterator= iterator->next; |
1528 |
} |
} |
1529 |
unprotect(env); unprotect(env); |
unprotect(loop); unprotect(foo); |
1530 |
} |
} |
1531 |
|
|
1532 |
/* "to" */ |
/* "to" */ |
1566 |
|
|
1567 |
iterator= env->head; |
iterator= env->head; |
1568 |
pack= new_val(env); |
pack= new_val(env); |
1569 |
protect(env, pack); |
protect(pack); |
1570 |
|
|
1571 |
if(iterator==NULL |
if(iterator==NULL |
1572 |
|| (iterator->item->type==symb |
|| (iterator->item->type==symb |
1596 |
|
|
1597 |
push_val(env, pack); |
push_val(env, pack); |
1598 |
|
|
1599 |
unprotect(env); |
unprotect(pack); |
1600 |
} |
} |
1601 |
|
|
1602 |
/* Read a string */ |
/* Read a string */ |