From 07082593ab4abfbf9a3dd6729cb2e548ec303115 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Fri, 14 Jan 2022 00:31:03 +0000 Subject: =?UTF-8?q?Implement=20code=20for=20running=20operators=20(both=20?= =?UTF-8?q?monadic=20and=20dyadic).=20Also=20implement=20=E2=8D=A8=20and?= =?UTF-8?q?=20=E2=8D=A5=20since=20they=20are=20very=20simple?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- operators.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 operators.c (limited to 'operators.c') diff --git a/operators.c b/operators.c new file mode 100644 index 0000000..ba999bd --- /dev/null +++ b/operators.c @@ -0,0 +1,64 @@ +#include +#include +#include + +#include "apl9.h" + +Rune primmonopnames[] = L"¨⍨⌸⌶&"; +Rune primdyadopnames[] = L"⍣.∘⍤⍥@⍠⌺"; + +opmonad monadoperatordefs[] = { + 0, /* ¨ */ + opSwitch, /* ⍨ */ + 0, /* ⌸ */ + 0, /* ⌶ */ + 0, /* & */ +}; + +opdyad dyadoperatordefs[] = { + 0, /* ⍣ */ + 0, /* . */ + 0, /* ∘ */ + 0, /* ⍤ */ + opOver, /* ⍥ */ + 0, /* @ */ + 0, /* ⍠ */ + 0, /* ⌺ */ +}; + +/* Monadic operators */ +Array * +opSwitch(Datum *lefto, Array *left, Array *right) +{ + if(lefto->tag == ArrayTag){ + incref(lefto->array); + return lefto->array; + }else if(lefto->tag == FunctionTag){ + if(left) + return runfunc(lefto->func, right, left); + else + return runfunc(lefto->func, right, right); + }else + return nil; +} + +/* Dyadic operators */ +Array * +opOver(Datum *lefto, Datum *righto, Array *left, Array *right) +{ + if(lefto->tag != FunctionTag || righto->tag != FunctionTag) + return nil; + if(left){ + Array *r = runfunc(righto->func, nil, right); + Array *l = runfunc(righto->func, nil, left); + Array *res = runfunc(lefto->func, l, r); + freearray(r); + freearray(l); + return res; + }else{ + Array *tmp = runfunc(righto->func, nil, right); + Array *res = runfunc(lefto->func, nil, tmp); + freearray(tmp); + return res; + } +} \ No newline at end of file -- cgit v1.2.3