aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/cc/cc.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-05-16 10:39:05 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-05-16 10:39:05 -0700
commit6c9563e91eb7aa738aa2656d141ce56ef66b50cf (patch)
tree85fd9a50777ae8f5ec14ca112d26c5f4b9262113 /sys/cmd/cc/cc.c
parent3f7474df0645224ce61fedcd908028f41971189e (diff)
feat: added directory for command line tools
Diffstat (limited to 'sys/cmd/cc/cc.c')
-rw-r--r--sys/cmd/cc/cc.c93
1 files changed, 93 insertions, 0 deletions
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 <libn/macro/map.h>
+
+/* 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;
+}