--- stack/stack.c 2002/02/05 22:28:04 1.32 +++ stack/stack.c 2002/02/06 11:15:05 1.38 @@ -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) { @@ -222,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 */ @@ -244,7 +241,8 @@ stackitem *temp= env->head; if((env->head)==NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + env->err=1; return; } @@ -259,15 +257,42 @@ 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); @@ -278,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); } @@ -292,6 +336,7 @@ extern void print(environment *env) { print_(env); + if(env->err) return; toss(env); } @@ -310,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. */ @@ -324,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; } @@ -348,21 +394,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 +428,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 +449,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. */ @@ -454,7 +497,7 @@ } /* Parse input. */ -int stack_read(environment *env, char *in_line) +void stack_read(environment *env, char *in_line) { char *temp, *rest; int itemp; @@ -483,7 +526,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; } @@ -515,18 +558,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. */ @@ -535,8 +576,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 +610,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 +629,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 +660,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 +699,7 @@ toss(env); } +/* List all defined words */ extern void words(environment *env) { symbol *temp; @@ -652,6 +714,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; @@ -663,6 +763,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 "); }