Brainfuck
From LXF Wiki
An esoteric language that has very few tokens, and thus is incredibly hard to read. However, it is incredibly easy to lex and parse, which makes writing a Brainfuck compiler somewhat fun. Brainfuck is the brain-child of Urban Müller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever. His first attempt at a compiler was 240 bytes in size - not bad.
The Language
A Brainfuck program has an implicit byte pointer, called simply 'the pointer', which is free to move around within an array of 30,000 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array. The Brainfuck programming language consists of eight commands, each of which is represented as a single character.
> Increment the pointer. < Decrement the pointer. + Increment the byte at the pointer. - Decrement the byte at the pointer. . Output the byte at the pointer. , Input a byte and store it in the byte at the pointer. [[ Jump past the matching ]] if the byte at the pointer is zero. ] Jump to the matching [.
Here is a Brainfuck script to print out the Green Bottles song:
>+++++++++[[<+++++++++++>-]]<[[>[-]]>[[-]]<<[[>+>+<<-]]>>[[<<+>>-]]>>> [[-]]<<<+++++++++<[[>>>+<<[>+>[-]]<<-]>[[<+>-]]>[<<++++++++++>>>+< -]<<-<-]+++++++++>[[<->-]]>>+>[[<[-]]<<+>>>-]>[[-]]+<<[[>+>-<<-]]<<< [[>>+>+<<<-]]>>>[[<<<+>>>-]]>[[<+>-]]<<-[[>[-]]<[[-]]]>>+<[[>[-]]<-]<+++ +++++[[<++++++<++++++>>-]]>>>[[>+>+<<-]]>>[[<<+>>-]]<[<<<<<.>>>>>- ]<<<<<<.>>[[-]]>[[-]]++++[[<++++++++>-]]<.>++++[[<++++++++>-]]<++.>+ ++++[[<+++++++++>-]]<.><+++++..--------.-------.>>[[>>+>+<<<-]]> >>[[<<<+>>>-]]<[[<<<<++++++++++++++.>>>>-]]<<<<[[-]]>++++[<+++++++ +>-]<.>+++++++++[[<+++++++++>-]]<--.---------.>+++++++[<------ ---->-]<.>++++++[[<+++++++++++>-]]<.+++..+++++++++++++.>++++++ ++[[<---------->-]]<--.>+++++++++[[<+++++++++>-]]<--.-.>++++++++ [[<---------->-]]<++.>++++++++[[<++++++++++>-]]<++++.----------- -.---.>+++++++[[<---------->-]]<+.>++++++++[[<+++++++++++>-]]<-. >++[[<----------->-]]<.+++++++++++..>+++++++++[[<---------->-]]< -----.---.>>>[[>+>+<<-]]>>[[<<+>>-]]<[[<<<<<.>>>>>-]]<<<<<<.>>>+++ +[[<++++++>-]]<--.>++++[[<++++++++>-]]<++.>+++++[[<+++++++++>-]]<. ><+++++..--------.-------.>>[[>>+>+<<<-]]>>>[[<<<+>>>-]]<[<<<<++ ++++++++++++.>>>>-]<<<<[[-]]>++++[[<++++++++>-]]<.>+++++++++[<++ +++++++>-]<--.---------.>+++++++[[<---------->-]]<.>++++++[<++ +++++++++>-]<.+++..+++++++++++++.>++++++++++[[<---------->-]]< -.---.>+++++++[[<++++++++++>-]]<++++.+++++++++++++.++++++++++. ------.>+++++++[[<---------->-]]<+.>++++++++[[<++++++++++>-]]<-. -.---------.>+++++++[[<---------->-]]<+.>+++++++[<++++++++++>- ]<--.+++++++++++.++++++++.---------.>++++++++[[<---------->-]] <++.>+++++[[<+++++++++++++>-]]<.+++++++++++++.----------.>++++ +++[[<---------->-]]<++.>++++++++[[<++++++++++>-]]<.>+++[<-----> -]<.>+++[[<++++++>-]]<..>+++++++++[[<--------->-]]<--.>+++++++[< ++++++++++>-]<+++.+++++++++++.>++++++++[[<----------->-]]<++++ .>+++++[[<+++++++++++++>-]]<.>+++[[<++++++>-]]<-.---.++++++.---- ---.----------.>++++++++[[<----------->-]]<+.---.[[-]]<<<->[[-]]>[ -]<<[[>+>+<<-]]>>[[<<+>>-]]>>>[[-]]<<<+++++++++<[[>>>+<<[>+>[-]]<<-] >[[<+>-]]>[[<<++++++++++>>>+<-]]<<-<-]+++++++++>[[<->-]]>>+>[[<[-]]< <+>>>-]>[[-]]+<<[[>+>-<<-]]<<<[[>>+>+<<<-]]>>>[[<<<+>>>-]]<>>[[<+>-]]< <-[[>[-]]<[[-]]]>>+<[[>[-]]<-]<++++++++[[<++++++<++++++>>-]]>>>[>+>+ <<-]>>[[<<+>>-]]<[[<<<<<.>>>>>-]]<<<<<<.>>[[-]]>[[-]]++++[<++++++++> -]<.>++++[[<++++++++>-]]<++.>+++++[[<+++++++++>-]]<.><+++++..--- -----.-------.>>[[>>+>+<<<-]]>>>[[<<<+>>>-]]<[<<<<++++++++++++++ .>>>>-]<<<<[[-]]>++++[[<++++++++>-]]<.>+++++++++[[<+++++++++>-]]<- -.---------.>+++++++[[<---------->-]]<.>++++++[[<+++++++++++>-]] <.+++..+++++++++++++.>++++++++[[<---------->-]]<--.>+++++++++[ <+++++++++>-]<--.-.>++++++++[[<---------->-]]<++.>++++++++[<++ ++++++++>-]<++++.------------.---.>+++++++[[<---------->-]]<+. >++++++++[[<+++++++++++>-]]<-.>++[[<----------->-]]<.+++++++++++ ..>+++++++++[[<---------->-]]<-----.---.+++.---.[[-]]<<<]
And here is a quick knock-up of a fairly basic Brainfuck interpreter Hudzilla wrote for this wiki. It's in the Delphi language (Object Pascal-esque), so you'll need Kylix or Delphi to compile it. Sorry, but it: is entirely untested/might crash your machine/has no warranty/might not work in the slightest/has no support/isn't optimized/etc
function dobf();
begin
Quit := false;
memo2.Text := '';
memo1.Text := memo1.Text + chr(255);
IP := 0;
MP := 1;
SP := 1;
repeat
Application.ProcessMessages;
inc(IP);
IPValue := ord(memo1.Text[[IP]]);
MPValue := Memory[[MP]];
case IPValue of
255: Quit := true; // @
60: dec(MP); // <
62: inc(MP); // >
43: Memory[[MP]] := (MPValue + 1) Mod 256; // +
45: Memory[[MP]] := (MPValue + 255) Mod 256; // -
46: begin // .
if MPValue <code> 0 then MPValue :</code> 255;
memo2.Text := memo2.Text + chr(MPValue);
memo2.SelStart := length(memo2.Text);
Application.ProcessMessages;
end;
44: begin // ,
while (Length(edit1.Text) <code> 0) AND (Quit </code> false) do begin
Application.ProcessMessages;
end;
Memory[[MP]] := ord(edit1.text[[1]]);
edit1.Text := Copy(edit1.text, 1, 1);
end;
93: begin // ]
dec(SP);
IP := Stack[[SP]];
end;
91: begin // [
if MPValue = 0 then begin
Level := 1;
Repeat
inc(IP);
IPValue := ord(memo1.text[[IP]]);
if IPValue = 91 then inc(Level);
if IPValue = 93 then dec(Level);
until Level <= 0;
end
else begin
Stack[[SP]] := IP - 1;
inc(SP);
end;
end;
end;
until Quit = true;
Temp := Pos(chr(255), memo1.Text);
if Temp > 0 then memo1.Text := copy(memo1.Text, 1, Temp - 1);
end;

