--- stack/stack.c 2002/02/12 23:16:56 1.73 +++ stack/stack.c 2002/02/13 00:16:00 1.75 @@ -155,49 +155,39 @@ } } -/* Generic push function. */ -void push(environment *env, stackitem* in_item) -{ - in_item->next= env->head; - env->head= in_item; -} - /* Push a value onto the stack */ void push_val(environment *env, value *val) { stackitem *new_item= malloc(sizeof(stackitem)); new_item->item= val; val->refcount++; - push(env, new_item); + new_item->next= env->head; + env->head= new_item; } /* Push an integer onto the stack. */ void push_int(environment *env, int in_val) { value *new_value= malloc(sizeof(value)); - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item= new_value; new_value->content.val= in_val; new_value->type= integer; new_value->refcount=1; - push(env, new_item); + push_val(env, new_value); } /* Copy a string onto the stack. */ void push_cstring(environment *env, const char *in_string) { value *new_value= malloc(sizeof(value)); - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item=new_value; new_value->content.ptr= malloc(strlen(in_string)+1); strcpy(new_value->content.ptr, in_string); new_value->type= string; new_value->refcount=1; - push(env, new_item); + push_val(env, new_value); } /* Mangle a symbol name to a valid C identifier name */ @@ -252,8 +242,6 @@ /* Push a symbol onto the stack. */ void push_sym(environment *env, const char *in_string) { - stackitem *new_item; /* The new stack item */ - /* ...which will contain... */ value *new_value; /* A new symbol value */ /* ...which might point to... */ symbol **new_symbol; /* (if needed) A new actual symbol */ @@ -266,10 +254,7 @@ const char *dlerr; /* Dynamic linker error */ char *mangled; /* Mangled function name */ - /* Create a new stack item containing a new value */ - new_item= malloc(sizeof(stackitem)); new_value= malloc(sizeof(value)); - new_item->item=new_value; /* The new value is a symbol */ new_value->type= symb; @@ -313,7 +298,7 @@ new_fvalue->refcount= 1; } } - push(env, new_item); + push_val(env, new_value); } /* Print newline. */ @@ -606,10 +591,7 @@ pack->content.ptr= temp; pack->refcount= 1; - temp= malloc(sizeof(stackitem)); - temp->item= pack; - - push(env, temp); + push_val(env, pack); rev(env); } @@ -1144,6 +1126,8 @@ /* 'to' */ extern void to(environment *env) { int i, start, ending; + stackitem *temp_head; + value *temp_val; if((env->head)==NULL || env->head->next==NULL) { printerr("Too Few Arguments"); @@ -1163,17 +1147,23 @@ start= env->head->item->content.val; toss(env); if(env->err) return; - push_sym(env, "["); + temp_head= env->head; + env->head= NULL; if(ending>=start) { - for(i= start; i<=ending; i++) + for(i= ending; i>=start; i--) push_int(env, i); } else { - for(i= start; i>=ending; i--) + for(i= ending; i<=start; i++) push_int(env, i); } - pack(env); if(env->err) return; + temp_val= malloc(sizeof(value)); + temp_val->content.ptr= env->head; + temp_val->refcount= 1; + temp_val->type= list; + env->head= temp_head; + push_val(env, temp_val); } /* Read a string */