diff options
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/functions.c b/functions.c index 4b66972..bc4d96c 100644 --- a/functions.c +++ b/functions.c @@ -123,6 +123,7 @@ double gcd_float(double, double); Array * runfunc(Function f, Array *left, Array *right) { + Array *result; if(f.type == FunctypeDfn){ Symtab *tmpsymtab = currentsymtab; currentsymtab = newsymtab(); @@ -144,7 +145,7 @@ runfunc(Function f, Array *left, Array *right) Datum *dfnres = evalline(f.dfn, 0); freesymtab(currentsymtab); currentsymtab = tmpsymtab; - return (*dfnres).array; /* TODO what if the evaluation failed */ + result = (*dfnres).array; /* TODO what if the evaluation failed */ }else if(f.type == FunctypePrim){ if(left){ fndyad d = dyadfunctiondefs[f.code]; @@ -152,14 +153,14 @@ runfunc(Function f, Array *left, Array *right) Rune *err = runesmprint("dyadic %C", primfuncnames[f.code]); throwerror(err, ENotImplemented); } - return d(left, right); + result = d(left, right); }else{ fnmonad m = monadfunctiondefs[f.code]; if(m == nil){ Rune *err = runesmprint("monadic %C", primfuncnames[f.code]); throwerror(err, ENotImplemented); } - return m(right); + result = m(right); } }else if(f.type == FunctypeOp && f.operator.type == OperatortypeHybrid){ opmonad m = hybridoperatordefs[f.operator.code]; @@ -167,7 +168,7 @@ runfunc(Function f, Array *left, Array *right) Rune *err = runesmprint("monadic %C", primhybridnames[f.operator.code]); throwerror(err, ENotImplemented); } - return m(f.operator.left, left, right); + result = m(f.operator.left, left, right); }else if(f.type == FunctypeOp){ /* TODO assumes prim op, not dop */ if(f.operator.dyadic){ @@ -176,14 +177,14 @@ runfunc(Function f, Array *left, Array *right) Rune *err = runesmprint("dyadic %C", primdyadopnames[f.operator.code]); throwerror(err, ENotImplemented); } - return d(f.operator.left, f.operator.right, left, right); + result = d(f.operator.left, f.operator.right, left, right); }else{ opmonad m = monadoperatordefs[f.operator.code]; if(m == nil){ Rune *err = runesmprint("monadic %C", primmonopnames[f.operator.code]); throwerror(err, ENotImplemented); } - return m(f.operator.left, left, right); + result = m(f.operator.left, left, right); } }else if(f.type == FunctypeQuad){ if(left){ @@ -191,13 +192,13 @@ runfunc(Function f, Array *left, Array *right) Rune *err = runesmprint("dyadic %S", f.quad->name); throwerror(err, ENotImplemented); } - return f.quad->dyadfn(left, right); + result = f.quad->dyadfn(left, right); }else{ if(f.quad->monadfn == nil){ Rune *err = runesmprint("monadic %S", f.quad->name); throwerror(err, ENotImplemented); } - return f.quad->monadfn(right); + result = f.quad->monadfn(right); } }else if(f.type == FunctypeHybrid){ fndyad d = hybridfunctiondefs[f.code]; @@ -205,13 +206,17 @@ runfunc(Function f, Array *left, Array *right) Rune *err = runesmprint("dyadic %C", primhybridnames[f.code]); throwerror(err, ENotImplemented); } - return d(left, right); + result = d(left, right); }else if(f.type == FunctypeTrain) - return runtrain(f.train.funcs, f.train.nfuncs, left, right, nil); + result = runtrain(f.train.funcs, f.train.nfuncs, left, right, nil); else{ throwerror(L"runfunc for this function type", ENotImplemented); - return nil; + result = nil; } + Array *tmp = result; + result = simplifyarray(result); + freearray(tmp); + return result; } Array * @@ -227,13 +232,9 @@ Array * runtrain(Function *funcs, int nfuncs, Array *left, Array *right, Array *acc) { if(nfuncs >= 3 && acc == nil && !(funcs[0].left != nil && nfuncs == 3)){ - Array *rtmp = runfunc(funcs[nfuncs-1], left, right); - Array *ltmp = runfunc(funcs[nfuncs-3], left, right); - Array *r = simplifyarray(rtmp); - Array *l = simplifyarray(ltmp); + Array *r = runfunc(funcs[nfuncs-1], left, right); + Array *l = runfunc(funcs[nfuncs-3], left, right); Array *c = runfunc(funcs[nfuncs-2], l, r); - freearray(rtmp); - freearray(ltmp); freearray(r); freearray(l); if(nfuncs == 3) @@ -241,10 +242,8 @@ runtrain(Function *funcs, int nfuncs, Array *left, Array *right, Array *acc) else return runtrain(funcs, nfuncs-3, left, right, c); }else if(nfuncs >= 2 && acc != nil && !(funcs[0].left != nil && nfuncs == 2)){ - Array *ltmp = runfunc(funcs[nfuncs-2], left, right); - Array *l = simplifyarray(ltmp); + Array *l = runfunc(funcs[nfuncs-2], left, right); Array *c = runfunc(funcs[nfuncs-1], l, acc); - freearray(ltmp); freearray(acc); freearray(l); if(nfuncs == 2) @@ -252,10 +251,8 @@ runtrain(Function *funcs, int nfuncs, Array *left, Array *right, Array *acc) else return runtrain(funcs, nfuncs-2, left, right, c); }else if(nfuncs == 2 && acc == nil){ - Array *rtmp = runfunc(funcs[1], left, right); - Array *r = simplifyarray(rtmp); + Array *r = runfunc(funcs[1], left, right); Array *c = runfunc(funcs[0], funcs[0].left, r); - freearray(rtmp); freearray(r); return c; }else if(nfuncs == 1 && acc != nil){ |