La wikipedia define este lenguaje de la siguiente manera:
"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:
program bf; {$APPTYPE CONSOLE} uses Windows, SysUtils, Classes; // Esta funcion interpreta el codigo procedure Brainfuck(Codigo: String); const // El tamaño del array, el estandar es 30000 Size = 30000; // Mensajes de error del interprete strPointerError = 'El puntero no es valido'; strBracketError = 'Se encontro un bracket sin pareja'; var i,j: Integer; Min, Max, P: PByte; begin // Obtenemos memoria para alojar el array de bytes GetMem(Min,Size); try // Inicializamos el array a cero FillChar(Min^,Size,#0); // Establecemos el limite superior e inferior Max:= Min; inc(Max,Size - 1); // Inicializamos el puntero en la primera posicion del array P:= Min; i:= 1; // Comenzamos a recorrer el codigo while (i <= Length(Codigo)) do begin case Codigo[i] of '>': begin // Incrementamos el puntero if P = Max then raise Exception.Create(strPointerError); inc(P); end; '<': begin // Decrementamos el puntero if P = Min then raise Exception.Create(strPointerError); dec(P); end; '+': inc(P^); // Incrementamos el valor actual '-': dec(P^); // Decrementamos el valor actual '.': Write(Char(P^)); // Imprimime el valor actual ',': Read(Char(P^)); // Lee un valor de la entrada '[': if P^ = 0 then // Comienza un bucle begin j:= 1; // Como la condicion es cero saltamos al final del bucle 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 // Finaliza un bucle begin j:= 1; repeat dec(i); // Como la condicion no es cero saltamos al principio del bucle 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 // Liberamos la memoria del array FreeMem(Min); end; end; begin // Comprobamos que nos han pasado el fichero con el codigo if ParamCount = 1 then try with TStringList.Create do try // Abrimos el fichero con el codigo LoadFromFile(ParamStr(1)); // Lo interpretamos Brainfuck(Text); finally Free; end; except // Si ocurre una excepcion la imprimimos 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 probar:
Hola mundo:
++++++++++ [>+++++++>++++++++++>+++>+<<<<-] 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:
>++++++++++>+>+[ [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[ [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>> ]<<< ] This program doesn't terminate; you will have to kill it. Daniel B Cristofani (cristofdathevanetdotcom)
En este introduces un numero y el lo dibuja girado 45 grados:
>>>>+>+++>+++>>>>>+++>>+[ -,[----------[---[+<++++[>-----<-]+>[<+>--------[<+>- [--->>+++++++++++++[<<[-<+>>]>[<]>-]<< [+>+++++[<-------->-]<[<+>-]]]]]]]] < [<<++[>>>>>>>>>>>+<<<<<<<<<<<-]<<+>+>+>>>+>+>>+>+<<<<<- [<<+>>>+>+>>>+<<<<<- [<<<<+>>->>>>->>+<<<<- [<<<<->+>>>>->>>->-<<<<<- [<<<->>>>+<- [<<<+>>>>->+>>+<<<<- [<<<<+>->+>>>+>>>>+<<<<<- [<<->>>->->>>-<<<<<- [<<<<->+>>>>+>+>>>+<<<<<- [<<<<+>>>>>>-<<- [<<+>>>->>>>-<<<<<- [>+>>>->+<<<<<- [>>+<<- [<<<->->>>->->>+<<<<- [<<<+>+>>>+>+<<- [>->-<<- [<<->>>+>+<<- [<<+>>>>>>->-<<<<<- [<<<<->>->>- [<<<<+>>>>>>>>+<<<<- [<<<<->>+>>>>>>>+<<<<<- [>->>>-<<<<-]]]]]]]]]]]]]]]]]]]]] >[[<<<<<<<<<<<+>>>>>>>>>>>-]>]+>>>>>>>+>>+<]> ]<<[-]<[-[>>>>+<<<<-]]<<<<<<++<+++<+++[>]<[ >>>>>++++++++[<++++++<++++>>-]>>>[-[<+<<<<.>>>>>-]]<<+<<-<<<<[ -[-[>+<-]>]>>>[.[>]]<<[<+>-]>>>[<<-[<++>-]>>-] <<[++[<+>--]>+<]>>>[<+>-]<<<<<<<< ]>>>>>++++++++++.>+[[-]<]<<< ] [Enter a number using ()-./0123456789abcdef and space, and hit return. Daniel B Cristofani (cristofdathevanetdotcom)