Befunge ist eine, ähnlich Forth (Programmiersprache), stack-Orientierte Programmiersprache, deren Programme in Form eines 2-dimensionalen Schema basieren.
Geschichte
Die Programmiersprache Befunge wurde 1993 von Chris Pressey erfunden.
Die Instruktionen in Befunge
0-9 | Push this number on the stack |
+ | Addition: Pop a and b, then push a+b |
- | Subtraction: Pop a and b, then push b-a |
* | Multiplication: Pop a and b, then push a*b |
/ | Integer division: Pop a and b, then push b/a, rounded down. If a is zero, ask the user what result they want. |
% | Modulo: Pop a and b, then push the remainder of the integer division of b/a. If a is zero, ask the user what result they want. |
! | Logical NOT: Pop a value; if the value is zero, push 1, otherwise zero. |
` | Greater than: Pop a and b, then push 1 if b>a, otherwise zero. |
> | Move right |
< | Move left |
^ | Move up |
v | Move down |
? | Move in a random direction |
_ | Pop a value; move right if value=0, left otherwise |
| | Pop a value; move down if value=0, up otherwise |
" | Start string mode: push each character's ASCII value all the way up to the next " |
: | Duplicate value on top of the stack |
\ | Swap two values on top of the stack |
$ | Pop value from the stack |
. | Pop value and output as an integer |
, | Pop value and output as ASCII character |
# | Trampoline: Skip next cell |
g | Pop y and x, then pushes ASCII value of the character at that position in the program |
p | Pop y, x and v, then change the character at the position x/y in the program to the character with ASCII value v |
& | Ask user for a number and push it |
~ | Ask user for a character and push ASCII value |
@ | End program |
Beispiele
Addition zweier Zahlen
4 3 + . @
Der Quellcode ähnelt einem Forth-Quellcode: 4 ud 3 werden nacheinander auf dem Stack abgelegt, dann werden beide Zahlen von Stack geholt, addiert und dann das Ergebnis wieder auf dem Stack abgelegt. Der Punkt . ist die Anweisung, die oberste Zahl des Stacks auszugeben. Mit dem Klammeraffen @ wird das Programm beendet.
v > . v 4 + @ > 3 ^
Das gleiche wie oben, nur mit Richtungsänderungen.
v*>.v 4*+*@ >3^**
Ganz kompakt, mit Füllzeichen.
Hello World
"!dlrow olleH" > v , ^ _ @
Das erste " markiert, das es sich um ASCII-Code handelt. Dann wird in Umgekehrter Reihenfolge Hello World! zeichenweise in den Stack gelesen. Das letzte " schließt den ASCII-Strom ab. Dann kommt eine Schleife, bei denen >, v und ^ die Richtungspfeile für den Programmfluß darstellen, und das , (Komma) die Print-Anweisung für ein ASCII-Zeichen darstellt. Das _ (Unterstrich) stellt die While-Bedingung dar, die solange erfüllt ist, solange der letze geholte Wert größer 0 ist.