# ksh93 brainfuck compiler typeset -T countcache_t=( typeset op="" integer count=0 typeset tabe="" # tab engine function flush { nameref t=${_.tabe} if [[ "${_.op}" != "" ]] ; then t.w ; printf "${_.op}\n" _.count fi # reset (( _.count=0 )) _.op="" return 0 } function write_op { typeset wop="$1" [[ "${wop}" != "${_.op}" ]] && _.flush (( _.count++ )) _.op="${wop}" return 0 } ) # manage tab intents typeset -T tab_t=( typeset indent="" function inc { _.indent+=$'\t' return 0 } function dec { _.indent="${_.indent:0:${#_.indent}-1}" return 0 } function w # write { printf "%s" "${_.indent}" return 0 } ) function write_bf_head { cat <') cc.write_op '(( p+=%d ))' ;; '<') cc.write_op '(( p-=%d ))' ;; '+') cc.write_op '(( t[p]+=%d ))' ;; '-') cc.write_op '(( t[p]-=%d ))' ;; '.') cc.flush ; tb.w ; print 'pc t[p]' ;; ',') cc.flush ; tb.w ; print 'gc t[p]' ;; '[') cc.flush ; tb.w ; print 'while (( t[p] != 0 )) ; do' ; tb.inc ;; ']') cc.flush ; tb.dec ; tb.w ; print 'done' ;; esac done return 0 } # main set -o noglob set -o nounset IFS='' tab_t tb countcache_t cc=( tabe=tb ) write_bf_head brainfuck2shell write_bf_tail exit 0