Re: Re: Turing Machine

From nat, 5 Years ago, written in Bash, viewed 337 times. This paste is a reply to Re: Turing Machine from nat - view diff
URL https://code.nat.moe/view/5c0be861 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. i_="\033[7m"
  25. n_="\033[0m"
  26. in="$(cat)"
  27. op="$(head -n1 "$1" | cut -d'|' -f1)"
  28.  
  29. echo -e "${b_}in:${n_} $in"
  30.  
  31. for (( i=0; i<${#in}; )); {
  32.   cur="${in:$i:1}"
  33.   act="$(grep -E "^$op\|$cur" "$1")"
  34.   lc="$(wc -l <<< "$act")"
  35.   [ $lc != 1 ] && echo "Fatal: $lc match for $op|$cur." && exit 1
  36.   [ -z "$act" ] && break
  37.   lop=$op
  38.   op="$(cut -d'|' -f5 <<< "$act")"
  39.   ch="$(cut -d'|' -f3 <<< "$act")"
  40.   in="${in:0:$i}$ch${in:$(($i+1))}"
  41.   p="${in:0:$i}$i_$ch$n_${in:$(($i+1))}"
  42.   mo="$(cut -d'|' -f4 <<< "$act")"
  43.   [[ $mo == 'r' ]] && let i=i+1 || let i--
  44.   echo -e "$p, ${b_}pos:${n_} $i, ${b_}cu:${n_} $cur, ${b_}op:${n_} $lop -> $op, ${b_}mo:${n_} $mo, ${b_}wr:${n_} $ch"
  45.   [[ $i -lt 0 ]] && break
  46. }
  47.  
  48. echo -e "${b_}out:${n_} $in"

Reply to "Re: Re: Turing Machine"

Here you can reply to the paste above

captcha