Function: calculator-reduce-stack-once
calculator-reduce-stack-once is a byte-compiled function defined in
calculator.el.gz.
Signature
(calculator-reduce-stack-once PREC)
Documentation
Worker for calculator-reduce-stack.
Source Code
;; Defined in /usr/src/emacs/lisp/calculator.el.gz
;;;---------------------------------------------------------------------
;;; Stack computations
(defun calculator-reduce-stack-once (prec)
"Worker for `calculator-reduce-stack'."
(cl-flet ((check (ar op) (and (listp op)
(<= prec (calculator-op-prec op))
(= ar (calculator-op-arity op))))
(call (op &rest args) (apply 'calculator-funcall
(nth 2 op) args)))
(pcase calculator-stack
;; reduce "... ( x )" --> "... x"
(`((,_ \) . ,_) ,(and X (pred numberp)) (,_ \( . ,_) . ,rest)
(cons X rest))
;; reduce "... x op y" --> "... r", r is the result
(`(,(and Y (pred numberp))
,(and O (pred (check 2)))
,(and X (pred numberp))
. ,rest)
(cons (call O X Y) rest))
;; reduce "... op x" --> "... r" for prefix op
(`(,(and X (pred numberp)) ,(and O (pred (check -1))) . ,rest)
(cons (call O X) rest))
;; reduce "... x op" --> "... r" for postfix op
(`(,(and O (pred (check +1))) ,(and X (pred numberp)) . ,rest)
(cons (call O X) rest))
;; reduce "... op" --> "... r" for 0-ary op
(`(,(and O (pred (check 0))) . ,rest)
(cons (call O) rest))
;; reduce "... y x" --> "... x"
;; (needed for 0-ary ops: replace current number with result)
(`(,(and X (pred numberp)) ,(and _Y (pred numberp)) . ,rest)
(cons X rest))
(_ nil)))) ; nil = done