Skip to content

Commit d7dc957

Browse files
authored
Merge pull request #12 from ryukinix/infix-to-prefix-fix
Make infix-to-prefix works with unary operations
2 parents d4c343a + 0b6ba4f commit d7dc957

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

lisp-inference.asd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
:description "An Inference Engine using Propositional Calculus"
99
:author "Manoel Vilela <[email protected]>"
1010
:license "BSD"
11-
:version "0.1.0"
11+
:version "0.2.0"
1212
:serial t
1313
:pathname "src"
1414
:components ((:file "package")
@@ -25,7 +25,7 @@
2525
:description "Lisp Inference Test Suit"
2626
:author "Manoel Vilela <[email protected]>"
2727
:license "BSD"
28-
:version "0.1.0"
28+
:version "0.2.0"
2929
:serial t
3030
:pathname "t"
3131
:depends-on (:lisp-inference :prove)

src/package.lisp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#:make-biconditional
4141
#:*valid-operators*
4242
#:prefix-to-infix
43+
#:infix-to-prefix
4344
#:print-truth-table ;; truth-table.lisp
4445
#:eval-expression
4546
#:equal-expression

src/parser.lisp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,19 @@
112112

113113

114114
(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))))))
115+
"INFIX-TO-PREFIX translate a infix expression to a prefix expression.
116+
117+
This function assumes that exp it is not ambiguous.
118+
In that case, use a completly 'parenthesed' expression
119+
120+
Returns a new prefixed list.
121+
"
122+
(cond ((atom exp) exp)
123+
((and (listp exp)
124+
(= 2 (length exp)))
125+
(list (car exp)
126+
(infix-to-prefix (cdr exp))))
127+
((null (cdr exp)) (infix-to-prefix (car exp)))
128+
(t (list (cadr exp)
129+
(infix-to-prefix (car exp))
130+
(infix-to-prefix (cddr exp))))))

src/truth-table.lisp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
(in-package :lisp-inference)
55

6+
(defparameter *truth-string* "T")
7+
(defparameter *false-string* "F")
8+
69
(defun propositionp (symbol)
710
"Check if the given SYMBOL can be a proposition (letters)"
811
(and (atom symbol)
@@ -106,8 +109,8 @@
106109

107110
(defun pretty-values (v)
108111
(if (not (null v))
109-
"T"
110-
"F"))
112+
*truth-string*
113+
*false-string*))
111114

112115
(defun prepare-table (evaluated-cases)
113116
"Get the evaluated cases after EVAL-OPERATIONS
@@ -204,10 +207,10 @@ a tautology."
204207

205208

206209
(defun main ()
207-
(format t "Example of usage: (^ p q)~%Operators: ~a ~%" *valid-operators*)
210+
(format t "Example of usage: (p ^ q)~%Operators: ~a ~%" *valid-operators*)
208211
(handler-case (loop do (princ "TRUTH-TABLE> ")
209212
do (force-output)
210-
do (print-truth-table (read)))
213+
do (print-truth-table (infix-to-prefix (read))))
211214
(end-of-file () )
212215
#+sbcl (sb-sys:interactive-interrupt () nil))
213216

t/test.lisp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,17 @@
102102
'(p))
103103
"EQUAL EXPRESSION 2")
104104

105+
(diag "== Infix Parsing")
106+
107+
(is (infix-to-prefix '(~ (p v q)))
108+
'(~ (v p q)))
109+
110+
(is (infix-to-prefix '(p => q))
111+
'(=> p q))
112+
113+
(is (infix-to-prefix '((p v q) <=> ((~ p) ^ (~ q))))
114+
'(<=> (v p q)
115+
(^ (~ p)
116+
(~ q))))
117+
105118
(finalize)

0 commit comments

Comments
 (0)