--- stack/stack.c 2002/02/04 21:47:26 1.28 +++ stack/stack.c 2002/02/05 12:14:42 1.29 @@ -110,8 +110,17 @@ return 1; } +/* Push a value onto the stack */ +void push_val(stackitem **stack_head, value *val) +{ + stackitem *new_item= malloc(sizeof(stackitem)); + new_item->item= val; + val->refcount++; + push(stack_head, new_item); +} + /* Push an integer onto the stack. */ -int push_val(stackitem **stack_head, int in_val) +int push_int(stackitem **stack_head, int in_val) { value *new_value= malloc(sizeof(value)); stackitem *new_item= malloc(sizeof(stackitem)); @@ -148,7 +157,7 @@ /* ...which will contain... */ value *new_value; /* A new symbol value */ /* ...which might point to... */ - symbol *new_symbol; /* (if needed) A new actual symbol */ + symbol **new_symbol; /* (if needed) A new actual symbol */ /* ...which, if possible, will be bound to... */ value *new_fvalue; /* (if needed) A new function value */ /* ...which will point to... */ @@ -166,19 +175,20 @@ new_value->refcount= 1; /* Look up the symbol name in the hash table */ - new_value->content.ptr= *hash(env->symbols, in_string); + new_symbol= hash(env->symbols, in_string); + new_value->content.ptr= *new_symbol; if(new_value->content.ptr==NULL) { /* If symbol was undefined */ /* Create a new symbol */ - new_symbol= malloc(sizeof(symbol)); - new_symbol->val= NULL; /* undefined value */ - new_symbol->next= NULL; - new_symbol->id= malloc(strlen(in_string)+1); - strcpy(new_symbol->id, in_string); + *new_symbol= malloc(sizeof(symbol)); + (*new_symbol)->val= NULL; /* undefined value */ + (*new_symbol)->next= NULL; + (*new_symbol)->id= malloc(strlen(in_string)+1); + strcpy((*new_symbol)->id, in_string); /* Intern the new symbol in the hash table */ - new_value->content.ptr= new_symbol; + new_value->content.ptr= *new_symbol; /* Try to load the symbol name as an external function, to see if we should bind the symbol to a new function pointer value */ @@ -190,8 +200,8 @@ new_fvalue= malloc(sizeof(value)); /* Create a new value */ new_fvalue->type=func; /* The new value is a function pointer */ new_fvalue->content.ptr=funcptr; /* Store function pointer */ - new_symbol->val= new_fvalue; /* Bind the symbol to the new - function value */ + (*new_symbol)->val= new_fvalue; /* Bind the symbol to the new + function value */ new_fvalue->refcount= 1; } } @@ -339,38 +349,52 @@ } -/* If the top element is a reference, determine if it's a reference to a - function, and if it is, toss the reference and execute the function. */ +/* If the top element is a symbol, determine if it's bound to a + function value, and if it is, toss the symbol and execute the + function. */ extern void eval(environment *env) { funcp in_func; - stackitem* temp= env->head; - - if(temp==NULL) { + value* val; + if(env->head==NULL) { printerr("Stack empty"); return; } - if(temp->item->type==symb - && ((symbol *)(temp->item->content.ptr))->val != NULL - && ((symbol *)(temp->item->content.ptr))->val->type == func) { - in_func= (funcp)(((symbol *)(temp->item->content.ptr))->val->content.ptr); - toss(env); - (*in_func)(env); - return; - } + /* if it's a symbol */ + if(env->head->item->type==symb) { + + /* If it's not bound to anything */ + if (((symbol *)(env->head->item->content.ptr))->val == NULL) { + printerr("Unbound variable"); + return; + } + /* If it contains a function */ + if (((symbol *)(env->head->item->content.ptr))->val->type == func) { + in_func= + (funcp)(((symbol *)(env->head->item->content.ptr))->val->content.ptr); + toss(env); + (*in_func)(env); /* Run the function */ + return; + } else { /* If it's not a function */ + val=((symbol *)(env->head->item->content.ptr))->val; + toss(env); /* toss the symbol */ + push_val(&(env->head), val); /* Return its bound value */ + } + } - if(temp->item->type==func) { - in_func= (funcp)(temp->item->content.ptr); + /* If it's a lone function value, run it */ + if(env->head->item->type==func) { + in_func= (funcp)(env->head->item->content.ptr); toss(env); (*in_func)(env); return; } - push(&(env->head), copy(temp)); - swap(env); - toss(env); +/* push(&(env->head), copy(env->head)); */ +/* swap(env); */ +/* toss(env); */ } /* Make a list. */ @@ -435,7 +459,7 @@ } /* If integer */ if((convert= sscanf(in_line, "%d %[^\n\r]", &itemp, rest))) { - push_val(&(env->head), itemp); + push_int(&(env->head), itemp); break; } /* Escape ';' with '\' */ @@ -535,7 +559,7 @@ result= (left==right); toss(env); toss(env); - push_val(&(env->head), result); + push_int(&(env->head), result); } /* Negates the top element on the stack. */ @@ -550,7 +574,7 @@ val= env->head->item->content.val; toss(env); - push_val(&(env->head), !val); + push_int(&(env->head), !val); } /* Compares the two top elements on the stack and return 0 if they're the