Logic evaluator (truth table listing)

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

Replies to Logic evaluator (truth table listing) rss

Title Name Language When
Re: Logic evaluator (truth table listing) NAT bash 5 Years ago.

Reply to "Logic evaluator (truth table listing)"

Here you can reply to the paste above

captcha