allow defining custom dyadic inverses of builtins

This commit is contained in:
dzaima 2022-12-26 23:57:55 +02:00
parent 013897cc16
commit 1afdd89075
3 changed files with 17 additions and 10 deletions

View File

@ -284,6 +284,8 @@ static void print_funBI(FILE* f, B x) { fprintf(f, "%s", pfn_repr(c(Fun,x)->extr
static B funBI_uc1(B t, B o, B x) { return c(BFn,t)->uc1(t, o, x); }
static B funBI_ucw(B t, B o, B w, B x) { return c(BFn,t)->ucw(t, o, w, x); }
static B funBI_im(B t, B x) { return c(BFn,t)->im(t, x); }
static B funBI_iw(B t, B w, B x) { return c(BFn,t)->iw(t, w, x); }
static B funBI_ix(B t, B w, B x) { return c(BFn,t)->ix(t, w, x); }
static B funBI_identity(B x) { return inc(c(BFn,x)->ident); }
void fns_init() {
gc_addFn(fun_gcFn);
@ -294,6 +296,8 @@ void fns_init() {
TIi(t_funBI,fn_uc1) = funBI_uc1;
TIi(t_funBI,fn_ucw) = funBI_ucw;
TIi(t_funBI,fn_im) = funBI_im;
TIi(t_funBI,fn_iw) = funBI_iw;
TIi(t_funBI,fn_ix) = funBI_ix;
bitUD[0] = a(bi_emptyIVec); // don't increment as it's already gc_add-ed
{ u64* p; B a=m_bitarrv(&p, 1); *p=0; bitUD[1] = a(a); gc_add(a); }
{ u64* p; B a=m_bitarrv(&p, 2); *p=0; bitp_set(p,1,1); bitUD[2] = a(a); bit2x[0] = a; gc_add(a); }

View File

@ -37,7 +37,8 @@ typedef struct BFn {
BBB2B uc1;
BBBB2B ucw;
BB2B im;
B rtInvReg;
BBB2B iw; B rtInvSwap;
BBB2B ix; B rtInvReg;
} BFn;
typedef struct BMd1 {
struct Md1;

View File

@ -602,15 +602,14 @@ static B empty_getU(Arr* x, usz n) {
static void funBI_visit(Value* x) {
mm_visit(((BFn*)x)->rtInvReg);
mm_visit(((BFn*)x)->rtInvSwap);
}
static B funBI_imRt(B t, B x) {
return c1(c(BFn, t)->rtInvReg, x);
}
static B funBI_imInit(B t, B x) {
B f = c(BFn, t)->rtInvReg = c1rt(invFnReg, inc(t));
c(BFn, t)->im = funBI_imRt;
return c1(f, x);
}
static B funBI_imRt(B t, B x) { return c1(c(BFn, t)->rtInvReg, x); }
static B funBI_iwRt(B t, B w, B x) { return c2(c(BFn, t)->rtInvSwap, w, x); }
static B funBI_ixRt(B t, B w, B x) { return c2(c(BFn, t)->rtInvReg, w, x); }
static B funBI_imInit(B t, B x) { B f=c(BFn,t)->rtInvReg; if(f.u==0) f=c(BFn,t)->rtInvReg=c1rt(invFnReg, inc(t)); c(BFn,t)->im=funBI_imRt; return c1(f, x); }
static B funBI_ixInit(B t, B w, B x) { B f=c(BFn,t)->rtInvReg; if(f.u==0) f=c(BFn,t)->rtInvReg=c1rt(invFnReg, inc(t)); c(BFn,t)->ix=funBI_ixRt; return c2(f, w, x); }
static B funBI_iwInit(B t, B w, B x) { B f=c(BFn,t)->rtInvSwap =c1rt(invFnSwap, inc(t)); c(BFn,t)->iw=funBI_iwRt; return c2(f, w, x); }
void* customObj(u64 size, V2v visit, V2v freeO) {
@ -636,7 +635,10 @@ static NOINLINE B m_bfn(BB2B c1, BBB2B c2, u8 id) {
f->uc1 = def_fn_uc1;
f->ucw = def_fn_ucw;
f->im = funBI_imInit;
f->rtInvReg = m_f64(0);
f->iw = funBI_iwInit;
f->ix = funBI_ixInit;
f->rtInvReg = m_f64(0);
f->rtInvSwap = m_f64(0);
B r = tag(f,FUN_TAG); gc_add(r);
return r;
}