Created
January 19, 2017 15:26
-
-
Save kuwa72/5f48d94b05c0e850922d305b44e7235a to your computer and use it in GitHub Desktop.
One line brainfuck compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{grep -o . | (echo '{ ptr=1;memory=() ';awk '/\[/{print "while [[ memory[$ptr] -ne 0 ]];do"} /\]/{print "done;"}/\+/{print "let $((memory[$ptr]++));"} /-/{print "let $((memory[$ptr]--));"} />/{print "let $((ptr++));"} /</{print "let $((ptr--));"} /\./{print "printf \"\\x$(printf %x $((memory[$ptr])))\";"} /,/{print "memory[$ptr]=$(head -c 1);"}';echo '}') | sh} |
Very short inplementation by @kou029w
https://twitter.com/kou029w/status/823051030078197760
optimizer for @kou029w implementation.
(s='s/]/done:\[/while]));do:[+-]/]&&)):>/((p++)):</((p--)):\./printf 0x%x $]))|xxd -r:]/((m[p]:^.$//';gfold -b1|gsed "${s//://;s/}"| uniq -c | awk '/++|--/{print gensub(/(p|m\[p\])([-+])+/, "\\1=\\1\\2"$1, 1, $2);next}{sub($1, "")}1')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is part of a Hello World program(print "H")
It is compile result. My program compile brainfuck to shell script.
Bash eval compile result and output "H".
Base concept @kinaba's brainfuck page.