{"id":130,"date":"2006-08-25T12:23:34","date_gmt":"2006-08-25T12:23:34","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2006\/08\/25\/ultimate-spaghetti-coding-with-linguine\/"},"modified":"2006-08-25T12:23:34","modified_gmt":"2006-08-25T12:23:34","slug":"ultimate-spaghetti-coding-with-linguine","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2006\/08\/25\/ultimate-spaghetti-coding-with-linguine\/","title":{"rendered":"Ultimate Spaghetti Coding with Linguine"},"content":{"rendered":"<p>Todays dose of programming pathology is a monstrosity called *Linguine*. Linguine is a language that (obviously) carries spaghetti code to an extreme. You can see the [language specification][linguine-spec], or download an [implementation with examples][linguine-impl].<br \/>\nIt&#8217;s really a remarkably simple language. There are only 10 commands, including input and output:<br \/>\n1. &#8220;`x=y`&#8221;: assign the value of y to the variable x.<br \/>\n2. &#8220;`x+y`&#8221;: assign the value of x+y to the variable x.<br \/>\n3. &#8220;`x-y`&#8221;: assign the value of x-y to the variable x.<br \/>\n4. &#8220;`x|y`&#8221;: assign the value of x NAND y to the variable x.<br \/>\n5. &#8220;`x&gt;y`&#8221;: assign the value of shifting x by y bits to x. If y is positive, then it shifts y bits right; if y is negative, then it shifts -y bits left.<br \/>\n6. &#8220;`x?`&#8221;: Input one character and store it in the variable x.<br \/>\n7. &#8220;`x$`&#8221;: Output the content of register x as a character.<br \/>\n8. &#8220;`x#`&#8221;: Output the content of register x as an integer.<br \/>\n9. &#8220;`x&lt;y:loc`&#8221;: if x &lt; y then jump to the program line labeled &#8220;loc&#8221;.<br \/>\n10. &#8220;`x~y:loc`&#8221;: if x = y then jump to the program line labeled &#8220;loc&#8221;.<br \/>\nCommand operands on either integer literals, or on the contents of an unlimited set of numbered registers each of which contains an integer. Valid operands are either an integer (which identifies a register number if it appears on the left, or a literal value on the right); or an operand preceeded by &#8220;*&#8221;, which performs an indirection on the value it precedes.  So for example, &#8220;*3&#8221; is the contents of register three. &#8220;**3&#8221; is the contents of the register whose number is stored in register 3, etc. Any of the operands may be any of these forms, so for example, &#8220;1~*3:**4&#8221; is a valid command.<br \/>\nA linguine statement consists of three parts: a line label, a list of commands, and a branch target, written &#8220;label[commands]target&#8221;. When executed, the line evaluates each command in the list in sequence. If it reaches the end of the list without having branched, then it will branch to the statement labeled by the branch target.<br \/>\nThe lines of the program may appear in any order. The first statement executed will be the statement whose label is smallest. Branching to 0 ends program execution.<br \/>\nComments start with a &#8220;&#8216;&#8221;, and extend to the end of the line.<br \/>\nAll quite simple, right?<br \/>\nAs usual, we&#8217;ll start with hello world.<br \/>\n1[0=72,0$,0+29,0$,0+7,0$,0$,0+3,0$,1=32,1$,0-24,0$,0+24,0$,0+3,0$,0-6,0$,0-8,0$,1+1,1$,1-23,1$]0<br \/>\nThat&#8217;s perfectly clear, isn&#8217;t it? Actually, it&#8217;s basically the same as the hello world program we saw in brainfuck.\tWe&#8217;ll piece it apart.<br \/>\n* <code>0=72,0$<\/code>: 72 is the ascii code for &#8220;H&#8221;. So store that in register 0. Then output register 0.<br \/>\n* <code>0+29,0$<\/code>: 101 is the ascii code for &#8220;e&#8221;. So add 29 to the contents of register 0, which will make the new value of register 0 be 101. Then output that.<br \/>\n* etc.<br \/>\nHow about something a tad more interesting? Let&#8217;s look at generating the<br \/>\nFibonacci sequence:<br \/>\n1[0=32,2=1,1#,0$,2#]2<br \/>\n2[1+*2,3=*1,1=*2,2=*3,0$,2#]2<br \/>\n* This starts with line 1:<br \/>\n* &#8220;<code>0=32<\/code>: Store the ascii code for a whitespace in register 0.<br \/>\n* &#8220;`2=1`&#8221;: store the value 0 in register 2.<br \/>\n* &#8220;`1#`&#8221;: output the value of register 1. (Since registers contain 0 when the program starts, that prints a zero.)<br \/>\n* &#8220;`0$`&#8221;: output the contents of register 0 as a character, which prints a whitespace.<br \/>\n* &#8220;`2#`&#8221;: print the contents of register 2 (1).<br \/>\n* Finally, branch to statement 2.<br \/>\n* Statement 2: registers 1 and 2 contain the previous two fibonacci numbers. So this adds them together to get the next one, and then rotates the values of the registers so that the newest fibonacci number is in register 2, and the one before that is in register one.<br \/>\n* &#8220;`1+*2`&#8221;: add the value of register 2 to register 1.<br \/>\n* &#8220;`3=*1`&#8221;: store the value of register 1 in register 3.<br \/>\n* &#8220;`1=*2`&#8221;: store the value of register 2 in register 1.<br \/>\n* &#8220;`2=*3`&#8221;: store the value of register 3 in register 2.<br \/>\n* &#8220;`0$,2#`&#8221;: output a space, and then output the value of register 2.<br \/>\n* Finally, the branch instruction repeats statement 2, to keep generating fibonacci numbers.<br \/>\nHere&#8217;s a nice little multiply program, which demonstrates how to write subroutines. Note that recursion doesn&#8217;t work here, because we need to use fixed register numbers. (You can set up recursion using additional levels of indirection, it&#8217;s just messy as heck.)<br \/>\n&#8216;Multiply: -2 *= *-3<br \/>\n&#8216;Programmed by Jeffry Johnston, 2005<br \/>\n&#8216;-1=return jump, -4=temp, -5=temp<br \/>\n100[-4=*-3,-5=*-2,-2=0,-4&lt;0:101]102<br \/>\n101[-4~0:*-1,-2-*-5,-4+1]101 &#039;-3 is negative<br \/>\n102[-4~0:*-1,-2+*-5,-4-1]102 &#039;-3 is positive<br \/>\nThis takes the address of the instruction to return to in register -1; it takes the left operand of multiple (which is also the target of the result) in register -2, and the right operand in register -3.  Registers -4 and -5 are used for temporary storage.<br \/>\n* Line 100: set up, and determine whether the right operand is positive or negative.<br \/>\n* &quot;`-4=*-3,-5=*-2`&quot;: Copy the contents of register -3 to register -4, and the contents of register -2 to register -5.<br \/>\n* &quot;`-2=0`&quot;: Store 0 in register -2.<br \/>\n* &quot;`-4&lt;0:101`&quot;: If the contents of register -4 is less than 0, then branch to statement 101.<br \/>\n* If you reach the end of this line, then the contents of register -4 is non-negative, and you branch to statement 102.<br \/>\n* Line 101: do multiplication by repeated subtraction<br \/>\n* &quot;`-4~0:*-1`&quot;: If the contents of register -4 is zero, then we&#039;re done, so return by branching to the line labeled by the contents of register -1.<br \/>\n* &quot;`-2-*-5`&quot;: Subtract the contents of register -5 from the contents of register -2 (the result register).<br \/>\n* &quot;`-4-1`&quot;: decrement (or increment, depending on your point of view) the contents of register four, to show that you&#039;ve done one subtraction.<br \/>\n* Repeat the line.<br \/>\n* Line 102: basically exactly the same as 101, except that the does a repeated add rather than a subtract.<br \/>\nAn example of calling this would be:<br \/>\n1[-1=2,-2=36,-3=13]200<br \/>\n2[-2#]0<br \/>\nThis stores 36 and 13 as the parameters to the multiple subroutine, and then invokes it by branching to it at line 200. The &quot;return address&quot; is set to statement 2 by storing 2 in register -1. When control returns to statement 2, the result of the multiplication is printed out.<br \/>\nOne last example, which you can puzzle out on your own. A brainfuck interpreter in 21 lines of Linguine:<br \/>\n&#039;BF interpreter in Linguine<br \/>\n&#039;Programmed by Jeffry Johnston, 2005<br \/>\n1[*-2?,*-2~33:2,-2+1]1<br \/>\n2[*-2=-1,-2+1]3<br \/>\n3[-3=**-1,-3~-1:0,-3~43:4,-3~44:6,-3~45:7,-3~46:9,-3~60:10,-3~62:11,-3~91:12,-3~93:14]15<br \/>\n4[*-2+1,*-2~256:5]15    &#039;+<br \/>\n5[*-2=0]15<br \/>\n6[*-2?,*-2~-1:5]15      &#039;,<br \/>\n7[*-2-1,*-2~-1:8]15     &#039;-<br \/>\n8[*-2=255]15<br \/>\n9[*-2$]15               &#039;.<br \/>\n10[-2-1]15              &#039;<br \/>\n12[*-2~0:13]15          &#8216;[<br \/>\n13[-3=1]16<br \/>\n14[-3=-1]16             &#8216;]<br \/>\n15[-1+1]3<br \/>\n16[-4=1]17<br \/>\n17[-1+*-3,*-1~91:18,*-1~93:19]20<br \/>\n18[-4+*-3]20<br \/>\n19[-4-*-3]20<br \/>\n20[-4~0:21]17<br \/>\n21[-3~1:15]3<br \/>\n[linguine-spec]: http:\/\/kidsquid.com\/files\/linguine\/README<br \/>\n[linguine-impl]: http:\/\/kidsquid.com\/files\/linguine\/linguine.tar.gz<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Todays dose of programming pathology is a monstrosity called *Linguine*. Linguine is a language that (obviously) carries spaghetti code to an extreme. You can see the [language specification][linguine-spec], or download an [implementation with examples][linguine-impl]. It&#8217;s really a remarkably simple language. There are only 10 commands, including input and output: 1. &#8220;`x=y`&#8221;: assign the value of [&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-130","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-26","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/130","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=130"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}