--- stack/stack.c 2002/03/08 16:09:30 1.93 +++ stack/stack.c 2002/03/10 13:00:01 1.101 @@ -48,10 +48,9 @@ { int i; - env->gc_limit= 20; + env->gc_limit= 400000; env->gc_count= 0; env->gc_ref= NULL; - env->gc_protect= NULL; env->head= NULL; for(i= 0; iinteractive= 1; } -void printerr(const char* in_string) { +void printerr(const char* in_string) +{ fprintf(stderr, "Err: %s\n", in_string); } @@ -80,8 +80,6 @@ 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. */ @@ -113,7 +111,9 @@ } } -value* new_val(environment *env) { +/* Create new value */ +value* new_val(environment *env) +{ value *nval= malloc(sizeof(value)); stackitem *nitem= malloc(sizeof(stackitem)); @@ -123,22 +123,23 @@ nitem->next= env->gc_ref; env->gc_ref= nitem; - env->gc_count++; - - protect(env, nval); - gc_init(env); - unprotect(env); + env->gc_count += sizeof(value); + nval->gc.flag.mark= 0; + nval->gc.flag.protect= 0; return nval; } -void gc_mark(value *val) { +/* Mark values recursively. + Marked values are not collected by the GC. */ +inline void gc_mark(value *val) +{ stackitem *iterator; - if(val==NULL || val->gc_garb==0) + if(val->gc.flag.mark) return; - val->gc_garb= 0; + val->gc.flag.mark= 1; if(val->type==list) { iterator= val->content.ptr; @@ -150,53 +151,57 @@ } } -extern void gc_init(environment *env) { +inline void gc_maybe(environment *env) +{ + if(env->gc_count < env->gc_limit) + return; + else + return gc_init(env); +} + +/* Start GC */ +extern void gc_init(environment *env) +{ stackitem *new_head= NULL, *titem, *iterator; symbol *tsymb; int i; - if(env->gc_count < env->gc_limit) - return; - - /* Garb by default */ - iterator= env->gc_ref; - while(iterator!=NULL) { - iterator->item->gc_garb= 1; - iterator= iterator->next; + if(env->interactive){ + printf("Garbage collecting."); } - /* Mark protected values */ - iterator= env->gc_protect; + /* Mark values on stack */ + iterator= env->head; while(iterator!=NULL) { gc_mark(iterator->item); iterator= iterator->next; } - /* Mark values in stack */ - iterator= env->head; - while(iterator!=NULL) { - gc_mark(iterator->item); - iterator= iterator->next; + if(env->interactive){ + printf("."); } /* Mark values in hashtable */ for(i= 0; isymbols[i]; while(tsymb!=NULL) { - gc_mark(tsymb->val); + if (tsymb->val != NULL) + gc_mark(tsymb->val); tsymb= tsymb->next; } } + if(env->interactive){ + printf("."); + } + env->gc_count= 0; - /* Sweep */ - while(env->gc_ref!=NULL) { + while(env->gc_ref!=NULL) { /* Sweep unused values */ - if(env->gc_ref->item->gc_garb) { + if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ - /* Remove content */ - switch(env->gc_ref->item->type) { + switch(env->gc_ref->item->type) { /* Remove content */ case string: free(env->gc_ref->item->content.ptr); break; @@ -206,40 +211,76 @@ env->gc_ref->item->content.ptr= titem->next; free(titem); } - break; default: - break; } free(env->gc_ref->item); /* Remove from gc_ref */ titem= env->gc_ref->next; free(env->gc_ref); /* Remove value */ env->gc_ref= titem; - } else { /* Save */ - titem= env->gc_ref->next; - env->gc_ref->next= new_head; - new_head= env->gc_ref; - env->gc_ref= titem; - env->gc_count++; + continue; + } else { + env->gc_count += sizeof(value); + if(env->gc_ref->item->type == string) + env->gc_count += strlen(env->gc_ref->item->content.ptr); } + + /* Keep values */ + titem= env->gc_ref->next; + env->gc_ref->next= new_head; + new_head= env->gc_ref; + new_head->item->gc.flag.mark= 0; + env->gc_ref= titem; } - env->gc_limit= env->gc_count*2; + if (env->gc_limit < env->gc_count*2) + env->gc_limit= env->gc_count*2; + env->gc_ref= new_head; + + if(env->interactive){ + printf("done\n"); + } + } -void protect(environment *env, value *val) +/* Protect values from GC */ +void protect(value *val) { - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item= val; - new_item->next= env->gc_protect; - env->gc_protect= new_item; + stackitem *iterator; + + if(val->gc.flag.protect) + return; + + val->gc.flag.protect= 1; + + if(val->type==list) { + iterator= val->content.ptr; + + while(iterator!=NULL) { + protect(iterator->item); + iterator= iterator->next; + } + } } -void unprotect(environment *env) +/* Unprotect values from GC */ +void unprotect(value *val) { - stackitem *temp= env->gc_protect; - env->gc_protect= env->gc_protect->next; - free(temp); + stackitem *iterator; + + if(!(val->gc.flag.protect)) + return; + + val->gc.flag.protect= 0; + + if(val->type==list) { + iterator= val->content.ptr; + + while(iterator!=NULL) { + unprotect(iterator->item); + iterator= iterator->next; + } + } } /* Push a value onto the stack */ @@ -251,7 +292,7 @@ env->head= new_item; } -/* Push an integer onto the stack. */ +/* Push an integer onto the stack */ void push_int(environment *env, int in_val) { value *new_value= new_val(env); @@ -262,6 +303,7 @@ push_val(env, new_value); } +/* Push a floating point number onto the stack */ void push_float(environment *env, float in_val) { value *new_value= new_val(env); @@ -276,8 +318,10 @@ void push_cstring(environment *env, const char *in_string) { value *new_value= new_val(env); + int length= strlen(in_string)+1; - new_value->content.ptr= malloc(strlen(in_string)+1); + new_value->content.ptr= malloc(length); + env->gc_count += length; strcpy(new_value->content.ptr, in_string); new_value->type= string; @@ -285,7 +329,8 @@ } /* Mangle a symbol name to a valid C identifier name */ -char *mangle_str(const char *old_string){ +char *mangle_str(const char *old_string) +{ char validchars[]= "0123456789abcdef"; char *new_string, *current; @@ -303,7 +348,8 @@ return new_string; /* The caller must free() it */ } -extern void mangle(environment *env){ +extern void mangle(environment *env) +{ char *new_string; if((env->head)==NULL) { @@ -342,9 +388,9 @@ char *mangled; /* Mangled function name */ new_value= new_val(env); - protect(env, new_value); + protect(new_value); new_fvalue= new_val(env); - protect(env, new_fvalue); + protect(new_fvalue); /* The new value is a symbol */ new_value->type= symb; @@ -372,21 +418,25 @@ mangled= mangle_str(in_string); /* mangle the name */ funcptr= dlsym(handle, mangled); /* and try to find it */ - free(mangled); + dlerr= dlerror(); if(dlerr != NULL) { /* If no function was found */ funcptr= dlsym(handle, in_string); /* Get function pointer */ dlerr= dlerror(); } + if(dlerr==NULL) { /* If a function was found */ 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 */ } + + free(mangled); } + push_val(env, new_value); - unprotect(env); unprotect(env); + unprotect(new_value); unprotect(new_fvalue); } /* Print newline. */ @@ -396,7 +446,8 @@ } /* Gets the type of a value */ -extern void type(environment *env){ +extern void type(environment *env) +{ int typenum; if((env->head)==NULL) { @@ -464,7 +515,8 @@ } } -extern void print_(environment *env) { +extern void print_(environment *env) +{ if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -482,7 +534,8 @@ toss(env); } -extern void princ_(environment *env) { +extern void princ_(environment *env) +{ if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -516,6 +569,7 @@ printf("Stack Empty\n"); return; } + print_st(env->head, 1); } @@ -575,11 +629,11 @@ env->err=3; return; } - protect(env, val); + protect(val); toss(env); /* toss the symbol */ if(env->err) return; push_val(env, val); /* Return its bound value */ - unprotect(env); + unprotect(val); } /* If the top element is a symbol, determine if it's bound to a @@ -593,6 +647,8 @@ eval_start: + gc_maybe(env); + if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -619,7 +675,7 @@ /* If it's a list */ case list: temp_val= env->head->item; - protect(env, temp_val); + protect(temp_val); toss(env); if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; @@ -628,7 +684,7 @@ push_val(env, iterator->item); if(env->head->item->type==symb - && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { + && (((symbol*)(env->head->item->content.ptr))->id[0] == ';')) { toss(env); if(env->err) return; @@ -640,7 +696,7 @@ } iterator= iterator->next; } - unprotect(env); + unprotect(temp_val); return; default: @@ -649,7 +705,8 @@ } /* Reverse (flip) a list */ -extern void rev(environment *env){ +extern void rev(environment *env) +{ stackitem *old_head, *new_head, *item; if((env->head)==NULL) { @@ -683,7 +740,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -714,7 +771,7 @@ push_val(env, pack); rev(env); - unprotect(env); + unprotect(pack); } /* Relocate elements of the list on the stack. */ @@ -835,7 +892,7 @@ /* Quit stack. */ extern void quit(environment *env) { - long i; + int i; clear(env); @@ -848,7 +905,7 @@ } env->gc_limit= 0; - gc_init(env); + gc_maybe(env); if(env->free_string!=NULL) free(env->free_string); @@ -881,7 +938,8 @@ } /* Internal forget function */ -void forget_sym(symbol **hash_entry) { +void forget_sym(symbol **hash_entry) +{ symbol *temp; temp= *hash_entry; @@ -916,7 +974,8 @@ } /* Returns the current error number to the stack */ -extern void errn(environment *env){ +extern void errn(environment *env) +{ push_int(env, env->err); } @@ -957,7 +1016,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.93 $\n\ + printf("Stack version $Revision: 1.101 $\n\ Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ Stack comes with ABSOLUTELY NO WARRANTY; for details type `warranty;'.\n\ This is free software, and you are welcome to redistribute it\n\ @@ -985,14 +1044,15 @@ toss(&myenv); /* No error check in main */ eval(&myenv); } - gc_init(&myenv); + gc_maybe(&myenv); } quit(&myenv); return EXIT_FAILURE; } /* "+" */ -extern void sx_2b(environment *env) { +extern void sx_2b(environment *env) +{ int a, b; float fa, fb; size_t len; @@ -1009,7 +1069,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); + protect(a_val); protect(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; @@ -1017,7 +1077,7 @@ strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); push_cstring(env, new_string); - unprotect(env); unprotect(env); + unprotect(a_val); unprotect(b_val); free(new_string); return; @@ -1072,7 +1132,8 @@ } /* "-" */ -extern void sx_2d(environment *env) { +extern void sx_2d(environment *env) +{ int a, b; float fa, fb; @@ -1131,7 +1192,8 @@ } /* ">" */ -extern void sx_3e(environment *env) { +extern void sx_3e(environment *env) +{ int a, b; float fa, fb; @@ -1190,31 +1252,35 @@ } /* "<" */ -extern void sx_3c(environment *env) { +extern void sx_3c(environment *env) +{ swap(env); if(env->err) return; sx_3e(env); } /* "<=" */ -extern void sx_3c3d(environment *env) { +extern void sx_3c3d(environment *env) +{ sx_3e(env); if(env->err) return; not(env); } /* ">=" */ -extern void sx_3e3d(environment *env) { +extern void sx_3e3d(environment *env) +{ sx_3c(env); if(env->err) return; not(env); } /* Return copy of a value */ -value *copy_val(environment *env, value *old_value){ +value *copy_val(environment *env, value *old_value) +{ stackitem *old_item, *new_item, *prev_item; value *new_value; - protect(env, old_value); + protect(old_value); new_value= new_val(env); - protect(env, new_value); + protect(new_value); new_value->type= old_value->type; switch(old_value->type){ @@ -1249,13 +1315,14 @@ break; } - unprotect(env); unprotect(env); + unprotect(old_value); unprotect(new_value); return new_value; } /* "dup"; duplicates an item on the stack */ -extern void sx_647570(environment *env) { +extern void sx_647570(environment *env) +{ if((env->head)==NULL) { printerr("Too Few Arguments"); env->err= 1; @@ -1265,8 +1332,8 @@ } /* "if", If-Then */ -extern void sx_6966(environment *env) { - +extern void sx_6966(environment *env) +{ int truth; if((env->head)==NULL || env->head->next==NULL) { @@ -1296,8 +1363,8 @@ } /* If-Then-Else */ -extern void ifelse(environment *env) { - +extern void ifelse(environment *env) +{ int truth; if((env->head)==NULL || env->head->next==NULL @@ -1332,8 +1399,8 @@ } /* "while" */ -extern void sx_7768696c65(environment *env) { - +extern void sx_7768696c65(environment *env) +{ int truth; value *loop, *test; @@ -1344,11 +1411,11 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; test= env->head->item; - protect(env, test); + protect(test); toss(env); if(env->err) return; do { @@ -1373,12 +1440,13 @@ } while(truth); - unprotect(env); unprotect(env); + unprotect(loop); unprotect(test); } /* "for"; for-loop */ -extern void sx_666f72(environment *env) { +extern void sx_666f72(environment *env) +{ value *loop; int foo1, foo2; @@ -1397,7 +1465,7 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo2= env->head->item->content.i; @@ -1421,12 +1489,12 @@ foo1--; } } - unprotect(env); + unprotect(loop); } /* Variant of for-loop */ -extern void foreach(environment *env) { - +extern void foreach(environment *env) +{ value *loop, *foo; stackitem *iterator; @@ -1443,11 +1511,11 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo= env->head->item; - protect(env, foo); + protect(foo); toss(env); if(env->err) return; iterator= foo->content.ptr; @@ -1458,15 +1526,16 @@ eval(env); if(env->err) return; iterator= iterator->next; } - unprotect(env); unprotect(env); + unprotect(loop); unprotect(foo); } /* "to" */ -extern void to(environment *env) { - int i, start, ending; - stackitem *temp_head; - value *temp_val; - +extern void to(environment *env) +{ + int ending, start, i; + stackitem *iterator, *temp; + value *pack; + if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -1485,8 +1554,7 @@ start= env->head->item->content.i; toss(env); if(env->err) return; - temp_head= env->head; - env->head= NULL; + push_sym(env, "["); if(ending>=start) { for(i= ending; i>=start; i--) @@ -1496,19 +1564,44 @@ push_int(env, i); } - temp_val= new_val(env); - protect(env, temp_val); + iterator= env->head; + pack= new_val(env); + protect(pack); - temp_val->content.ptr= env->head; - temp_val->type= list; - env->head= temp_head; - push_val(env, temp_val); + if(iterator==NULL + || (iterator->item->type==symb + && ((symbol*)(iterator->item->content.ptr))->id[0]=='[')) { + temp= NULL; + toss(env); + } else { + /* Search for first delimiter */ + while(iterator->next!=NULL + && (iterator->next->item->type!=symb + || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) + iterator= iterator->next; + + /* Extract list */ + temp= env->head; + env->head= iterator->next; + iterator->next= NULL; - unprotect(env); + pack->type= list; + pack->content.ptr= temp; + + if(env->head!=NULL) + toss(env); + } + + /* Push list */ + + push_val(env, pack); + + unprotect(pack); } /* Read a string */ -extern void readline(environment *env) { +extern void readline(environment *env) +{ char in_string[101]; if(fgets(in_string, 100, env->inputstream)==NULL) @@ -1518,7 +1611,8 @@ } /* "read"; Read a value and place on stack */ -extern void sx_72656164(environment *env) { +extern void sx_72656164(environment *env) +{ const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; const char strform[]= "\"%[^\"]\"%n"; const char intform[]= "%i%n"; @@ -1597,8 +1691,8 @@ return sx_72656164(env); } -extern void beep(environment *env) { - +extern void beep(environment *env) +{ int freq, dur, period, ticks; if((env->head)==NULL || env->head->next==NULL) { @@ -1638,11 +1732,11 @@ default: abort(); } -}; +} /* "wait" */ -extern void sx_77616974(environment *env) { - +extern void sx_77616974(environment *env) +{ int dur; if((env->head)==NULL) { @@ -1661,9 +1755,10 @@ toss(env); usleep(dur); -}; +} -extern void copying(environment *env){ +extern void copying(environment *env) +{ printf("GNU GENERAL PUBLIC LICENSE\n\ Version 2, June 1991\n\ \n\ @@ -1922,7 +2017,8 @@ of promoting the sharing and reuse of software generally.\n"); } -extern void warranty(environment *env){ +extern void warranty(environment *env) +{ printf(" NO WARRANTY\n\ \n\ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n\ @@ -2073,7 +2169,7 @@ if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); - env->err=1; + env->err= 1; return; } @@ -2091,3 +2187,29 @@ printerr("Bad Argument Type"); env->err=2; } + +/* "div" */ +extern void sx_646976(environment *env) +{ + int a, b; + + if((env->head)==NULL || env->head->next==NULL) { + printerr("Too Few Arguments"); + env->err= 1; + return; + } + + if(env->head->item->type==integer + && env->head->next->item->type==integer) { + a= env->head->item->content.i; + toss(env); if(env->err) return; + b= env->head->item->content.i; + toss(env); if(env->err) return; + push_int(env, (int)b/a); + + return; + } + + printerr("Bad Argument Type"); + env->err= 2; +}