--- stack/stack.c 2002/02/05 23:26:46 1.34 +++ stack/stack.c 2002/02/06 00:30:14 1.35 @@ -18,8 +18,6 @@ enum { integer, string, - ref, /* Reference (to an element in the - hash table) */ func, /* Function pointer */ symb, list @@ -70,6 +68,7 @@ { long i; + env->err=0; for(i= 0; isymbols[i]= NULL; } @@ -104,11 +103,10 @@ } /* Generic push function. */ -int push(stackitem** stack_head, stackitem* in_item) +void push(stackitem** stack_head, stackitem* in_item) { in_item->next= *stack_head; *stack_head= in_item; - return 1; } /* Push a value onto the stack */ @@ -121,7 +119,7 @@ } /* Push an integer onto the stack. */ -int push_int(stackitem **stack_head, int in_val) +void push_int(stackitem **stack_head, int in_val) { value *new_value= malloc(sizeof(value)); stackitem *new_item= malloc(sizeof(stackitem)); @@ -132,11 +130,10 @@ new_value->refcount=1; push(stack_head, new_item); - return 1; } /* Copy a string onto the stack. */ -int push_cstring(stackitem **stack_head, const char *in_string) +void push_cstring(stackitem **stack_head, const char *in_string) { value *new_value= malloc(sizeof(value)); stackitem *new_item= malloc(sizeof(stackitem)); @@ -148,11 +145,10 @@ new_value->refcount=1; push(stack_head, new_item); - return 1; } /* Push a symbol onto the stack. */ -int push_sym(environment *env, const char *in_string) +void push_sym(environment *env, const char *in_string) { stackitem *new_item; /* The new stack item */ /* ...which will contain... */ @@ -207,7 +203,6 @@ } } push(&(env->head), new_item); - return 1; } void printerr(const char* in_string) { @@ -246,6 +241,7 @@ if((env->head)==NULL) { printerr("Stack empty"); + env->err=1; return; } @@ -263,12 +259,6 @@ /* Prints the top element of the stack. */ void print_h(stackitem *stack_head) { - - if(stack_head==NULL) { - printerr("Stack empty"); - return; - } - switch(stack_head->item->type) { case integer: printf("%d", stack_head->item->content.val); @@ -279,13 +269,24 @@ case symb: printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); break; + case func: + printf("#", (funcp)(stack_head->item->content.ptr)); + break; + case list: + printf("#", (funcp)(stack_head->item->content.ptr)); + break; default: - printf("%p", (funcp)(stack_head->item->content.ptr)); + printf("#", (funcp)(stack_head->item->content.ptr)); break; } } extern void print_(environment *env) { + if(env->head==NULL) { + printerr("Stack empty"); + env->err=1; + return; + } print_h(env->head); } @@ -293,6 +294,7 @@ extern void print(environment *env) { print_(env); + if(env->err) return; toss(env); } @@ -311,12 +313,13 @@ /* Prints the stack. */ extern void printstack(environment *env) { - if(env->head != NULL) { - print_st(env->head, 1); - nl(); - } else { + if(env->head == NULL) { printerr("Stack empty"); + env->err=1; + return; } + print_st(env->head, 1); + nl(); } /* Swap the two top elements on the stack. */ @@ -326,11 +329,13 @@ if((env->head)==NULL) { printerr("Stack empty"); + env->err=1; return; } if(env->head->next==NULL) { printerr("Not enough arguments"); + env->err=1; return; } @@ -356,19 +361,24 @@ if(env->head == NULL) { printerr("Stack empty"); + env->err=1; return; } if(env->head->item->type!=symb) { printerr("Not a symbol"); + env->err=1; return; } + val=((symbol *)(env->head->item->content.ptr))->val; if(val == NULL){ printerr("Unbound variable"); + env->err=1; return; } toss(env); /* toss the symbol */ + if(env->err) return; push_val(&(env->head), val); /* Return its bound value */ } @@ -378,43 +388,30 @@ extern void eval(environment *env) { funcp in_func; - value* val; if(env->head==NULL) { printerr("Stack empty"); + env->err=1; return; } /* if it's a symbol */ if(env->head->item->type==symb) { - /* If it's not bound to anything */ - if (((symbol *)(env->head->item->content.ptr))->val == NULL) { - printerr("Unbound variable"); + rcl(env); /* get its contents */ + if(env->err) return; + if(env->head->item->type!=symb){ /* don't recurse symbols */ + eval(env); /* evaluate the value */ return; } - - /* If it contains a function */ - if (((symbol *)(env->head->item->content.ptr))->val->type == func) { - in_func= - (funcp)(((symbol *)(env->head->item->content.ptr))->val->content.ptr); - toss(env); - (*in_func)(env); /* Run the function */ - return; - } else { /* If it's not a function */ - val=((symbol *)(env->head->item->content.ptr))->val; - toss(env); /* toss the symbol */ - push_val(&(env->head), val); /* Return its bound value */ - } } /* If it's a lone function value, run it */ if(env->head->item->type==func) { in_func= (funcp)(env->head->item->content.ptr); toss(env); + if(env->err) return; (*in_func)(env); - return; } - } /* Make a list. */ @@ -543,6 +540,7 @@ /* Is top element a list? */ if(env->head==NULL || env->head->item->type!=list) { printerr("Stack empty or not a list"); + env->err=1; return; } @@ -570,6 +568,7 @@ if((env->head)==NULL || env->head->next==NULL) { printerr("Not enough elements to compare"); + env->err=1; return; } @@ -589,6 +588,7 @@ if((env->head)==NULL || env->head->item->type!=integer) { printerr("Stack empty or element is not a integer"); + env->err=1; return; } @@ -614,6 +614,7 @@ if(env->head==NULL || env->head->next==NULL || env->head->item->type!=symb) { printerr("Define what?"); + env->err=1; return; } @@ -696,6 +697,10 @@ while(fgets(in_string, 100, stdin) != NULL) { stack_read(&myenv, in_string); + if(myenv.err) { + printf("(error %d) ", myenv.err); + myenv.err=0; + } printf("okidok\n "); }