Befunge

Befunge is an esoteric programming language by Chris Pressey, which is similar to Forth stack - oriented. The programs are based on a 2-dimensional pattern. The source code consists of ASCII characters in a 80 × 25 characters large array. Chris Pressey invented Befunge 1993 with the aim to define a possible hard compilable language. One difficulty for compiler example, represents the p command, which can modify the source code dynamically at runtime.

It is interesting Befunge for research and teaching, practical applications are likely to have rather not. For the understanding of self-modifying code Befunge is well suited for experimentation, unconventional methods of the multiple use of program code can be well on Befunge.

  • 3.1 Conway's Game of Life
  • 3.2 A castrated chess program
  • 3.3 BefBef

The instructions in Befunge (93 )

Examples

Addition of two numbers

4 3 . @ The source code is similar to a Forth source code: 4 and 3 are successively placed on the stack, then the two numbers are popped from the stack, added, and then the result is placed back on the stack. The point. is the instruction to output the highest number of stacks. The program with the @ @ ended. Point numbers are not supported. Once, for example, would be " 0.12 " come out, the program prints " 0".

V>. v 4 @ > 3 ^ Same as above, only with changes in direction.

V * >. v 4 * * @ > 3 ^ ** Very compact, with padding.

Hello World

"! dlroW olleH " > v                  ,                  :                 ^ _ @ The first "indicates that it is ASCII code. Then in Reverse order is Hello World! Character by character read into the stack. The last " concludes the ASCII stream. Then comes a loop where >, v and ^ represent the directional arrows for the program flow, and the, (comma ) is the print statement for an ASCII character. The _ (underscore ) represents the While condition is fulfilled as long as long as the last fetched value is greater than 0.

Hello World without loop

Hello World Extended

91 70 pv   P173 v < v <   > " dlroW olleH! " >: |        v: - 1g17 <   ^ P17_ $ 70g, v   ^ _ # & @ # P173 < Here the character for a newline (ASCII code 10 ) is written on the stack, as well as a counter with the value 3 Now "Hello World! " issued and the counter is decreased by 1. This output is repeated until the arrived at Counter 0. If this is the case, then a newline is output, asked the counter reset to 3 and the user for a number. If this number is 0 by the user, the program is terminated, otherwise the program will begin again with the outputting " Hello World!" of.

Fibonacci sequence

These programs output the Fibonacci sequence. Depending on the interpreter just the numbers to 233 are output.

0.1>. : 00p 00 g \ v      ^ < First, the 0 is placed on the stack and output immediately. Then, a 1 is placed on the stack. Then the program enters the loop: double stack, issue value double again, supreme character at position 0/ 0 save in your code, add supreme values ​​, get value from position 0 / 0 and swap the top two values ​​.

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

0.1> # < # <# <:. # < # 0 < # 0 < p # < # < # 0 < # 0 < g # <\ # < Other examples in Befunge

Conway's Game of Life

Implementation of the 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 > 41 G1 - 51gg v 1p - 1vg 1 g15 - 1g14 < g61g > 41 g51g1 -g v 14 * 1v4 g 1g15g14 < ** 5 > ^ 4 > g1 - 1g1 51 g v ** ^ _ ^ V4 GG15 1 g14 < *** > v! > 1g1 51 g1 g v g8 - v14 / * 25 - * 4 * 88 < 19 > g51gg "" - v 4 * 5 v < v -2: _3v   > ^ | $ V < - 3_! ^ <<< | < *** > g51gp ^> 51gp ^> v 14 ^ " " <^ g14 " " < A castrated chess program

Example of input: A2A3

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

028p038p108p018pv       vp91 56 p900 < v_v # - 1 "! ": <>: "<" - # v_: " ^" - # v_ v       > "*" 09g: 19g \ 19gg29p p 29g28g # ^ _ " " - # v_: "v" - # _ ^ v       ^ p91 g91g81p90 g90g 8 0pg91g90g92 $ <<                            >: > 38g7p38g1 38 p ^ p811p800 <                          > 28g! 28p ^ p810p80 - 10 <<                                                     ^ p81 - 10p800 <                                                     ^ p810p801 < _v # - " > ": <                                       ^ - "0": _v # ` \ 1 " 9 ": _v #` -1 "0": <#                                                " ": > # > 1 - # V_V! # # # # # # # # # # # # # # # # # # # # # # # > 19g \ 48gp ^ p # 82! G82 < "! DlroW olleH " 0 > 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 48 p1 ^ >>                                          > $ 0 > # ^ <                                   v_ ^                             > * ^ ^ 3_v ​​# - "_": <                                                                >: "," - # V_4 ^                                                            ^ 5_v # - "*": <                                   > # @ ^ literature

  • Oliver Lau, rocket science - A Plea for esoteric programming languages, c't 22/ 07, pp. 192-199.
112269
de