bf-rasp-minimal-13 / code /bfrasp_run.c
turtlecannon's picture
bf-rasp-minimal-13: all outputs of bf-rasp programs up to length 13
222d213 verified
Raw History Blame Contribute Delete
5.5 kB
// bfrasp_run.c: fast bf-rasp generator (the same semantics as bfrasp.py; see the dataset card).
//
// Library: cc -O2 -shared -fPIC -o libbfrasp_run.so bfrasp_run.c
// int bfrasp_generate(const char *prog, int T, unsigned char *out); // 0 if invalid, else 1
// int bfrasp_measures(const char *prog, int *depth, int *n_attn, int *width);
// Command line: cc -O2 -o bfrasp_run bfrasp_run.c
// ./bfrasp_run [T] programs on stdin (one per line) -> T output bytes each on stdout
// ./bfrasp_run --measures programs on stdin -> "depth n_attn width" per line
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXL 64
static const char ALPHA[] = "T0123+-%=<!&?CPMDS";
static const int ARITY[] = {0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 1, 2, 3, 1, 1, 3, 0, 0};
enum { S_T, S_0, S_1, S_2, S_3, S_ADD, S_SUB, S_MOD, S_EQ, S_LT, S_NOT, S_AND, S_IF, S_C, S_P, S_M, S_D, S_S };
enum { TN, TB };
static int result_type(int s, const int *t) {
switch (s) {
case S_T: case S_2: case S_3: case S_ADD: case S_SUB: case S_MOD: return TN;
case S_0: case S_1: case S_EQ: case S_LT: return TB;
case S_NOT: return t[0] == TB ? TB : -1;
case S_AND: return t[0] == TB && t[1] == TB ? TB : -1;
case S_IF: return t[0] != TB ? -1 : (t[1] == TB && t[2] == TB ? TB : TN);
case S_C: return t[0] == TB ? TN : -1;
case S_P: return t[0];
case S_M: return t[2];
}
return -1;
}
// Program -> nodes (symbol, input nodes in push order) and the output node; -1 if invalid.
static int compile(const char *p, int *sym, int (*args)[3], int *nnodes) {
int stack[MAXL + 2], ty[MAXL], h = 0, n = 0, len = (int)strlen(p);
if (len > MAXL) return -1;
for (int i = 0; i < len; i++) {
const char *q = strchr(ALPHA, p[i]);
if (!q || !p[i]) return -1;
int s = (int)(q - ALPHA);
if (s == S_D) { if (h < 1) return -1; stack[h] = stack[h - 1]; h++; continue; }
if (s == S_S) { if (h < 2) return -1; int t = stack[h - 1]; stack[h - 1] = stack[h - 2]; stack[h - 2] = t; continue; }
int k = ARITY[s], ts[3];
if (h < k) return -1;
for (int j = 0; j < k; j++) { args[n][j] = stack[h - k + j]; ts[j] = ty[stack[h - k + j]]; }
int t = result_type(s, ts);
if (t < 0) return -1;
h -= k; sym[n] = s; ty[n] = t; stack[h++] = n++;
}
if (h != 1) return -1;
*nnodes = n;
return stack[0];
}
int bfrasp_generate(const char *p, int T, unsigned char *out) {
int sym[MAXL], args[MAXL][3], n;
int top = compile(p, sym, args, &n);
if (top < 0) return 0;
unsigned char v[MAXL] = {0}, cnt[MAXL] = {0}, prev[MAXL] = {0}, val[MAXL][256], has[MAXL][256];
for (int i = 0; i < n; i++) if (sym[i] == S_M) memset(has[i], 0, 256);
unsigned char x = 0; // the sequence starts with the token 0
for (int q = 0; q < T; q++) {
for (int i = 0; i < n; i++) {
const int *a = args[i]; unsigned char r = 0;
switch (sym[i]) {
case S_T: r = x; break;
case S_0: case S_1: case S_2: case S_3: r = (unsigned char)(sym[i] - S_0); break;
case S_ADD: r = v[a[0]] + v[a[1]]; break;
case S_SUB: r = v[a[0]] - v[a[1]]; break;
case S_MOD: r = v[a[1]] ? v[a[0]] % v[a[1]] : v[a[0]]; break;
case S_EQ: r = v[a[0]] == v[a[1]]; break;
case S_LT: r = v[a[0]] < v[a[1]]; break;
case S_NOT: r = 1 - v[a[0]]; break;
case S_AND: r = v[a[0]] & v[a[1]]; break;
case S_IF: r = v[a[0]] ? v[a[1]] : v[a[2]]; break;
case S_C: r = cnt[i] = cnt[i] + v[a[0]]; break; // count of k <= q, mod 256
case S_P: r = prev[i]; prev[i] = v[a[0]]; break; // value at q - 1 (0 at q = 0)
case S_M: r = has[i][v[a[1]]] ? val[i][v[a[1]]] : 0; // latest k < q with K[k] = Q[q]
has[i][v[a[0]]] = 1; val[i][v[a[0]]] = v[a[2]]; break;
}
v[i] = r;
}
x = v[top]; out[q] = x;
}
return 1;
}
int bfrasp_measures(const char *p, int *depth, int *n_attn, int *width) {
int sym[MAXL], args[MAXL][3], n, d[MAXL], per[MAXL + 1] = {0};
int top = compile(p, sym, args, &n);
if (top < 0) return 0;
*n_attn = 0; *width = 0;
for (int i = 0; i < n; i++) {
int m = 0;
for (int j = 0; j < ARITY[sym[i]]; j++) if (d[args[i][j]] > m) m = d[args[i][j]];
int att = sym[i] == S_C || sym[i] == S_P || sym[i] == S_M;
d[i] = m + att;
if (att) { (*n_attn)++; if (++per[d[i]] > *width) *width = per[d[i]]; }
}
*depth = d[top];
return 1;
}
#ifndef BFRASP_LIB
int main(int argc, char **argv) {
int measures = argc > 1 && !strcmp(argv[1], "--measures");
int T = (!measures && argc > 1) ? atoi(argv[1]) : 4095;
unsigned char *y = malloc(T > 0 ? T : 1);
char line[256];
while (fgets(line, sizeof line, stdin)) {
line[strcspn(line, "\r\n")] = 0;
if (measures) {
int d, a, w;
if (!bfrasp_measures(line, &d, &a, &w)) { fprintf(stderr, "invalid program %s\n", line); return 1; }
printf("%d %d %d\n", d, a, w);
} else {
if (!bfrasp_generate(line, T, y)) { fprintf(stderr, "invalid program %s\n", line); return 1; }
fwrite(y, 1, T, stdout);
}
}
return 0;
}
#endif