Author Topic: [Resource] Brain F*ck implementation  (Read 3447 times)

Code:

function ord(%c)
{
   if(!strLen(%c))
      return 0;
   return strPos(mT(),%c)+1;
}
function chr(%c)
{
   if(%c==0)
      return "";
   return getSubStr(mT(),%c-1,1);
}
function mH(%i)
{
   %m="0123456789ABCDEF";
   while(mFloor(%i))
   {
      %s=%s@getSubStr(%m,%i%16,1);
      %i/=16;
   }
   return(strLen(%s<2)?"0":"")@%s;
}
function mT()
{
   for(%i=1;%i<255;%i++)
      %s=%s@"\\x"@mH(%i);
   return collapseEscape(%s);
}
function brain_interpret(%str,%in,%max)
{
   %cells=(strLen(%max)?mFloor(%max):30000);
   for(%i=0;%i<strLen(%str);%i++)
      if(strPos("+-<>[].,",getSubStr(%str,%i,1))!=-1)
         %strip=%strip@getSubStr(%str,%i,1);
   for(%i=0;%i<strLen(%strip);%i++)
   {
      switch$(getSubStr(%strip,%i,1))
      {
         case "+" :
            if(%cell[%pointer] < 255)
               %cell[%pointer]++;
         case "-" :
            if(%cell[%pointer] > 0)
               %cell[%pointer]--;
         case ">" :
            if(%pointer + 1 < %cells)
               %pointer++;
         case "<" :
            if(%pointer - 1 > 0)
               %pointer--;
         case "." :
            %out=%out@chr(mFloor(%cell[%pointer]));
         case "," :
            if(strLen(%in))
            {
               %cell[%pointer]=ord(getSubStr(%in,0,1));
               %in=getSubStr(%in,1,strLen(%in)-1);
            }
         case "[" :
            if(%cell[%pointer] <= 0)
            {
               for(%a=%b=0;%b+%i<strLen(%strip);%b++)
               {
                  if(getSubStr(%strip,%i+%b+1,1)$="[")
                     %a++;
                  if(getSubStr(%strip,%i+%b+1,1)$="]" && !%a)
                  {
                     %i+=%b+2;
                     break;
                  }
                  if(getSubStr(%strip,%i+%b+1,1)$="]" && %a)
                     %a--;
               }
            }
         case "]" :
            if(%cell[%pointer] > 0)
            {
               for(%a=%b=0;%i-%b-1>0;%b++)
               {
                  if(getSubStr(%strip,%i-%b-1,1)$="]")
                     %a++;
                  if(getSubStr(%strip,%i-%b-1,1)$="[" && !%a)
                  {
                     %i=%i-%b-1;
                     break;
                  }
                  if(getSubStr(%strip,%i-%b-1,1)$="[" && %a)
                     %a--;
               }
            }
      }
   }
   return %out;
}



Implementation:
String brain_interpret ( String code )
String brain_interpret ( String code , String input )
String brain_interpret ( String code , String input , Integer maxCells )

Max Cells default to 30000

Information:
Code consists of these characters
+-><,.

Each character has a specific role

Character         C equivalent
(Start)char array[30000];char *ptr=array;
>++ptr;
<--ptr;
+++*ptr;
---*ptr;
.putchar(*ptr);
,*ptr=getchar();
[while (*ptr) {
]}

Examples:
brain_interpret(",[.>,]","brain"); Returns "brain"
brain_interpret("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."); Returns "Hello"

More Information:
Wikipedia article

Compact Version (995 bytes) (Compressed: 605 bytes)

function ord(%c){if(!strLen(%c))return 0;return strPos(mT(),%c)+1;}function chr(%c){if(%c==0)return "";return getSubStr(mT(),%c-1,1);}function mH(%i){%m="0123456789ABCDEF";while(mFloor(%i)){%s=%s@getSubStr(%m,%i%16,1);%i/=16;}return(strLen(%s<2)?"0":"")@%s;}function mT(){for(%i=1;%i<255;%i++)%s=%s@"\\x"@mH(%i);return collapseEscape(%s);}function brain_compile(%s,%n,%x){%l=(strLen(%x)?mFloor(%x):30000);for(%i=0;%i<strLen(%s);%i++)if(strPos("+-<>[].,",getSubStr(%str,%i,1))!=-1)%r=%r@getSubStr(%str,%i,1);%c="";for(%i=0;%i<strLen(%r);%i++){switch$(getSubStr(%r,%i,1)){case "+":%c=%c@"if(_p<255)_p++;\n";case "-":%c=%c@"if(_p>0)_p--;\n";case ">":%c=%c@"if(%p+1<%l)%p++;\n";case "<":%c=%c@"if(%p-1>0)%p--;\n";case ".":%c=%c@"%o=%o@chr(mFloor(_p));\n";case ",":if(strLen(%in)){%c=%c@"_p="@ord(getSubStr(%n,0,1))@";\n";%n=getSubStr(%n,1,strLen(%n)-1);}case "[":%c=%c@"while(_p){\n";case "]":%c=%c@"}\n";}}%c=strReplace(%c,"_p","%j[%p]");eval(%c);return %o;}


