Re: Logic evaluator (truth table listing)

From NAT, 5 Years ago, written in Bash, viewed 416 times. This paste is a reply to Logic evaluator (truth table listing) from NAT - view diff
URL https://code.nat.moe/view/8f1c7daa Embed
Download Paste or View Raw
  1. #!/bin/bash
  2. # Notice: only work when in PATH, or call using absolute path.
  3.  
  4. trap 'quit' 2 15
  5.  
  6. b=$(tput bold)
  7. n=$(tput sgr0)
  8.  
  9. # Operators
  10.  
  11. function disj {
  12.         [[ $1 == "T" ]] && echo "T" && return
  13.         [[ $2 == "T" ]] && echo "T" && return
  14.         echo "F" && return
  15. }
  16.  
  17. function conj {
  18.         [[ $1 == "T" && $2 == "T" ]] && echo "T" && return
  19.         echo "F" && return
  20. }
  21.  
  22. function impl {
  23.         [[ $1 == "T" && $2 == "F" ]] && echo "F" && return
  24.         echo "T" && return
  25. }
  26.  
  27. function req {
  28.         impl $2 $1
  29. }
  30.  
  31. function eq {
  32.         [[ $1 == $2 ]] && echo "T" && return
  33.         echo "F" && return
  34. }
  35.  
  36. function not {
  37.         [[ $1 == "T" ]] && echo "F" && return
  38.         echo "T" && return
  39. }
  40.  
  41. [[ $1 == source ]] && return
  42.  
  43. # Codegen
  44.  
  45. function mkcode {
  46.         tmp="$(mktemp)"
  47.  
  48.         expr_src="$(cat)"
  49.         vars="$(_uppercase <<< "$expr_src")"
  50.         core_expr="$(sed -e 's/(/$(/g; s/[A-Z]/$&/g;s/^$//; ' <<< "$expr_src")"
  51.  
  52.         {
  53.  
  54.         echo -e '#!/bin/bash\n. '$0' source'
  55.  
  56.         for var in $vars; do echo 'echo -n "'$var'; "'; done;
  57.  
  58.         [[ ! -z $vars ]] && echo 'echo'
  59.  
  60.         for var in $vars; do
  61.                 echo 'for '$var' in T F; do '
  62.         done
  63.  
  64.                 for _var in $vars; do
  65.                         echo 'echo -n "$'$_var'; "'
  66.                 done
  67.  
  68.                 echo 'echo "'$b'=>'$n' '$core_expr';"; '
  69.  
  70.         for var in $vars; do echo 'done; '; done
  71.  
  72.         } > $tmp
  73.  
  74.         bash $tmp
  75.         rm $tmp
  76. }
  77.  
  78. function _uppercase {
  79.         tr ' ' '\n' | grep -E '[A-Z]' | sed 's/.*\([A-Z]\).*/\1/' | sort | uniq | tr '\n' ' '
  80. }
  81.  
  82. function quit {
  83.  
  84.         [[ -e "$tmpd" ]] && rm -r "$tmpd"
  85.         exit 0
  86.  
  87. }
  88.  
  89. function main {
  90.  
  91.         tmpd="$(mktemp -d)"
  92.         cd $tmpd; touch disj conj impl req eq not
  93.  
  94.         echo "nato's logic evaluator (truth table listing) tool, "
  95.         echo "version: 0.1.2; "
  96.         echo
  97.         echo "commands: "
  98.         echo "  disj <p> <q>: disjunction of p and q. (or)"
  99.         echo "  conj <p> <q>: conjunction of p and q. (and)"
  100.         echo "  impl <p> <q>: p implies q. (condition)"
  101.         echo "   req <p> <q>: p require q. (n. condition)"
  102.         echo "    eq <p> <q>: p equals q."
  103.         echo "   not <p>    : not p."
  104.         echo
  105.         echo "example: (P ⊃ (~R · I)) will be written as: "
  106.         echo "(impl P (conj (not R) I))"
  107.         echo
  108.         echo "note that the outer parentheses are REQUIRED, "
  109.         echo "anything outside the paren will be treated as formating string. e.g.: "
  110.         echo "?> conjoining A and B is: (conj A B)"
  111.         echo "A; B; "
  112.         echo "T; T; => conjoining T and T is: T;"
  113.         echo "T; F; => conjoining T and F is: F;"
  114.         echo "F; T; => conjoining F and T is: F;"
  115.         echo "F; F; => conjoining F and F is: F;"
  116.         echo
  117.         echo "You can also evaluate multiple expressions in same time: e.g.:"
  118.         echo "?> (not A), (eq (not B) A)"
  119.         echo "A; B; "
  120.         echo "T; T; => F, F;"
  121.         echo "T; F; => F, T;"
  122.         echo "F; T; => T, T;"
  123.         echo "F; F; => T, F;"
  124.         echo
  125.  
  126.         while IFS="" read -r -e -d $'\n' -p "$flags$b?>$n " expr_; do
  127.                 history -s "$expr_"
  128.                 mkcode <<< "$expr_"
  129.         done
  130. }
  131.  
  132. main
  133. quit
  134.  

Reply to "Re: Logic evaluator (truth table listing)"

Here you can reply to the paste above

captcha