Arithmetic evaluation: Difference between revisions

CL bugfixes and better error handling
(add mildly cheeky E example)
(CL bugfixes and better error handling)
Line 858:
(defun group-parentheses (tokens &optional (delimited nil))
(do ((new-tokens '()))
((endp tokens) (values (nreverse new-tokens) '()))
(when delimited
(cerror "Insert it." "Expected right parenthesis."))
(return (values (nreverse new-tokens) tokens'()))
(let ((token (pop tokens)))
(case token
Line 867 ⟶ 870:
tokens remaining-tokens)))
((:rparen)
(assert delimitedif ()not "Unexpected right parenthesis."delimited)
(cerror "Ignore it." "Unexpected right parenthesis.")
(return (values (nreverse new-tokens) tokens)))
(return (values (nreverse new-tokens) tokens))))
(otherwise
(push token new-tokens))))))
Line 874 ⟶ 878:
(defun group-operations (expression)
(flet ((gop (exp) (group-operations exp)))
(if (or (eql (car expression) :integer) ; <integer>expression
(destructuring-bind (A &optional (endpx (cdrnil expression)xp) B (y nil yp) C &rest ; (e1others)
(endp (cdddr expression))) ; (e1 op e2)
expression (cond
((not xp) (gop A))
(destructuring-bind (A x B y C &rest others) expression
(let ((Anot (gop A)yp) (Blist (gop B)A) (Cx (gop CB)))
(t (iflet (and(a (findgop xA)) (B (gop B)) (C #(:addgop :subtractC)))
(if (and (find yx #(:multiplyadd :divide)subtract))
(gop (list* A x (list B (find y C)#(:multiply others:divide)))
(gop (list* (list A x (list B) y C) others))))))))
(gop (list* (list A x B) y C others))))))))))
 
(defun evaluate-expression (expression)
Anonymous user