--- stack/stack.c 2002/01/08 00:03:16 1.5 +++ stack/stack.c 2002/01/08 13:46:05 1.7 @@ -11,7 +11,7 @@ typedef struct stack_item { - enum {value, string, ref, func, symbol} type; + enum {value, string, ref, func, symbol, list} type; union { void* ptr; int val; @@ -121,15 +121,17 @@ int push_ref(stackitem** stack_head, hashtbl in_hash, const char* in_string) { - void* handle; + static void* handle= NULL; void* symbol; - + stackitem* new_item= malloc(sizeof(stackitem)); new_item->content.ptr= *hash(in_hash, in_string); new_item->type= ref; - if(new_item->content.ptr==NULL) { + if(handle==NULL) handle= dlopen(NULL, RTLD_LAZY); + + if(new_item->content.ptr==NULL) { symbol= dlsym(handle, in_string); if(dlerror()==NULL) def_func(in_hash, symbol, in_string); @@ -148,6 +150,12 @@ { stackitem* temp= *stack_head; + if((*stack_head)==NULL) + return; + + if((*stack_head)->type==string) + free((*stack_head)->content.ptr); + *stack_head= (*stack_head)->next; free(temp); } @@ -167,8 +175,11 @@ printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); break; case ref: + printf("%ld: %s\n", counter, ((stackitem*)stack_head->content.ptr)->id); + break; case func: case symbol: + default: printf("%ld: %p\n", counter, stack_head->content.ptr); break; } @@ -243,9 +254,10 @@ case string: printf("%s", (char*)(*stack_head)->content.ptr); break; - case symbol: - printf("%s", (*stack_head)->id); case ref: + printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); + break; + case symbol: default: printf("%p", (*stack_head)->content.ptr); break; @@ -254,6 +266,37 @@ toss(stack_head); } +extern void pack(stackitem** stack_head) +{ + void* delimiter; + stackitem *iterator, *temp, *pack; + + if((*stack_head)==NULL) + return; + + delimiter= (*stack_head)->content.ptr; + toss(stack_head); + + iterator= *stack_head; + + while(iterator->next!=NULL && iterator->next->content.ptr!=delimiter) + iterator= iterator->next; + + temp= *stack_head; + *stack_head= iterator->next; + iterator->next= NULL; + + if(*stack_head!=NULL && (*stack_head)->content.ptr==delimiter) + toss(stack_head); + + pack= malloc(sizeof(stackitem)); + pack->type= list; + pack->content.ptr= temp; + + push(stack_head, pack); +} + + extern void nl() { printf("\n");