ref: d82e1615b58bf59f1d8b14dd62c588be6ea1881c
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 *(*nilad)(void); Array *(*monad)(Array *); Array *(*dyad)(Array *, Array *); } primspecs[] = { "⊢", NameclassFunc, nil, primfn_same, primfn_right, "⊣", NameclassFunc, nil, primfn_same, primfn_left, "+", NameclassFunc, nil, nil, nil, "-", NameclassFunc, nil, 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 * primnilad(int id) { if(primspecs[id].nilad) return primspecs[id].nilad(); else error(EInternal, "primitive %s has no niladic definition", primsymb(id)); } Array * primmonad(int id, Array *y) { if(primspecs[id].monad) return primspecs[id].monad(y); else error(EInternal, "primitive %s has no monadic definition", primsymb(id)); } Array * primdyad(int id, Array *x, Array *y) { if(primspecs[id].dyad) return primspecs[id].dyad(x, y); else error(EInternal, "primitive %s has no dyadic definition", primsymb(id)); } /* 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; }