--- stack/symbols.c 2003/08/04 11:23:43 1.1 +++ stack/symbols.c 2003/08/04 14:32:27 1.3 @@ -1,4 +1,3 @@ -#include #include "stack.h" /* Print newline. */ @@ -294,7 +293,7 @@ } /* long names are a pain */ - sym= CAR(env->head)->content.ptr; + sym= CAR(env->head)->content.sym; /* Bind the symbol to the value */ sym->val= CAR(CDR(env->head)); @@ -360,11 +359,11 @@ protect(a_val); protect(b_val); toss(env); if(env->err) return; toss(env); if(env->err) return; - len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; + len= strlen(a_val->content.string)+strlen(b_val->content.string)+1; new_string= malloc(len); assert(new_string != NULL); - strcpy(new_string, b_val->content.ptr); - strcat(new_string, a_val->content.ptr); + strcpy(new_string, b_val->content.string); + strcat(new_string, a_val->content.string); push_cstring(env, new_string); unprotect(a_val); unprotect(b_val); free(new_string); @@ -1282,6 +1281,78 @@ toss(env); if(env->err) return; } + +/* General assoc function */ +void assocgen(environment *env, funcp eqfunc) +{ + value *key, *item; + + /* Needs two values on the stack, the top one must be an association + list */ + if(env->head->type==empty || CDR(env->head)->type==empty) { + printerr("Too Few Arguments"); + env->err= 1; + return; + } + + if(CAR(env->head)->type!=tcons) { + printerr("Bad Argument Type"); + env->err= 2; + return; + } + + key=CAR(CDR(env->head)); + item=CAR(env->head); + + while(item->type == tcons){ + if(CAR(item)->type != tcons){ + printerr("Bad Argument Type"); + env->err= 2; + return; + } + push_val(env, key); + push_val(env, CAR(CAR(item))); + eqfunc(env); if(env->err) return; + + /* Check the result of 'eqfunc' */ + if(env->head->type==empty) { + printerr("Too Few Arguments"); + env->err= 1; + return; + } + if(CAR(env->head)->type!=integer) { + printerr("Bad Argument Type"); + env->err= 2; + return; + } + + if(CAR(env->head)->content.i){ + toss(env); if(env->err) return; + break; + } + toss(env); if(env->err) return; + + if(item->type!=tcons) { + printerr("Bad Argument Type"); + env->err= 2; + return; + } + + item=CDR(item); + } + + if(item->type == tcons){ /* A match was found */ + push_val(env, CAR(item)); + } else { + push_int(env, 0); + } + swap(env); if(env->err) return; + toss(env); if(env->err) return; + swap(env); if(env->err) return; + toss(env); +} + + /* 2: 3 => */ /* 1: [ [ 1 . 2 ] [ 3 . 4 ] ] => 1: [ 3 . 4 ] */ extern void assq(environment *env)