Skip to content

Add Brainfuck frontend (bfcas) to CIR#2

Draft
Copilot wants to merge 4 commits into
mainfrom
copilot/rewrite-in-brainfuck
Draft

Add Brainfuck frontend (bfcas) to CIR#2
Copilot wants to merge 4 commits into
mainfrom
copilot/rewrite-in-brainfuck

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 20, 2026

Adds a Brainfuck-to-CIR compiler and bfcas runner tool, enabling Brainfuck programs to execute on the CIR VM.

VM bug fixes (prerequisite)

Two bugs in core/cir.h prevented arithmetic from working at all:

  • Register flag lost on write: WordFlag::Register was cleared whenever a value was assigned to a register (via operator=), causing subsequent expect_flag(Register) guards on Add, Sub, Load, etc. to always throw. Fixed by having move() re-set the flag after assignment, and routing all register-writing ops through move().
  • Inc/Dec no-op: operator++/operator-- return a new Word by value; the results were silently discarded. Fixed by assigning the return value back via move().

New stdlib functions (core/std.h)

  • std.putchar — writes r0 as a character
  • std.getchar — reads one byte into r0

Brainfuck compiler (core/bf.h)

BrainfuckCompiler directly emits Program objects (no assembly text round-trip). Jump targets for [/] are resolved via a two-pass stack approach: [ emits a je with a placeholder, patched when the matching ] is encountered.

Register layout:

Register Role
r0 I/O (putchar/getchar)
r1 Data pointer (current tape cell address)
r2 Temporary cell value
r3 Tape base (freed on exit)

CLI tool (tools/bf/main.cpp)

New bfcas binary — compile and run .bf files directly:

bfcas examples/bf/hello.bf
# Hello World!

Example

examples/bf/hello.bf — Hello World in Brainfuck.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 3 commits March 20, 2026 12:27
Co-authored-by: zhrexx <134622710+zhrexx@users.noreply.github.com>
Co-authored-by: zhrexx <134622710+zhrexx@users.noreply.github.com>
Co-authored-by: zhrexx <134622710+zhrexx@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor code to brainfuck implementation Add Brainfuck frontend (bfcas) to CIR Mar 20, 2026
Copilot AI requested a review from zhrexx March 20, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants