-
Notifications
You must be signed in to change notification settings - Fork 26
/
prim.c
53 lines (43 loc) · 1.24 KB
/
prim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* prim.c -- primitives and primitive dispatching ($Revision: 1.1.1.1 $) */
#include "es.h"
#include "prim.h"
static Dict *prims;
extern List *prim(char *s, List *list, Binding *binding, int evalflags) {
List *(*p)(List *, Binding *, int);
p = (List *(*)(List *, Binding *, int)) dictget(prims, s);
if (p == NULL)
fail("es:prim", "unknown primitive: %s", s);
return (*p)(list, binding, evalflags);
}
static char *list_prefix;
static void listwithprefix(void *arg, char *key, void *value) {
if (strneq(key, list_prefix, strlen(list_prefix)))
addtolist(arg, key, value);
}
extern List *primswithprefix(char *prefix) {
Ref(List *, primlist, NULL);
list_prefix = prefix;
dictforall(prims, listwithprefix, &primlist);
RefReturn(primlist);
}
PRIM(primitives) {
static List *primlist = NULL;
if (primlist == NULL) {
globalroot(&primlist);
dictforall(prims, addtolist, &primlist);
primlist = sortlist(primlist);
}
return primlist;
}
extern void initprims(void) {
prims = mkdict();
globalroot(&prims);
prims = initprims_controlflow(prims);
prims = initprims_io(prims);
prims = initprims_etc(prims);
prims = initprims_sys(prims);
prims = initprims_proc(prims);
prims = initprims_access(prims);
#define primdict prims
X(primitives);
}