Working with Befunge
I love exposing myself to new ideas and ways of interacting with the world. This interest spans a variety of topics and can take a variety of forms. Sometimes I'm learning about technologies I think are intriguing and might shake up the way we go about our lives as developers and human beings. Sometimes it involves reading papers and articles and listening to talks.
This post is about a less practical or enlightening topic, but that presented an opportunity to learn more about programming languages. I'm talking about Befunge, an esoteric programming language that is older than I am and has virtually no practical applications.
What is Befunge?
Befunge is a programming language with 3 important elements: the program, cursor, and stack. A program in Befunge is a file, containing only ASCII characters, that is treated as a 2-dimensional array of 1-byte values whose edges "wrap around". The cursor contains the current position in the program and current direction, it represents the next cell to be executed by the interpreter. The stack is a LIFO stack that can be pushed to and popped from by instructions. A Befunge interpreter starts at the top left of the file with the current direction to the right and then begins executing instructions. The value in the cell determines which instruction is going to be executed. Below is a list of some categories of instruction.
Unconditional Direction Changing ("<", ">", "v", "^") - these make the cursor direction match the direction of the arrow
Conditional Direction Changing ("_", "|") - these pop a value from the stack and set the direction depending on whether that value is zero
Number Pushing ("0", "1", .., "9") - these push their numeric value onto the stack
Arithmetic ("*", "+", ...) - these pop values from the stack, operate on them, and push the result
Input ("&", "~") - these take input from the user and put the result on the stack
Output (".", ",") - these pop a value from the stack and output it to the user
This list is not complete, it just aims to give you a sense of how the language operates. For more information see the esolangs.org page for Befunge.
Befunge and me
I learned Befunge a few years ago for fun and wrote a variety of small programs culminating in an elegantly formatted program that prints the lyrics of the song baby shark. It was an experience I would recommend to people interested in making their brain hurt and challenging the way they think about programs and programming.
After learning Befunge and writing programs in it, I decided to dig deeper into the language and write an interactive interpreter and debugger with support for breakpoints. I decided to use Rust, a language I am increasingly fond of, aiming to produce one of the fastest available tools in this space and to lay the groundwork for targeting WebAssembly in the future. The code can be found on my GitHub here and requires a valid Rust and Cargo installation to run.
I will be revisiting Befunge soon with my Enterprise Befunge (EFunge) semi-satirical project. It will feature a dialect of Befunge that is slightly easier to use and tunes the language to be "useful" for writing simple command-line programs.