I recently found a new language that has an implementation that will run on the QL – Befunge. Befunge is the first two-dimensional language. This means that the Program Counter, instead of just going down a line of code, can move up, down, left, and right. This is easily done by making each Befunge command a single character.
Like FORTH and Postscript, Befunge is a stack oriented language. Each command either is data to be put on the stack, or is an operator to do something with what ever is on the top of the stack. Numbers are limited to input via single digits, but may be expanded by manipulating the stack. To get 23, you would push 7 on the stack, push 3 on the stack, do a mulitply command (leaving 21 on the stack), push 2 on the stack, and do an add command (leaving 23 on the stack). In Befunge it would look something like this:
73*2+
To then print out the value on the stack, just use the integer print command, the period (.).
Befunge code space is an 80×24 array of characters. The Program Counter starts off in the upper left hand corner and moves from left to right. It continues this way until it hits the end of a line and wraps around to the start of the same line or until it hits a command that changes its direction.
I would love to talk about how fun and easy the language is, but I still have not quite figured it out. The examples used in the Befunge language documentation look easy, but the example code that comes with it is still causing my brain to hurt.
Now as far as running Befunge on the QL – let me start with a little history on how I ran across Befunge. The Fall 1999 edition of “The Perl Journal” had the results of the latest Obfuscated Perl Contest. One of the entries was a Befunge interpreter. Since the code was rather cryptic (as is all Obfuscated programs) and I did not know if there were any Perl5-isms in the code. I tried to get a copy from “The Perl Journal” web page. Having forgot my subscribers account name and password, that did not go well. Time to try Yahoo! and do a search on Befunge. Look, a nice Befunge web page, with documentation and a list of implementations. One of the implementations is in C. Not being a C person, I was not up to the tasking of porting it to the QL. Then I saw the answer, an implementation in Z-code.
For those that don’t know, Z-code is a data file used for the old Infocom adventure games. Infocom adventure games are written in the language Inform, then compiled, and then run with ZIP (Z-code Interpreter). A version of ZIP has been ported to the QL and I’ve used it a number of times over the years.
I downloaded the Z-code implementation of Befunge, unzipped it and fired off ZIP with zbefunge.Z5 as the data file. After some initial complaining about not been run on a “real” Z-code interpreter, I was able to the the main screen up. The Z-Code version has a built in editor, from which you can then run a program.
I typed in the “Hello World” example program, hit the function key to run the code and “Hello World” was printed out on the screen. It looks like it works.
Once nice feature of the Z-code implementation of Befunge, is a debug option. Using this option, you can step through the Befunge program watching the program counter move about. As the program runs, you can see, at the bottom of the screen, the top 6-or-so values on the stack. You can also toggle over to a screen where the output is shown.
Using the debug feature I am starting to get a hang on how the Program Counter moves about.
This implementation also allows for loading and saving Befunge programs. I tried loading some of the programs that come with Befunge, but the interpreter had an error. I then typed in the “Hello World” program and saved it. When I loaded it back in, I got an error, but the program then showed up fine. It looked at the saved file and it has a bunch of extra non-ASCII stuff. It looks like the saving and loading feature has some problems, but it does seem to work.
The main web page for Befunge is:
http://www.cats-eye.com/befunge/
The web page to get the Z-code implementation of Befunge is:
http://www.meta.demon.co.uk/zbefunge.html
Here is a few example Befunge programs:
Listing 1: (Hello World)
v
>v"Hello world!"0<
,: ^_25*,@
Listing 2: (Factorial)
v
>v"Please enter a number (1-16) : "0<
,: >$*99g1-:99p#v_.25*,@
^_&:1-99p>:1-:!|10 <
^ <
Listing 3: (To Upper)
v, < < <
>~:"a"1-`!#^_:"z"1+`#^_"aA"--^