BrainF#ckery Compiler
- 2 Devlogs
- 3 Total hours
A brainf#ck to assembly compiler (really just a transpiler) written in python, that can utilize tools like NASM and GCC to take brainf#ck and turn it into a fully fledged windows .exe
A brainf#ck to assembly compiler (really just a transpiler) written in python, that can utilize tools like NASM and GCC to take brainf#ck and turn it into a fully fledged windows .exe
A LOT!
Since the last devlog, I’ve practically finished the BF -> Assembly compiler, and even used subprocess.run() to call NASM and GCC to build the script! It currently looks for a file called in.bf and then compiles it into an EXE that gets dumped straight into a build/ folder along with the out.asm and out.obj files!
Right now, all the compiler really does is go character by character in the input file, and if the character is one of the special 8, (<>+-,.[]) I just append some assembly into the out.asm file that corresponds with the instruction. For example, for + I just add inc byte [ebx] which increments the byte stored at the location the EBX register is pointing at. (EBX stores the tape pointer, and you manipulate it with < and >). The cool thing about writing the compiler like this is that it literally just ignores everything else, so I don’t have to write code to “clean” the input file of newlines, tabs, spaces, comments, ect.
Now for [ and ], they’re a bit special. The way these guys work is I have a stack and counter. Whenever I add a [ to the assembly, I first increment the counter by 1 and then push the value to the stack. Then I use fstrings to modify the assembly I’m about to add by doing this:
cmp byte [ebx], 0
jz loopEnd{bracketCounter}
Now to break this down, cmp is comparing the current cell (byte [ebx]) with 0. Then, the next instruction, jz only jumps to the given location in code if the two values compared previously are equal to eachother. The fstrings come into play because they modify this jump location to be loopEnd(whatever the counter is), and this location becomes the corrisponding ] when that instruction is read.
Well, firstly I need to make a bunch of QoL changes to the compiler, like allowing the user to pass custom input and output names/directories, specify whether or not to fully compile or leave it as a chunk of assembly, ect
I also want to make a slightly higher level BF-esque language, where you can do stuff like + 2, 5 to increase cell 2 by 5, if statements, batch outputting, ect, but that’s not gonna be the easiest thing…
- Elio
BrainF#ckery, or BFery for short, is a compiler for Brainf#ck! It’s written fully in python and takes BF and converts it into working assembly that you can plug into NASM and gcc to turn BF into a working executable!
Shut Up.
BFery will essentially just read and translate any BF directly into assembly. It’s actually really simple, since most of BF converts into a single line of assembly, like >, which moves the tape pointer up one, translates to inc rbx. That’s it! the rest are as follows:
< -> dec rbx (dec means to decrease)+ -> inc byte [rbx] (byte tells NASM to only write one byte of data, and the two brackets here are the equivalent of dereferencing a pointer in C.)- -> dec byte [rbx]
[ ] , .) I have yet to figure out…OK Thats really all for this devlog, thanks for stoppin’ by!