Knowledge (XXG)

Befunge

Source 📝

279:(however, it has been shown that Befunge-93 is Turing Complete with unbounded stack word size). The later Funge-98 specification provides Turing completeness by removing the size restrictions on the program; rather than wrapping around at a fixed limit, the movement of a Funge-98 instruction pointer follows a model dubbed "Lahey-space" after its originator, Chris Lahey. In this model, the grid behaves like a torus of finite size with respect to wrapping, while still allowing itself to be extended indefinitely. 314:: each instruction is compiled to a snippet of C code, and control flows through the snippets just as it does in a Befunge interpreter (that is, conditionally on the value of some 'direction' register). This does not result in a significant advantage over a good interpreter. Note that the bef2c compiler is not correct since it does not handle either 'p' or string mode, but it would not be impossible to make it do so (although the C language might not be well-suited for this). 1282: 25: 303:
As stated, the design goal for Befunge was to create a language which was difficult to compile. This was attempted with the implementation of self-modifying code (the 'p' instruction can write new instructions into the playfield) and a multi-dimensional playfield (the same instruction can be executed
219:
The name "Befunge" originated from a typing error in an online discussion. While it was designed to be difficult to compile, compilers such as bef2c and Betty have managed to implement the language using various techniques. Befunge programs are characterized by their use of arrows to change control
215:
Befunge was created by Chris Pressey in 1993 for the Amiga. The language was designed to be as hard to compile as possible, featuring self-modifying code and a multi-dimensional playfield. Despite this, several compilers have been written for the language. The original Befunge-93 specification
204:. It differs from conventional languages in that programs are arranged on a two-dimensional grid. "Arrow" instructions direct the control flow to the left, right, up or down, and loops are constructed by sending the control flow in a cycle. It has been described as "a cross between 400:
order and output as text characters to give "Hello". A space is character number 32 in ASCII, which here is constructed by multiplying 4 and 8, before being output as text. The remaining code then outputs "World!" in a similar way, followed by ASCII character 10 (a
329:
The technique of using arrows to change control flow is demonstrated in the random number generator program below. The Befunge instruction pointer begins in the upper left corner and will travel to the right if not redirected. Following the arrows around, the
488:!dlrow ,olleHH"). Then the "_" operation will pop the duplicated value, and go right if it's a zero, left otherwise. (This assumes a compliant interpreter that "returns" 0 when popping an empty stack.) When it goes left, it pops and prints the top value as an 244:. Nevertheless, a number of compilers have subsequently been written. A number of extensions to the original "Befunge-93" specification also exist, most notably Funge-98, which extends the concept to an arbitrary number of dimensions and can be 492:
character. It then duplicates the next character and loops back to the "_" test, continuing to print the rest of the stack until it is empty and so the next value popped is 0, at which point "@" ends the program.
976:
treat strings as comments in contexts where the values are not used. Similarly, in Befunge, there is no comment syntax: to embed documentation in the code, the programmer simply routes the control flow
317:
The etty compiler, for example, treats every possible straight line of instructions as a subprogram, and if a 'p' instruction alters that subprogram, that subprogram is recompiled. This variation on
1179: 321:
results in a much better advantage over an interpreter, since many instructions can be executed in native code without making intervening decisions on the 'direction' register.
263:
The Befunge-93 specification restricts each valid program to a grid of 80 instructions horizontally by 25 instructions vertically. Program execution which exceeds these limits
1314: 1100: 1319: 334:
instructions send the instruction pointer in random cardinal directions until the pointer hits a digit, pushing it to the stack. Then the arrows navigate to the
1309: 1172: 480:
ordering means that "H" is now the top of the stack and will be the first printed, "e" is second, and so on. To print the characters, the program enters a
216:
limited programs to an 80x25 grid, and while not Turing-complete, subsequent extensions like Funge-98 expanded the concept to achieve Turing completeness.
1304: 1285: 1165: 245: 307:
Nevertheless, these obstacles have been overcome, to some degree, and Befunge compilers have been written using appropriate techniques.
193: 1269: 1006: 108: 1050: 1259: 1199: 953: 46: 275:. Since a Befunge-93 program can only have a single stack and its storage array is bounded, the Befunge-93 language is not 1188: 973: 201: 197: 181: 1249: 205: 177: 1254: 1234: 1016: 969: 89: 61: 35: 318: 68: 42: 1142: 338:
to output the digit from the stack and return the pointer to the first directional randomiser. There is no
75: 209: 1152: 220:
flow, and they can produce outputs like random number sequences or classic "Hello, World!" messages.
57: 389: 249: 241: 1076: 1115: 127: 1094: 472:
The following code is a slightly more complicated version. It adds the ASCII character 10 (a
477: 397: 267:
to a corresponding point on the other side of the grid; a Befunge program is in this manner
264: 342:
to terminate this program, so it produces an endless stream of random numbers from 1 to 9.
276: 1054: 252:
operating simultaneously on the same space. Befunge-extensions and variants are called
952:
Most one-dimensional programming languages require some syntactic distinction between
1298: 995: 311: 82: 484:
that first duplicates the top value on the stack (so now the stack would look like "
1224: 481: 292: 1157: 957: 789:
Start string mode: push each character's ASCII value all the way up to the next
24: 683:
Logical NOT: Pop a value. If the value is zero, push 1; otherwise, push zero.
476:
character) to the stack, and then pushes "!dlrow ,olleH" to the stack. Again,
1209: 990: 961: 485: 473: 402: 310:
The bef2c compiler included with the standard Befunge-93 distribution uses
1264: 1244: 1219: 1214: 1011: 1001: 903:, then push ASCII value of the character at that position in the program 268: 156: 1147: 1239: 233: 1229: 981:
the "comment" area, so that the text in that area is never executed.
228:
The language was originally created by Chris Pressey in 1993 for the
943: 489: 393: 272: 229: 1161: 1036: 295:
in an online discussion, where the word 'before' was intended.
18: 862:
A "put" call (a way to store a value for later use). Pop
392:. First the letters "olleH" are pushed onto the stack as 232:, as an attempt to devise a language which is as hard to 998:– PlayStation programming game using a similar language 832:
Pop value and output as an integer followed by a space
895:
A "get" call (a way to retrieve data in storage). Pop
665:, then push the remainder of the integer division of 405:
character, moving the output cursor to a new line).
882:) in the program to the character with ASCII value 769:Pop a value; move right if value=0, left otherwise 171: 151: 136: 126: 49:. Unsourced material may be challenged and removed. 923:Ask user for a character and push its ASCII value 960:— although that distinction may be as trivial as 396:numbers. These are then popped from the stack in 779:Pop a value; move down if value=0, up otherwise 388:The following code is an example of the classic 1173: 8: 759:Start moving in a random cardinal direction 121: 1099:: CS1 maint: numeric names: authors list ( 16:2-dimensional esoteric programming language 1180: 1166: 1158: 964:'s rule that any character not in the set 120: 109:Learn how and when to remove this message 842:Pop value and output as ASCII character 822:Pop value from the stack and discard it 1315:Non-English-based programming languages 1028: 1092: 1320:Programming languages created in 1993 7: 1310:Stack-oriented programming languages 812:Swap two values on top of the stack 802:Duplicate value on top of the stack 47:adding citations to reliable sources 1148:Befunge-93 Reference Implementation 913:Ask user for a number and push it 14: 546:Push this number onto the stack. 1281: 1280: 1260:Shakespeare Programming Language 874:, then change the character at ( 23: 304:in four different directions). 34:needs additional citations for 1305:Esoteric programming languages 1189:Esoteric programming languages 1: 968:is a comment. Languages like 202:esoteric programming language 1250:One-instruction set computer 1053:. 1997-11-04. Archived from 534:Befunge-93 instruction list 236:as possible. Note that the 1336: 1278: 1195: 540: 507:"!dlrow ,olleH" 176: 1143:Befunge-93 Specification 495: 407: 344: 319:just-in-time compilation 852:Bridge: Skip next cell 1153:Funge-98 Specification 631:Integer division: Pop 390:"Hello World!" program 347:v>>>>>v 325:Sample Befunge-93 code 1114:Oerjan (2014-01-18). 1075:Ais523 (2008-12-18). 1051:"The Befunge FAQ v.4" 647:, rounded towards 0. 192:is a two-dimensional 606:Multiplication: Pop 250:instruction pointers 43:improve this article 1037:"Befunge – Esolang" 719:Start moving right 242:self-modifying code 240:command allows for 137:First appeared 123: 749:Start moving down 729:Start moving left 709:, otherwise zero. 693:Greater than: Pop 449:"World!" 291:is derived from a 1292: 1291: 950: 949: 701:, then push 1 if 581:Subtraction: Pop 422:"Hello" 187: 186: 119: 118: 111: 93: 1327: 1284: 1283: 1182: 1175: 1168: 1159: 1130: 1129: 1127: 1126: 1111: 1105: 1104: 1098: 1090: 1088: 1087: 1072: 1066: 1065: 1063: 1062: 1047: 1041: 1040: 1033: 967: 939: 930: 920: 910: 892: 859: 849: 839: 829: 819: 809: 799: 792: 786: 776: 766: 756: 746: 739:Start moving up 736: 726: 716: 690: 680: 654: 628: 603: 578: 553: 543: 538: 537: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 384: 381: 378: 375: 372: 371:>>>> 369: 366: 363: 360: 357: 354: 351: 348: 341: 337: 333: 271:equivalent to a 248:, with multiple 239: 167: 164: 162: 160: 158: 147: 145: 124: 114: 107: 103: 100: 94: 92: 51: 27: 19: 1335: 1334: 1330: 1329: 1328: 1326: 1325: 1324: 1295: 1294: 1293: 1288: 1274: 1191: 1186: 1139: 1134: 1133: 1124: 1122: 1113: 1112: 1108: 1091: 1085: 1083: 1077:"Chris Pressey" 1074: 1073: 1069: 1060: 1058: 1049: 1048: 1044: 1035: 1034: 1030: 1025: 987: 965: 946:. Does nothing 938: 928: 918: 908: 890: 857: 847: 837: 827: 817: 807: 797: 790: 784: 774: 764: 754: 744: 734: 724: 714: 688: 678: 652: 626: 601: 576: 551: 541: 536: 531: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 470: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 386: 385: 382: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 339: 335: 331: 327: 301: 285: 277:Turing-complete 237: 226: 155: 143: 141: 115: 104: 98: 95: 52: 50: 40: 28: 17: 12: 11: 5: 1333: 1331: 1323: 1322: 1317: 1312: 1307: 1297: 1296: 1290: 1289: 1279: 1276: 1275: 1273: 1272: 1267: 1262: 1257: 1252: 1247: 1242: 1237: 1232: 1227: 1222: 1217: 1212: 1207: 1202: 1196: 1193: 1192: 1187: 1185: 1184: 1177: 1170: 1162: 1156: 1155: 1150: 1145: 1138: 1137:External links 1135: 1132: 1131: 1116:"Talk:Befunge" 1106: 1067: 1042: 1027: 1026: 1024: 1021: 1020: 1019: 1014: 1009: 1004: 999: 993: 986: 983: 948: 947: 941: 935: 934: 931: 925: 924: 921: 915: 914: 911: 905: 904: 893: 887: 886: 860: 854: 853: 850: 844: 843: 840: 834: 833: 830: 824: 823: 820: 814: 813: 810: 804: 803: 800: 794: 793: 787: 781: 780: 777: 771: 770: 767: 761: 760: 757: 751: 750: 747: 741: 740: 737: 731: 730: 727: 721: 720: 717: 711: 710: 691: 685: 684: 681: 675: 674: 655: 649: 648: 629: 623: 622: 604: 598: 597: 579: 573: 572: 556:Addition: Pop 554: 548: 547: 544: 535: 532: 496: 408: 345: 326: 323: 300: 297: 284: 281: 265:"wraps around" 225: 222: 185: 184: 174: 173: 169: 168: 153: 149: 148: 138: 134: 133: 130: 117: 116: 31: 29: 22: 15: 13: 10: 9: 6: 4: 3: 2: 1332: 1321: 1318: 1316: 1313: 1311: 1308: 1306: 1303: 1302: 1300: 1287: 1277: 1271: 1268: 1266: 1263: 1261: 1258: 1256: 1253: 1251: 1248: 1246: 1243: 1241: 1238: 1236: 1233: 1231: 1228: 1226: 1223: 1221: 1218: 1216: 1213: 1211: 1208: 1206: 1203: 1201: 1198: 1197: 1194: 1190: 1183: 1178: 1176: 1171: 1169: 1164: 1163: 1160: 1154: 1151: 1149: 1146: 1144: 1141: 1140: 1136: 1121: 1117: 1110: 1107: 1102: 1096: 1082: 1078: 1071: 1068: 1057:on 2001-04-17 1056: 1052: 1046: 1043: 1038: 1032: 1029: 1022: 1018: 1015: 1013: 1010: 1008: 1005: 1003: 1000: 997: 996:Carnage Heart 994: 992: 989: 988: 984: 982: 980: 975: 971: 963: 959: 955: 945: 942: 937: 936: 932: 927: 926: 922: 917: 916: 912: 907: 906: 902: 898: 894: 889: 888: 885: 881: 877: 873: 869: 865: 861: 856: 855: 851: 846: 845: 841: 836: 835: 831: 826: 825: 821: 816: 815: 811: 806: 805: 801: 796: 795: 788: 783: 782: 778: 773: 772: 768: 763: 762: 758: 753: 752: 748: 743: 742: 738: 733: 732: 728: 723: 722: 718: 713: 712: 708: 704: 700: 696: 692: 687: 686: 682: 677: 676: 672: 668: 664: 660: 656: 651: 650: 646: 642: 638: 634: 630: 625: 624: 621: 617: 613: 609: 605: 600: 599: 596: 592: 588: 584: 580: 575: 574: 571: 567: 563: 559: 555: 550: 549: 545: 539: 533: 494: 491: 487: 483: 479: 475: 406: 404: 399: 395: 391: 343: 324: 322: 320: 315: 313: 312:threaded code 308: 305: 298: 296: 294: 290: 282: 280: 278: 274: 270: 269:topologically 266: 261: 259: 255: 251: 247: 246:multithreaded 243: 235: 231: 223: 221: 217: 213: 211: 207: 203: 199: 195: 191: 183: 179: 175: 172:Influenced by 170: 166: 154: 150: 139: 135: 132:Chris Pressey 131: 129: 125: 113: 110: 102: 91: 88: 84: 81: 77: 74: 70: 67: 63: 60: –  59: 55: 54:Find sources: 48: 44: 38: 37: 32:This article 30: 26: 21: 20: 1225:Iota and Jot 1204: 1123:. Retrieved 1119: 1109: 1084:. Retrieved 1080: 1070: 1059:. Retrieved 1055:the original 1045: 1031: 978: 966:+-<>,. 954:comment text 951: 933:End program 900: 896: 883: 879: 875: 871: 867: 863: 706: 702: 698: 694: 670: 666: 662: 658: 657:Modulo: Pop 644: 640: 639:, then push 636: 632: 619: 615: 614:, then push 611: 607: 594: 590: 589:, then push 586: 582: 569: 565: 564:, then push 561: 557: 471: 387: 328: 316: 309: 306: 302: 293:typing error 288: 286: 262: 257: 253: 227: 218: 214: 189: 188: 105: 99:January 2012 96: 86: 79: 72: 65: 53: 41:Please help 36:verification 33: 958:source code 299:Compilation 194:stack-based 163:/Befunge-93 1299:Categories 1270:Whitespace 1125:2014-01-23 1086:2014-01-23 1061:2014-01-23 1023:References 1007:Whitespace 198:reflective 69:newspapers 1210:Brainfuck 991:Brainfuck 962:Brainfuck 474:line feed 403:line feed 287:The word 283:Etymology 254:Fungeoids 128:Developer 58:"Befunge" 1286:Category 1265:Unlambda 1245:Malbolge 1220:INTERCAL 1215:FRACTRAN 1095:cite web 1012:Malbolge 1002:INTERCAL 985:See also 256:or just 210:Lemmings 1240:LOLCODE 1205:Befunge 1200:Beatnik 1120:Esolang 1081:Esolang 940:(space) 289:Befunge 234:compile 224:History 190:Befunge 157:catseye 152:Website 142: ( 122:Befunge 83:scholar 1230:JSFuck 979:around 974:Python 870:, and 446:,,,,,, 258:Funges 85:  78:  71:  64:  56:  944:No-op 909:& 490:ASCII 419:,,,,, 394:ASCII 350:12345 273:torus 230:Amiga 206:Forth 182:FALSE 178:Forth 165:.html 161:/node 90:JSTOR 76:books 1255:Piet 1235:Leet 1101:link 1017:Piet 972:and 970:Lisp 956:and 899:and 725:< 715:> 705:> 697:and 661:and 635:and 610:and 585:and 560:and 525:> 498:> 482:loop 478:LIFO 455:> 452:< 428:> 425:< 410:> 398:LIFO 383:< 368:6789 356:> 208:and 144:1993 140:1993 62:news 542:0-9 365:v?v 353:^?^ 212:". 159:.tc 45:by 1301:: 1118:. 1097:}} 1093:{{ 1079:. 866:, 818:$ 673:. 522:_@ 519::, 501:25 486:\n 458:25 431:48 362:?^ 260:. 200:, 196:, 180:, 1181:e 1174:t 1167:v 1128:. 1103:) 1089:. 1064:. 1039:. 929:@ 919:~ 901:x 897:y 891:g 884:v 880:y 878:, 876:x 872:v 868:x 864:y 858:p 848:# 838:, 828:. 808:\ 798:: 791:" 785:" 775:| 765:_ 755:? 745:v 735:^ 707:a 703:b 699:b 695:a 689:` 679:! 671:a 669:/ 667:b 663:b 659:a 653:% 645:a 643:/ 641:b 637:b 633:a 627:/ 620:b 618:* 616:a 612:b 608:a 602:* 595:a 593:- 591:b 587:b 583:a 577:- 570:b 568:+ 566:a 562:b 558:a 552:+ 528:^ 516:v 513:v 510:: 504:* 467:@ 464:, 461:* 443:v 440:v 437:, 434:* 416:v 413:v 380:. 377:^ 374:v 359:? 340:@ 336:. 332:? 238:p 146:) 112:) 106:( 101:) 97:( 87:· 80:· 73:· 66:· 39:.

Index


verification
improve this article
adding citations to reliable sources
"Befunge"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
Developer
catseye.tc/node/Befunge-93.html
Forth
FALSE
stack-based
reflective
esoteric programming language
Forth
Lemmings
Amiga
compile
self-modifying code
multithreaded
instruction pointers
"wraps around"
topologically
torus
Turing-complete
typing error

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.