{"id":83,"date":"2006-07-21T11:03:00","date_gmt":"2006-07-21T11:03:00","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/07\/21\/friday-pathological-programming-befunge-the-2-dimensional-language\/"},"modified":"2006-07-21T11:03:00","modified_gmt":"2006-07-21T11:03:00","slug":"friday-pathological-programming-befunge-the-2-dimensional-language","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/07\/21\/friday-pathological-programming-befunge-the-2-dimensional-language\/","title":{"rendered":"Friday Pathological Programming: Befunge, the 2-dimensional language"},"content":{"rendered":"<p> Today, we&#8217;re going to take a look at a brilliant language called <a href=\"http:\/\/catseye.mine.nu:8080\/projects\/befunge93\/\"><b>Befunge<\/b><\/a>. Befunge is the work of an evil genius named Chris Pressey.\n<\/p>\n<p> Normal programming languages are based on a basically one-dimensional syntax; the program is a string, a sequence of characters, and it&#8217;s processed by reading that string in a straight-ahead fashion. But that&#8217;s not Befunge! Befunge is something like a two-dimensional turing machine: it says that the program and data are written on a two dimensional<em>torus<\/em>. Each instruction in Befunge is a single character, and where it&#8217;s located on the torus is crucial. (In case you&#8217;re not familiar with a torus, it&#8217;s what you get if you take a very flexible sheet of paper, and roll it so that you connect the top edge to the bottom, and then roll that tube so that you connect the left edge to the right. You get a donut shape where moving up from what used to be the top of the page puts you on the bottom of the page; moving left from the left edge of the page puts you on the right.) This torus is called the <em>playfield<\/em>.\n<\/p>\n<p> The basics of computation in Befunge are pretty straightforward. It&#8217;s a stack based language. Operations take their parameters from the stack, and leave their results on the stack. Nothing too complicated there. There are arithmetic operators, IO operators, control flow operators, all operating on the values on the stack.\n<\/p>\n<p> The arithmetic operators are the usual kinds of things: There are operators for addition (+), subtraction (-), division (\/), multiplication (*), modulo (%), and logical negation (!). Digit characters are treated as operators that push the numeric value of the digit onto the stack. (So &#8220;99&#8221; will create a stack with two nines.) Comparisons are done using &#8220;`&#8221;, which pops the top two values from the stack and compares them. So if the top of the stack was &#8220;x&#8221;, and the value beneath it was &#8220;y&#8221;, then &#8220;`&#8221; would leave a &#8220;0&#8221; on the stack if x&le;y, and 1 if x &gt; y.\n<\/p>\n<p> For IO, there are four operators. &#8220;&amp;&#8221; reads an integer from standard input and pushes it onto the stack. &#8220;~&#8221; reads a single character from standard input, and leaves it on the stack. &#8220;.&#8221; pops a value off the stack and writes it to standard out as an integer. &#8220;,&#8221; pops a value off the stack and writes it to standard out as a character.\n<\/p>\n<p> Of course, we can&#8217;t have a stack-based language without some stack operators: &#8220;:&#8221; makes a duplicate of the top value on the stack; &#8220;$&#8221; discards the top value on the stack; &#8220;&#8221; swaps the top two values of the stack.<\/p>\n<\/p>\n<p> So far, nothing has looked particularly pathological &#8211; in fact, nothing even looks particularly strange, other that the fact that it&#8217;s pretty illegible because of the single-character operators. But now, we get to control flow, and <em>that<\/em> is where the insanity\/brilliance of Befunge reveals itself.\n<\/p>\n<p> In Befunge, there&#8217;s a read-head that moves over the program. Each step, it executes the instruction under the head. But instead of just moving left or right, it can move left, right, up, or down. &#8220;&gt;&#8221; is an instruction that tells the head to start moving to the right; &#8220;&lt;&quot; tells the head to start moving left; &quot;^&quot; means start moving up, and &quot;v&quot; means to start moving down. So, for example:\n<\/p>\n<pre>\n&gt;v\n^&lt;\n<\/pre>\n<p> Is a program that runs an infinite loop: the head will just cycle over those four characters. An even more interesting infinite loop (taken from the befunge documentation) is:<\/p>\n<pre>\n&gt;v&gt;v\n&gt;^\n^  &lt;\n<\/pre>\n<p> Conditionals work by picking the direction that the head will move: &#8220;_&#8221; pops the stack, and if the value is zero, then it makes the head move right (&#8220;&gt;&#8221;); if it&#8217;s non-zero, it makes the head move left (&#8220;&lt;&quot;). Similarly, &quot;|&quot; pops a value, and makes the head move up if the value was non-zero, or down if it was zero.  To make things confusing, &quot;#&quot; means &quot;skip the next instruction.&quot; (Actually, it&#039;s important for when a vertical and horizontal control flow cross.) And finally,<br \/>\n&quot;@&quot; is the exit command; it makes the head stop, and the program halt.\n<\/p>\n<p> There&#8217;s also a little hack for strings. A double-quote character (&#8220;) starts a string; when one is encountered, the head keeps moving in the same direction, but instead of executing the characters as instuctions, it just pushes the character values onto the stack. When the second quote is found, it goes back to executing instructions.<\/p>\n<p> Finally, just in case the basic two dimensional flow control isn&#8217;t pathological enough, there are two instructions for modifying cells on the playfield! &#8220;g&#8221; pops an X and Y value off the stack, and pushes the character at (X,Y) onto the stack; &#8220;p&#8221; pops an X, Y, and a character off the stack, and writes the character onto location (X,Y) of the playfield. (The coordinates are relative to the cell where the program started.)<\/p>\n<p> So, let&#8217;s look at a couple of befunge programs. As usual, we start with the good old &#8220;hello world&#8221;.<\/p>\n<pre>\nv\n&gt;v\"Hello world!\"0&lt;\n,:\n^_25*,@\n<\/pre>\n<p>We start at the top left, head moving right. It moves until it hits the &#8220;v&#8221; character, which makes it go down; then &#8220;&lt;&quot; makes it go left. 0 pushes a zero onto the stack. Then we&#039;ve got a quote &#8211; so it starts pushing characters onto the stack until the next quote. When we get to the next quote, if we wrote the stack so that the top comes first, it would look like : (&#039;H&#039; &#039;e&#039; &#039;l&#039; &#039;l&#039; &#039;o&#039; &#039; &#039; &#039;w&#039; &#039;o&#039; &#039;r&#039; &#039;l&#039; &#039;d&#039; &#039;!&#039; 0 ).<\/p>\n<p> Then we hit a &#8220;v&#8221; which makes the head go down. This is the beginning of a loop; the leftmost two characters of rows 2, 3, and 4 are a while loop! The head goes down to &#8220;:&#8221; which duplicates the top of the stack; then it hits &#8220;_&#8221;, which turns left if the value on top of the stack is not zero; then the head turns up, outputs a character, turns right, and we&#8217;re back at the start of the loop. So the loop will output each character until we get the the &#8220;0&#8221; we pushed before the string; then at the &#8220;_&#8221;, we turn right.  2 and 5 are pushed on the stack and multiplied, leaving a 10, which is the linefeed character (basically &#8220;n&#8221; for you C weenies). It outputs the linefeed, and then exits.  <\/p>\n<p> How about a truly pathological example? Here&#8217;s a self-reproducing program in 12 bytes.<\/p>\n<pre>\n:0g,:93+`#@_1+\n<\/pre>\n<p>Stepping through that:<\/p>\n<ol>\n<li> Dup the value on top of the stack. That&#8217;ll produce a &#8220;0&#8221; if the stack is empty.  (So first pass, stack=[0])\n<li> &#8220;0&#8221; Push a zero on the stack.  (First pass, stack=[0,0])\n<li> &#8220;g&#8221;: Fetch the value at (x,y) on the stack; that&#8217;s (0,0) initially. (First pass, stack = [&#8216;:&#8217;])\n<li> &#8220;,&#8221;: Output it. So we printed the character at (0,0) (First pass, stack = [])\n<li> &#8220;:&#8221; dup the stack top again. (First pass, stack = [0])\n<li> &#8220;93+&#8221;. Get the number 12 onto the stack. (First pass, stack = [12,0])\n<li> Compare what was on top of the stack to twelve. Leave a 0 there if it was, or a 1 if it wasn&#8217;t. (First pass, stack = [0]).\n<li> &#8220;#&#8221; skip over the next character.\n<li> &#8220;_&#8221; go right if the top of stack is zero; left if it&#8217;s one. So if the value copied by the second &#8220;:&#8221; was greater than 12, then go left (in which case you hit &#8220;@&#8221; and halt); otherwise, keep going right.\n<li> &#8220;1+&#8221;: add one to the top of the stack (First pass, stack = ([1])). Then keep going right until you hit the right edge, and the you jump back to the left edge, so you&#8217;re at the first &#8220;:&#8221; again.\n<li> Now the whole thing repeats, except that there&#8217;s a one on the stack. So the &#8220;g&#8221; will fetch (1,0); the &#8220;12&#8221; will be compared to 1, and the &#8220;1+&#8221; on the end will leave &#8220;2&#8221; on the stack. So now it fetches and outputs (2,0). And so on, until it reaches the &#8220;(_)&#8221; after outputting (12,0), when it halts.\n<\/ol>\n<p> One more, which I&#8217;ll let you figure out for yourself. Here&#8217;s a program that prompts you for a number, and computes its factorial:<\/p>\n<pre>\nv\n&gt;v\"Please enter a number (1-16) : \"0$*99g1-:99p#v_.25*,@\n^_&amp;:1-99p&gt;:1-:!|10          &lt;\n^     &lt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today, we&#8217;re going to take a look at a brilliant language called Befunge. Befunge is the work of an evil genius named Chris Pressey. Normal programming languages are based on a basically one-dimensional syntax; the program is a string, a sequence of characters, and it&#8217;s processed by reading that string in a straight-ahead fashion. But [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[92],"tags":[],"class_list":["post-83","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-1l","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/83","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=83"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/83\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=83"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=83"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=83"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}