--- stack/stack.c 2002/03/17 12:49:27 1.116 +++ stack/stack.c 2002/03/20 13:20:29 1.118 @@ -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; @@ -124,8 +123,11 @@ value *nval= malloc(sizeof(value)); stackitem *nitem= malloc(sizeof(stackitem)); + assert(nval != NULL); + assert(nitem != NULL); + nval->content.ptr= NULL; - nval->type= integer; + nval->type= empty; nitem->item= nval; nitem->next= env->gc_ref; @@ -336,6 +338,7 @@ int length= strlen(in_string)+1; new_value->content.ptr= malloc(length); + assert(new_value != NULL); env->gc_count += length; strcpy(new_value->content.ptr, in_string); new_value->type= string; @@ -350,6 +353,7 @@ char *new_string, *current; new_string= malloc((strlen(old_string)*2)+4); + assert(new_string != NULL); strcpy(new_string, "sx_"); /* Stack eXternal */ current= new_string+3; while(old_string[0] != '\0'){ @@ -419,9 +423,11 @@ /* Create a new symbol */ (*new_symbol)= malloc(sizeof(symbol)); + assert((*new_symbol) != NULL); (*new_symbol)->val= NULL; /* undefined value */ (*new_symbol)->next= NULL; (*new_symbol)->id= malloc(strlen(in_string)+1); + assert((*new_symbol)->id != NULL); strcpy((*new_symbol)->id, in_string); /* Intern the new symbol in the hash table */ @@ -499,8 +505,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 +534,56 @@ break; case tcons: printf("[ "); + tstack= stack; do { - print_val(CAR(val), noquote); + titem=malloc(sizeof(stackitem)); + assert(titem != NULL); + 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 +596,7 @@ env->err= 1; return; } - print_val(CAR(env->head), 0); + print_val(CAR(env->head), 0, NULL); nl(); } @@ -570,7 +615,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 +632,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 +809,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 +824,6 @@ value *iterator, *temp, *ending; ending=new_val(env); - ending->type=empty; iterator= env->head; if(iterator->type == empty @@ -1066,7 +1109,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.116 $\n\ + printf("Stack version $Revision: 1.118 $\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\ @@ -1126,6 +1169,7 @@ toss(env); if(env->err) return; len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; new_string= malloc(len); + assert(new_string != NULL); strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); push_cstring(env, new_string); @@ -1725,6 +1769,7 @@ } env->in_string= malloc(strlen(CAR(env->head)->content.ptr)+1); + assert(env->in_string != NULL); env->free_string= env->in_string; /* Save the original pointer */ strcpy(env->in_string, CAR(env->head)->content.ptr); toss(env); if(env->err) return; @@ -1732,6 +1777,7 @@ inlength= strlen(env->in_string)+1; match= malloc(inlength); + assert(match != NULL); if(sscanf(env->in_string, blankform, &readlength) != EOF && readlength != -1) {