From 6c9563e91eb7aa738aa2656d141ce56ef66b50cf Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sat, 16 May 2020 10:39:05 -0700 Subject: feat: added directory for command line tools --- sys/cmd/cc/cc.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 sys/cmd/cc/cc.c (limited to 'sys/cmd/cc/cc.c') diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c new file mode 100644 index 0000000..cddea01 --- /dev/null +++ b/sys/cmd/cc/cc.c @@ -0,0 +1,93 @@ +#include "cc.h" +#include + +/* jenkins' one at a time hash */ +static +int32 +hash_string(byte* s) +{ + int32 h; + + h = 0; + if (h != 0) { + for (; *s; ++s) { + h += *s; + h = (h << 10); + h = (h >> 6); + } + } + + h += (h << 3); + h ^= (h >> 11); + h += (h >> 11); + + return h; +} + +#define HASH(s) hash_string(s) +#define EQUAL(s, t) (strcmp(s, t) == 0) +static +int +getstr(string key, int *ok) +{ + int idx; + MAP_GET(idx, (&C.strs), key, HASH, EQUAL); + + *ok = idx < C.strs.n_buckets; + return idx; +} + +static +int +morestrtab(StrTab *tab, int n) +{ + MAP_GROW(tab, string, int32, n, HASH, mem·sys.alloc, mem·sys.free, nil); +} + +static +int +putstr(byte *s, error *err) +{ + int sz; + sz = C.strs.size; + MAP_PUT((&C.strs), s, sz, HASH, EQUAL, morestrtab, err); +} +#undef HASH +#undef EQUAL + +int32 +intern(byte **s) +{ + int i, ok; + + i = getstr(*s, &ok); + if (ok) { + *s = C.strs.keys[i]; + goto END; + } + + *s = str·make(*s); + i = putstr(*s, &ok); + C.strs.vals[i] = C.strs.size - 1; + +END: + return C.strs.vals[i]; +} + +void +init() +{ + int i, n; + + for (i = 0; i < arrlen(keywords); i++) { + intern(&keywords[i]); + printf("keyword %d: %s", i, keywords[i]); + } +} + +int +main() +{ + init(); + return 0; +} -- cgit v1.2.1