--- stack/stack.c 2002/02/08 00:25:57 1.54 +++ stack/stack.c 2002/02/08 00:59:34 1.55 @@ -948,3 +948,58 @@ if(env->err) return; push_int(&(env->head), a+b); } + +/* Return copy of a value */ +value *copy_val(value *old_value){ + stackitem *old_item, *new_item, *prev_item; + + value *new_value=malloc(sizeof(value)); + + new_value->type=old_value->type; + new_value->refcount=0; /* This is increased if/when this + value is referenced somewhere, like + in a stack item or a variable */ + switch(old_value->type){ + case integer: + new_value->content.val=old_value->content.val; + break; + case string: + (char *)(new_value->content.ptr) + = strdup((char *)(old_value->content.ptr)); + break; + case func: + case symb: + new_value->content.ptr=old_value->content.ptr; + break; + case list: + new_value->content.ptr=NULL; + + prev_item=NULL; + old_item=(stackitem *)(old_value->content.ptr); + + while(old_item != NULL) { /* While list is not empty */ + new_item= malloc(sizeof(stackitem)); + new_item->item=copy_val(old_item->item); /* recurse */ + new_item->next=NULL; + if(prev_item != NULL) /* If this wasn't the first item */ + prev_item->next=new_item; /* point the previous item to the + new item */ + else + new_value->content.ptr=new_item; + old_item=old_item->next; + prev_item=new_item; + } + break; + } + return new_value; +} + +/* duplicates an item on the stack */ +extern void dup(environment *env) { + if((env->head)==NULL) { + printerr("Too Few Arguments"); + env->err=1; + return; + } + push_val(&(env->head), copy_val(env->head->item)); +}