Turing Machine

From nat, 5 Years ago, written in Bash, viewed 342 times.
URL https://code.nat.moe/view/8b1f3dbe Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. #
  3. # turing: simple turing
  4. #
  5. # write program in this way:
  6. # status_name|current_cursor|write_char|cursor_move|goto_status
  7. #
  8. # example (bin add):
  9. # s|0|0|r|s
  10. # s|1|1|r|s
  11. # s|_|_|l|c
  12. # c|1|0|l|c
  13. # c|_|1|l|f
  14. # c|0|1|l|f
  15. # f|0|0|l|f
  16. # f|1|1|l|f
  17. #
  18. # in: 0101
  19. # out: 0110
  20.  
  21. [[ -z $1 || ! -e $1 ]] && echo 'usage: '$0' <truing program>' && exit 1
  22.  
  23. b_="\033[1m"
  24. n_="\033[0m"
  25. in="$(cat)"
  26. op="$(head -n1 "$1" | cut -d'|' -f1)"
  27.  
  28. echo -e "${b_}in:${n_} $in"
  29.  
  30. for (( i=0; i<${#in}; )); {
  31.   cur="${in:$i:1}"
  32.   act="$(grep -E "^$op\|$cur" "$1")"
  33.   lc="$(wc -l <<< "$act")"
  34.   [ $lc != 1 ] && echo "Fatal: $lc match for $op|$cur." && exit 1
  35.   lop=$op
  36.   op="$(cut -d'|' -f5 <<< "$act")"
  37.   ch="$(cut -d'|' -f3 <<< "$act")"
  38.   in="${in:0:$i}$ch${in:$(($i+1))}"
  39.   p="${in:0:$i}$b_$ch$n_${in:$(($i+1))}"
  40.   mo="$(cut -d'|' -f4 <<< "$act")"
  41.   [[ $mo == 'r' ]] && let i=i+1 || let i--
  42.   echo -e "$p, ${b_}pos:${n_} $i, ${b_}cu:${n_} $cur, ${b_}op:${n_} $lop -> $op, ${b_}mo:${n_} $mo, ${b_}wr:${n_} $ch"
  43.   [[ $i -lt 0 ]] && break
  44. }
  45.  
  46. echo -e "${b_}out:${n_} $in"
  47.  

Replies to Turing Machine rss

Title Name Language When
Re: Turing Machine nat bash 5 Years ago.

Reply to "Turing Machine"

Here you can reply to the paste above

captcha