--- stack/stack.c 2002/02/16 00:51:32 1.88 +++ stack/stack.c 2002/03/07 01:21:07 1.90 @@ -24,6 +24,8 @@ env->gc_limit= 20; env->gc_count= 0; + env->gc_ref= NULL; + env->gc_protect= NULL; env->head= NULL; for(i= 0; ihead)==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } env->head= env->head->next; /* Remove the top stack item */ free(temp); /* Free the old top stack item */ + + gc_init(env); } /* Returns a pointer to a pointer to an element in the hash table. */ @@ -87,16 +91,17 @@ value *nval= malloc(sizeof(value)); stackitem *nitem= malloc(sizeof(stackitem)); - if(env->gc_count >= env->gc_limit) - gc_init(env); - nval->content.ptr= NULL; + protect(env, nval); + + gc_init(env); nitem->item= nval; nitem->next= env->gc_ref; env->gc_ref= nitem; env->gc_count++; + unprotect(env); return nval; } @@ -124,12 +129,21 @@ symbol *tsymb; int i; + if(env->gc_count < env->gc_limit) + return; + while(iterator!=NULL) { iterator->item->gc_garb= 1; iterator= iterator->next; } /* Mark */ + iterator= env->gc_protect; + while(iterator!=NULL) { + gc_mark(iterator->item); + iterator= iterator->next; + } + iterator= env->head; while(iterator!=NULL) { gc_mark(iterator->item); @@ -148,6 +162,7 @@ /* Sweep */ while(env->gc_ref!=NULL) { + if(env->gc_ref->item->gc_garb) { switch(env->gc_ref->item->type) { case string: @@ -178,10 +193,25 @@ } } - env->gc_limit= env->gc_count+20; + env->gc_limit= env->gc_count*2; env->gc_ref= new_head; } +void protect(environment *env, value *val) +{ + stackitem *new_item= malloc(sizeof(stackitem)); + new_item->item= val; + new_item->next= env->gc_protect; + env->gc_protect= new_item; +} + +void unprotect(environment *env) +{ + stackitem *temp= env->gc_protect; + env->gc_protect= env->gc_protect->next; + free(temp); +} + /* Push a value onto the stack */ void push_val(environment *env, value *val) { @@ -216,20 +246,19 @@ /* Mangle a symbol name to a valid C identifier name */ char *mangle_str(const char *old_string){ - char validchars[] - ="0123456789abcdef"; + char validchars[]= "0123456789abcdef"; char *new_string, *current; - new_string=malloc((strlen(old_string)*2)+4); + new_string= malloc((strlen(old_string)*2)+4); strcpy(new_string, "sx_"); /* Stack eXternal */ - current=new_string+3; + current= new_string+3; while(old_string[0] != '\0'){ - current[0]=validchars[(unsigned char)(old_string[0])/16]; - current[1]=validchars[(unsigned char)(old_string[0])%16]; - current+=2; + current[0]= validchars[(unsigned char)(old_string[0])/16]; + current[1]= validchars[(unsigned char)(old_string[0])%16]; + current+= 2; old_string++; } - current[0]='\0'; + current[0]= '\0'; return new_string; /* The caller must free() it */ } @@ -239,13 +268,13 @@ if((env->head)==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } if(env->head->item->type!=string) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } @@ -298,18 +327,18 @@ if(handle==NULL) /* If no handle */ handle= dlopen(NULL, RTLD_LAZY); - mangled=mangle_str(in_string); /* mangle the name */ + mangled= mangle_str(in_string); /* mangle the name */ funcptr= dlsym(handle, mangled); /* and try to find it */ free(mangled); - dlerr=dlerror(); + dlerr= dlerror(); if(dlerr != NULL) { /* If no function was found */ funcptr= dlsym(handle, in_string); /* Get function pointer */ - dlerr=dlerror(); + dlerr= dlerror(); } if(dlerr==NULL) { /* If a function was found */ new_fvalue= new_val(env); /* Create a new value */ - new_fvalue->type=func; /* The new value is a function pointer */ - new_fvalue->content.ptr=funcptr; /* Store function pointer */ + new_fvalue->type= func; /* The new value is a function pointer */ + new_fvalue->content.ptr= funcptr; /* Store function pointer */ (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new function value */ } @@ -497,9 +526,11 @@ env->err=3; return; } + protect(env, val); toss(env); /* toss the symbol */ if(env->err) return; push_val(env, val); /* Return its bound value */ + unprotect(env); } /* If the top element is a symbol, determine if it's bound to a @@ -534,20 +565,25 @@ in_func= (funcp)(env->head->item->content.ptr); toss(env); if(env->err) return; - return (*in_func)(env); + return in_func(env); /* If it's a list */ case list: temp_val= env->head->item; + protect(env, temp_val); toss(env); if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; + unprotect(env); + while(iterator!=NULL) { push_val(env, iterator->item); + if(env->head->item->type==symb && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { toss(env); if(env->err) return; + if(iterator->next == NULL){ goto eval_start; } @@ -569,25 +605,25 @@ if((env->head)==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } if(env->head->item->type!=list) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } - old_head=(stackitem *)(env->head->item->content.ptr); - new_head=NULL; + old_head= (stackitem *)(env->head->item->content.ptr); + new_head= NULL; while(old_head != NULL){ - item=old_head; - old_head=old_head->next; - item->next=new_head; - new_head=item; + item= old_head; + old_head= old_head->next; + item->next= new_head; + new_head= item; } - env->head->item->content.ptr=new_head; + env->head->item->content.ptr= new_head; } /* Make a list. */ @@ -636,12 +672,12 @@ /* Is top element a list? */ if(env->head==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } if(env->head->item->type!=list) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } @@ -673,7 +709,7 @@ if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } @@ -693,13 +729,13 @@ if((env->head)==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } if(env->head->item->type!=integer) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } @@ -724,20 +760,18 @@ /* Needs two values on the stack, the top one must be a symbol */ if(env->head==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } if(env->head->item->type!=symb) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } /* long names are a pain */ - sym=env->head->item->content.ptr; - - /* if the symbol was bound to something else, throw it away */ + sym= env->head->item->content.ptr; /* Bind the symbol to the value */ sym->val= env->head->next->item; @@ -760,6 +794,7 @@ env->symbols[i]= NULL; } + env->gc_limit= 0; gc_init(env); if(env->free_string!=NULL) @@ -889,6 +924,7 @@ toss(&myenv); /* No error check in main */ eval(&myenv); } + gc_init(&myenv); } quit(&myenv); return EXIT_FAILURE; @@ -903,7 +939,7 @@ if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } @@ -911,6 +947,7 @@ && env->head->next->item->type==string) { a_val= env->head->item; b_val= env->head->next->item; + protect(env, a_val); protect(env, b_val); toss(env); if(env->err) return; toss(env); if(env->err) return; len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; @@ -918,6 +955,7 @@ strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); push_cstring(env, new_string); + unprotect(env); unprotect(env); free(new_string); return; } @@ -928,10 +966,10 @@ env->err=2; return; } - a=env->head->item->content.val; + a= env->head->item->content.val; toss(env); if(env->err) return; - b=env->head->item->content.val; + b= env->head->item->content.val; toss(env); if(env->err) return; push_int(env, a+b); } @@ -952,6 +990,7 @@ env->err=2; return; } + a=env->head->item->content.val; toss(env); if(env->err) return; b=env->head->item->content.val; @@ -975,6 +1014,7 @@ env->err=2; return; } + a=env->head->item->content.val; toss(env); if(env->err) return; b=env->head->item->content.val; @@ -986,42 +1026,46 @@ value *copy_val(environment *env, value *old_value){ stackitem *old_item, *new_item, *prev_item; - value *new_value=new_val(env); + value *new_value= new_val(env); - new_value->type=old_value->type; + protect(env, old_value); + new_value->type= old_value->type; switch(old_value->type){ case integer: - new_value->content.val=old_value->content.val; + new_value->content.val= old_value->content.val; break; case string: - (char *)(new_value->content.ptr) - = strdup((char *)(old_value->content.ptr)); + (char *)(new_value->content.ptr)= + strdup((char *)(old_value->content.ptr)); break; case func: case symb: - new_value->content.ptr=old_value->content.ptr; + new_value->content.ptr= old_value->content.ptr; break; case list: - new_value->content.ptr=NULL; + new_value->content.ptr= NULL; - prev_item=NULL; - old_item=(stackitem *)(old_value->content.ptr); + prev_item= NULL; + old_item= (stackitem*)(old_value->content.ptr); while(old_item != NULL) { /* While list is not empty */ new_item= malloc(sizeof(stackitem)); - new_item->item=copy_val(env, old_item->item); /* recurse */ - new_item->next=NULL; + new_item->item= copy_val(env, old_item->item); /* recurse */ + new_item->next= NULL; if(prev_item != NULL) /* If this wasn't the first item */ - prev_item->next=new_item; /* point the previous item to the + prev_item->next= new_item; /* point the previous item to the new item */ else - new_value->content.ptr=new_item; - old_item=old_item->next; - prev_item=new_item; + new_value->content.ptr= new_item; + old_item= old_item->next; + prev_item= new_item; } break; } + + unprotect(env); + return new_value; } @@ -1029,7 +1073,7 @@ extern void sx_647570(environment *env) { if((env->head)==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } push_val(env, copy_val(env, env->head->item)); @@ -1042,7 +1086,7 @@ if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } @@ -1115,9 +1159,11 @@ } loop= env->head->item; + protect(env, loop); toss(env); if(env->err) return; test= env->head->item; + protect(env, test); toss(env); if(env->err) return; do { @@ -1126,7 +1172,7 @@ if(env->head->item->type != integer) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } @@ -1141,30 +1187,82 @@ } } while(truth); + + unprotect(env); unprotect(env); } -/* "for"; For-loop */ + +/* "for"; for-loop */ extern void sx_666f72(environment *env) { + value *loop; + int foo1, foo2; + + if(env->head==NULL || env->head->next==NULL + || env->head->next->next==NULL) { + printerr("Too Few Arguments"); + env->err= 1; + return; + } + + if(env->head->next->item->type!=integer + || env->head->next->next->item->type!=integer) { + printerr("Bad Argument Type"); + env->err= 2; + return; + } + + loop= env->head->item; + protect(env, loop); + toss(env); if(env->err) return; + + foo2= env->head->item->content.val; + toss(env); if(env->err) return; + + foo1= env->head->item->content.val; + toss(env); if(env->err) return; + + if(foo1<=foo2) { + while(foo1<=foo2) { + push_int(env, foo1); + push_val(env, loop); + eval(env); if(env->err) return; + foo1++; + } + } else { + while(foo1>=foo2) { + push_int(env, foo1); + push_val(env, loop); + eval(env); if(env->err) return; + foo1--; + } + } + unprotect(env); +} + +/* Variant of for-loop */ +extern void foreach(environment *env) { value *loop, *foo; stackitem *iterator; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } if(env->head->next->item->type != list) { printerr("Bad Argument Type"); - env->err=2; + env->err= 2; return; } loop= env->head->item; + protect(env, loop); toss(env); if(env->err) return; foo= env->head->item; + protect(env, foo); toss(env); if(env->err) return; iterator= foo->content.ptr; @@ -1175,6 +1273,7 @@ eval(env); if(env->err) return; iterator= iterator->next; } + unprotect(env); unprotect(env); } /* "to" */ @@ -1235,9 +1334,9 @@ const char strform[]= "\"%[^\"]\"%n"; const char intform[]= "%i%n"; const char blankform[]= "%*[ \t]%n"; - const char ebrackform[]= "%*1[]]%n"; - const char semicform[]= "%*1[;]%n"; - const char bbrackform[]= "%*1[[]%n"; + const char ebrackform[]= "]%n"; + const char semicform[]= ";%n"; + const char bbrackform[]= "[%n"; int itemp, readlength= -1; static int depth= 0;