ref: fe152caa60e1086c3f5b973e83135d04bde81fe1
dir: /prim.c/
#include <u.h> #include <libc.h> #include <thread.h> #include "dat.h" #include "fns.h" /* NOTE: In LPA, system functions are treated as primitives as well */ /* monadic functions */ static Array *primfn_same(Array *); /* dyadic functions */ static Array *primfn_left(Array *, Array *); static Array *primfn_right(Array *, Array *); struct { char *spelling; int nameclass; Array *(*monad)(Array *); Array *(*dyad)(Array *, Array *); } primspecs[] = { "⊢", NameclassFunc, primfn_same, primfn_right, "⊣", NameclassFunc, primfn_same, primfn_left, "+", NameclassFunc, nil, nil, "-", NameclassFunc, nil, nil, }; char * primsymb(int id) { return primspecs[id].spelling; } int primclass(int id) { return primspecs[id].nameclass; } int primvalence(int id) { int valence = 0; if(primspecs[id].monad) valence |= Monadic; if(primspecs[id].dyad) valence |= Dyadic; return valence; } int primid(char *s) { for(int i = 0; i < nelem(primspecs); i++){ char *x = primspecs[i].spelling; if(strncmp(s, x, strlen(x)) == 0) return i; } return -1; } Array * primmonad(int id, Array *y) { if(primspecs[id].monad) return primspecs[id].monad(y); else{ print("primitive %s has no monadic definition! (acts like ⊢)\n", primsymb(id)); return y; } } Array * primdyad(int id, Array *x, Array *y) { if(primspecs[id].dyad) return primspecs[id].dyad(x, y); else{ print("primitive %s has no dyadic definition! (acts like ⊣)\n", primsymb(id)); return x; } } /* monadic functions */ static Array * primfn_same(Array *a) { return a; } /* dyadic functions */ static Array * primfn_left(Array *x, Array *) { return x; } static Array * primfn_right(Array *, Array *y) { return y; }