--- stack/stack.c 2002/02/12 22:13:12 1.72 +++ stack/stack.c 2002/02/14 19:49:48 1.82 @@ -58,8 +58,9 @@ stackitem *head; /* Head of the stack */ hashtbl symbols; /* Hash table of all variable bindings */ 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 */ @@ -73,7 +74,6 @@ env->in_string= NULL; env->err= 0; - env->non_eval_flag= 0; for(i= 0; isymbols[i]= NULL; } @@ -100,13 +100,18 @@ free(item); /* free the stackitem */ item=temp; /* go to next stackitem */ } - free(val); /* Free the actual list value */ break; case integer: case func: + break; case symb: + free(((symbol*)(val->content.ptr))->id); + if(((symbol*)(val->content.ptr))->val!=NULL) + free_val(((symbol*)(val->content.ptr))->val); + free(val->content.ptr); break; } + free(val); /* Free the actual list value */ } } @@ -155,49 +160,39 @@ } } -/* Generic push function. */ -void push(environment *env, stackitem* in_item) -{ - in_item->next= env->head; - env->head= in_item; -} - /* Push a value onto the stack */ void push_val(environment *env, value *val) { stackitem *new_item= malloc(sizeof(stackitem)); new_item->item= val; val->refcount++; - push(env, new_item); + new_item->next= env->head; + env->head= new_item; } /* Push an integer onto the stack. */ 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; + new_value->refcount= 1; - push(env, new_item); + push_val(env, new_value); } /* Copy a string onto the stack. */ 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; + new_value->refcount= 1; - push(env, new_item); + push_val(env, new_value); } /* Mangle a symbol name to a valid C identifier name */ @@ -221,7 +216,6 @@ } extern void mangle(environment *env){ - value *new_value; char *new_string; if((env->head)==NULL) { @@ -241,19 +235,12 @@ toss(env); if(env->err) return; - new_value= malloc(sizeof(value)); - new_value->content.ptr= new_string; - new_value->type= string; - new_value->refcount=1; - - push_val(env, new_value); + push_cstring(env, new_string); } /* 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 +253,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 +297,7 @@ new_fvalue->refcount= 1; } } - push(env, new_item); + push_val(env, new_value); } /* Print newline. */ @@ -353,14 +337,17 @@ } /* Prints the top element of the stack. */ -void print_h(stackitem *stack_head) +void print_h(stackitem *stack_head, int noquote) { switch(stack_head->item->type) { case integer: printf("%d", stack_head->item->content.val); break; case string: - printf("%s", (char*)stack_head->item->content.ptr); + if(noquote) + printf("%s", (char*)stack_head->item->content.ptr); + else + printf("\"%s\"", (char*)stack_head->item->content.ptr); break; case symb: printf("%s", ((symbol *)(stack_head->item->content.ptr))->id); @@ -373,7 +360,7 @@ stack_head=(stackitem *)(stack_head->item->content.ptr); printf("[ "); while(stack_head != NULL) { - print_h(stack_head); + print_h(stack_head, noquote); printf(" "); stack_head=stack_head->next; } @@ -388,7 +375,8 @@ env->err=1; return; } - print_h(env->head); + print_h(env->head, 0); + nl(); } /* Prints the top element of the stack and then discards it. */ @@ -399,13 +387,30 @@ toss(env); } +extern void princ_(environment *env) { + if(env->head==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + print_h(env->head, 1); +} + +/* Prints the top element of the stack and then discards it. */ +extern void princ(environment *env) +{ + princ_(env); + if(env->err) return; + toss(env); +} + /* Only to be called by function printstack. */ void print_st(stackitem *stack_head, long counter) { if(stack_head->next != NULL) print_st(stack_head->next, counter+1); printf("%ld: ", counter); - print_h(stack_head); + print_h(stack_head, 0); nl(); } @@ -413,10 +418,10 @@ extern void printstack(environment *env) { if(env->head == NULL) { + printf("Stack Empty\n"); return; } print_st(env->head, 1); - nl(); } /* Swap the two top elements on the stack. */ @@ -489,14 +494,14 @@ value* temp_val; stackitem* iterator; + eval_start: + if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; return; } - eval_start: - switch(env->head->item->type) { /* if it's a symbol */ case symb: @@ -574,22 +579,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 +611,7 @@ pack->content.ptr= temp; pack->refcount= 1; - temp= malloc(sizeof(stackitem)); - temp->item= pack; - - push(env, temp); + push_val(env, pack); rev(env); } @@ -735,9 +736,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 +777,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,15 +812,7 @@ 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 */ @@ -810,8 +829,11 @@ init_env(&myenv); while(1) { - if(myenv.in_string==NULL) + if(myenv.in_string==NULL) { + nl(); printstack(&myenv); + printf("> "); + } read(&myenv); if(myenv.err) { printf("(error %d) ", myenv.err); @@ -1145,6 +1167,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 +1188,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++) + for(i= ending; i>=start; i--) push_int(env, i); } else { - for(i= start; i>=ending; 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 */ @@ -1188,23 +1217,27 @@ /* 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; if(env->in_string==NULL) { + if(depth > 0) { + printf("]> "); + } 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 +1246,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) { + } else if(sscanf(env->in_string, intform, &itemp, &readlength) != EOF + && readlength != -1) { push_int(env, itemp); - } else if(sscanf(env->in_string, strform, match, rest) > 0) { + } else if(sscanf(env->in_string, strform, match, &readlength) != EOF + && readlength != -1) { push_cstring(env, match); - } else if(sscanf(env->in_string, symbform, match, rest) > 0) { + } 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);