📜 ⬆️ ⬇️

Brainfuck Interpreter on Bash

Recently, the popularity of the esoteric language Brainfuck is gaining momentum. The number of applications written in Brainfuck does not increase in arithmetic, but even exponentially. Very similar situation with Android.

Today I decided to launch my first hello world in this language. I am a happy NetBSD user on the toaster, and I was unable to configure the Internet. Therefore, I could not use ready-made solutions to run programs on Brainfuck.

The only way out was to write the interpreter yourself. From the software were only Bash and system utilities.
')
 $ cat bf.sh
 #! / bin / bash
 C = "s [0] = 0; p = 0;"
 while read -n1 c;  do case $ c in
	 \ +) C = "$ C s [\ $ p] = \ $ ((\ $ {s [\ $ p]} + 1));" ;;
	 \ -) C = "$ C s [\ $ p] = \ $ ((\ $ {s [\ $ p]} - 1));" ;;
	 \>) C = "$ C p = \ $ ((\ $ p + 1));" ;;
	 \ <) C = "$ C p = \ $ ((\ $ p-1));" ;;
	 \.) C = "$ C printf \\\\\ $ (printf '% 03o' \ $ {s [\ $ p]});" ;;
	 \,) C = "$ C read -n1 c; s [\ $ p] = \` printf '% d' \ "'\ $ c \" \ `;" ;;
	 \ [) C = "$ C while [[\ $ {s [\ $ p]}> 0]]; do" ;;
	 \]) C = "$ C done;" ;;
 esac;  done <$ 1; 
 eval $ C

 $ cat hello.b
 ++++++++++ [> +++++++> +++++++++> +++> + <<<< -]> ++
 .> +. +++++++ .. +++.> ++. << ++++++++++++++.>. +++.
 ------.--------.> +.>.

 $ ./bf.sh hello.b
 Hello World!

Source: https://habr.com/ru/post/112989/


All Articles