--- stack/stack.c 2002/02/11 22:07:47 1.71 +++ stack/stack.c 2002/02/14 05:21:06 1.78 @@ -9,7 +9,7 @@ /* strcmp, strcpy, strlen, strcat, strdup */ #include -#define HASHTBLSIZE 65536 +#define HASHTBLSIZE 2048 /* First, define some types. */ @@ -60,6 +60,8 @@ int err; /* Error flag */ int non_eval_flag; char *in_string; /* Input pending to be read */ + char *free_string; /* Free this string when all input is + read from in_string */ } environment; /* A type for pointers to external functions */ @@ -155,49 +157,39 @@ } } -/* Generic push function. */ -void push(stackitem** stack_head, stackitem* in_item) -{ - in_item->next= *stack_head; - *stack_head= in_item; -} - /* Push a value onto the stack */ -void push_val(stackitem **stack_head, value *val) +void push_val(environment *env, value *val) { stackitem *new_item= malloc(sizeof(stackitem)); new_item->item= val; val->refcount++; - push(stack_head, new_item); + new_item->next= env->head; + env->head= new_item; } /* Push an integer onto the stack. */ -void push_int(stackitem **stack_head, int in_val) +void push_int(environment *env, int in_val) { value *new_value= malloc(sizeof(value)); - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item= new_value; new_value->content.val= in_val; new_value->type= integer; new_value->refcount=1; - push(stack_head, new_item); + push_val(env, new_value); } /* Copy a string onto the stack. */ -void push_cstring(stackitem **stack_head, const char *in_string) +void push_cstring(environment *env, const char *in_string) { value *new_value= malloc(sizeof(value)); - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item=new_value; new_value->content.ptr= malloc(strlen(in_string)+1); strcpy(new_value->content.ptr, in_string); new_value->type= string; new_value->refcount=1; - push(stack_head, new_item); + push_val(env, new_value); } /* Mangle a symbol name to a valid C identifier name */ @@ -246,14 +238,12 @@ new_value->type= string; new_value->refcount=1; - push_val(&(env->head), new_value); + push_val(env, new_value); } /* Push a symbol onto the stack. */ void push_sym(environment *env, const char *in_string) { - stackitem *new_item; /* The new stack item */ - /* ...which will contain... */ value *new_value; /* A new symbol value */ /* ...which might point to... */ symbol **new_symbol; /* (if needed) A new actual symbol */ @@ -266,10 +256,7 @@ const char *dlerr; /* Dynamic linker error */ char *mangled; /* Mangled function name */ - /* Create a new stack item containing a new value */ - new_item= malloc(sizeof(stackitem)); new_value= malloc(sizeof(value)); - new_item->item=new_value; /* The new value is a symbol */ new_value->type= symb; @@ -313,7 +300,7 @@ new_fvalue->refcount= 1; } } - push(&(env->head), new_item); + push_val(env, new_value); } /* Print newline. */ @@ -477,7 +464,7 @@ } toss(env); /* toss the symbol */ if(env->err) return; - push_val(&(env->head), val); /* Return its bound value */ + push_val(env, val); /* Return its bound value */ } /* If the top element is a symbol, determine if it's bound to a @@ -522,7 +509,7 @@ if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; while(iterator!=NULL) { - push_val(&(env->head), iterator->item); + push_val(env, iterator->item); if(env->head->item->type==symb && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { toss(env); @@ -574,22 +561,21 @@ /* Make a list. */ extern void pack(environment *env) { - void* delimiter; stackitem *iterator, *temp; value *pack; - delimiter= env->head->item->content.ptr; /* Get delimiter */ - toss(env); - iterator= env->head; - if(iterator==NULL || iterator->item->content.ptr==delimiter) { + 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->content.ptr!=delimiter) + && (iterator->next->item->type!=symb + || ((symbol*)(iterator->next->item->content.ptr))->id[0]!='[')) iterator= iterator->next; /* Extract list */ @@ -607,10 +593,7 @@ pack->content.ptr= temp; pack->refcount= 1; - temp= malloc(sizeof(stackitem)); - temp->item= pack; - - push(&(env->head), temp); + push_val(env, pack); rev(env); } @@ -670,7 +653,7 @@ result= (left==right); toss(env); toss(env); - push_int(&(env->head), result); + push_int(env, result); } /* Negates the top element on the stack. */ @@ -692,7 +675,7 @@ val= env->head->item->content.val; toss(env); - push_int(&(env->head), !val); + push_int(env, !val); } /* Compares the two top elements on the stack and return 0 if they're the @@ -735,9 +718,22 @@ toss(env); toss(env); } +extern void clear(environment *); +void forget_sym(symbol **); + /* Quit stack. */ extern void quit(environment *env) { + long i; + + clear(env); + if (env->err) return; + for(i= 0; isymbols[i]!= NULL) { + forget_sym(&(env->symbols[i])); + env->symbols[i]= NULL; + } + } exit(EXIT_SUCCESS); } @@ -763,12 +759,25 @@ } } +/* Internal forget function */ +void forget_sym(symbol **hash_entry) { + symbol *temp; + + temp= *hash_entry; + *hash_entry= (*hash_entry)->next; + + if(temp->val!=NULL) { + free_val(temp->val); + } + free(temp->id); + free(temp); +} + /* Forgets a symbol (remove it from the hash table) */ extern void forget(environment *env) { char* sym_id; stackitem *stack_head= env->head; - symbol **hash_entry, *temp; if(stack_head==NULL) { printerr("Too Few Arguments"); @@ -785,20 +794,12 @@ sym_id= ((symbol*)(stack_head->item->content.ptr))->id; toss(env); - hash_entry= hash(env->symbols, sym_id); - temp= *hash_entry; - *hash_entry= (*hash_entry)->next; - - if(temp->val!=NULL) { - free_val(temp->val); - } - free(temp->id); - free(temp); + return forget_sym(hash(env->symbols, sym_id)); } /* Returns the current error number to the stack */ extern void errn(environment *env){ - push_int(&(env->head), env->err); + push_int(env, env->err); } extern void read(environment*); @@ -853,7 +854,7 @@ strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); free_val(a_val); free_val(b_val); - push_cstring(&(env->head), new_string); + push_cstring(env, new_string); free(new_string); return; } @@ -873,7 +874,7 @@ b=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), a+b); + push_int(env, a+b); } } @@ -902,7 +903,7 @@ b=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), b-a); + push_int(env, b-a); } } @@ -931,7 +932,7 @@ b=env->head->item->content.val; toss(env); if(env->err) return; - push_int(&(env->head), b>a); + push_int(env, b>a); } } @@ -987,7 +988,7 @@ env->err=1; return; } - push_val(&(env->head), copy_val(env->head->item)); + push_val(env, copy_val(env->head->item)); } /* "if", If-Then */ @@ -1078,7 +1079,7 @@ toss(env); if(env->err) return; do { - push_val(&(env->head), test); + push_val(env, test); eval(env); if(env->head->item->type != integer) { @@ -1091,7 +1092,7 @@ toss(env); if(env->err) return; if(truth) { - push_val(&(env->head), loop); + push_val(env, loop); eval(env); } else { toss(env); @@ -1132,8 +1133,8 @@ iterator= foo->content.ptr; while(iterator!=NULL) { - push_val(&(env->head), iterator->item); - push_val(&(env->head), loop); + push_val(env, iterator->item); + push_val(env, loop); eval(env); if(env->err) return; iterator= iterator->next; } @@ -1145,6 +1146,8 @@ /* 'to' */ extern void to(environment *env) { int i, start, ending; + stackitem *temp_head; + value *temp_val; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1164,18 +1167,23 @@ start= env->head->item->content.val; toss(env); if(env->err) return; - push_sym(env, "["); + temp_head= env->head; + env->head= NULL; if(ending>=start) { - for(i= start; i<=ending; i++) - push_int(&(env->head), i); + for(i= ending; i>=start; i--) + push_int(env, i); } else { - for(i= start; i>=ending; i--) - push_int(&(env->head), i); + for(i= ending; i<=start; i++) + push_int(env, i); } - push_sym(env, "["); - pack(env); if(env->err) return; + temp_val= malloc(sizeof(value)); + temp_val->content.ptr= env->head; + temp_val->refcount= 1; + temp_val->type= list; + env->head= temp_head; + push_val(env, temp_val); } /* Read a string */ @@ -1183,20 +1191,20 @@ char in_string[101]; fgets(in_string, 100, stdin); - push_cstring(&(env->head), in_string); + push_cstring(env, in_string); } /* Read a value and place on stack */ extern void read(environment *env) { - const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%[\001-\377]"; - const char strform[]= "\"%[^\"]\"%[\001-\377]"; - const char intform[]= "%i%[\001-\377]"; - const char blankform[]= "%*[ \t]%[\001-\377]"; - const char ebrackform[]= "%*1[]]%[\001-\377]"; - const char semicform[]= "%*1[;]%[\001-\377]"; - const char bbrackform[]= "%*1[[]%[\001-\377]"; + const char symbform[]= "%[a-zA-Z0-9!$%*+./:<=>?@^_~-]%n"; + 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"; - int itemp; + int itemp, readlength= -1; static int depth= 0; char *rest, *match; size_t inlength; @@ -1205,6 +1213,7 @@ readline(env); if(env->err) return; env->in_string= malloc(strlen(env->head->item->content.ptr)+1); + env->free_string= env->in_string; /* Save the original pointer */ strcpy(env->in_string, env->head->item->content.ptr); toss(env); if(env->err) return; } @@ -1213,32 +1222,37 @@ match= malloc(inlength); rest= malloc(inlength); - if(sscanf(env->in_string, blankform, rest)) { + if(sscanf(env->in_string, blankform, &readlength)!=EOF + && readlength != -1) { ; - } else if(sscanf(env->in_string, intform, &itemp, rest) > 0) { - push_int(&(env->head), itemp); - } else if(sscanf(env->in_string, strform, match, rest) > 0) { - push_cstring(&(env->head), match); - } else if(sscanf(env->in_string, symbform, match, rest) > 0) { + } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF + && readlength != -1) { + push_int(env, itemp); + } else if(sscanf(env->in_string, strform, match, &readlength) != EOF + && readlength != -1) { + push_cstring(env, match); + } else if(sscanf(env->in_string, symbform, match, &readlength) != EOF + && readlength != -1) { push_sym(env, match); - } else if(sscanf(env->in_string, ebrackform, rest) > 0) { - push_sym(env, "["); + } else if(sscanf(env->in_string, ebrackform, &readlength) != EOF + && readlength != -1) { pack(env); if(env->err) return; - if(depth!=0) depth--; - } else if(sscanf(env->in_string, semicform, rest) > 0) { + if(depth != 0) depth--; + } else if(sscanf(env->in_string, semicform, &readlength) != EOF + && readlength != -1) { push_sym(env, ";"); - } else if(sscanf(env->in_string, bbrackform, rest) > 0) { + } else if(sscanf(env->in_string, bbrackform, &readlength) != EOF + && readlength != -1) { push_sym(env, "["); depth++; } else { - free(rest); - rest= NULL; + free(env->free_string); + env->in_string = env->free_string = NULL; + free(match); + } + if ( env->in_string != NULL) { + env->in_string += readlength; } - - free(env->in_string); - free(match); - - env->in_string= rest; if(depth) return read(env);