--- stack/stack.c 2002/01/07 23:35:37 1.4 +++ stack/stack.c 2002/01/31 21:47:20 1.15 @@ -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; @@ -25,6 +25,8 @@ typedef stackitem* hashtbl[HASHTBLSIZE]; typedef void (*funcp)(stackitem**); + +/* Initiates a newly created hash table. */ void init_hashtbl(hashtbl out_hash) { long i; @@ -33,6 +35,7 @@ out_hash[i]= NULL; } +/* Returns a pointer to an element in the hash table. */ stackitem** hash(hashtbl in_hashtbl, const char* in_string) { long i= 0; @@ -61,7 +64,7 @@ } } - +/* Generic push function. */ int push(stackitem** stack_head, stackitem* in_item) { in_item->next= *stack_head; @@ -69,6 +72,7 @@ return 1; } +/* Push a value on the stack. */ int push_val(stackitem** stack_head, int in_val) { stackitem* new_item= malloc(sizeof(stackitem)); @@ -79,6 +83,7 @@ return 1; } +/* Copy a string onto the stack. */ int push_cstring(stackitem** stack_head, const char* in_string) { stackitem* new_item= malloc(sizeof(stackitem)); @@ -90,6 +95,7 @@ return 1; } +/* Create a new hash entry. */ int mk_hashentry(hashtbl in_hashtbl, stackitem* in_item, const char* id) { in_item->id= malloc(strlen(id)+1); @@ -100,6 +106,7 @@ return 1; } +/* Define a function a new function in the hash table. */ void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) { stackitem* temp= malloc(sizeof(stackitem)); @@ -110,6 +117,7 @@ mk_hashentry(in_hashtbl, temp, id); } +/* Define a new symbol in the hash table. */ void def_sym(hashtbl in_hashtbl, const char* id) { stackitem* temp= malloc(sizeof(stackitem)); @@ -119,17 +127,20 @@ mk_hashentry(in_hashtbl, temp, id); } +/* Push a reference to an entry in the hash table onto the stack. */ 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) { - handle= dlopen(NULL, RTLD_LAZY); + if(handle==NULL) + handle= dlopen(NULL, RTLD_LAZY); + symbol= dlsym(handle, in_string); if(dlerror()==NULL) def_func(in_hash, symbol, in_string); @@ -144,36 +155,69 @@ return 1; } +/* Discard the top element of the stack. */ extern void toss(stackitem** stack_head) { 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); } +/* Print newline. */ +extern void nl() +{ + printf("\n"); +} -/* print_stack(stack); */ -void print_st(stackitem* stack_head, long counter) -{ - if(stack_head->next != NULL) - print_st(stack_head->next, counter+1); +/* Prints the top element of the stack. */ +void print_(stackitem** stack_head) +{ + if((*stack_head)==NULL) + return; - switch(stack_head->type){ + switch((*stack_head)->type) { case value: - printf("%ld: %d\n", counter, (int)stack_head->content.val); + printf("%d", (*stack_head)->content.val); break; case string: - printf("%ld: \"%s\"\n", counter, (char*)stack_head->content.ptr); + printf("%s", (char*)(*stack_head)->content.ptr); break; case ref: - case func: + printf("%s", ((stackitem*)(*stack_head)->content.ptr)->id); + break; case symbol: - printf("%ld: %p\n", counter, stack_head->content.ptr); + default: + printf("%p", (*stack_head)->content.ptr); break; } } +/* Prints the top element of the stack and then discards it. */ +extern void print(stackitem** stack_head) +{ + print_(stack_head); + toss(stack_head); +} + +/* Only to be called by function printstack. */ +void print_st(stackitem* stack_head, long counter) +{ + if(stack_head->next != NULL) + print_st(stack_head->next, counter+1); + + printf("%ld: ", counter); + print_(&stack_head); + nl(); +} + +/* Prints the stack. */ extern void printstack(stackitem** stack_head) { if(*stack_head != NULL) { @@ -182,7 +226,9 @@ } } - +/* If the top element is a reference, determine if it's a reference to a + function, and if it is execute and pop the reference and execute the + function. */ extern void eval(stackitem** stack_head) { funcp in_func; @@ -198,6 +244,7 @@ } } +/* Parse input. */ int stack_read(stackitem** stack_head, hashtbl in_hash, char* in_line) { char *temp, *rest; @@ -208,15 +255,34 @@ temp= malloc(inlength); rest= malloc(inlength); - if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest)) >= 1) - push_cstring(stack_head, temp); - else if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest)) >= 1) - push_val(stack_head, itemp); - else if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest)) >= 1) - push_ref(stack_head, in_hash, temp); - else if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) >= 1) - if(*temp==';') + do { + if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { + push_cstring(stack_head, temp); + break; + } + + if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { + push_val(stack_head, itemp); + break; + } + + if((convert= sscanf(in_line, "\\%c%[^\n\r]", temp, rest))) { + temp[1]= '\0'; + push_ref(stack_head, in_hash, temp); + break; + } + + if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) { + push_ref(stack_head, in_hash, temp); + break; + } + + if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') { eval(stack_head); + break; + } + } while(0); + free(temp); @@ -231,24 +297,129 @@ return 1; } -extern void print(stackitem** stack_head) +/* Make a list. */ +extern void pack(stackitem** stack_head) { + void* delimiter; + stackitem *iterator, *temp, *pack; + if((*stack_head)==NULL) return; - if((*stack_head)->type==value) - printf("%d", (*stack_head)->content.val); - else if((*stack_head)->type==string) - printf("%s", (char*)(*stack_head)->content.ptr); - else - printf("%p", (*stack_head)->content.ptr); + 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); +} + +/* Push elements of the list on the stack. */ +extern void expand(stackitem** stack_head) +{ + stackitem *temp, *new_head; + + if((*stack_head)==NULL || (*stack_head)->type!=list) + return; + new_head= temp= (*stack_head)->content.ptr; toss(stack_head); + + while(temp->next!=NULL) + temp= temp->next; + + temp->next= *stack_head; + *stack_head= new_head; } -extern void nl() +/* Swap the two top elements on the stack. */ +extern void swap(stackitem** stack_head) { - printf("\n"); + stackitem* temp= (*stack_head); + + if((*stack_head)==NULL || (*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) +{ + void *left, *right; + int result; + + if((*stack_head)==NULL || (*stack_head)->next==NULL) + return; + + left= (*stack_head)->content.ptr; + swap(stack_head); + right= (*stack_head)->content.ptr; + result= (left==right); + + toss(stack_head); toss(stack_head); + push_val(stack_head, (left==right)); +} + +/* Negates the top element on the stack. */ +extern void not(stackitem** stack_head) +{ + int value; + + if((*stack_head)==NULL) + return; + + value= (*stack_head)->content.val; + toss(stack_head); + push_val(stack_head, !value); +} + +/* Compares the two top elements on the stack and return 0 if they're the + same. */ +extern void neq(stackitem** stack_head) +{ + eq(stack_head); + not(stack_head); +} + +/* Give a symbol some content. */ +extern void def(stackitem** stack_head) +{ + stackitem *temp, *value; + + if(*stack_head==NULL || (*stack_head)->next==NULL + || (*stack_head)->type!=ref) + return; + + temp= (*stack_head)->content.ptr; + value= (*stack_head)->next; + temp->content= value->content; + value->content.ptr=NULL; + temp->type= value->type; + + toss(stack_head); toss(stack_head); +} + +/* Quit stack. */ +extern void quit() +{ + exit(EXIT_SUCCESS); } int main() @@ -266,8 +437,7 @@ printf("okidok\n "); } - - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } /* Local Variables: */