--- stack/stack.c 2002/01/31 23:09:07 1.17 +++ stack/stack.c 2002/02/02 20:06:11 1.25 @@ -6,6 +6,8 @@ #include /* dlopen, dlsym, dlerror */ #include +/* assert */ +#include #define HASHTBLSIZE 65536 @@ -13,13 +15,14 @@ { enum { value, /* Integer */ - string, - ref, /* Reference (to an element in the hash table) */ + string, + ref, /* Reference (to an element in the + hash table) */ func, /* Function pointer */ symbol, list - } type; /* Tells what kind of stack element */ - + } type; /* Type of stack element */ + union { void* ptr; /* Pointer to the content */ int val; /* ...or an integer */ @@ -31,10 +34,10 @@ typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table declaration */ -typedef void (*funcp)(stackitem**); /* Function pointer declaration */ - +typedef void (*funcp)(stackitem**); /* funcp is a pointer to a + void function (stackitem **) */ -/* Initiates a newly created hash table. */ +/* Initialize a newly created hash table. */ void init_hashtbl(hashtbl out_hash) { long i; @@ -48,7 +51,7 @@ { long i= 0; unsigned long out_hash= 0; - char key= 0; + char key= '\0'; stackitem** position; while(1){ /* Hash in_string */ @@ -61,11 +64,11 @@ out_hash= out_hash%HASHTBLSIZE; position= &(in_hashtbl[out_hash]); - while(1){ /* Return position if empty */ - if(*position==NULL) + while(1){ + if(*position==NULL) /* If empty */ return position; - if(strcmp(in_string, (*position)->id)==0) /* Return position if match */ + if(strcmp(in_string, (*position)->id)==0) /* If match */ return position; position= &((*position)->next); /* Try next */ @@ -84,6 +87,7 @@ int push_val(stackitem** stack_head, int in_val) { stackitem* new_item= malloc(sizeof(stackitem)); + assert(new_item != NULL); new_item->content.val= in_val; new_item->type= value; @@ -114,7 +118,7 @@ return 1; } -/* Define a function a new function in the hash table. */ +/* Define a new function in the hash table. */ void def_func(hashtbl in_hashtbl, funcp in_func, const char* id) { stackitem* temp= malloc(sizeof(stackitem)); @@ -154,7 +158,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; } @@ -190,26 +196,31 @@ } /* 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: + case func: + printf("%s", temp->id); + break; default: - printf("%p", (*stack_head)->content.ptr); + printf("%p", temp->content.ptr); break; } } @@ -237,7 +248,7 @@ { if(*stack_head != NULL) { print_st(*stack_head, 1); - printf("\n"); + nl(); } else { printerr("Stack empty"); } @@ -248,19 +259,60 @@ extern void eval(stackitem** stack_head) { funcp in_func; + stackitem* temp= *stack_head; - if((*stack_head)==NULL || (*stack_head)->type!=ref) { - printerr("Stack empty or not a reference"); + if(temp==NULL) { + printerr("Stack empty"); return; } - if(((stackitem*)(*stack_head)->content.ptr)->type==func) { - in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; + while(temp->type==ref) + temp= temp->content.ptr; + + if(temp->type==func) { + in_func= (funcp)(temp->content.ptr); toss(stack_head); (*in_func)(stack_head); return; - } else - printerr("Not a function"); + } + + printerr("Couldn't evaluate"); +} + +/* Make a list. */ +extern void pack(stackitem** stack_head) +{ + void* delimiter; + stackitem *iterator, *temp, *pack; + + delimiter= (*stack_head)->content.ptr; /* Get delimiter */ + toss(stack_head); + + iterator= *stack_head; + + 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)); + pack->type= list; + pack->content.ptr= temp; + + push(stack_head, pack); } /* Parse input. */ @@ -270,6 +322,7 @@ int itemp; size_t inlength= strlen(in_line)+1; int convert= 0; + static int non_eval_flag= 0; temp= malloc(inlength); rest= malloc(inlength); @@ -292,14 +345,35 @@ break; } /* If symbol */ - if((convert= sscanf(in_line, "%[^ ;\n\r]%[^\n\r]", temp, rest))) { + if((convert= sscanf(in_line, "%[^][ ;\n\r]%[^\n\r]", temp, rest))) { push_ref(stack_head, in_hash, temp); break; } - /* If ';' */ - if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest)) && *temp==';') { - eval(stack_head); /* Evaluate top element */ - break; + /* If single char */ + if((convert= sscanf(in_line, "%c%[^\n\r]", temp, rest))) { + if(*temp==';') { + if(!non_eval_flag) { + eval(stack_head); /* Evaluate top element */ + break; + } + + push_ref(stack_head, in_hash, ";"); + break; + } + + if(*temp==']') { + push_ref(stack_head, in_hash, "["); + pack(stack_head); + if(non_eval_flag!=0) + non_eval_flag--; + break; + } + + if(*temp=='[') { + push_ref(stack_head, in_hash, "["); + non_eval_flag++; + break; + } } } while(0); @@ -317,42 +391,6 @@ return 1; } -/* Make a list. */ -extern void pack(stackitem** stack_head) -{ - 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) - toss(stack_head); - - /* Push list */ - pack= malloc(sizeof(stackitem)); - pack->type= list; - pack->content.ptr= temp; - - push(stack_head, pack); -} - /* Relocate elements of the list on the stack. */ extern void expand(stackitem** stack_head) { @@ -368,6 +406,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; @@ -464,6 +505,13 @@ exit(EXIT_SUCCESS); } +/* Clear stack */ +extern void clear(stackitem** stack_head) +{ + while(*stack_head!=NULL) + toss(stack_head); +} + int main() { stackitem* s= NULL;