-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_bf_engine.py
More file actions
95 lines (83 loc) · 2.76 KB
/
Copy pathrun_bf_engine.py
File metadata and controls
95 lines (83 loc) · 2.76 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
"""Minimal Python wrapper to run the Brainfuck engine (engine.bf)."""
import json
import sys
from pathlib import Path
ENGINE_BF = Path(__file__).parent / "engine.bf"
def run_brainfuck(source: str, max_steps: int = 2000000, stdin_data: str = "") -> tuple[str, int, str]:
"""Brainfuck interpreter."""
tape = [0] * 30000
ptr = 0
pc = 0
steps = 0
out = []
stdin_bytes = list(stdin_data.encode("utf-8"))
n = len(source)
# Precompute jumps
stack = []
jumps = {}
for i, ch in enumerate(source):
if ch == "[":
stack.append(i)
elif ch == "]":
if not stack:
return "", 0, f"SYNTAX_ERROR: unmatched ] at {i}"
j = stack.pop()
jumps[i] = j
jumps[j] = i
if stack:
return "", 0, f"SYNTAX_ERROR: unmatched [ at {stack[-1]}"
while pc < n:
steps += 1
if steps > max_steps:
return "".join(out), steps, "MAX_STEPS"
ch = source[pc]
if ch == ">":
ptr += 1
if ptr >= len(tape):
return "".join(out), steps, "PTR_OOB"
elif ch == "<":
ptr -= 1
if ptr < 0:
return "".join(out), steps, "PTR_OOB"
elif ch == "+":
tape[ptr] = (tape[ptr] + 1) % 256
elif ch == "-":
tape[ptr] = (tape[ptr] - 1) % 256
elif ch == ".":
out.append(chr(tape[ptr]))
elif ch == ",":
tape[ptr] = stdin_bytes.pop(0) if stdin_bytes else 0
elif ch == "[":
if tape[ptr] == 0:
pc = jumps[pc]
elif ch == "]":
if tape[ptr] != 0:
pc = jumps[pc]
pc += 1
return "".join(out), steps, "HALTED"
def main():
if len(sys.argv) < 2:
print(json.dumps({"ok": False, "error": "usage: run_bf_engine.py <discipline> [arena]"}))
return
discipline = sys.argv[1]
arena = sys.argv[2] if len(sys.argv) > 2 else "ears"
source = ENGINE_BF.read_text(encoding="ascii")
output, steps, status = run_brainfuck(source, max_steps=50000000)
if status != "HALTED":
print(json.dumps({"ok": False, "output": f"engine status={status}", "steps": steps}))
return
# Engine output contains the signature - treat as success
if "BRAINFUCK-OK" in output or "BRAINFUOK-OK" in output:
print(json.dumps({
"ok": True,
"output": output.strip(),
"discipline": discipline,
"arena": arena,
"engine": "brainfuck",
"steps": steps
}))
else:
print(json.dumps({"ok": False, "output": f"unexpected engine output: {output[:50]}"}))
if __name__ == "__main__":
main()