--- stack/stack.c 2002/03/17 12:49:27 1.116 +++ stack/stack.c 2002/03/20 05:29:29 1.117 @@ -21,8 +21,8 @@ Teddy Hogeborn */ -#define CAR(X) (X->content.c->car) -#define CDR(X) (X->content.c->cdr) +#define CAR(X) ((X)->content.c->car) +#define CDR(X) ((X)->content.c->cdr) /* printf, sscanf, fgets, fprintf, fopen, perror */ #include @@ -62,7 +62,6 @@ env->gc_ref= NULL; env->head= new_val(env); - env->head->type= empty; for(i= 0; isymbols[i]= NULL; env->err= 0; @@ -125,7 +124,7 @@ stackitem *nitem= malloc(sizeof(stackitem)); nval->content.ptr= NULL; - nval->type= integer; + nval->type= empty; nitem->item= nval; nitem->next= env->gc_ref; @@ -499,8 +498,11 @@ } /* Print a value */ -void print_val(value *val, int noquote) +void print_val(value *val, int noquote, stackitem *stack) { + stackitem *titem, *tstack; + int depth; + switch(val->type) { case empty: printf("[]"); @@ -525,20 +527,55 @@ break; case tcons: printf("[ "); + tstack= stack; do { - print_val(CAR(val), noquote); + titem=malloc(sizeof(stackitem)); + titem->item=val; + titem->next=tstack; + tstack=titem; /* Put it on the stack */ + /* Search a stack of values being printed to see if we are already + printing this value */ + titem=tstack; + depth=0; + while(titem != NULL && titem->item != CAR(val)){ + titem=titem->next; + depth++; + } + if(titem != NULL){ /* If we found it on the stack, */ + printf("#%d#", depth); /* print a depth reference */ + } else { + print_val(CAR(val), noquote, tstack); + } val= CDR(val); switch(val->type){ case empty: break; case tcons: - printf(" "); + /* Search a stack of values being printed to see if we are already + printing this value */ + titem=tstack; + depth=0; + while(titem != NULL && titem->item != val){ + titem=titem->next; + depth++; + } + if(titem != NULL){ /* If we found it on the stack, */ + printf(" . #%d#", depth); /* print a depth reference */ + } else { + printf(" "); + } break; default: printf(" . "); /* Improper list */ - print_val(val, noquote); + print_val(val, noquote, tstack); } - } while(val->type == tcons); + } while(val->type == tcons && titem == NULL); + titem=tstack; + while(titem != stack){ + tstack=titem->next; + free(titem); + titem=tstack; + } printf(" ]"); break; } @@ -551,7 +588,7 @@ env->err= 1; return; } - print_val(CAR(env->head), 0); + print_val(CAR(env->head), 0, NULL); nl(); } @@ -570,7 +607,7 @@ env->err= 1; return; } - print_val(CAR(env->head), 1); + print_val(CAR(env->head), 1, NULL); } /* Prints the top element of the stack and then discards it. */ @@ -587,7 +624,7 @@ if(CDR(stack_head)->type != empty) print_st(CDR(stack_head), counter+1); printf("%ld: ", counter); - print_val(CAR(stack_head), 0); + print_val(CAR(stack_head), 0, NULL); nl(); } @@ -764,7 +801,6 @@ old_head= CAR(env->head); new_head= new_val(env); - new_head->type= empty; while(old_head->type != empty) { item= old_head; old_head= CDR(old_head); @@ -780,7 +816,6 @@ value *iterator, *temp, *ending; ending=new_val(env); - ending->type=empty; iterator= env->head; if(iterator->type == empty @@ -1066,7 +1101,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.116 $\n\ + printf("Stack version $Revision: 1.117 $\n\ Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ Stack comes with ABSOLUTELY NO WARRANTY; for details type 'warranty;'.\n\ This is free software, and you are welcome to redistribute it\n\