--- stack/stack.c 2002/02/04 21:47:26 1.28 +++ stack/stack.c 2002/02/06 21:26:46 1.42 @@ -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,15 +103,23 @@ } /* 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 */ +void push_val(stackitem **stack_head, value *val) +{ + stackitem *new_item= malloc(sizeof(stackitem)); + new_item->item= val; + val->refcount++; + push(stack_head, new_item); } /* Push an integer onto the stack. */ -int push_val(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)); @@ -122,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)); @@ -138,17 +145,16 @@ 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... */ value *new_value; /* A new symbol value */ /* ...which might point to... */ - symbol *new_symbol; /* (if needed) A new actual symbol */ + symbol **new_symbol; /* (if needed) A new actual symbol */ /* ...which, if possible, will be bound to... */ value *new_fvalue; /* (if needed) A new function value */ /* ...which will point to... */ @@ -166,19 +172,20 @@ new_value->refcount= 1; /* Look up the symbol name in the hash table */ - new_value->content.ptr= *hash(env->symbols, in_string); + new_symbol= hash(env->symbols, in_string); + new_value->content.ptr= *new_symbol; - if(new_value->content.ptr==NULL) { /* If symbol was undefined */ + if(*new_symbol==NULL) { /* If symbol was undefined */ /* Create a new symbol */ - new_symbol= malloc(sizeof(symbol)); - new_symbol->val= NULL; /* undefined value */ - new_symbol->next= NULL; - new_symbol->id= malloc(strlen(in_string)+1); - strcpy(new_symbol->id, in_string); + (*new_symbol)= malloc(sizeof(symbol)); + (*new_symbol)->val= NULL; /* undefined value */ + (*new_symbol)->next= NULL; + (*new_symbol)->id= malloc(strlen(in_string)+1); + strcpy((*new_symbol)->id, in_string); /* Intern the new symbol in the hash table */ - new_value->content.ptr= new_symbol; + new_value->content.ptr= *new_symbol; /* Try to load the symbol name as an external function, to see if we should bind the symbol to a new function pointer value */ @@ -190,13 +197,12 @@ new_fvalue= malloc(sizeof(value)); /* Create a new value */ new_fvalue->type=func; /* The new value is a function pointer */ new_fvalue->content.ptr=funcptr; /* Store function pointer */ - new_symbol->val= new_fvalue; /* Bind the symbol to the new - function value */ + (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new + function value */ new_fvalue->refcount= 1; } } push(&(env->head), new_item); - return 1; } void printerr(const char* in_string) { @@ -212,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 */ @@ -234,7 +241,8 @@ stackitem *temp= env->head; if((env->head)==NULL) { - printerr("Stack empty"); + printerr("Too Few Arguments"); + env->err=1; return; } @@ -249,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); @@ -268,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); } @@ -282,6 +336,7 @@ extern void print(environment *env) { print_(env); + if(env->err) return; toss(env); } @@ -300,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. */ @@ -314,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; } @@ -338,39 +394,91 @@ return out_item; } +/* Recall a value from a symbol, if bound */ +extern void rcl(environment *env) +{ + value *val; + + if(env->head == NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } -/* If the top element is a reference, determine if it's a reference to a - function, and if it is, toss the reference and execute the function. */ + if(env->head->item->type!=symb) { + 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 */ +} + +/* If the top element is a symbol, determine if it's bound to a + function value, and if it is, toss the symbol and execute the + function. */ extern void eval(environment *env) { funcp in_func; - stackitem* temp= env->head; - - if(temp==NULL) { - printerr("Stack empty"); + if(env->head==NULL) { + printerr("Too Few Arguments"); + env->err=1; return; } - if(temp->item->type==symb - && ((symbol *)(temp->item->content.ptr))->val != NULL - && ((symbol *)(temp->item->content.ptr))->val->type == func) { - in_func= (funcp)(((symbol *)(temp->item->content.ptr))->val->content.ptr); - toss(env); - (*in_func)(env); - return; + /* if it's a symbol */ + if(env->head->item->type==symb) { + + 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(temp->item->type==func) { - in_func= (funcp)(temp->item->content.ptr); + /* 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; } - push(&(env->head), copy(temp)); - swap(env); - toss(env); + 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. */ @@ -413,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; @@ -435,7 +544,7 @@ } /* If integer */ if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { - push_val(&(env->head), itemp); + push_int(&(env->head), itemp); break; } /* Escape ';' with '\' */ @@ -445,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; } @@ -477,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. */ @@ -497,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; } @@ -525,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; } @@ -535,7 +649,7 @@ result= (left==right); toss(env); toss(env); - push_val(&(env->head), result); + push_int(&(env->head), result); } /* Negates the top element on the stack. */ @@ -543,14 +657,21 @@ { 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; } val= env->head->item->content.val; toss(env); - push_val(&(env->head), !val); + push_int(&(env->head), !val); } /* Compares the two top elements on the stack and return 0 if they're the @@ -567,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; } @@ -600,6 +727,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; @@ -611,8 +791,12 @@ 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 "); } - - exit(EXIT_SUCCESS); + quit(&myenv); + return EXIT_FAILURE; }