Knowledge (XXG)

Befunge

Source 📝

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

Index

Chris Pressey

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

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