Skip to content

Commit 1d7796d

Browse files
committed
Make infix-to-prefix works with unary operations
Add new cond rule to treat specially special unary expressions like (~ p).
1 parent d4c343a commit 1d7796d

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

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))))))

0 commit comments

Comments
 (0)