1 |
|
/* -*- coding: utf-8; -*- */ |
2 |
/* |
/* |
3 |
stack - an interactive interpreter for a stack-based language |
stack - an interactive interpreter for a stack-based language |
4 |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn |
Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn |
166 |
extern void gc_init(environment *env) |
extern void gc_init(environment *env) |
167 |
{ |
{ |
168 |
stackitem *new_head= NULL, *titem; |
stackitem *new_head= NULL, *titem; |
|
cons *iterator; |
|
169 |
symbol *tsymb; |
symbol *tsymb; |
170 |
int i; |
int i; |
171 |
|
|
195 |
|
|
196 |
if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ |
if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ |
197 |
|
|
198 |
if(env->gc_ref->item->type==string) /* Remove content */ |
/* Remove content */ |
199 |
|
switch(env->gc_ref->item->type){ |
200 |
|
case string: |
201 |
free(env->gc_ref->item->content.ptr); |
free(env->gc_ref->item->content.ptr); |
202 |
|
break; |
203 |
|
case tcons: |
204 |
|
free(env->gc_ref->item->content.c); |
205 |
|
break; |
206 |
|
case empty: |
207 |
|
case integer: |
208 |
|
case tfloat: |
209 |
|
case func: |
210 |
|
case symb: |
211 |
|
/* Symbol strings are freed when walking the hash table */ |
212 |
|
} |
213 |
|
|
214 |
free(env->gc_ref->item); /* Remove from gc_ref */ |
free(env->gc_ref->item); /* Remove from gc_ref */ |
215 |
titem= env->gc_ref->next; |
titem= env->gc_ref->next; |
246 |
/* Keep values */ |
/* Keep values */ |
247 |
env->gc_count += sizeof(value); |
env->gc_count += sizeof(value); |
248 |
if(env->gc_ref->item->type==string) |
if(env->gc_ref->item->type==string) |
249 |
env->gc_count += strlen(env->gc_ref->item->content.ptr); |
env->gc_count += strlen(env->gc_ref->item->content.ptr)+1; |
250 |
|
|
251 |
titem= env->gc_ref->next; |
titem= env->gc_ref->next; |
252 |
env->gc_ref->next= new_head; |
env->gc_ref->next= new_head; |
298 |
{ |
{ |
299 |
value *new_value= new_val(env); |
value *new_value= new_val(env); |
300 |
|
|
301 |
new_value->content.c= malloc(sizeof(cons)); |
new_value->content.c= malloc(sizeof(pair)); |
302 |
assert(new_value->content.c!=NULL); |
assert(new_value->content.c!=NULL); |
303 |
|
env->gc_count += sizeof(pair); |
304 |
new_value->type= tcons; |
new_value->type= tcons; |
305 |
CAR(new_value)= val; |
CAR(new_value)= val; |
306 |
CDR(new_value)= env->head; |
CDR(new_value)= env->head; |
498 |
toss(env); |
toss(env); |
499 |
} |
} |
500 |
|
|
501 |
/* Prints the top element of the stack. */ |
/* Print a value */ |
502 |
void print_val(value *val, int noquote) |
void print_val(value *val, int noquote) |
503 |
{ |
{ |
504 |
switch(val->type) { |
switch(val->type) { |
527 |
printf("[ "); |
printf("[ "); |
528 |
do { |
do { |
529 |
print_val(CAR(val), noquote); |
print_val(CAR(val), noquote); |
530 |
switch(CDR(val)->type){ |
val= CDR(val); |
531 |
|
switch(val->type){ |
532 |
case empty: |
case empty: |
533 |
break; |
break; |
534 |
case tcons: |
case tcons: |
536 |
break; |
break; |
537 |
default: |
default: |
538 |
printf(" . "); /* Improper list */ |
printf(" . "); /* Improper list */ |
539 |
print_val(CDR(val), noquote); |
print_val(val, noquote); |
540 |
} |
} |
|
val= CDR(val); |
|
541 |
} while(val->type == tcons); |
} while(val->type == tcons); |
542 |
printf(" ]"); |
printf(" ]"); |
543 |
break; |
break; |
734 |
unprotect(temp_val); |
unprotect(temp_val); |
735 |
return; |
return; |
736 |
|
|
737 |
default: |
case empty: |
738 |
|
case integer: |
739 |
|
case tfloat: |
740 |
|
case string: |
741 |
return; |
return; |
742 |
} |
} |
743 |
} |
} |
1341 |
case integer: |
case integer: |
1342 |
case func: |
case func: |
1343 |
case symb: |
case symb: |
1344 |
|
case empty: |
1345 |
new_value->content= old_value->content; |
new_value->content= old_value->content; |
1346 |
break; |
break; |
1347 |
case string: |
case string: |
1350 |
break; |
break; |
1351 |
case tcons: |
case tcons: |
1352 |
|
|
1353 |
new_value->content.c= malloc(sizeof(cons)); |
new_value->content.c= malloc(sizeof(pair)); |
1354 |
assert(new_value->content.c!=NULL); |
assert(new_value->content.c!=NULL); |
1355 |
|
env->gc_count += sizeof(pair); |
1356 |
|
|
1357 |
CAR(new_value)= copy_val(env, CAR(old_value)); /* recurse */ |
CAR(new_value)= copy_val(env, CAR(old_value)); /* recurse */ |
1358 |
CDR(new_value)= copy_val(env, CDR(old_value)); /* recurse */ |
CDR(new_value)= copy_val(env, CDR(old_value)); /* recurse */ |
1710 |
int count= -1; |
int count= -1; |
1711 |
float ftemp; |
float ftemp; |
1712 |
static int depth= 0; |
static int depth= 0; |
1713 |
char *match, *ctemp; |
char *match; |
1714 |
size_t inlength; |
size_t inlength; |
1715 |
|
|
1716 |
if(env->in_string==NULL) { |
if(env->in_string==NULL) { |
1744 |
} else { |
} else { |
1745 |
push_float(env, ftemp); |
push_float(env, ftemp); |
1746 |
} |
} |
1747 |
|
} else if(sscanf(env->in_string, "\"\"%n", &readlength) != EOF |
1748 |
|
&& readlength != -1) { |
1749 |
|
push_cstring(env, ""); |
1750 |
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
} else if(sscanf(env->in_string, strform, match, &readlength) != EOF |
1751 |
&& readlength != -1) { |
&& readlength != -1) { |
1752 |
push_cstring(env, match); |
push_cstring(env, match); |
2372 |
|
|
2373 |
CAR(env->head)=CDR(CAR(env->head)); |
CAR(env->head)=CDR(CAR(env->head)); |
2374 |
} |
} |
2375 |
|
|
2376 |
|
extern void cons(environment *env) |
2377 |
|
{ |
2378 |
|
value *val; |
2379 |
|
|
2380 |
|
if(env->head->type==empty || CDR(env->head)->type==empty) { |
2381 |
|
printerr("Too Few Arguments"); |
2382 |
|
env->err= 1; |
2383 |
|
return; |
2384 |
|
} |
2385 |
|
|
2386 |
|
val=new_val(env); |
2387 |
|
val->content.c= malloc(sizeof(pair)); |
2388 |
|
assert(val->content.c!=NULL); |
2389 |
|
|
2390 |
|
env->gc_count += sizeof(pair); |
2391 |
|
val->type=tcons; |
2392 |
|
|
2393 |
|
CAR(val)= CAR(CDR(env->head)); |
2394 |
|
CDR(val)= CAR(env->head); |
2395 |
|
|
2396 |
|
push_val(env, val); |
2397 |
|
|
2398 |
|
swap(env); if(env->err) return; |
2399 |
|
toss(env); if(env->err) return; |
2400 |
|
swap(env); if(env->err) return; |
2401 |
|
toss(env); if(env->err) return; |
2402 |
|
} |