Ahora es el turno de un interprete de
Brainfucker
La wikipedia define este lenguaje de la siguiente manera:
Cita:
Empezado por Wikipedia
Brainfuck (jodecerebros) es un lenguaje de programación esotérico, diseñado por Urban Müller en 1993, con el objetivo de hacer un lenguaje que fuera a la vez muy simple, Turing completo y que requiriese un compilador pequeño. Müller basó Brainfuck en la máquina de Turing.
|
Para saber mas sobre este lenguaje:
http://es.wikipedia.org/wiki/Brainfuck (Español)
http://en.wikipedia.org/wiki/Brainfuck (Ingles)
Ahora que ya sabemos algo sobre el Brainfucker, vamos con su interprete:
Código Delphi
[-]
program bf;
{$APPTYPE CONSOLE}
uses Windows, SysUtils, Classes;
procedure Brainfuck(Codigo: String);
const
Size = 30000;
strPointerError = 'El puntero no es valido';
strBracketError = 'Se encontro un bracket sin pareja';
var
i,j: Integer;
Min, Max, P: PByte;
begin
GetMem(Min,Size);
try
FillChar(Min^,Size,#0);
Max:= Min;
inc(Max,Size - 1);
P:= Min;
i:= 1;
while (i <= Length(Codigo)) do
begin
case Codigo[i] of
'>': begin if P = Max then
raise Exception.Create(strPointerError);
inc(P);
end;
'<': begin if P = Min then
raise Exception.Create(strPointerError);
dec(P);
end;
'+': inc(P^); '-': dec(P^); '.': Write(Char(P^)); ',': Read(Char(P^)); '[': if P^ = 0 then begin
j:= 1;
repeat
inc(i);
if i > Length(Codigo) then
raise Exception.Create(strBracketError);
case Codigo[i] of
'[': inc(j);
']': dec(j);
end;
until (j=0);
end;
']': if P^ <> 0 then begin
j:= 1;
repeat
dec(i);
if i < 1 then
raise Exception.Create(strBracketError);
case Codigo[i] of
'[': dec(j);
']': inc(j);
end;
until (j=0);
end;
end;
inc(i);
end;
finally
FreeMem(Min);
end;
end;
begin
if ParamCount = 1 then
try
with TStringList.Create do
try
LoadFromFile(ParamStr(1));
Brainfuck(Text);
finally
Free;
end;
except
On E: Exception do
begin
Writeln(E.Message);
end;
end;
end.
Ya tenemos nuestro flamante interprete de Brainfucker, pero haciendo honor a su nombre, es jodidamente complicado programar en este lenguaje

. Así que sera mejor que pongamos algunos ejemplo para probarlo.
El hola mundo:
Código:
++++++++++
[>+++++++>++++++++++>+++>+<<<<-] The initial loop to set up useful values in the array
>++. Print 'H'
>+. Print 'e'
+++++++. Print 'l'
. Print 'l'
+++. Print 'o'
>++. Print ' '
<<+++++++++++++++. Print 'W'
>. Print 'o'
+++. Print 'r'
------. Print 'l'
--------. Print 'd'
>+. Print '!'
>. Print newline
La serie de Fibbonaci:
Código:
>++++++++++>+>+[
[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
]<<<
]
This program doesn't terminate; you will have to kill it.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/
Este es la leche, introduces un numero y el lo dibuja girado 45 grados:
Código:
>>>>+>+++>+++>>>>>+++>>+[
-,[----------[---[+<++++[>-----<-]+>[<+>--------[<+>-
[--->>+++++++++++++[<<[-<+>>]>[<]>-]<<
[+>+++++[<-------->-]<[<+>-]]]]]]]]
<
[<<++[>>>>>>>>>>>+<<<<<<<<<<<-]<<+>+>+>>>+>+>>+>+<<<<<-
[<<+>>>+>+>>>+<<<<<-
[<<<<+>>->>>>->>+<<<<-
[<<<<->+>>>>->>>->-<<<<<-
[<<<->>>>+<-
[<<<+>>>>->+>>+<<<<-
[<<<<+>->+>>>+>>>>+<<<<<-
[<<->>>->->>>-<<<<<-
[<<<<->+>>>>+>+>>>+<<<<<-
[<<<<+>>>>>>-<<-
[<<+>>>->>>>-<<<<<-
[>+>>>->+<<<<<-
[>>+<<-
[<<<->->>>->->>+<<<<-
[<<<+>+>>>+>+<<-
[>->-<<-
[<<->>>+>+<<-
[<<+>>>>>>->-<<<<<-
[<<<<->>->>-
[<<<<+>>>>>>>>+<<<<-
[<<<<->>+>>>>>>>+<<<<<-
[>->>>-<<<<-]]]]]]]]]]]]]]]]]]]]]
>[[<<<<<<<<<<<+>>>>>>>>>>>-]>]+>>>>>>>+>>+<]>
]<<[-]<[-[>>>>+<<<<-]]<<<<<<++<+++<+++[>]<[
>>>>>++++++++[<++++++<++++>>-]>>>[-[<+<<<<.>>>>>-]]<<+<<-<<<<[
-[-[>+<-]>]>>>[.[>]]<<[<+>-]>>>[<<-[<++>-]>>-]
<<[++[<+>--]>+<]>>>[<+>-]<<<<<<<<
]>>>>>++++++++++.>+[[-]<]<<<
]
[Enter a number using ()-./0123456789abcdef and space, and hit return.
Daniel B Cristofani (cristofdathevanetdotcom)
http://www.hevanet.com/cristofd/brainfuck/]
Como podéis ver, este interprete si que es
algo de lo mas inútil en el mundo real, pero puede resultar fascinante para algún friki como yo
PD: La función de entrada ',' no termina de convencerme como esta implementada, si alguien se anima a mejorarla ...