--- stack/stack.c 2002/02/14 12:01:58 1.79 +++ stack/stack.c 2002/02/14 19:49:48 1.82 @@ -58,7 +58,6 @@ 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 */ @@ -75,7 +74,6 @@ env->in_string= NULL; env->err= 0; - env->non_eval_flag= 0; for(i= 0; isymbols[i]= NULL; } @@ -102,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 */ } } @@ -174,7 +177,7 @@ new_value->content.val= in_val; new_value->type= integer; - new_value->refcount=1; + new_value->refcount= 1; push_val(env, new_value); } @@ -187,7 +190,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= 1; push_val(env, new_value); } @@ -213,7 +216,6 @@ } extern void mangle(environment *env){ - value *new_value; char *new_string; if((env->head)==NULL) { @@ -233,12 +235,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. */ @@ -340,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); @@ -360,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; } @@ -375,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. */ @@ -386,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(); } @@ -400,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. */ @@ -476,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: @@ -731,8 +749,8 @@ for(i= 0; isymbols[i]!= NULL) { forget_sym(&(env->symbols[i])); - env->symbols[i]= NULL; } + env->symbols[i]= NULL; } exit(EXIT_SUCCESS); } @@ -811,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); @@ -1210,6 +1231,9 @@ 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);