#
# 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
[ -z "$act" ] && break
lop=$op
op="$(cut -d'|' -f5 <<< "$act")"
ch="$(cut -d'|' -f3 <<< "$act")"
in="${in:0:$i}$ch${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_}