Befunge

from Wikipedia, the free encyclopedia

Befunge is an esoteric programming language by Chris Pressey that is similar to Forth Stack -oriented. The programs are based on a 2-dimensional scheme. The source text consists of ASCII characters in an 80 × 25 character arrangement. Chris Pressey invented Befunge in 1993 with the aim of defining a language that was as difficult to compile as possible. The p command, which can dynamically change the source code at runtime , is a problem for compilers .

Befunge is interesting for research and teaching, practical applications are unlikely to exist. In order to understand self-modifying code , Befunge is well suited for experimentation; even unconventional methods for reusing program code can be represented well on Befunge.

The instructions in Befunge (93)

0-9 Put this number on the stack
+ Addition: Get a and b , then push a + b onto the stack
- Subtraction: Get a and b , then push b - a onto the stack
* Multiplication: Get a and b , then push a · b onto the stack
/ Integer division: Get a and b , then push b / a (rounded down) onto the stack. If a = 0, ask the user for the result.
% Modulo: Get a and b , then push the remainder of the integer division of b / a onto the stack. If a = 0, ask the user for the result.
! Logical NOT: Get the value; if zero, push 1, otherwise zero on the stack.
` Greater than: Get a and b , then push 1 if b > a , otherwise zero.
> Go right
< Go left
^ Go upstairs
v Go downstairs
? Go in a random direction
_ Get a value from the stack; go to the right if value = 0, otherwise to the left.
| Get a value from the stack; go down if value = 0, otherwise up.
" Start string mode: Shift the ASCII value of each character up to the next ".
: Duplicate value on top of the stack.
\ Swap two values ​​on top of the stack.
$ Get a value from the stack.
. Get a value from the stack and output it as an integer.
, Get a value from the stack and output it as an ASCII character.
# Trampoline: go to the next cell.
g Get y and x from the stack, then push the ASCII value of the character at position x / y in the program onto the stack.
p Get y , x and v from the stack, then change the character at position x / y in the program to the character with the ASCII value v .
& Ask the user for a number and push it onto the stack.
~ Ask the user for a character and push its ASCII value onto the stack.
@ Quit program

Examples

Adding two numbers

4 3 + . @

The source code is similar to a Forth source code: 4 and 3 are placed on the stack one after the other, then both numbers are fetched from the stack, added and then the result is placed back on the stack. The point .is the instruction to output the top number of the stack. The @program is terminated with the at sign . Point numbers are not supported. As soon as z. For example, the result would be "0.12", the program outputs "0".

v   > . v

4   +   @

> 3 ^

Same as above, only with changes of direction.

v*>.v
4*+*@
>3^**

Very compact, with filler characters.

Hello World

0"!dlroW olleH" > v
                 ,
                 :
               ^ _ @

The first "signals that it is ASCII code. Then, in reverse order, Hello World! Read into the stack one character at a time. The last "completes the ASCII stream. Then comes a loop in which >, vand represent ^the directional arrows for the program flow, and the ,(comma) represents the print statement for an ASCII character. The _(underscore) represents the while condition, which is fulfilled as long as the last fetched value is greater than 0.

Hello World without a loop

"!dlroW olleH"v
 @,,,,,,,,,,,,<

The same thing is done here as above, only instead of a loop the print function is simply executed several times in a row, so that the stack is empty at the end.

Hello World Extended

 91+70pv
 v p173<        v  <
 >"!dlroW olleH">,:|
      v:-1g17      <
 ^ p17_$70g,v
 ^_ #&@#p173<

Here the character for a line break ( ASCII code 10) is written on the stack, as is a counter with the value 3. Now “Hello World!” Is output and the counter is reduced by 1. This output repeats itself until it reaches counter 0. If this is the case, a line break is output, the counter is reset to 3 and the user is asked for a number. If this number from the user is 0, the program is ended, otherwise the program starts again with the output of "Hello World!".

Fibonacci sequence

These programs output the Fibonacci sequence . Depending on the interpreter, only the numbers up to 233 are output.

 0.1>:.:00p+00g\v
    ^           <

First the 0 is put on the stack and immediately output. Then a 1 is put on the stack. Then the program enters the endless loop: double the stack, output the value, double it again, store the top character at position 0/0 in the code, add top values, get the value from position 0/0 and swap the two top values.

The following program does the same, only it was written for exactly one line and is therefore a bit confusing:

0.1> #<:#<.#<:#<0#<0#<p#<+#<0#<0#<g#<\# <

Here again in shortened form:

0.1>:#\.#g:#00#00#+p# <

There are commands for the execution and for the reverse direction. The trampoline skips commands from the "wrong" direction.

More examples in Befunge

Conways Game of Life

Implementation of Game of Life by Dmitry M. Litvinov

v>>31g> ::51gg:2v++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9p BXY|-+<v3*89<%+ *                                                      *   +
21 >98 *7^>+\-0|<+ *                                                     *    +
*5 ^:+ 1pg15\,:< + *                                                     ***  +
10^  <>$25*,51g1v+                                                            +
-^ p<| -*46p15:+<+                                                            +
> 31^> 151p>92*4v+                                                            +
 ^_ ".",   ^ vp1<+                                                            +
>v >41p      >0 v+                                                            +
:5! vg-1g15-1g14<+                                                            +
+1-+>+41g1-51gg+v+                                                            +
1p-1vg+1g15-1g14<+                                                            +
g61g>+41g51g1-g+v+                                                            +
14*1v4+g+1g15g14<+                           * *                              +
5>^4>1g1+51g1-g+v+                           * *                              +
^ _^v4+gg15+1g14<+                           ***                              +
>v! >1g1+51g1+g+v+                                                            +
g8-v14/*25-*4*88<+                                                            +
19+>g51gg" "- v  +                                                            +
4*5  v<   v-2:_3v+                                                            +
 >^   |!-3_$  v<-+                                                            +
^    < <      <|<+                                                         ***+
>g51gp ^ >51gp^>v+                                                            +
^14"+"<  ^g14"!"<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

A castrated chess program

Example for input: a2a3

**********>9"+  ",>,,1-:v  >:,,"@"  v>1+:4`!v>1~5v>01g1+802g-g:"a"`  v>+03g1+8v
*RNBQKBNR*,       ^"-+" _v "v:+1," "< v4-1~:_^v+5< >_"(: niw U">:#,_@#^!+`g40<0
*PPPPPPPP*+vp00:+1g  <p  <  >:,"G"`!|p>4*%:7`v->$$^|!-"K":g-g408+1g30<>:"p"-7^4
*........*5>8`!55+,#v_9"+  ", v v5 $<^\0\_v#!<_^   >:"."-!#v_"a"`#v_#|^v"."p-g<
*........*5v9.-\9g00<#v!:-1,, < >5+, ^    v                ># $0 #<^#< >01g1+8v
*........* >"|",1-::!|>#v_"+-"^             v"q"+7gp90+"5"*"e"g<<v01p9p8pp-g20<
*........* ^,gg00-\9 <  >55^v      ,7 $$$ < > %00p09g"@"4*+:8%v9|<`8\9p00:+1g$<
*pppppppp*^                 < >  >                             ^>1-:::00g9*+\!|
*rnbqkbnr*>:"`"+,04g:"9"\-,p^    |-"P"_v#-"N":_v#-"K":_v#-"B"<1 ^p90+g90*gg00\<
**********^g30pg20g10"."+!+`g<   >2g1+v>$2v%2g$<       v< >gv:>+02p8/8%1+:01p0v
>01g:"`"+,02g:"9"\-,g:"P"-804^ v%2gp40<   >100g2/2%#v_v$| %2<|-"Q":_v#-"R":gg2<
^_"!niw I">:#,_@             v _g2/2%2*vv0_v#%2/4g00<\< >#^ #<     v>#$ <
 ^-"k"_$                 #v_  ^v:+g10-1<>\->\00g8/2%  v>g2/2%2*1-0v> g2/2%2*1-v