I can make it a bit smaller, but that would be pointless ;-;
« Last Edit: September 21, 2013, 04:45:30 PM by MARBLE MAN »



now make one using as few bytes as possible

now make one using as few bytes as possible
that's the spirit!

I won't,I am now!

I made it compile the brain code into torquescript with this:

function brain_compile(%str,%in,%max)
{
   %cells=(strLen(%max)?mFloor(%max):30000);
   for(%i=0;%i<strLen(%str);%i++)
      if(strPos("+-<>[].,",getSubStr(%str,%i,1))!=-1)
         %strip=%strip@getSubStr(%str,%i,1);
   %code="";
   for(%i=0;%i<strLen(%strip);%i++)
   {
      switch$(getSubStr(%strip,%i,1))
      {
         case "+" :
            %code=%code@"if(_p<255)_p++;\n";
         case "-" :
            %code=%code@"if(_p>0)_p--;\n";
         case ">" :
            %code=%code@"if(_r+1<"@%cells@")_r++;\n";
         case "<" :
            %code=%code@"if(_r-1>0)_r--;\n";
         case "." :
            %code=%code@"%out=%out@chr(mFloor(_p));\n";
         case "," :
            if(strLen(%in))
            {
               %code=%code@"_p="@ord(getSubStr(%in,0,1))@";\n";
               %in=getSubStr(%in,1,strLen(%in)-1);
            }
         case "[" :
            %code=%code@"while(_p){\n";
         case "]" :
            %code=%code@"}\n";
      }
   }
   %code=strReplace(%code,"_p","%cell[%pointer]");
   %code=strReplace(%code,"_r","%pointer");
   eval(%code);
   return %out;
}
« Last Edit: September 21, 2013, 04:37:08 PM by MARBLE MAN »

Compact Version (995 bytes) (Compressed: 605 bytes) (Brain Code: 23747 bytes)

function ord(%c){if(!strLen(%c))return 0;return strPos(mT(),%c)+1;}function chr(%c){if(%c==0)return "";return getSubStr(mT(),%c-1,1);}function mH(%i){%m="0123456789ABCDEF";while(mFloor(%i)){%s=%s@getSubStr(%m,%i%16,1);%i/=16;}return(strLen(%s<2)?"0":"")@%s;}function mT(){for(%i=1;%i<255;%i++)%s=%s@"\\x"@mH(%i);return collapseEscape(%s);}function brain_compile(%s,%n,%x){%l=(strLen(%x)?mFloor(%x):30000);for(%i=0;%i<strLen(%s);%i++)if(strPos("+-<>[].,",getSubStr(%str,%i,1))!=-1)%r=%r@getSubStr(%str,%i,1);%c="";for(%i=0;%i<strLen(%r);%i++){switch$(getSubStr(%r,%i,1)){case "+":%c=%c@"if(_p<255)_p++;\n";case "-":%c=%c@"if(_p>0)_p--;\n";case ">":%c=%c@"if(%p+1<%l)%p++;\n";case "<":%c=%c@"if(%p-1>0)%p--;\n";case ".":%c=%c@"%o=%o@chr(mFloor(_p));\n";case ",":if(strLen(%in)){%c=%c@"_p="@ord(getSubStr(%n,0,1))@";\n";%n=getSubStr(%n,1,strLen(%n)-1);}case "[":%c=%c@"while(_p){\n";case "]":%c=%c@"}\n";}}%c=strReplace(%c,"_p","%j[%p]");eval(%c);return %o;}


I can make it a bit smaller, but that would be pointless ;-;
« Last Edit: September 21, 2013, 04:52:22 PM by MARBLE MAN »



Now, rewrite the parser in brain forget.

Now, rewrite the parser in brain forget.
no make a torquescript parser in brainforget for this brainforget parser in torquescript

Now, rewrite the parser in brain forget.
here

no make a torquescript parser in brainforget for this brainforget parser in torquescript
kill me

Haha, pretty cool!

Do BeFunge next? :D


Noo, MGIFOS :(
I kinda want to make that
char limits of torque :c

Haha, pretty cool!

Do BeFunge next? :D
maybe ;/
« Last Edit: September 22, 2013, 12:23:58 PM by MARBLE MAN »

I kinda want to make that
char limits of torque :c

you could just give it the number of stars instead of a string containing the stars