-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.h
62 lines (53 loc) · 1.71 KB
/
symbol.h
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
54
55
56
57
58
59
60
61
#ifndef CIAPOS_SYMBOL_H
#define CIAPOS_SYMBOL_H
#include <stdint.h>
#include "dict.h"
#include "slice.h"
typedef uint32_t ciapos_symbol;
static inline unsigned long ciapos_str_hash(char const *_str) {
unsigned char const *str = (unsigned char const *) _str;
unsigned long hash = 5381;
int c;
while ((c = *str++)) hash = ((hash << 5) + hash) + c;
return hash;
}
static inline int ciapos_str_eq(char const *a, char const *b) {
if (!a && !b) return 1;
if ((!a && b) || (a && !b)) return 0;
while (*a == *b && *a) a++, b++;
return *a == *b;
}
CIAPOS_DICT(
ciapos_str2sym,
char const *,
ciapos_symbol,
ciapos_str_hash,
ciapos_str_eq)
CIAPOS_SLICE(ciapos_sym2str, char const *)
typedef struct {
uint32_t symcnt;
ciapos_sym2str sym2str;
ciapos_str2sym str2sym;
char pkgname[256];
} ciapos_symreg;
#define CIAPOS_TAGNIL 0
#define CIAPOS_TAGSYM 1
#define CIAPOS_TAGINT 2
#define CIAPOS_TAGREAL 3
#define CIAPOS_TAGSTR 4
#define CIAPOS_TAGFN 5
#define CIAPOS_TAGOPAQUE 6
#define CIAPOS_TAGENV 7
#define CIAPOS_TAGTUP 8
void ciapos_symreg_init(ciapos_symreg *registry);
void ciapos_symreg_deinit(ciapos_symreg *registry);
ciapos_symbol ciapos_symbolof_pkg(ciapos_symreg *registry, char const *pkg, char const *str);
static inline ciapos_symbol ciapos_symbolof(ciapos_symreg *registry, char const *str) {
return ciapos_symbolof_pkg(registry, registry->pkgname, str);
}
void ciapos_symreg_setpkg(ciapos_symreg *self, char const *pkgname);
void ciapos_symreg_alias(ciapos_symreg *self, ciapos_symbol newname, ciapos_symbol oldname);
static inline char const *ciapos_symname(ciapos_symreg *self, ciapos_symbol sym) {
return ciapos_sym2str_get(&self->sym2str, sym);
}
#endif