|
| 1 | +(defpackage lisp-inference/web |
| 2 | + (:use #:cl |
| 3 | + #:weblocks-ui/form |
| 4 | + #:weblocks/html) |
| 5 | + (:import-from #:weblocks/widget |
| 6 | + #:render |
| 7 | + #:update |
| 8 | + #:defwidget) |
| 9 | + (:import-from #:weblocks/actions |
| 10 | + #:make-js-action) |
| 11 | + (:import-from #:weblocks/app |
| 12 | + #:defapp) |
| 13 | + (:export #:main |
| 14 | + #:*propostion* |
| 15 | + #:*port*) |
| 16 | + (:nicknames #:webapp)) |
| 17 | + |
| 18 | +(in-package lisp-inference/web) |
| 19 | + |
| 20 | +(defvar *proposition* '(P => Q) "Default proposition") |
| 21 | +(defvar *port* (find-port:find-port)) |
| 22 | + |
| 23 | +(defapp truth-table |
| 24 | + :prefix "/" |
| 25 | + :description "Lisp Inference Truth Table") |
| 26 | + |
| 27 | +(defwidget table () |
| 28 | + ((prop |
| 29 | + :initarg :prop |
| 30 | + :accessor prop) |
| 31 | + (truth |
| 32 | + :initarg :truth |
| 33 | + :initform nil |
| 34 | + :accessor truth))) |
| 35 | + |
| 36 | +(defun truth-table (exp) |
| 37 | + (with-output-to-string (s) |
| 38 | + (let ((inference:*output-stream* s)) |
| 39 | + (inference:print-truth-table (inference:infix-to-prefix exp))))) |
| 40 | + |
| 41 | +(defun create-table (exp) |
| 42 | + (make-instance 'table |
| 43 | + :prop (format nil "~a" exp) |
| 44 | + :truth (truth-table exp))) |
| 45 | + |
| 46 | +(defun update-table (table exp) |
| 47 | + (setf (prop table) (format nil "~a" exp)) |
| 48 | + (setf (truth table) (truth-table exp))) |
| 49 | + |
| 50 | +(defgeneric update-proposition (table exp)) |
| 51 | + |
| 52 | +(defmethod update-proposition (table (exp list)) |
| 53 | + (update-table table exp) |
| 54 | + (update table)) |
| 55 | + |
| 56 | +(defmethod update-proposition (table (string string)) |
| 57 | + (update-proposition |
| 58 | + table |
| 59 | + (mapcar (lambda (x) |
| 60 | + (intern (string-upcase x))) |
| 61 | + (str:words (string-trim '(#\( #\)) string))))) |
| 62 | + |
| 63 | +(defmethod render ((table table)) |
| 64 | + (with-html |
| 65 | + (:h1 "Lisp Inference Truth Table System") |
| 66 | + (with-html-form (:POST (lambda (&key prop &allow-other-keys) |
| 67 | + (update-proposition table prop))) |
| 68 | + (:input :type "text" |
| 69 | + :name "prop" |
| 70 | + :placeholder (prop table)) |
| 71 | + (:input :type "submit" |
| 72 | + :value "Eval")) |
| 73 | + (:pre (truth table)))) |
| 74 | + |
| 75 | +(defmethod render ((string string)) |
| 76 | + (with-html |
| 77 | + (:pre string))) |
| 78 | + |
| 79 | +(defmethod weblocks/session:init ((app truth-table)) |
| 80 | + (declare (ignorable app)) |
| 81 | + (create-table *proposition*)) |
| 82 | + |
| 83 | +(defun main (&optional (port *port*)) |
| 84 | + (weblocks/debug:on) |
| 85 | + (weblocks/server:stop) |
| 86 | + (weblocks/server:start :port port)) |
0 commit comments