--- stack/stack.c 2002/02/04 21:47:26 1.28 +++ stack/stack.c 2002/02/05 23:26:46 1.34 @@ -58,6 +58,7 @@ typedef struct { stackitem *head; /* Head of the stack */ hashtbl symbols; /* Hash table of all variable bindings */ + int err; /* Error flag */ } environment; /* A type for pointers to external functions */ @@ -110,8 +111,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 +158,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 +176,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 */ + if(*new_symbol==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 +201,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; } } @@ -338,39 +349,72 @@ return out_item; } - -/* 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. */ -extern void eval(environment *env) +/* Recall a value from a symbol, if bound */ +extern void rcl(environment *env) { - funcp in_func; - stackitem* temp= env->head; + value *val; - if(temp==NULL) { + 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); + if(env->head->item->type!=symb) { + printerr("Not a symbol"); + return; + } + val=((symbol *)(env->head->item->content.ptr))->val; + if(val == NULL){ + printerr("Unbound variable"); return; } + toss(env); /* toss the symbol */ + push_val(&(env->head), val); /* Return its bound value */ +} + +/* 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; + value* val; + if(env->head==NULL) { + printerr("Stack empty"); + 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); } /* Make a list. */ @@ -435,7 +479,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 '\' */ @@ -445,7 +489,7 @@ 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_sym(env, temp); break; } @@ -535,7 +579,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 +594,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 @@ -600,6 +644,47 @@ toss(env); } +/* List all defined words */ +extern void words(environment *env) +{ + symbol *temp; + int i; + + for(i= 0; isymbols[i]; + while(temp!=NULL) { + printf("%s\n", temp->id); + temp= temp->next; + } + } +} + +/* Forgets a symbol (remove it from the hash table) */ +extern void forget(environment *env) +{ + char* sym_id; + stackitem *stack_head= env->head; + symbol **hash_entry, *temp; + + if(stack_head==NULL || stack_head->item->type!=symb) { + printerr("Stack empty or not a symbol"); + return; + } + + sym_id= ((symbol*)(stack_head->item->content.ptr))->id; + toss(env); + + hash_entry= hash(env->symbols, sym_id); + temp= *hash_entry; + *hash_entry= (*hash_entry)->next; + + if(temp->val!=NULL) { + free_val(temp->val); + } + free(temp->id); + free(temp); +} + int main() { environment myenv;