Brainfuck Code interpreter written in G'MIC

This is where I’ll share my Brainfuck Interpreter coded in G’MIC as it’s quite a subject by itself. For those who don’t know, Brainfuck is a esoteric language which utilize 8 characters. The rules can be found in this link - Brainfuck - Wikipedia

To use my G’MIC brainfuck Interpreter, you need to have additional argument after $2 up to the number of comma. However, codes with this doesn’t seem to work. I don’t know why. If anyone can figure that out, that’d be great. Fixed. It seems to multiply arguments now in the second last example.

G'MIC Brainfuck Interpreter
rep_gmic_brainfuck:
skip ${2=30000}
rm

# 43 <=> + 
# 44 <=> ,
# 45 <=> -
# 46 <=> .
# 60 <=> <
# 62 <=> >
# 91 <=> [
# 93 <=> ]

1 => brainfuck_code
('$1') => str_to_convert_into_brainfuck_code

$=argpos
 
# 1. Remove unused characters
eval[-1] >"
 inrange(i,42,47,0,0)?(
  da_push(#-2,i);
 ):(
  find([60,62,91,93],i,0,1)!=-1?(
   da_push(#-2,i);
  );
 );
 end(
  resize(#-2,1,da_size(#-2),1,1,0,0);
 );"
 
# 2. Remove temporary image as it's no longer needed.
rm.
 
# 3. Evaluate []
eval. >"begin(level=0;);
 if(i==91,++level;);
 if(i==93,--level;);
 if(level,if(i==44,run('error unsupported_gmic_feature');););
 if(level<0,run('error inv_bracks'););
 end(
  if(level,run('error inv_bracks'););
 );"
 
# 4. Process Brainfuck Code for input args validity
eval. >"begin(comma_count=0;);
 if(i==44,++comma_count;);
 end(
  comma_count;
  run('comma_count=',comma_count);
 );
 "
 
if $#!=($comma_count+2) error inv_arg_count fi
 
$comma_count

repeat $comma_count {
 if size('${argpos{3+$>}}')>1 error str_not_val fi
 num={'${argpos{3+$>}}'}
 set. $num,$>
}

=> user_args

1

#5. Code generation to convert Brainfuck code into G'MIC eval code

num_of_code_char:=h#$brainfuck_code
code_string=""

repeat $num_of_code_char {
 index_code={i(#$brainfuck_code,0,$>)}
 if $index_code==43 code_string.=ind_list[ind]++;ind_list[ind]%=256;                           continue fi
 if $index_code==44 code_string.=ind_list[ind]=i(#$user_args,user_index,0)%256;++user_index;   continue fi
 if $index_code==45 code_string.=ind_list[ind]--;ind_list[ind]%=256;                           continue fi
 if $index_code==46 code_string.=da_push(#-1,ind_list[ind]);                                   continue fi
 if $index_code==60 code_string.=--ind;                                                        continue fi
 if $index_code==62 code_string.=++ind;                                                        continue fi
 if $index_code==91 code_string.=repeat(inf,if(!ind_list[ind],break(););                       continue fi
 if $index_code==93 code_string.=if(!inrange(ind,0,$2,1,1),run("'error out_of_bound'");););             fi
}

#6. Execute Brainfuck code

eval begin(ind=0;user_index=0;ind_list=vector(#$2,0););if(!inrange(ind,0,$2,1,1),run("'error out_of_bound'"););$code_string;end(resize(#-1,1,da_size(#-1),1,1,0,0););

u {t}

rm

That being said, here’s my test result:

C:\Windows\System32>gmic rep_gmic_brainfuck \"++++++++++[>+++++++>++++++++++>+++<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.\",10 echo ${}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ Hello World!
[gmic]-0./ End G'MIC interpreter.
C:\Windows\System32>gmic rep_gmic_brainfuck \"-[--->+<]>-.[---->+++++<]>-.+.++++++++++.+[---->+<]>+++.-[--->++<]>-.++++++++++.+[---->+<]>+++.[-->+++++++<]>.++.-------------.[--->+<]>---..+++++.-[---->+<]>++.+[->+++<]>.++++++++++++..---.[-->+<]>--------.\",100 echo ${}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ This is pretty cool.
[gmic]-0./ End G'MIC interpreter.
C:\Windows\System32>gmic rep_gmic_brainfuck \">,>,<<++++++[>-------->--------<<-]>[>[>+>+<<-]>[<+>-]<<-]>[-]>+>>++++++++++<[->-[>>>]++++++++++<<+[<<<]>>>>]<-<++++++++++>>>[-<<<->>>]<<<<++++++[>++++++++>[++++++++>]<[<]>-]>>[.<<]<[<<]>>.\",30000,8,7 echo ${}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ 56
[gmic]-0./ End G'MIC interpreter.
C:\Windows\System32>gmic rep_gmic_brainfuck \"+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]\",20 echo ${}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
[gmic]-0./ End G'MIC interpreter.

My own code:

C:\Windows\System32>gmic rep_gmic_brainfuck \"+++++++[>++++++++++<-]>+.++++++>+++++[>+++++++<-]>++++.<<.----.------.>>-------.<<[-]++++++++++[>++++++++++<-]>+++++.<<++[>>+++++<<-]>>.>.<-------------.>>++++[>++++<-]>[<<<+>>>-]<<<-.-------.>+.\",20 echo ${}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ G'MIC is fun!
[gmic]-0./ End G'MIC interpreter.
1 Like

Nicely done, how about iota or jot? I was considering those after reading about SK combinator calculus recently (seems a bit of a coincidence!)

Thanks! I don’t know enough of that language, but you may benefit from my permutation/combination command as I saw something that seem to relate to that. Theoretically, could offer options. I’m back to more useful codes.

1 Like