Welcome to another issue of Hello World in Every Language! Today, we’re fortunate to have another community contribution to the series. Time to learn how to say Hello World in Befunge with Stuin!
Table of Contents
Befunge is a programming language with some very unusual standards, as a two-dimensional, self-modifying, stack-based programming language. Wikipedia has it that it was designed in 1993 by Chris Pressey, merely to be as difficult to compile as possible.
All operations in Befunge are limited to a single character, and the source text can be traversed in any direction through the file. The “Instruction Pointer” starts in the upper left corner and proceeds to the right, running every character it crosses. When it reaches an arrow,
< > ^ v, that pointer will turn and travel in the new indicated direction.
Instead of variables, all operations act on one global stack of integers, similar to Forth. 0-9 will just push their value,
+ - * / % pop the top two values and push their normal result,
. prints out the top number, and
, prints the corresponding character instead.
In total there are 26 operators (not including numbers). An example might be better.
Hello World in Befunge
There are many valid ways to set up a Befunge program. People tend to design for either artistic or compactness purposes. A typical example for a Hello World looks a little something like this:
"!dlroW ,olleH"> , v | : < @
As stated earlier, everything works off of a stack. A stack in programming is a list of values, set up sort of like a stack of papers. Only the top number can be retrieved, and new numbers are always placed on the very top. Because of this, strings have to be read in backwards, as the last letter in will be the first letter out.
The program starts out in string mode to read in the characters. At the end of the first line, our stack will look something like this: (with translated characters below)
33 100 108 114 111 87 32 44 111 108 108 101 72 ! d l r o W , o l l e H
The following section is a literal loop, to print out each of the values off the stack:
> , v | : < @
In order, the arrow sends it right,
, prints out the H, the arrows loop it back around,
: copies the top value of the stack, and
| takes that copy for an if statement.
|, if the top value is 0 (or null), then the pointer starts to go downwards, otherwise it is sent up.
_ works the same way, except it means right on 0 and left for anything else.
The pointer will continue to run through the loop until the stack is empty. When it finishes, the if bar drops it down to the bottom. The
@ indicates end of program.
How to Run the Solution
Because of the particular design of the language, your best option is a Befunge interpreter. This is both for availability and because watching it run is the only way to really understand (or debug) it.
There are compilers as well, even through the technical challenges:
For a much more thorough look at Befunge, it’s list of operators, and its various derivatives, I recommend the Esolang Wiki.
Sample Programs in Every Language
As always, thanks for sticking around. Also, shout out to Stuin for their first article. If you liked it, let them know in the comments.
If you’d like to be the next community contributor to this series, let me know. We’re always looking for authors!
The ACT/SAT discourse is back, and I found a pretty cool article debunking many of the common arguments for them.
The Sample Programs repository is in its fourth Hacktoberfest. Are you interested in making a contribution?