--- stack/stack.c 2002/02/13 19:53:55 1.77 +++ stack/stack.c 2002/02/14 22:44:49 1.83 @@ -8,6 +8,8 @@ #include /* strcmp, strcpy, strlen, strcat, strdup */ #include +/* mtrace, muntrace */ +#include #define HASHTBLSIZE 2048 @@ -58,8 +60,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 +76,6 @@ env->in_string= NULL; env->err= 0; - env->non_eval_flag= 0; for(i= 0; isymbols[i]= NULL; } @@ -100,13 +102,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 value structure */ } } @@ -172,7 +179,7 @@ new_value->content.val= in_val; new_value->type= integer; - new_value->refcount=1; + new_value->refcount= 0; push_val(env, new_value); } @@ -185,7 +192,7 @@ 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= 0; push_val(env, new_value); } @@ -211,7 +218,6 @@ } extern void mangle(environment *env){ - value *new_value; char *new_string; if((env->head)==NULL) { @@ -231,12 +237,7 @@ 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. */ @@ -338,14 +339,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); @@ -358,7 +362,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; } @@ -373,7 +377,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. */ @@ -384,13 +389,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(); } @@ -398,10 +420,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. */ @@ -474,14 +496,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: @@ -531,7 +553,7 @@ /* Reverse (flip) a list */ extern void rev(environment *env){ - stackitem *item, *temp, *prev= NULL; + stackitem *old_head, *new_head, *item; if((env->head)==NULL) { printerr("Too Few Arguments"); @@ -545,16 +567,15 @@ return; } - item= (stackitem *)(env->head->item->content.ptr); - while(item->next!=NULL){ - temp= item->next; - item->next= prev; - prev= item; - item= temp; + 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->next= prev; - - env->head->item->content.ptr=item; + env->head->item->content.ptr=new_head; } /* Make a list. */ @@ -590,7 +611,7 @@ pack= malloc(sizeof(value)); pack->type= list; pack->content.ptr= temp; - pack->refcount= 1; + pack->refcount= 0; push_val(env, pack); rev(env); @@ -719,6 +740,7 @@ extern void clear(environment *); void forget_sym(symbol **); +extern void words(environment *); /* Quit stack. */ extern void quit(environment *env) @@ -726,13 +748,24 @@ long i; clear(env); + + words(env); + if (env->err) return; for(i= 0; isymbols[i]!= NULL) { + while(env->symbols[i]!= NULL) { forget_sym(&(env->symbols[i])); - env->symbols[i]= NULL; } + env->symbols[i]= NULL; } + + words(env); + + if(env->free_string!=NULL) + free(env->free_string); + + muntrace(); + exit(EXIT_SUCCESS); } @@ -807,11 +840,16 @@ { environment myenv; + mtrace(); + 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); @@ -1179,7 +1217,7 @@ temp_val= malloc(sizeof(value)); temp_val->content.ptr= env->head; - temp_val->refcount= 1; + temp_val->refcount= 0; temp_val->type= list; env->head= temp_head; push_val(env, temp_val); @@ -1195,56 +1233,66 @@ /* 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; + char *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; } inlength= strlen(env->in_string)+1; 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) { + } 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; + } + if ( env->in_string != NULL) { + env->in_string += readlength; } - - free(env->in_string); - free(match); - env->in_string= rest; + free(match); if(depth) return read(env);