00 What is Befunge 93?

Befunge – Wikipedia

Befunge-93/Befunge-93.markdown at master · catseye/Befunge-93 (github.com)

Befunge is an esoteric programming language by Chris Pressey.

Chris Pressey invented Befunge in 1993 with the goal of defining a language that is as difficult to compile as possible. A difficulty for compilers is for example the p-command, which can dynamically change the program at runtime.

The program consists of ASCII characters in an 80×25 character array. All functions are executed on a stack.

The basics of Befunge 93

Probably the most unique element of Befunge-93 programming is the program counter (PC). In almost all computer programming languages, the program counter moves continuously forward through the program, occasionally jumping to another location in the code (but continuing anyway).

However, different rules apply to the PC in Befunge-93. It can move forward, backward, left or right. A Befunge-93 program is treated like an 80×25 torus (a page that wraps around the edges) of ASCII text. Certain commands change the direction of the PC’s progress. By default, the PC points to the upper left corner of the program and is oriented to move from left to right.

Each command in Befunge-93 is a single character, as is the largest unit of data that can be specified in the program’s source code; Befunge-93 programs have a maximum total size of 80×25 commands and data bytes. There are no run-time variables, only a single run-time stack. Befunge-93 programs can be modified by themselves. Because of the 2-dimensional nature of the PC, they also allow some extremely whimsical code.

The stack

Similar to Forth and PostScript, Befunge-93 supports a LIFO, Reverse Polish Notation (RPN or Postfix) stack of signed long integer numbers (i.e., each cell of the stack can contain as much as a signed long integer in the C language on the same platform).

The operation of putting a value on the stack is called a “push”, and the operation of taking a value off the stack is called a “pop”.

The digits from 0 to 9 are valid Befunge-93 commands that push their respective values onto the stack. A double quote ” toggles string mode, and while string mode is active, the ASCII value of all character cells is pushed onto the stack until another ” is found.

There are a few basic calculation commands:

+Addition
Subtraction
/Integer Division
*Multiplication
%Modulo
!logical Not

These are explained in more detail in the Commands section.

To push a number greater than 9 onto the stack, calculations must be performed with numbers less than or equal to 9. In any other language this would be a problem. In Befunge-93, it is a joy. For example, to push ‘123’ onto the stack, you could push 9, then 9, then multiply (leaving 81), then push 7, then 6, then multiply (leaving 81 and 42), then add (leaving 123.): 9976+

This assumes, of course, that the pc starts at or before the first 9 and works to the right. If this section represents a complete Befunge-93 program, this assumption is correct: the pc starts at the top left of the torus and is initially oriented to the right.

If the stack is empty when you take something out of the stack, you should be aware that this will not result in an underflow! It will simply push a value of 0 onto the stack.

The program counter in detail

There are 5 commands that unconditionally control the direction of the PC:

>Move the PC to the right
<Move the PC to the left
vMove the PC down
^Move the PC up
?Move the PC randomly

If the PC hits the “edge” of the program, it simply continues on the other side.

Branches

The standard ‘if’ statement in Befunge-93 is either _ or |, depending on how you want to branch. Both statements take a value from the stack, check whether it is true (non-zero), and change the direction of the PC accordingly:

__ acts like < if the value is true or > if it is false;
|| acts like ^ if the value is true or v if it is false.

While” loops can be created by inserting an “if” into an infinite loop. For example _@

This program fragment clears all non-zero values from the stack and the first zero value, then exits [@ is the exit command].

Input

&The & (ampersand) command will get a numeric value (in decimal) from the standard input and push it on the stack.
~~ (tilde) will get the next ASCII character from standard input and push it on the stack.

For example,

&, outputs “A” when the user types “65”, and….

~. outputs “65 ” when the user enters “A”.

Output

.The . command will pop a value off the stack and output it as a decimal integer, followed by a space. 
,, will pop a value, interpret it as the ASCII value of a character, and output that character (not followed by a space.)

For example,

665+*1-, prints ASCII 65 (“A”.), and….

665+*1-. prints “65”.

Special commands

#is the ‘bridge’ command… it causes the next command which would normally be executed to be skipped over, and not executed.

For example,

123…@ would output “3 2 1”, but

123#…@ would output “3 2″, skipping one of the ‘. “s. Clever use of # can make for very interesting code!

:is the duplication command. It makes a copy of the top element of the stack. 
$pops a value off the stack, but does nothing with it. 

123.$.@ results in “3 1”.

\swaps the top two elements of the stack. 

123...@ results in “2 3 1”.

`(back-quote) is the ‘greater-than’ command. It compares the top two values on the stack, and returns ‘1’ if the first is greater than the second. 

65`. prints “1” and…

25`. prints “0”.

Self-modification

The last two commands to be explained are those that allow the contents of the torus in which the program is stored to be examined and modified. This “playing field” can be used as an auxiliary memory when the stack alone is not enough. But be careful, the torus also contains the running program.

gThe g command examines the contents of the playfield. It pops a y coordinate off the stack, then an x coordinate. It pushes the value found at (xy) onto the stack. If the thing at (xy) is a Befunge-93 instruction, the value pushed will be the ASCII value of that character. From the point of view of the program text, x determines the column and y determines the row; (0, 0) refers to the first (leftmost) column and the first (topmost) row of the program source.
pThe p command alters the contents of the playfield. It pops a y coordinate off the stack, then an x coordinate, and then a value. It places the value into the torus at (xy). If the program, at some later point, executes the instruction at (xy), it will be the interpreted as the Befunge instruction in the ASCII character set with the same value as was put there with the p instruction.

Command overview

CommandStack – ascendingStack (Resul)
+add<Value1> <Value2><Value1 + Value2>
sub<Value1> <Value2><Value1 – Value2>
*mult<Value1> <Value2><Value1 * Value2>
/div<Value1> <Value2><Value1 / Value2>
%mod<Value1> <Value2><Value1 mod Value2>
!not<Value><0 if Value<> 0, else 1>
greater than<Value1> <Value2><1 if Value1 > Value2, else 0>
>right
<left
^up
vdown
?random
_branch horizontally<Value><>
|branch vertically<Value><>
zeichenmodus
:dub<Value><Value> <Value>
\swap<Value1> <Value2><Value2> <Value1>
$pop<Value><>
.output number<Value>interprets <Value> as number
,output letter<Value>interprets <Value> as letter
#skip
gget<x> <y><Value of (x,y)>
pput<Value> <x> <y>stores Valueat (x,y)
&input number<Value>
~input letter<ASCII-Value>
@end