diff options
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 73 |
1 files changed, 34 insertions, 39 deletions
diff --git a/functions.c b/functions.c index 37cafa1..34f5db1 100644 --- a/functions.c +++ b/functions.c @@ -128,39 +128,20 @@ runfunc(Function f, Array *left, Array *right) Array *result; if(f.type == FunctypeDfn || (f.type == FunctypeOp && f.operator.type == OperatortypeDop)){ Rune *code; + Datum *lefto = nil; + Datum *righto = nil; if(f.type == FunctypeDfn) code = f.dfn; else{ + lefto = f.operator.left; + righto = f.operator.right; code = f.operator.dop; } - pushdfnframe(code); - if(left){ - Symbol *alpha = getsym(L"⍺", 1); - alpha->value.tag = ArrayTag; - alpha->value.array = left; - alpha->undefined = 0; - incref(left); - } - - Symbol *omega = getsym(L"⍵", 1); - omega->value.tag = ArrayTag; - omega->value.array = right; - omega->undefined = 0; - incref(right); - - if(f.type == FunctypeOp){ - if(f.operator.dyadic){ - Symbol *omegaop = getsym(L"⍹", 1); - omegaop->value = *f.operator.right; - omegaop->undefined = 0; - } - Symbol *alphaop = getsym(L"⍶", 1); - alphaop->value = *f.operator.left; - alphaop->undefined = 0; - } + pushdfnframe(code, lefto, righto, left, right); Datum *dfnres = evalline(code, nil, 0); popdfnframe(); + result = (*dfnres).array; /* TODO what if the evaluation failed */ }else if(f.type == FunctypePrim){ if(left){ @@ -236,12 +217,26 @@ runfunc(Function f, Array *left, Array *right) } Array * -rundfn(Rune *code, Array *left, Array *right) +rundfn(Rune *code, Datum *lefto, Datum *righto, Array *left, Array *right) { - Function dfn; - dfn.type = FunctypeDfn; - dfn.dfn = code; - return runfunc(dfn, left, right); + if(lefto == nil && righto == nil){ + Function dfn; + dfn.type = FunctypeDfn; + dfn.dfn = code; + return runfunc(dfn, left, right); + }else if(lefto != nil){ + Function dop; + dop.type = FunctypeOp; + dop.operator.type = OperatortypeDop; + dop.operator.left = lefto; + dop.operator.right = righto; + dop.operator.dyadic = righto != nil; + dop.operator.dop = code; + return runfunc(dop, left, right); + }else{ + throwerror(L"Malformed call to rundfn", ENotImplemented); + return nil; + } } Array * @@ -286,19 +281,19 @@ runtrain(Function *funcs, int nfuncs, Array *left, Array *right, Array *acc) Array * fnNegate(Array *right) { - return rundfn(L"0-⍵", nil, right); + return rundfn(L"0-⍵", nil, nil, nil, right); } Array * fnSign(Array *right) { - return rundfn(L"(¯1×⍵<0)+⍵>0", nil, right); + return rundfn(L"(¯1×⍵<0)+⍵>0", nil, nil, nil, right); } Array * fnRecip(Array *right) { - return rundfn(L"1÷⍵", nil, right); + return rundfn(L"1÷⍵", nil, nil, nil, right); } Array * @@ -331,13 +326,13 @@ fnPiTimes(Array *right) Array * fnMagnitude(Array *right) { - return rundfn(L"⍵××⍵", nil, right); + return rundfn(L"⍵××⍵", nil, nil, nil, right); } Array * fnCeiling(Array *right) { - return rundfn(L"-⌊-⍵", nil, right); + return rundfn(L"-⌊-⍵", nil, nil, nil, right); } Array * @@ -478,7 +473,7 @@ Array * fnSplit(Array *right) { Rune *code = L"0≡≢⍴⍵: ⍵ ⋄ (⊂⍵)⌷⍨¨⍳≢⍵"; - return rundfn(code, nil, right); + return rundfn(code, nil, nil, nil, right); } Array * @@ -714,7 +709,7 @@ fnSelfRef1(Array *right) { DfnFrame *dfn = getcurrentdfn(); if(dfn) - return rundfn(dfn->code, nil, right); + return rundfn(dfn->code, nil, nil, nil, right); else{ throwerror(nil, ESyntax); return nil; @@ -1111,7 +1106,7 @@ fnIndex(Array *left, Array *right) Array * fnCatenateLast(Array *left, Array *right) { - return rundfn(L"⍉(⍉⍺)⍪⍉⍵", left, right); + return rundfn(L"⍉(⍉⍺)⍪⍉⍵", nil, nil, left, right); } Array * @@ -1255,7 +1250,7 @@ fnSelfRef2(Array *left, Array *right) { DfnFrame *dfn = getcurrentdfn(); if(dfn) - return rundfn(dfn->code, left, right); + return rundfn(dfn->code, nil, nil, left, right); else{ throwerror(nil, ESyntax); return nil; |