Befunge-93 Interpreter

Published 2023-06-12


This is a Befunge-93 interpreter that I made in a few days. Tested on all examples from Rosetta Code that fit. You can finally code in an esoteric language inside a fantasy console!

Befunge is a stack-based, esoteric programming language, in which the code is represented on a two-dimensional grid. The control flow of the program is directed using instructions such as v, >, ^ and < (respectively: down, right, up, left). If a border is reached, the program wraps around (both directions). Single character instructions can pop values of the stack, or place them on top of the stack, and act based on the retrieved values. Read more on Wikipedia.

Here's the list of all instructions explained, also on Wikipedia. There are not many of them, and most are self-explanatory (arrow instructions, arithmetic operators, numbers). Just like with any other language, the more fun you have with it, the more you'll learn!

In my opinion, Befunge really tests (and improves) your logical thinking and flow control skills. This is also a language like no other, in which you can actually visualize the control flow on a grid. Lastly, this is a stack based language, so it teaches you how to operate under a set of limitations (of course, you can just store and read values using g and p instructions, but that's cheating (not really)).

Also, you don't have to care if you don't want to! Democracy!

Sure:

First, " enables string mode; all characters until next " will be pushed into the stack as ASCII values. Then, the string hello world! is pushed onto the stack (values go bottom to top on the stack). Then , prints a single character, so all characters are printed. Finally, @ ends the program.

Push 1, then prompt user for n (&, stack: 1 [n]). Swap two top values (\, stack: [n] 1). Prompt user for a number a (stack: [n] 1 [a]), then multiply (*, stack: [n] 1*[a]). Swap values again (stack: 1*[a] [n]), subtract 1 (stack: 1*[a] [n]-1), and if the new value is 0, go right (_), swap values (stack: 0 1*[a]), print number (.) and end the program (@). Otherwise, repeat.

Sadly, I made this one myself. I'll let you figure this one out. Most important aspects of this code:

If you want to share your code, you can either share a screenshot, or you can copy the save file, which should be in your usual PICO-8 cart location under the name _picofunge_save.p8. Interwebz!

All the usual ASCII characters from your keyboard work, so just poke the interpreter in any way you want!

Use arrow keys to navigate the grid. Use Backspace to erase an instruction and go back one cell, or Space to go forward.

Use Tab to quickly run code. You can also use the Tab button as an alternative for Enter in situations where you would normally use the latter, since Enter opens the pause menu.

Use Enter to enter the pause menu, in which you'll find the following options:

To reload code, restart the cart. There is no LOAD button, since there are only 5 menu items available to me :(

If you encounter any bugs or weird behaviour, please let me know either in this thread or on Twitter @szczm_. Bonus points for including a screenshot!

If you have any ideas on how you could use this project, the source code is extensively documented — each function and class is explained and I tried to comment every important bit that needed explanation or description. This cart is released under a CC4-BY-NC-SA licence on this website thanks to an upload option, and this is also released on GitHub under the MIT licence. Basically, do anything you want with it, if you want to, and I'll be happy if you let me know!

Please don't ask.