/* bytefunc.c by Michael Thorpe 2009-05-19 */ #include #include "lisp.h" static obj *bytefunc_tostring(obj *in) { obj *code,*objs; obj *out=0; code=(in->value.bytefunc.code->type->tostring)(INCREF(in->value.bytefunc.code)); if(code) { objs=(in->value.bytefunc.objs->type->tostring)(INCREF(in->value.bytefunc.objs)); if(objs) { out=new_string(0,9+code->value.s.len+1+1+objs->value.s.len+1); if(out) { memcpy(out->value.s.data,"(#lambda ",9); memcpy(out->value.s.data+9,code->value.s.data,code->value.s.len); out->value.s.data[9+code->value.s.len]=' '; out->value.s.data[9+code->value.s.len+1]='\''; memcpy(out->value.s.data+9+code->value.s.len+1+1,objs->value.s.data,objs->value.s.len); out->value.s.data[9+code->value.s.len+1+1+objs->value.s.len]=')'; } DECREF(objs); } DECREF(code); } DECREF(in); return(out); } static void drop_bytefunc(obj *o) { DECREF(o->value.bytefunc.code); DECREF(o->value.bytefunc.objs); } static obj *lambda(obj *code,obj *objs) { obj *code2,*o; if(&string_objtype != code->type) { DECREF(objs); return(throwtypeerror(code)); } if(1 != code->rcnf.refcount) { /* Make a copy of it so that it's immutable */ code2=new_string(code->value.s.data,code->value.s.len); DECREF(code); if(!code2) { DECREF(objs); return(0); } code=code2; } o=new_object(&bytefunc_objtype); if(!o) { DECREF(code); DECREF(objs); return(0); } o->value.bytefunc.code=code; o->value.bytefunc.objs=objs; return(o); } static obj *code_of_bytefunc(obj *o) { obj *c; if(&bytefunc_objtype != o->type) { return(throwtypeerror(o)); } c=new_string(o->value.bytefunc.code->value.s.data,o->value.bytefunc.code->value.s.len); DECREF(o); return(c); /* This is fine even when new_string fails */ } static obj *objs_of_bytefunc(obj *o) { obj *o2; if(&bytefunc_objtype != o->type) { return(throwtypeerror(o)); } o2=INCREF(o->value.bytefunc.objs); DECREF(o); return(o2); } static int bytefunc_init() { if(store_builtin_ns2("#lambda",lambda) || store_builtin_ns1("code-of-bytefunc",code_of_bytefunc) || store_builtin_ns1("objects-of-bytefunc",objs_of_bytefunc)) return(1); return(0); } objtype bytefunc_objtype={ name: "bytefunc", tostring: bytefunc_tostring, dropobj: drop_bytefunc, init: bytefunc_init, };