Zum Inhalt springen

Befunge

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 10. Oktober 2004 um 16:30 Uhr durch Michaelsy (Diskussion | Beiträge) (Kategorie:Programmiersprache). Sie kann sich erheblich von der aktuellen Version unterscheiden.

Befunge ist eine, ähnlich Forth, 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 und 3 werden nacheinander auf dem Stack abgelegt, dann werden beide Zahlen vom 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 letzte geholte Wert größer 0 ist.

Weitere Beispiele in Befunge

Conways Game of Life

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"!"<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Ein kastriertes Schachprogramm

**********>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  ^ <   <   <        <<  <    $<                   < <