aboutsummaryrefslogtreecommitdiff
path: root/docs/history/07-zygote/code/mkgenome.c
blob: 7d9f0bc6189fd8cff16715ab7a4671d2f4cf85a3 (plain) (blame)
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
/* 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;
}