--- stack/stack.c 2002/02/02 19:30:07 1.24 +++ stack/stack.c 2002/02/02 22:22:04 1.27 @@ -46,7 +46,7 @@ out_hash[i]= NULL; } -/* Returns a pointer to an element in the hash table. */ +/* Returns a pointer to a pointer to an element in the hash table. */ stackitem** hash(hashtbl in_hashtbl, const char* in_string) { long i= 0; @@ -64,7 +64,7 @@ out_hash= out_hash%HASHTBLSIZE; position= &(in_hashtbl[out_hash]); - while(position != NULL){ + while(1){ if(*position==NULL) /* If empty */ return position; @@ -73,8 +73,6 @@ position= &((*position)->next); /* Try next */ } - return NULL; /* end of list reached without finding - an empty position */ } /* Generic push function. */ @@ -207,9 +205,15 @@ return; } - while(temp->type==ref) + while(temp->type==ref) { temp= temp->content.ptr; + if(temp->type!=ref) { + printf("ref-> %s", temp->id); + return; + } + } + switch(temp->type) { case value: printf("%d", temp->content.val); @@ -218,6 +222,7 @@ printf("\"%s\"", (char*)temp->content.ptr); break; case symbol: + case func: printf("%s", temp->id); break; default: @@ -255,6 +260,35 @@ } } +/* Swap the two top elements on the stack. */ +extern void swap(stackitem** stack_head) +{ + stackitem* temp= (*stack_head); + + if((*stack_head)==NULL) { + printerr("Stack empty"); + return; + } + + if((*stack_head)->next==NULL) + return; + + *stack_head= (*stack_head)->next; + temp->next= (*stack_head)->next; + (*stack_head)->next= temp; +} + +stackitem* copy(stackitem* in_item) +{ + stackitem* out_item= malloc(sizeof(stackitem)); + + memcpy(out_item, in_item, sizeof(stackitem)); + out_item->next= NULL; + + return out_item; +} + + /* 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. */ extern void eval(stackitem** stack_head) @@ -275,9 +309,11 @@ toss(stack_head); (*in_func)(stack_head); return; - } - - printerr("Couldn't evaluate"); + } + + push(stack_head, copy(temp)); + swap(stack_head); + toss(stack_head); } /* Make a list. */ @@ -419,24 +455,6 @@ *stack_head= new_head; /* ...and voila! */ } -/* Swap the two top elements on the stack. */ -extern void swap(stackitem** stack_head) -{ - stackitem* temp= (*stack_head); - - if((*stack_head)==NULL) { - printerr("Stack empty"); - return; - } - - if((*stack_head)->next==NULL) - return; - - *stack_head= (*stack_head)->next; - temp->next= (*stack_head)->next; - (*stack_head)->next= temp; -} - /* Compares two elements by reference. */ extern void eq(stackitem** stack_head) { @@ -530,7 +548,3 @@ exit(EXIT_SUCCESS); } - -/* Local Variables: */ -/* compile-command:"make CFLAGS=\"-Wall -g -rdynamic -ldl\" stack" */ -/* End: */