--- stack/stack.c 2002/02/02 20:06:11 1.25 +++ stack/stack.c 2002/02/02 20:49:34 1.26 @@ -205,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); @@ -254,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) @@ -274,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. */ @@ -418,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) {