{"id":408,"date":"2007-05-04T08:30:00","date_gmt":"2007-05-04T08:30:00","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2007\/05\/04\/even-more-stack-pathology\/"},"modified":"2007-05-04T08:30:00","modified_gmt":"2007-05-04T08:30:00","slug":"even-more-stack-pathology","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2007\/05\/04\/even-more-stack-pathology\/","title":{"rendered":"Even More Stack Pathology"},"content":{"rendered":"<p> I thought that it would be fun to stick with the &#8220;stack-based&#8221; theme of last week&#8217;s pathological post, but this time, to pick an utterly pointlessly twisted stack based language, but one that would be appreciated by <a href=\"http:\/\/scienceblogs.com\/insolence\/2007\/04\/i_hope_this_doesnt_go_to_his_head_for_ob_1.php\">the mascot of one of my fellow ScienceBlogs<\/a>. Orac, this one&#8217;s for you! \ud83d\ude42   Our target these week is the language &#8220;Enema&#8221;. <\/p>\n<p><!--more--><\/p>\n<p> Enema is a remarkably simple stack based language. In fact, it mostly looks like a<br \/>\nthoroughly trivial language &#8211; something like False, only simpler. It has a small family<br \/>\nof simple commands:<\/p>\n<dl>\n<dt> 0-9<\/dt>\n<dd> Push the value of the digit onto the stack.<\/dd>\n<dt>+<\/dt>\n<dd> Pop the top two values on the stack, add them, and push the result.<\/dd>\n<dt>&#8211;<\/dt>\n<dd> Pop the top two values on the stack, subtract the first from the second, and push the result.<\/dd>\n<dt>*<\/dt>\n<dd> Pop the top two values on the stack, multiply them, and push the result.<\/dd>\n<dt>\/<\/dt>\n<dd> Pop the top two values on the stack, integer-divide the first by the second, and push the result.<\/dd>\n<dt>%<\/dt>\n<dd> Pop the top two values on the stack, integer-divide the first by the second, and<br \/>\nand push the remainder onto the stack.<\/dd>\n<dt>&amp;<\/dt>\n<dd> Pop the top two values on the stack, perform logical-and on them, and push the result.<\/dd>\n<dt>|<\/dt>\n<dd> Pop the top two values on the stack, perform logical or on them, and push the result.<\/dd>\n<dt>^<\/dt>\n<dd> Pop the top two values on the stack, perform logical exclusive-or on them, and push the result.<\/dd>\n<dt>I<\/dt>\n<dd> Input a single character, and push it onto the stack.<\/dd>\n<dt>O<\/dt>\n<dd> Output the value on top of the stack as a character.<\/dd>\n<dt>X<\/dt>\n<dd> Discard the value on top of the stack.<\/dd>\n<dt>D<\/dt>\n<dd> Duplicated the value on top of the stack.<\/dd>\n<dt>S<\/dt>\n<dd> Swap the top two values on the stack.<\/dd>\n<dt>R<\/dt>\n<dd>Rotate the stack: take the top three values on the stack S1,S2,S3, and push them back as S3,S1,S2.<\/dd>\n<dt>G<\/dt>\n<dd> Pop the top-most stack value as a memory address and push the value stored at<br \/>\nthat address.<\/dd>\n<dt>P<\/dt>\n<dd> Pop the top value on the stack as a memory address, and the second value on the stack<br \/>\nas a number, and store them number in memory at the address<\/dd>\n<dt>[<\/dt>\n<dd> Mark the beginning of a loop.<\/dd>\n<dt>]<\/dt>\n<dd> Mark the end of a loop. When code reaches the &#8220;]&#8221; and the end of a loop, it jumps back<br \/>\nto the corresponding &#8220;[&#8220;.<\/dd>\n<dt> B<\/dt>\n<dd> Exit (break out of) the current loop, starting execution at the instruction after the next &#8220;]&#8221;.<\/dd>\n<dt>Z<\/dt>\n<dd> Conditional execution: skip the next instruction if the value on top of the stack is greater than 0.<\/dd>\n<dt>&#8220;chars&#8221;<\/dt>\n<dd>Push the ascii codes of the characters between the quotes onto the stack.<\/dd>\n<dt>?<\/dt>\n<dd>Push the current depth of the stack onto the top of the stack.<\/dd>\n<dt>#<\/dt>\n<dd>Push the maximum address of memory that has been used by the program onto the stack. (This is effectively the amount of memory that has been allocated by the program.)<\/dd>\n<dt>: <em>char<\/em> <em>code<\/em> :<\/dt>\n<dd> Define <em>char<\/em> as a new function, whose body is the code up to the next &#8220;:&#8221;.<\/dd>\n<dt>! <em>char<\/em><\/dt>\n<dd> Discard the function definition associated with the character.<\/dd>\n<dt> Q<\/dt>\n<dd>Return from function call.<\/dd>\n<dt>.<\/dt>\n<dd> End of program: halt.<\/dd>\n<\/dl>\n<p> So, for example, here&#8217;s a basic &#8220;Hello world&#8221; program:<\/p>\n<pre>\n052*\"!dlroW ,olleH\"[DZBO].\n<\/pre>\n<p> It pushes 0, 5, and 2 onto the stack; then multiplies 5 times 2, leaving 10 on top, and 0 beneath it. Then it pushes the characters of &#8220;Hello world&#8221; onto the stack &#8211; so the stack is &#8220;0\/10\/&#8221;!&#8221;\/&#8221;d&#8221;\/&#8221;l&#8221;\/&#8221;r&#8221;\/&#8221;o&#8221;\/&#8221;W&#8221;\/&#8221; &#8220;\/&#8221;,&#8221;\/&#8221;o&#8221;\/&#8221;l&#8221;\/&#8221;l&#8221;\/&#8221;e&#8221;\/&#8221;H&#8221;. Then it runs a loop containing the commands &#8220;DZBO&#8221;. &#8220;D&#8221; duplicates the top value on the stack; then &#8220;Z&#8221; skips the next instruction if the stack-top is not zero. So if the character on top of the stack was not zero, then it skips to &#8220;O&#8221;, and outputs the stack top, and then goes back to the next iteration.  If the stack top <em>was<\/em> 0, then it executes &#8220;B&#8221;, which breaks out of the loop, and halts. So it will output each of the characters in &#8220;Hello, World!&#8221;, followed by character 10 (which is newline); and then it will halt. Very straightforward, if awkward.<\/p>\n<p> Ok&#8230; So far, we&#8217;ve got a pretty typical (if ugly-ish) stack-based language. But there&#8217;s nothing particularly pathological about it. It&#8217;s just another stack language. So why does this justify a name like &#8220;Enema&#8221;? Why is it worth a coveted FPP slot? Here&#8217;s<br \/>\na simple example to illustrate why:<\/p>\n<pre>\n23*\n68*+\nO\n<\/pre>\n<p> This is a very simple program. It multiplies 2*3, and then adds 48 to it (converting the numeric value to a digit character value), and outputs it. So it outputs &#8220;6&#8221;.<\/p>\n<pre>\n:23:\n23*\n68*+\nO\n<\/pre>\n<p> This is the same program, except that we&#8217;ve added a function definition to the beginning. The program <em>now<\/em> outputs &#8220;9&#8221; &#8211; because we&#8217;ve redefined &#8220;2&#8221; to push the value &#8220;3&#8221; onto the stack.<\/p>\n<p> &#8220;:&#8221; can <em>redefine<\/em> <em>any<\/em> command in  the program. &#8220;:23:&#8221; will redefine <em>2<\/em> so that instead of pushing the value 2 onto the stack, it pushes the value 3!  &#8220;::stuffQ&#8221; will <em>redefine<\/em> the &#8220;:&#8221; command so that instead of starting a definition, it will now be a command that does &#8220;stuff&#8221;. <\/p>\n<p> That gives you some idea of how twisted this can get. Alas, it&#8217;s really <em>hard<\/em> to write programs when your primitives can be arbitrarily redefined out from under you! So the author just totally wimped out, and never uses this in any of their examples.<\/p>\n<p> But for you, my readers, I&#8217;m willing to endure all sorts of terrible punishment. So here is my rendition of a new and extra-twisted version of an enema program to<br \/>\ncompute the integer exponent of a number.<\/p>\n<pre>\n:qX:\n:39Q:\n:41Q:\n{ output null-terminated string to stdout }\n:o[DZBO]qQ:\n{ count n-th power of v (v n -&gt; p) }\n:p4[SDZBRDR*SR1-S]qSqQ:\n{ convert integer to null-terminated string (n -&gt; 0 c1 c2 c3 ...) }\n:i0S[D34+%\"0\"+S34+\/DZB]qQ:\n{ read integer terminated with character &lt; &#039;0&#039; }\n:r0[I&quot;0&quot;-D1+ZBS34+*+]qQ:\n{ prompt user to enter data }\n:a0&quot; :eulav&quot;352*&quot; atad retnE&quot;or0&quot; :rewop&quot;3orQ:\n{ main program }\n?Zapio52*O.\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I thought that it would be fun to stick with the &#8220;stack-based&#8221; theme of last week&#8217;s pathological post, but this time, to pick an utterly pointlessly twisted stack based language, but one that would be appreciated by the mascot of one of my fellow ScienceBlogs. Orac, this one&#8217;s for you! \ud83d\ude42 Our target these week [&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-408","post","type-post","status-publish","format-standard","hentry","category-pathological-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-6A","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/408","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=408"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}