cli.lisp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. ;; cli.lisp -- Command line interface entry point and option parsing
  2. ;; Copyright (C) 2024 Alexander Rosenberg
  3. ;;
  4. ;; This program is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. (defpackage #:truth-table/cli
  17. (:use #:common-lisp #:truth-table/base
  18. #:truth-table/args)
  19. (:export #:toplevel #:main))
  20. (in-package :truth-table/cli)
  21. (defun eval-and-typeset-propositions (prop-strs &key (format "unicode")
  22. (implicit-and t)
  23. multi-char-names
  24. include-intermediate
  25. (include-vars t)
  26. pretty-print
  27. latin-truths)
  28. "Evaluate and then typeset PROP-STRS as a table, which is a list of
  29. proposition strings. For a description of the key parameters, see each of the
  30. functions involved in evaluating and typesetting."
  31. (loop with vars = '()
  32. for prop-str in prop-strs
  33. for (parsed-exp parsed-vars)
  34. = (multiple-value-list
  35. (parse-proposition-string prop-str
  36. :implicit-and implicit-and
  37. :multi-char-names multi-char-names))
  38. when parsed-exp
  39. collect parsed-exp into exps
  40. do (dolist (var parsed-vars)
  41. (pushnew var vars :test 'equal))
  42. finally
  43. (let ((table (create-combined-truth-table
  44. exps (nreverse vars)
  45. :include-intermediate include-intermediate
  46. :include-vars include-vars)))
  47. (return (typeset-table-to-format table format
  48. :pretty-print pretty-print
  49. :latin-truths latin-truths)))))
  50. (defun word-wrap-string (string &optional (cols 80))
  51. (with-output-to-string (str)
  52. (loop with word = ()
  53. with word-len = 0
  54. with cur-col = 0
  55. for char across string
  56. when (whitespace-p char) do
  57. (if (>= (+ cur-col word-len 1) cols)
  58. (progn
  59. (terpri str)
  60. (setq cur-col 0))
  61. (unless (zerop cur-col)
  62. (format str " ")
  63. (incf cur-col)))
  64. (format str "~{~c~}" (nreverse word))
  65. (setq word nil
  66. cur-col (+ cur-col word-len)
  67. word-len 0)
  68. else do
  69. (push char word)
  70. (incf word-len)
  71. finally
  72. (if (>= (+ cur-col word-len 1) cols)
  73. (terpri str)
  74. (format str " "))
  75. (format str "~{~c~}" (nreverse word)))))
  76. (defun ascii-string-p (str)
  77. "Return true if STR is only ASCII characters."
  78. (loop for char across str
  79. unless (<= (char-code char) 127)
  80. do (return-from ascii-string-p))
  81. t)
  82. (defun format-syntax-string (syntax-list &key ascii-only)
  83. "Format SYNTAX-LIST into a string suitable for printing in a table in
  84. `print-syntax-help'."
  85. (format nil "~{~a~^, ~}"
  86. (sort (copy-list
  87. (if ascii-only
  88. (remove-if-not 'ascii-string-p
  89. syntax-list)
  90. syntax-list))
  91. 'string<)))
  92. (defun print-syntax-help (ascii-only)
  93. "Print the syntax help message."
  94. (loop
  95. for ((sym (name . nicks) desc examples) . rest-desc)
  96. = *operator-descriptions* then rest-desc
  97. for ((_sym . syntax) . rest-st) = *operator-symbol-table* then rest-st
  98. for syntax-str = (format-syntax-string syntax :ascii-only ascii-only)
  99. while sym
  100. maximize (length name) into name-col-len
  101. maximize (length syntax-str) into syntax-col-len
  102. collect syntax-str into syntax-entries
  103. finally
  104. (setq name-col-len (max name-col-len (length "Operator"))
  105. syntax-col-len (max syntax-col-len (length "Syntax")))
  106. (with-draw-table (t (list name-col-len syntax-col-len)
  107. (if ascii-only
  108. *table-border-ascii-alist*
  109. *table-border-unicode-alist*)
  110. :padding 1 :align :left)
  111. (:row '("Operator" "Syntax"))
  112. (:seperator)
  113. (loop for (sym (name . nicks) desct) in *operator-descriptions*
  114. for syntax-str in syntax-entries do
  115. (:row (list name syntax-str)))))
  116. (terpri)
  117. (loop for (sym . syntax) in *operand-symbol-table*
  118. for name = (symbol-name sym)
  119. for syntax-str = (format-syntax-string syntax :ascii-only ascii-only)
  120. collect (string-downcase name) into names
  121. maximize (length name) into name-col-len
  122. collect syntax-str into syntax-strs
  123. maximize (length syntax-str) into syntax-col-len
  124. finally
  125. (setq name-col-len (max name-col-len (length "Operand"))
  126. syntax-col-len (max syntax-col-len (length "Syntax")))
  127. (with-draw-table (t (list name-col-len syntax-col-len)
  128. (if ascii-only
  129. *table-border-ascii-alist*
  130. *table-border-unicode-alist*)
  131. :padding 1 :align :left)
  132. (:row '("Operand" "Syntax"))
  133. (:seperator)
  134. (loop for name in names
  135. for syntax-str in syntax-strs
  136. do (:row (list name syntax-str)))))
  137. (format t "~%~a~%Example:~% abc|d = ~a~%"
  138. (word-wrap-string "Two operands next to each other is treated as an
  139. 'implicit and' (unless this feature is disabled).")
  140. (typeset-proposition '(or (and "a" "b" "c") "d")
  141. :lookup-table (if ascii-only
  142. *operator-ascii-lookup-alist*
  143. *operator-unicode-lookup-alist*))))
  144. (defparameter *command-line-spec*
  145. '((#\h "help" help nil "print this message, then exit")
  146. (nil "syntax-help" syntax-help nil "print a syntax help message, then exit")
  147. (#\f "format" format t
  148. "specify the output format (*unicode*, ascii, latex, or html)")
  149. (#\s "subexps" subexps nil "include sub-expressions in the output table")
  150. (#\n "no-vars" no-vars nil "do not include variables in the output table")
  151. (#\m "multi-char" multi-char nil "allow multi-character variable names")
  152. (#\i "no-implicit" no-implicit nil "do not use implicit 'and' operations")
  153. (#\p "pretty" pretty nil "pretty print latex, html, etc. output")
  154. (#\l "latin" latin nil "use the Latin T and F characters for truth values"))
  155. "Specification for `parse-command-line'. This is of the format:
  156. (short long symbol has-arg-p desc).")
  157. (defun main (argv)
  158. "The main entry point to the program. ARGV is the list of command line
  159. arguments."
  160. (let ((cmdline-error nil))
  161. (handler-bind
  162. (((or proposition-parse-error proposition-eval-error
  163. table-format-error)
  164. (lambda (c)
  165. (format *error-output* "error: ~a~%" c)
  166. (uiop:quit 1)))
  167. (command-line-error
  168. (lambda (c) ;; finish parsing command line before exiting
  169. (format *error-output* "error: ~a~%" c)
  170. (setq cmdline-error t)
  171. (continue))))
  172. (destructuring-bind ((&rest prop-strs) &rest opts)
  173. (parse-command-line *command-line-spec* argv)
  174. (cond
  175. ((option-value 'help opts)
  176. (print-usage t *command-line-spec* "truth-table"
  177. :general-args "<propositions...>")
  178. (uiop:quit (if cmdline-error 1 0)))
  179. ((and (not cmdline-error) ;; if option parsing failed, error out
  180. (option-value 'syntax-help opts))
  181. (let ((format (option-value 'format opts)))
  182. (cond
  183. ((or (not format)
  184. (equal format "unicode"))
  185. (print-syntax-help nil))
  186. ((equal format "ascii")
  187. (print-syntax-help t))
  188. (t
  189. (cerror "Exit without printing anything" 'command-line-error
  190. :message (format nil "The syntax help table is only ~
  191. available in ASCII or Unicode.")))))
  192. (uiop:quit))
  193. ((null prop-strs)
  194. (cerror *cli-parse-continue-string* 'no-input-error))
  195. (cmdline-error
  196. (format *error-output* "Try -h or --help for more information.~%")
  197. (uiop:quit 1))
  198. (t
  199. (let ((format (option-value 'format opts)))
  200. (when (or (not format) (zerop (length format)))
  201. (setq format "unicode"))
  202. (princ (eval-and-typeset-propositions
  203. prop-strs :format format
  204. :implicit-and (not (option-value 'no-implicit opts))
  205. :multi-char-names (option-value 'multi-char opts)
  206. :include-vars (not (option-value 'no-vars opts))
  207. :include-intermediate (option-value 'subexps opts)
  208. :pretty-print (option-value 'pretty opts)
  209. :latin-truths (option-value 'latin opts)))
  210. (terpri))))))))
  211. (defun toplevel ()
  212. "Top-level function to be passed to `save-lisp-and-die'."
  213. (handler-case
  214. (with-user-abort:with-user-abort
  215. (main (uiop:command-line-arguments)))
  216. (with-user-abort:user-abort ()
  217. (format *error-output* "Keyboard interrupt~%")
  218. (uiop:quit 1))))