--- stack/stack.c 2002/01/31 21:47:20 1.15 +++ stack/stack.c 2002/02/02 20:06:11 1.25 @@ -6,27 +6,38 @@ #include /* dlopen, dlsym, dlerror */ #include +/* assert */ +#include #define HASHTBLSIZE 65536 typedef struct stack_item { - enum {value, string, ref, func, symbol, list} type; + enum { + value, /* Integer */ + string, + ref, /* Reference (to an element in the + hash table) */ + func, /* Function pointer */ + symbol, + list + } type; /* Type of stack element */ + union { - void* ptr; - int val; - } content; + void* ptr; /* Pointer to the content */ + int val; /* ...or an integer */ + } content; /* Stores a pointer or an integer */ - char* id; - struct stack_item* next; + char* id; /* Symbol name */ + struct stack_item* next; /* Next element */ } stackitem; -typedef stackitem* hashtbl[HASHTBLSIZE]; -typedef void (*funcp)(stackitem**); - +typedef stackitem* hashtbl[HASHTBLSIZE]; /* Hash table 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; @@ -40,10 +51,10 @@ { long i= 0; unsigned long out_hash= 0; - char key= 0; + char key= '\0'; stackitem** position; - while(1){ + while(1){ /* Hash in_string */ key= in_string[i++]; if(key=='\0') break; @@ -54,13 +65,13 @@ position= &(in_hashtbl[out_hash]); while(1){ - if(*position==NULL) + if(*position==NULL) /* If empty */ return position; - if(strcmp(in_string, (*position)->id)==0) + if(strcmp(in_string, (*position)->id)==0) /* If match */ return position; - position= &((*position)->next); + position= &((*position)->next); /* Try next */ } } @@ -76,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; @@ -106,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)); @@ -123,7 +135,6 @@ stackitem* temp= malloc(sizeof(stackitem)); temp->type= symbol; - mk_hashentry(in_hashtbl, temp, id); } @@ -137,17 +148,19 @@ new_item->content.ptr= *hash(in_hash, in_string); new_item->type= ref; - if(new_item->content.ptr==NULL) { - if(handle==NULL) + if(new_item->content.ptr==NULL) { /* If hash entry empty */ + if(handle==NULL) /* If no handle */ handle= dlopen(NULL, RTLD_LAZY); - symbol= dlsym(handle, in_string); - if(dlerror()==NULL) - def_func(in_hash, symbol, in_string); + symbol= dlsym(handle, in_string); /* Get function pointer */ + if(dlerror()==NULL) /* If existing function pointer */ + def_func(in_hash, symbol, in_string); /* Store function pointer */ else - def_sym(in_hash, in_string); + def_sym(in_hash, in_string); /* Make symbol */ - new_item->content.ptr= *hash(in_hash, in_string); + new_item->content.ptr= *hash(in_hash, in_string); /* The new reference + shouldn't point at + NULL */ new_item->type= ref; } @@ -155,13 +168,19 @@ return 1; } +void printerr(const char* in_string) { + fprintf(stderr, "Err: %s\n", in_string); +} + /* Discard the top element of the stack. */ extern void toss(stackitem** stack_head) { stackitem* temp= *stack_head; - if((*stack_head)==NULL) + if((*stack_head)==NULL) { + printerr("Stack empty"); return; + } if((*stack_head)->type==string) free((*stack_head)->content.ptr); @@ -177,24 +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; + } + + while(temp->type==ref) + temp= temp->content.ptr; - switch((*stack_head)->type) { + 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; } } @@ -222,26 +248,71 @@ { if(*stack_head != NULL) { print_st(*stack_head, 1); - printf("\n"); + nl(); + } else { + printerr("Stack empty"); } } /* 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. */ + function, and if it is, toss the reference and execute the function. */ extern void eval(stackitem** stack_head) { funcp in_func; + stackitem* temp= *stack_head; - if((*stack_head)==NULL || (*stack_head)->type!=ref) + if(temp==NULL) { + printerr("Stack empty"); return; + } + + while(temp->type==ref) + temp= temp->content.ptr; - if(((stackitem*)(*stack_head)->content.ptr)->type==func) { - in_func= (funcp)((stackitem*)(*stack_head)->content.ptr)->content.ptr; + if(temp->type==func) { + in_func= (funcp)(temp->content.ptr); toss(stack_head); (*in_func)(stack_head); return; + } + + 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. */ @@ -251,35 +322,58 @@ int itemp; size_t inlength= strlen(in_line)+1; int convert= 0; + static int non_eval_flag= 0; temp= malloc(inlength); rest= malloc(inlength); do { + /* If string */ if((convert= sscanf(in_line, "\"%[^\"\n\r]\" %[^\n\r]", temp, rest))) { push_cstring(stack_head, temp); break; } - + /* If value */ if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { push_val(stack_head, itemp); break; } - + /* Escape ';' with '\' */ 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))) { + /* If symbol */ + 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; + /* 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); @@ -297,53 +391,31 @@ return 1; } -/* Make a list. */ -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); -} - -/* Push elements of the list on the stack. */ +/* Relocate 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) + /* Is top element a list? */ + if((*stack_head)==NULL || (*stack_head)->type!=list) { + printerr("Stack empty or not a list"); return; + } + /* The first list element is the new stack head */ 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; + /* Connect the the tail of the list with the old stack head */ temp->next= *stack_head; - *stack_head= new_head; + *stack_head= new_head; /* ...and voila! */ } /* Swap the two top elements on the stack. */ @@ -351,7 +423,12 @@ { stackitem* temp= (*stack_head); - if((*stack_head)==NULL || (*stack_head)->next==NULL) + if((*stack_head)==NULL) { + printerr("Stack empty"); + return; + } + + if((*stack_head)->next==NULL) return; *stack_head= (*stack_head)->next; @@ -365,8 +442,10 @@ void *left, *right; int result; - if((*stack_head)==NULL || (*stack_head)->next==NULL) + if((*stack_head)==NULL || (*stack_head)->next==NULL) { + printerr("Not enough elements to compare"); return; + } left= (*stack_head)->content.ptr; swap(stack_head); @@ -382,8 +461,10 @@ { int value; - if((*stack_head)==NULL) + if((*stack_head)==NULL || (*stack_head)->type!=value) { + printerr("Stack empty or element is not a value"); return; + } value= (*stack_head)->content.val; toss(stack_head); @@ -404,8 +485,10 @@ stackitem *temp, *value; if(*stack_head==NULL || (*stack_head)->next==NULL - || (*stack_head)->type!=ref) + || (*stack_head)->type!=ref) { + printerr("Define what?"); return; + } temp= (*stack_head)->content.ptr; value= (*stack_head)->next; @@ -422,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;