--- stack/stack.c 2002/02/02 18:11:54 1.22 +++ stack/stack.c 2002/02/02 19:30:07 1.24 @@ -160,7 +160,9 @@ else def_sym(in_hash, in_string); /* Make symbol */ - new_item->content.ptr= *hash(in_hash, in_string); /* XXX */ + new_item->content.ptr= *hash(in_hash, in_string); /* The new reference + shouldn't point at + NULL */ new_item->type= ref; } @@ -196,26 +198,30 @@ } /* Prints the top element of the stack. */ -void print_(stackitem** stack_head) +extern void print_(stackitem** stack_head) { - if((*stack_head)==NULL) { + stackitem* temp= *stack_head; + + if(temp==NULL) { printerr("Stack empty"); return; } - switch((*stack_head)->type) { + while(temp->type==ref) + temp= temp->content.ptr; + + switch(temp->type) { case value: - printf("%d", (*stack_head)->content.val); + printf("%d", temp->content.val); break; case string: - printf("\"%s\"", (char*)(*stack_head)->content.ptr); - break; - case ref: - printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); + printf("\"%s\"", (char*)temp->content.ptr); break; case symbol: + printf("%s", temp->id); + break; default: - printf("%p", (*stack_head)->content.ptr); + printf("%p", temp->content.ptr); break; } } @@ -280,27 +286,27 @@ void* delimiter; stackitem *iterator, *temp, *pack; - if((*stack_head)==NULL) { - printerr("Stack empty"); - return; - } - delimiter= (*stack_head)->content.ptr; /* Get delimiter */ toss(stack_head); iterator= *stack_head; - /* Search for first delimiter */ - while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) - iterator= iterator->next; - - /* Extract list */ - temp= *stack_head; - *stack_head= iterator->next; - iterator->next= NULL; - - if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) + if(iterator==NULL || iterator->content.ptr==delimiter) { + temp= NULL; toss(stack_head); + } else { + /* Search for first delimiter */ + while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) + iterator= iterator->next; + + /* Extract list */ + temp= *stack_head; + *stack_head= iterator->next; + iterator->next= NULL; + + if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) + toss(stack_head); + } /* Push list */ pack= malloc(sizeof(stackitem)); @@ -401,6 +407,9 @@ new_head= temp= (*stack_head)->content.ptr; toss(stack_head); + if(temp==NULL) + return; + /* Search the end of the list */ while(temp->next!=NULL) temp= temp->next; @@ -497,6 +506,13 @@ exit(EXIT_SUCCESS); } +/* Clear stack */ +extern void clear(stackitem** stack_head) +{ + while(*stack_head!=NULL) + toss(stack_head); +} + int main() { stackitem* s= NULL;