--- stack/stack.c 2002/02/05 22:25:04 1.31 +++ stack/stack.c 2002/02/06 00:52:31 1.36 @@ -18,8 +18,6 @@ enum { integer, string, - ref, /* Reference (to an element in the - hash table) */ func, /* Function pointer */ symb, list @@ -58,6 +56,7 @@ typedef struct { stackitem *head; /* Head of the stack */ hashtbl symbols; /* Hash table of all variable bindings */ + int err; /* Error flag */ } environment; /* A type for pointers to external functions */ @@ -69,6 +68,7 @@ { long i; + env->err=0; for(i= 0; isymbols[i]= NULL; } @@ -103,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 */ @@ -120,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)); @@ -131,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)); @@ -147,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... */ @@ -206,7 +203,6 @@ } } push(&(env->head), new_item); - return 1; } void printerr(const char* in_string) { @@ -244,7 +240,8 @@ stackitem *temp= env->head; if((env->head)==NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + env->err=1; return; } @@ -262,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); @@ -278,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("Too Few Arguments"); + env->err=1; + return; + } print_h(env->head); } @@ -292,6 +294,7 @@ extern void print(environment *env) { print_(env); + if(env->err) return; toss(env); } @@ -310,12 +313,13 @@ /* Prints the stack. */ extern void printstack(environment *env) { - if(env->head != NULL) { - print_st(env->head, 1); - nl(); - } else { - printerr("Stack empty"); + if(env->head == NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; } + print_st(env->head, 1); + nl(); } /* Swap the two top elements on the stack. */ @@ -324,12 +328,14 @@ stackitem *temp= env->head; if((env->head)==NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + env->err=1; return; } if(env->head->next==NULL) { - printerr("Not enough arguments"); + printerr("Too Few Arguments"); + env->err=1; return; } @@ -348,21 +354,31 @@ return out_item; } +/* Recall a value from a symbol, if bound */ extern void rcl(environment *env) { value *val; if(env->head == NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + env->err=1; return; } if(env->head->item->type!=symb) { - printerr("Not a symbol"); + printerr("Bad Argument Type"); + env->err=2; return; } + val=((symbol *)(env->head->item->content.ptr))->val; + if(val == NULL){ + printerr("Unbound Variable"); + env->err=3; + return; + } toss(env); /* toss the symbol */ + if(env->err) return; push_val(&(env->head), val); /* Return its bound value */ } @@ -372,32 +388,20 @@ extern void eval(environment *env) { funcp in_func; - value* val; if(env->head==NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + 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"); - 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 */ + 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; - } 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 */ } } @@ -405,10 +409,9 @@ 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. */ @@ -483,7 +486,7 @@ break; } /* If symbol */ - if((convert= sscanf(in_line, "%[^][ ;\n\r_]%[^\n\r]", temp, rest))) { + if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { push_sym(env, temp); break; } @@ -535,8 +538,14 @@ stackitem *temp, *new_head; /* Is top element a list? */ - if(env->head==NULL || env->head->item->type!=list) { - printerr("Stack empty or not a list"); + if(env->head==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + if(env->head->item->type!=list) { + printerr("Bad Argument Type"); + env->err=2; return; } @@ -563,7 +572,8 @@ int result; if((env->head)==NULL || env->head->next==NULL) { - printerr("Not enough elements to compare"); + printerr("Too Few Arguments"); + env->err=1; return; } @@ -581,8 +591,15 @@ { int val; - if((env->head)==NULL || env->head->item->type!=integer) { - printerr("Stack empty or element is not a integer"); + if((env->head)==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + + if(env->head->item->type!=integer) { + printerr("Bad Argument Type"); + env->err=2; return; } @@ -605,9 +622,15 @@ symbol *sym; /* Needs two values on the stack, the top one must be a symbol */ - if(env->head==NULL || env->head->next==NULL - || env->head->item->type!=symb) { - printerr("Define what?"); + if(env->head==NULL || env->head->next==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + + if(env->head->item->type!=symb) { + printerr("Bad Argument Type"); + env->err=2; return; } @@ -638,6 +661,59 @@ toss(env); } +/* List all defined words */ +extern void words(environment *env) +{ + symbol *temp; + int i; + + for(i= 0; isymbols[i]; + while(temp!=NULL) { + printf("%s\n", temp->id); + temp= temp->next; + } + } +} + +/* 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"); + env->err=1; + return; + } + + if(stack_head->item->type!=symb) { + printerr("Bad Argument Type"); + env->err=2; + return; + } + + 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); +} + +/* Returns the current error number to the stack */ +extern void errn(environment *env){ + push_int(&(env->head), env->err); +} + int main() { environment myenv; @@ -649,6 +725,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 "); }