aboutsummaryrefslogtreecommitdiff
path: root/docs/history/07-zygote/code/mkgenome.c
diff options
context:
space:
mode:
authorGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
committerGravatar Moon Flower <moonflower@elhenney.local> 2026-10-12 08:46:23 +0330
commitecc350ca5c2eb50be8309c5f14f8bf0d87db9a02 (patch)
tree70e0af76a1b13affff7c6cc8aac7b3e6ef960478 /docs/history/07-zygote/code/mkgenome.c
a
Diffstat (limited to 'docs/history/07-zygote/code/mkgenome.c')
-rw-r--r--docs/history/07-zygote/code/mkgenome.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/docs/history/07-zygote/code/mkgenome.c b/docs/history/07-zygote/code/mkgenome.c
new file mode 100644
index 0000000..7d9f0bc
--- /dev/null
+++ b/docs/history/07-zygote/code/mkgenome.c
@@ -0,0 +1,35 @@
+/* assembles the genome at a fixed origin and writes it as a c header */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#define ORG 48000
+static char tok[30000][99];
+static int lab[30000], nt, img[17536], n;
+static char *ops = "q@!$_~cjzr,?.+-*&|^><";
+static int val(char *s)
+{
+ int j; char *e; long v;
+ for (j = nt; j--;) if (tok[j][0] == ':' && !strcmp(tok[j] + 1, s)) return lab[j];
+ v = strtol(s, &e, 0);
+ if (!*s || *e) { fprintf(stderr, "unknown label %s\n", s); exit(1); }
+ return (int)v;
+}
+static void put(int w) { if (n >= 17536) exit(1); img[n++] = w; }
+int main(void)
+{
+ int pass, i; char *q, *c;
+ while (nt < 30000 && scanf("%98s", tok[nt]) == 1) nt++;
+ for (pass = 0; pass < 2; pass++)
+ for (n = i = 0; i < nt; i++) {
+ q = tok[i];
+ if (*q == '"') while (*++q) put(*q);
+ else if (*q == ':') lab[i] = ORG + n;
+ else if (!q[1] && (c = strchr(ops, *q))) put((c - ops) * 2);
+ else if (*q == '#') put(pass ? val(q + 1) : 0);
+ else put(pass ? val(q) * 2 + 1 : 1);
+ }
+ printf("#define GORG %d\n#define GLEN %d\nstatic const int genome[] = {", ORG, n);
+ for (i = 0; i < n; i++) printf("%s%d", !i ? "\n" : i % 16 ? "," : ",\n", img[i]);
+ printf("\n};\n");
+ return 0;
+}