^_   0^`"a":_v#-".":gg40g3<  1 >:03p0`\04gg"a"  vv0_v#<v-1*2%2/4g0<v_v#%2/4g00<
 ^ !p80:+1g8$<>04g0`*904g`*^ >g:03p04gg"."-! v  `>\->0vv           <\<>4pg8/8%v
              ^*`g309`0g3  <  v*%2/2g00!-3g40_v<*v0+g1<>05p06p1g03p2g0^  v_v#:<
Chess program on Befunge'93  > 3g04g1+:04pgv  3 !>3p02gv v!p30:+g50g30-1 < >v
"Hungry dragon" v1.1         |<  vp3_v#-"."<  p|< vp40+<v_03g04g06g+:04pg"."-!|
by mtve at frox25.dhs.org  ^ <   <   <        <<  <    $<                   < <

BefBef

BefBef is a Befunge interpreter written in Befunge by Wim Rijnders.

 
028p038p108p018pv
     vp91+56p900<       v_v#!-+1"!":<                 >:"<"-!#v_:"^"-!#v_  v
     >"*"09g:19g\19gg29p p 29g28g  #^_ :" "-!#v_:"v"-#^_    v
     ^p91+g91g81p90+g90g 8 0pg91g90g92$       <    <
                          >:         >38g7p38g1+38p^p811p800<
                        >28g!28p                   ^p810p80-10<         <
                                                   ^p81-10p800         <
                                                   ^p810p801<     _v#!-">":<
                                     ^  -"0":_v#`\+1"9":_v#` -1"0":<    #
                                              >    #     >:"!"1+-!#v_v
#######################>19g+\48gp                  ^      p #82!g82<
0"!dlroW olleH">v     #              ^               g7-1g83_v#!-":":<
               ,:     #     >$,                    ^ <      #>:"p"-!#v_v
               ^_25*,@#   v_^#-4:_v#-3:_v#-1:_v#-2:\g7p83:-1_v#:g83<2<
#######################   >:5-#v_v$                ^ #    0 #<
                       ^  _v#-6< > $6      v  >$09g+48p1  >>       ^
                                        >$0>         #             ^     <
                                 v_                         ^

                           >*                        ^     ^3_v#!-"_": <
                                                              >:","-#v_4 ^
                                                          ^5_v#!-"*":<

                                 >                          #@          ^

literature

  • Oliver Lau: Hexenwerk - A plea for esoteric programming languages. c't 22/07, pp. 192-199.

Web links