#!/bin/bash
#
# turing: simple turing
#
# write program in this way:
# status_name|current_cursor|write_char|cursor_move|goto_status
#
# example (bin add):
# s|0|0|r|s
# s|1|1|r|s
# s|_|_|l|c
# c|1|0|l|c
# c|_|1|l|f
# c|0|1|l|f
# f|0|0|l|f
# f|1|1|l|f
#
# in: 0101
# out: 0110
[[ -z $1 || ! -e $1 ]] && echo 'usage: '$0' <truing program>' && exit 1
b_="\033[1m"
i_="\033[7m"
n_="\033[0m"
in="$(cat)"
op="$(head -n1 "$1" | cut -d'|' -f1)"
echo -e "${b_}in:${n_} $in"
for (( i=0; i<${#in}; )); {
cur="${in:$i:1}"
act="$(grep -E "^$op\|$cur" "$1")"
lc="$(wc -l <<< "$act")"
[ $lc != 1 ] && echo "Fatal: $lc match for $op|$cur." && exit 1
lop=$op
op="$(cut -d'|' -f5 <<< "$act")"
ch="$(cut -d'|' -f3 <<< "$act")"
in="${in:0:$i}$ch${in:$(($i+1))}"
p="${in:0:$i}$b_$ch$n_${in:$(($i+1))}"
p="${in:0:$i}$i_$ch$n_${in:$(($i+1))}"
mo="$(cut -d'|' -f4 <<< "$act")"
[[ $mo == 'r' ]] && let i=i+1 || let i--
echo -e "$p, ${b_}pos:${n_} $i, ${b_}cu:${n_} $cur, ${b_}op:${n_} $lop -> $op, ${b_}mo:${n_} $mo, ${b_}wr:${n_} $ch"
[[ $i -lt 0 ]] && break
}
echo -e "${b_}out:${n_} $in"
Replies to Re: Turing Machine 
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}