{"id":2917,"date":"2024-11-03T18:19:10","date_gmt":"2024-11-03T18:19:10","guid":{"rendered":"https:\/\/andyjohnson.uk\/blog\/?p=2917"},"modified":"2024-11-03T18:32:10","modified_gmt":"2024-11-03T18:32:10","slug":"bf","status":"publish","type":"post","link":"https:\/\/andyjohnson.uk\/blog\/2024\/11\/03\/bf\/","title":{"rendered":"BF"},"content":{"rendered":"\n<p>When you know deep-down that a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Brainfuck\">Brainfuck<\/a> interpreter should only ever be written in C, but you&#8217;re not quite ready to leave the comfort of C#. So in a spirit of minimalism you find yourself writing C# in a C style just for the hell of it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void Execute(string program, Stream? input, Stream? output)\n{\n    int ip = 0;\n    int dp = 0;\n    var data = new byte&#91;30000];\n\n    while(true)\n    {\n        if (ip >= program.Length)\n            break;\n        switch(program&#91;ip])\n        {\n            case '>':\n                dp++;\n                break;\n            case '&lt;':\n                dp--;\n                break;\n            case '+':\n                data&#91;dp]++;\n                break;\n            case '-':\n                data&#91;dp]--;\n                break;\n            case '.':\n                output?.Write(data, dp, 1);\n                break;\n            case ',':\n                input?.Read(data, dp, 1);\n                break;\n            case '&#91;':\n                if (data&#91;dp] == 0)\n                    ip = JumpTo(']', +1, program, ip);\n                    break;\n            case ']':\n                if (data&#91;dp] != 0)\n                    ip = JumpTo('&#91;', -1, program, ip);\n                break;\n        }\n        ip++;\n    }\n}\n\n\nprivate static int JumpTo(char match, int incr, string program, int ip)\n{\n    while (true)\n    {\n        ip += incr;\n        if ( (match == ']' &amp;&amp; program&#91;ip] == '&#91;') || \n             (match == '&#91;' &amp;&amp; program&#91;ip] == ']') )\n            ip = JumpTo(match, incr, ip, program);\n        else if (program&#91;ip] == match)\n            return ip;\n    }\n}<\/code><\/pre>\n\n\n\n<p>This made me wonder if the style of a language affects the style of its implementation. Not sure.<\/p>\n\n\n\n<p>(For any impressionable children reading this: don&#8217;t do it this way. Use descriptive variable names and comments and put braces around your if\/else clauses.)<\/p>\n\n\n\n<p>(And for any hiring managers reading this: its deliberate, I promise.)<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you know deep-down that a Brainfuck interpreter should only ever be written in C, but you&#8217;re not quite ready to leave the comfort of C#. So in a spirit of minimalism you find yourself writing C# in a C style just for the hell of it. This made me wonder if the style of &hellip; <a href=\"https:\/\/andyjohnson.uk\/blog\/2024\/11\/03\/bf\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;BF&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,62],"tags":[60,61,42],"class_list":["post-2917","post","type-post","status-publish","format-standard","hentry","category-dont-to-this","category-programming","tag-brainfuck","tag-programming","tag-software"],"_links":{"self":[{"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/posts\/2917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/comments?post=2917"}],"version-history":[{"count":6,"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/posts\/2917\/revisions"}],"predecessor-version":[{"id":2925,"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/posts\/2917\/revisions\/2925"}],"wp:attachment":[{"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/media?parent=2917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/categories?post=2917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andyjohnson.uk\/blog\/wp-json\/wp\/v2\/tags?post=2917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}