bf-minimal-14 / code /bfrun.c
turtlecannon's picture
Add files using upload-large-folder tool
7d56523 verified
Raw History Blame Contribute Delete
3.7 kB
// Fast interpreter for the brainphoque machine (same results as bf.py's run_reference).
// Library: cc -O2 -shared -fPIC -o libbfrun.so bfrun.c (used by bf.py)
// Tool: cc -O2 -DBFRUN_MAIN -o bfrun bfrun.c
// bfrun < programs.txt > outputs.bin
// The tool reads one program per line and writes, per program, a 4-byte little-endian length n
// followed by the n output bytes (trailing zeros removed); a run that does not complete has n = 0
// and is reported on stderr. Every symbol costs one step; a run completes if it halts or prints
// its 4,095th byte within 65,536 steps.
#include <stdio.h>
#include <string.h>
#define TAPE 256
#define T 4095
#define BUDGET 65536
#define MAXP 4096
// Returns 1 halted, 2 printed T bytes, 0 cut off (no output), -1 bad symbol.
// out receives up to T bytes; *n = bytes printed (before removing trailing zeros), *steps = steps used.
int bf_run(const char *prog, unsigned char *out, int *n, int *steps) {
int L = (int)strlen(prog), match[MAXP], stack[MAXP], sp = 0;
if (L > MAXP) return -1;
for (int i = 0; i < L; i++) {
match[i] = -1;
if (prog[i] == '[') stack[sp++] = i;
else if (prog[i] == ']' && sp) { int j = stack[--sp]; match[i] = j; match[j] = i; }
else if (!strchr("<>+-[].ZRLNCGHWVX", prog[i]) || !prog[i]) return -1;
}
unsigned char tape[TAPE] = {0}, x;
unsigned char p = 0; // wraps mod 256 = TAPE
int pc = 0, s = 0, no = 0, k;
#define EMIT(v) do { out[no++] = (v); if (no == T) { *n = no; *steps = s; return 2; } } while (0)
while (pc < L) {
if (s >= BUDGET) { *n = 0; *steps = s; return 0; }
s++;
switch (prog[pc]) {
case '+': tape[p]++; break;
case '-': tape[p]--; break;
case '>': p++; break;
case '<': p--; break;
case '[': if (match[pc] >= 0 && !tape[p]) pc = match[pc]; break;
case ']': if (match[pc] >= 0 && tape[p]) pc = match[pc]; break;
case '.': EMIT(tape[p]); break;
case 'Z': tape[p] = 0; break;
case 'X': tape[p] = 16; break;
case 'R': x = tape[p]; tape[p] = 0; tape[(unsigned char)(p + 1)] += x; break;
case 'L': x = tape[p]; tape[p] = 0; tape[(unsigned char)(p + 1)] += 3 * x; break;
case 'N': x = tape[p]; tape[p] = 0; tape[(unsigned char)(p - 1)] -= x; break;
case 'C': x = tape[p]; tape[p] = 0; tape[(unsigned char)(p + 1)] += x; tape[(unsigned char)(p + 2)] += x; break;
case 'G': for (k = 0; k < TAPE && tape[p]; k++) p++; if (tape[p]) { *n = 0; *steps = s; return 0; } break;
case 'H': for (k = 0; k < TAPE && tape[p]; k++) p--; if (tape[p]) { *n = 0; *steps = s; return 0; } break;
case 'W': if (tape[p]) { tape[p] = 0; tape[(unsigned char)(p + 1)]++; } break;
case 'V': while (tape[p]) { EMIT(tape[p]); p++; } break;
}
pc++;
}
#undef EMIT
*n = no; *steps = s; return 1;
}
#ifdef BFRUN_MAIN
int main(void) {
static char line[MAXP + 2]; static unsigned char out[T];
long long count = 0, bad = 0;
while (fgets(line, sizeof line, stdin)) {
line[strcspn(line, "\r\n")] = 0;
int n = 0, s = 0, r = bf_run(line, out, &n, &s);
if (r <= 0) { n = 0; bad++; fprintf(stderr, "%s: %s\n", line, r < 0 ? "bad program" : "does not complete"); }
while (n > 0 && out[n - 1] == 0) n--;
unsigned char len[4] = {n & 255, (n >> 8) & 255, 0, 0};
fwrite(len, 1, 4, stdout); fwrite(out, 1, n, stdout); count++;
}
fprintf(stderr, "%lld programs, %lld without output\n", count, bad);
return 0;
}
#endif