//deep-bindによる。シンボルが見つからなかったら登録。 //見つかったらそこに値をいれておく。 void bindsym(int symaddr, int valaddr){ int addr,num; char *name; name = symname(symaddr); if((addr=findsym(name)) == NIL){ addr = freshcell(); SET_NAME(addr,name); SET_CDR(addr,E); E = addr; } switch(GET_TAG(valaddr)){ case NUM: { SET_TAG(addr,NUM); num = GET_NUMBER(valaddr); SET_NUMBER(addr,num); break; } case SYM: { SET_TAG(addr,SYM); name = GET_NAME(valaddr); SET_NAME(addr,name); break; } case LIS: { SET_TAG(addr,LIS); SET_BIND(addr,valaddr); break; } } }
int makesym(char *name){ int addr; addr = freshcell(); SET_TAG(addr,SYM); SET_NAME(addr,name); return(addr); }
int makenum(int num){ int addr; addr = freshcell(); SET_TAG(addr,NUM); SET_NUMBER(addr,num); return(addr); }
//空リストを作る。シンボルnilを空リストとも解釈している。 int makeempty(void){ int addr; addr = freshcell(); SET_TAG(addr,SYM); SET_NAME(addr,"nil"); return(addr); }
//シンボルTを返す。 int makeT(void){ int addr; addr = freshcell(); SET_TAG(addr,SYM); SET_NAME(addr,"t"); return(addr); }
int cons(int car, int cdr){ int addr; addr = freshcell(); SET_TAG(addr,LIS); SET_CAR(addr,car); SET_CDR(addr,cdr); return(addr); }
void bindfunc1(char *name, int addr){ int sym,val; sym = makesym(name); val = freshcell(); SET_TAG(val,FUNC); SET_BIND(val,addr); SET_CDR(val,0); bindsym(sym,val); }
void bindfunc(char *name, tag tag, int(*func)(int)){ int sym,val; sym = makesym(name); val = freshcell(); SET_TAG(val,tag); SET_SUBR(val,func); SET_CDR(val,0); bindsym(sym,val); }
void initcell(void){ int addr,addr1; for(addr=0; addr < HEAPSIZE; addr++){ heap[addr].tag = EMP; heap[addr].cdr = addr+1; } H = 0; //0番地はnil、1番地はTとして環境レジスタを設定する。初期環境 addr = freshcell(); //symbol nil SET_TAG(addr,SYM); SET_NAME(addr,"nil"); addr1 = freshcell(); //symbol t SET_TAG(addr1,SYM); SET_NAME(addr1,"t"); SET_CDR(addr1,addr); E = addr1; }
void bindfunc(char *name, tag tag, int func){ int sym,val; sym = makesym(name); val = freshcell(); SET_TAG(val,tag); switch(tag){ case SUBR: case FSUBR: SET_SUBR(val,func); break; case LAMBDA: SET_BIND(val,func); break; } SET_CDR(val,0); bindsym(sym,val); }
void bindfunc(char *name, ftype ftype, int func){ int addr; addr = freshcell(); SET_NAME(addr,name); SET_TAG(addr,FUN); SET_FTYPE(addr,ftype); switch(ftype){ case SUBR: case FSUBR: SET_SUBR(addr,func); case LAMBDA: SET_BIND(addr,func); } SET_CDR(addr,E); E = addr; }