Skip to content

Commit b9d1f4b

Browse files
committed
Improve output and implement infix-to-prefix
This allows truth-infix macros being written and now we can entry infix expressions! There is no precedence evaluation of operators yet, but this works fine.
1 parent dcd9a12 commit b9d1f4b

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

src/package.lisp

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
:prefix-to-infix
3333
:print-truth-table
3434
:truth
35+
:truth-infix
3536
:lookup-internal-operators
3637
:intern-symbol
3738
:main

src/parser.lisp

+8
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,11 @@
109109
(a (first-operand exp)))
110110
(cons op (list (prefix-to-infix a)))))
111111
(t (swap-operand-operator exp))))
112+
113+
114+
(defun infix-to-prefix (exp)
115+
(cond ((atom exp) exp)
116+
((null (cdr exp)) (infix-to-prefix (car exp)))
117+
(t (list (cadr exp)
118+
(infix-to-prefix (car exp))
119+
(infix-to-prefix (cddr exp))))))

src/truth-table.lisp

+27-18
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@
134134
(defun princ-n (string n)
135135
"Just print the STRING by N times"
136136
(dotimes (_ n) (princ string)))
137-
137+
(defun print-bar (spaces)
138+
(princ "+")
139+
(princ-n "-" (1- (reduce #'+ spaces)))
140+
(princ "+")
141+
(princ #\newline))
138142

139143
(defun print-truth-table (exp)
140144
"Given a EXP with prefixed notation generate
@@ -147,31 +151,36 @@
147151
for p = (princ-to-string x)
148152
collect (concatenate 'string " " p " |")))
149153
(spaces (mapcar #'length printable-header)))
150-
(princ-n "-" (reduce #'+ spaces))
151-
(princ #\newline)
154+
(print-bar spaces)
155+
(princ "|")
152156
(loop for exp in printable-header do (princ exp))
153157
(princ #\newline)
154-
(princ-n "-" (reduce #'+ spaces))
155-
(princ #\newline)
158+
(print-bar spaces)
156159
(loop for n-value from 1 below n-values
157-
do (progn (loop for n-exp from 0 below (length header)
158-
do (let* ((space (nth n-exp spaces))
159-
(half-space (floor (- space 2) 2))
160-
(val (nth n-value (nth n-exp truth-table))))
161-
(princ-n " " half-space)
162-
(princ val)
163-
(princ-n " " half-space)
164-
(if (oddp space)
165-
(princ " |")
166-
(princ "|"))))
167-
(princ #\newline)))
168-
(princ-n "-" (reduce #'+ spaces))
169-
(princ #\newline)))
160+
do (progn
161+
(princ "|")
162+
(loop for n-exp from 0 below (length header)
163+
do (let* ((space (nth n-exp spaces))
164+
(half-space (floor (- space 2) 2))
165+
(val (nth n-value (nth n-exp truth-table))))
166+
(princ-n " " half-space)
167+
(princ val)
168+
(princ-n " " half-space)
169+
(if (oddp space)
170+
(princ " |")
171+
(princ "|"))))
172+
(princ #\newline)))
173+
(print-bar spaces)))
170174

171175
(defmacro truth (exp)
172176
"A easy way to generate a truth table"
173177
`(print-truth-table (quote ,exp)))
174178

179+
(defmacro truth-infix (exp)
180+
"A easy and infix way of EXP generate a truth table.
181+
Ex.: (truth-infix (p ^ q)) "
182+
`(print-truth-table (infix-to-prefix (quote , exp))))
183+
175184
(defun main ()
176185
(truth (=> (v p (~ q)) (=> p q)))
177186
(truth (^ p q))

0 commit comments

Comments
 (0)