BrainF#ckery Devlog #2!
What’s Changed?
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!
How does the Compiler actually work?
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.
What’s next?
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…
See Ya Later!
- Elio
) to run first and now it works fine! I also just added some more error logging with the textual notification system in case something goes wrong and bumped up the PyPi python package version to
