Brain$*&!

Brain$*&!

I've collected here a few programs I've written in brainfuck and other esoteric programming languages as well as a few useful utilities for said languages. Here's a sloppy implementation of a classic CS recursion problem. I wrote it on short notice for a contest, which explains why the solution is a bit bigger than it probably should be. It's a bit big to display here, so take a look at the source: hanoi.b

Season's Greetings! :-)

After you've written a few dozen of these things, you start to dislike doing all the strings by hand. Wouldn't it be nice if there were a program you could run that would generate the optimal set of brainfuck instructions to print a particular string? What would you pay for such a program? A thousand dollars? Two thousand? Nothing at all? Oh, the last one? Well that's just fine, cause you're getting it for free. It's quick, and it's dirty, but I believe it produces the optimal solution, though I haven't tried to prove it. Actually, it turns out that it doesn't produce an optimal program, but it might produce the optimal solution for the class of solutions that it could potentially produce and/or discard. Maybe. It does pretty well, but there are programs that do better, and it can be done better by hand if you're really patient. I'm not.: str2b.pl

Here's a slightly obfuscated brainfuck to C compiler written in perl. It handles comments and does some minor optimizations for size and speed. It accepts brainfuck code on standard input, and outputs C to standard out.

bfc.pl#!/usr/bin/perl
# Copyright (C) Thomas Schumm 
@f=qw(- +);$c=char;print"#include <stdio.h>\nmain(){$c m[30000];$c*p=m;
";for(<>){s/[*\/]//g;s!([^+-.<>[\]\s]+ *)+!/*$&*/!g;s!([-+<>])\1*!'*'x
($1lt'<')."p$f[ord($1)/2%2]=".length($&).';'!ge;s/\./put$c(*p);/g;
s/,/*p=get$c();/g;s/\[/while(*p){/g;s/]/}/g;print}print"}\n"

There's a new language similar to brainfuck called Path. It's got the same sort of memory structure, but it's got more instructions for fancy control flow. It's a bit like programming in ASCII art.

Here's a simple example that prints out the ASCII value of the first character from standard input. I've hightlighted a handy-dandy reusable divide/modulo by ten widget (which may be able to be made simpler, depending on your program's needs).

Any time one learns a new programming language, one must write a quine in that language. The first path quine I made is not very impressive. It's over 9k, and most of that is on one line. Yuck. The reference Path interpreter chokes on lines that long. However, I do think it's the first Path quine on the web (I wrote it on 2003-09-15 and posted it here on 2003-10-14), and other than the evil line, it is quite aesthetically pleasing.

Of course, I couldn't leave it at that. After a number of false starts, I finally got around to doing something much nicer. AFAIK, it's the second, and currently smallest Path quine on the web. It's less than half the size, and has no lines over 78 characters long. The "data" portion of the quine is RLE compressed to save space, and the ASCII values are stored there as two digits: a/10 and a%10 + 1. I tried other bases, but decimal turned out to be optimal. The "code" portion looks unoptimized size-wise and has lots of redundant or dead instructions, but it is written specifically to take advantage of the RLE compression. Long strings of identical instructions are less expensive than the kind of ASCII salad that one gets when trying to squeeze down size.

I'm sure both have room for improvement, but they're a start. Note: both include comments which are not reproduced in their output, but their outputs are true quines (i.e. identical, but with the comments removed). Also, either can be trivially modified to include those comments (only requires re-bootstrapping them), at the expense of some size (4234 bytes for quine1, 1128 bytes for quine2).

Speaking of quines, I've written a few polyglot quines. My first attempt was a polyglot quine in Python and C, but that proved too easy. I then produced a three language quine in Perl, Python and C. It is much more interesting. Both could use some size optimization (there are a few obvious things I know I'm missing), and I don't care for the long lines, but I like them anyway.

Update: I've got a signifcantly smaller version of my tri-language quine, which I've now also made into valid C++, making it a three and a half language quine. Check out polyglotC++PerlPythonC.c. The old one has some rather silly unnecessary things but I'm going to keep it around anyway because I like the trick I did with the exception. No doubt I'll be back in a few weeks with a better one with more languages. I'm thinking maybe bash next.

quine1.path   /\ Copyright (C) Thomas Schumm 
/\/>----\
++\^-<--/
+<!\-
+-\++++>>\
>}>-..<<<^-\
\/!++\
/<++++\
\v->++/.>\
!<  \+!/>+++\
++ />^-\-^< /
+\!\. </
\++++\+
/.<\ +\++++++++.#
\v</ ^\!>>-/
!\!\>>/
\++++++++++++++++++++++++++++++++>>++... 9101 instructions snipped ...++>>${
quine2.path///>>>>>>>>>${
}>\!>+>+>+>{
}>\!>++>+++>++++++>{
... 147 lines snipped ...
}>\!>+++>++++++>+>{
}>\!>+++>+++++++++>+++>{
\\\<<<!/<<<<<\
\\\\\\\\v<<<</
\\\\\\\\\+++++++++++++++++++++++++++++++++++++++++++++++\
/>.--------------------------.........+++++++++++++++.../
}++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\
>/>>.<.++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
>\v<++.-----------------------------------------------------\
>//++++++++++++++<.++++++++++++++++++++++++++++++.----------/
>\\+++++++++.>-----------\
<<<</.-------------------//>.<\
-//!\------------------->!\vv-/
><</+++++++++++++++++++++++/
\{{\++++++++++++++++++++\
\\\>.+++++++++++++++++++/
/!\<<<!/+++++++\
 /.>>-\\<v->+++/
>\<<vv/!>/
>\\\\\<<<\
\v<<<<<<</
## Copyright (C) Thomas Schumm