--- stack/stack.c 2002/02/05 23:00:42 1.33 +++ stack/stack.c 2002/02/06 11:44:02 1.40 @@ -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) { @@ -223,6 +218,7 @@ switch (val->type){ /* and free the contents if necessary */ case string: free(val->content.ptr); + break; case list: /* lists needs to be freed recursively */ item=val->content.ptr; while(item != NULL) { /* for all stack items */ @@ -245,7 +241,8 @@ stackitem *temp= env->head; if((env->head)==NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + env->err=1; return; } @@ -255,20 +252,47 @@ } /* Print newline. */ -extern void nl(environment *env) +extern void nl() { printf("\n"); } -/* Prints the top element of the stack. */ -void print_h(stackitem *stack_head) -{ +/* Gets the type of a value */ +extern void type(environment *env){ + int typenum; - if(stack_head==NULL) { - printerr("Stack empty"); + if((env->head)==NULL) { + printerr("Too Few Arguments"); + env->err=1; return; } + typenum=env->head->item->type; + toss(env); + switch(typenum){ + case integer: + push_sym(env, "integer"); + break; + case string: + push_sym(env, "string"); + break; + case symb: + push_sym(env, "symbol"); + break; + case func: + push_sym(env, "function"); + break; + case list: + push_sym(env, "list"); + break; + default: + push_sym(env, "unknown"); + break; + } +} +/* Prints the top element of the stack. */ +void print_h(stackitem *stack_head) +{ switch(stack_head->item->type) { case integer: printf("%d", stack_head->item->content.val); @@ -279,13 +303,32 @@ case symb: printf("'%s'", ((symbol *)(stack_head->item->content.ptr))->id); break; + case func: + printf("#", (funcp)(stack_head->item->content.ptr)); + break; + case list: + /* A list is just a stack, so make stack_head point to it */ + stack_head=(stackitem *)(stack_head->item->content.ptr); + printf("[ "); + while(stack_head != NULL) { + print_h(stack_head); + printf(" "); + stack_head=stack_head->next; + } + printf("]"); + break; default: - printf("%p", (funcp)(stack_head->item->content.ptr)); + printf("#", (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); } @@ -293,6 +336,7 @@ extern void print(environment *env) { print_(env); + if(env->err) return; toss(env); } @@ -311,12 +355,11 @@ /* 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) { + return; } + print_st(env->head, 1); + nl(); } /* Swap the two top elements on the stack. */ @@ -325,12 +368,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; } @@ -355,20 +400,25 @@ 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"); + 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 */ } @@ -378,43 +428,57 @@ 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"); + 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); + } +} + +/* Reverse a list */ +extern void rev(environment *env){ + stackitem *old_head, *new_head, *item; + + 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; } + 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; + } + env->head->item->content.ptr=new_head; } /* Make a list. */ @@ -457,10 +521,11 @@ temp->item= pack; push(&(env->head), temp); + rev(env); } /* Parse input. */ -int stack_read(environment *env, char *in_line) +void stack_read(environment *env, char *in_line) { char *temp, *rest; int itemp; @@ -489,7 +554,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; } @@ -521,18 +586,16 @@ } } while(0); - free(temp); if(convert<2) { free(rest); - return 0; + return; } stack_read(env, rest); free(rest); - return 1; } /* Relocate elements of the list on the stack. */ @@ -541,8 +604,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; } @@ -569,7 +638,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; } @@ -587,8 +657,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; } @@ -611,9 +688,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; } @@ -659,6 +742,44 @@ } } +/* 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; @@ -670,6 +791,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 "); }