1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc.el.
32 ;; Declare functions which are defined elsewhere.
33 (declare-function calc-refresh-evaltos
"calc-ext" (&optional which-var
))
34 (declare-function calc-execute-kbd-macro
"calc-prog" (mac arg
&rest prefix
))
35 (declare-function math-is-true
"calc-ext" (expr))
36 (declare-function calc-explain-why
"calc-stuff" (why &optional more
))
37 (declare-function calc-alg-edit
"calc-yank" (str))
38 (declare-function math-composite-inequalities
"calc-prog" (x op
))
39 (declare-function math-flatten-lands
"calc-rewr" (expr))
40 (declare-function math-multi-subst
"calc-map" (expr olds news
))
41 (declare-function calcFunc-vmatches
"calc-rewr" (expr pat
))
42 (declare-function math-simplify
"calc-alg" (top-expr))
43 (declare-function math-known-matrixp
"calc-arith" (a))
44 (declare-function math-parse-fortran-subscr
"calc-lang" (sym args
))
45 (declare-function math-to-radians-2
"calc-math" (a))
46 (declare-function math-read-string
"calc-ext" ())
47 (declare-function math-read-brackets
"calc-vec" (space-sep math-rb-close
))
48 (declare-function math-read-angle-brackets
"calc-forms" ())
49 (declare-function math-to-percentsigns
"calccomp" (x))
51 (defvar calc-quick-calc-history nil
52 "The history list for quick-calc.")
55 (defun calc-do-quick-calc ()
58 (if (eq major-mode
'calc-mode
)
59 (calc-algebraic-entry t
)
63 (let* ((calc-command-flags nil
)
64 (calc-dollar-values calc-quick-prev-results
)
66 (enable-recursive-minibuffers t
)
67 (calc-language (if (memq calc-language
'(nil big
))
69 (entry (calc-do-alg-entry "" "Quick calc: " t
'calc-quick-calc-history
))
70 (alg-exp (mapcar 'math-evaluate-expr entry
)))
71 (when (and (= (length alg-exp
) 1)
72 (eq (car-safe (car alg-exp
)) 'calcFunc-assign
)
73 (= (length (car alg-exp
)) 3)
74 (eq (car-safe (nth 1 (car alg-exp
))) 'var
))
75 (set (nth 2 (nth 1 (car alg-exp
))) (nth 2 (car alg-exp
)))
76 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp
))))
77 (setq alg-exp
(list (nth 2 (car alg-exp
)))))
78 (setq calc-quick-prev-results alg-exp
79 buf
(mapconcat (function (lambda (x)
80 (math-format-value x
1000)))
84 (if (and (= (length alg-exp
) 1)
85 (memq (car-safe (car alg-exp
)) '(nil bigpos bigneg
))
87 (= calc-number-radix
10))
88 (setq buf
(concat buf
" ("
89 (let ((calc-number-radix 16))
90 (math-format-value (car alg-exp
) 1000))
92 (let ((calc-number-radix 8))
93 (math-format-value (car alg-exp
) 1000))
95 (let ((calc-number-radix 2))
96 (math-format-value (car alg-exp
) 1000))
97 (if (and (integerp (car alg-exp
))
99 (< (car alg-exp
) 127))
100 (format ", \"%c\"" (car alg-exp
))
103 (if (and (< (length buf
) (frame-width)) (= (length entry
) 1)
104 (featurep 'calc-ext
))
105 (let ((long (concat (math-format-value (car entry
) 1000)
107 (if (<= (length long
) (- (frame-width) 8))
110 (message "Result: %s" buf
)))
111 (if (eq last-command-event
10)
113 (kill-new shortbuf
)))))
116 (defun calc-do-calc-eval (str separator args
)
122 ((and (consp str
) (not (symbolp (car str
))))
123 (let ((calc-language nil
)
124 (math-expr-opers (math-standard-ops))
125 (calc-internal-prec 12)
127 (calc-symbolic-mode nil
)
128 (calc-matrix-mode nil
)
129 (calc-angle-mode 'deg
)
130 (calc-number-radix 10)
131 (calc-twos-complement-mode nil
)
132 (calc-leading-zeros nil
)
133 (calc-group-digits nil
)
134 (calc-point-char ".")
135 (calc-frac-format '(":" nil
))
136 (calc-prefer-frac nil
)
137 (calc-hms-format "%s@ %s' %s\"")
138 (calc-date-format '((H ":" mm C SS pp
" ")
139 Www
" " Mmm
" " D
", " YYYY
))
140 (calc-float-format '(float 0))
141 (calc-full-float-format '(float 0))
142 (calc-complex-format nil
)
143 (calc-matrix-just nil
)
144 (calc-full-vectors t
)
145 (calc-break-vectors nil
)
146 (calc-vector-commas ",")
147 (calc-vector-brackets "[]")
148 (calc-matrix-brackets '(R O
))
149 (calc-complex-mode 'cplx
)
150 (calc-infinite-mode nil
)
151 (calc-display-strings nil
)
152 (calc-simplify-mode nil
)
153 (calc-display-working-message 'lots
)
156 (set (car strp
) (nth 1 strp
))
157 (setq strp
(cdr (cdr strp
))))
158 (calc-do-calc-eval (car str
) separator args
)))
159 ((eq separator
'eval
)
161 ((eq separator
'macro
)
163 (let* ((calc-buffer (current-buffer))
164 (calc-window (get-buffer-window calc-buffer
))
165 (save-window (selected-window)))
169 (select-window calc-window
)
170 (calc-execute-kbd-macro str nil
(car args
)))
171 (and (window-point save-window
)
172 (select-window save-window
)))
173 (save-window-excursion
174 (select-window (get-largest-window))
175 (switch-to-buffer calc-buffer
)
176 (calc-execute-kbd-macro str nil
(car args
)))))
179 (or (not (integerp str
))
181 (calc-pop (min str
(calc-stack-size))))
186 (<= str
(calc-stack-size))
187 (math-format-value (calc-top-n str
(car args
)) 1000)))
188 ((eq separator
'rawtop
)
191 (<= str
(calc-stack-size))
192 (calc-top-n str
(car args
))))
194 (let* ((calc-command-flags nil
)
196 (calc-language (if (memq calc-language
'(nil big
))
197 'flat calc-language
))
198 (calc-dollar-values (mapcar
203 (setq x
(math-read-exprs x
))
213 (res (if (stringp str
)
214 (math-read-exprs str
)
217 (if (eq (car res
) 'error
)
218 (calc-eval-error (cdr res
))
219 (setq res
(mapcar 'calc-normalize res
))
220 (and (memq 'clear-message calc-command-flags
)
222 (cond ((eq separator
'pred
)
224 (if (= (length res
) 1)
225 (math-is-true (car res
))
226 (calc-eval-error '(0 "Single value expected"))))
228 (if (= (length res
) 1)
230 (calc-eval-error '(0 "Single value expected"))))
231 ((eq separator
'list
)
233 ((memq separator
'(num rawnum
))
234 (if (= (length res
) 1)
235 (if (math-constp (car res
))
236 (if (eq separator
'num
)
237 (math-format-value (car res
) 1000)
242 (calc-explain-why (car calc-next-why
))
243 "Number expected"))))
244 (calc-eval-error '(0 "Single value expected"))))
245 ((eq separator
'push
)
249 (setq buf
(concat buf
250 (and buf
(or separator
", "))
251 (math-format-value (car res
) 1000))
255 (defvar calc-eval-error nil
256 "Determines how calc handles errors.
257 If nil, return a list containing the character position of error.
258 STRING means return error message as string rather than list.
259 The value t means abort and give an error message.")
261 (defun calc-eval-error (msg)
263 (if (eq calc-eval-error
'string
)
265 (error "%s" (nth 1 msg
)))
269 ;;;; Reading an expression in algebraic form.
272 (defun calc-auto-algebraic-entry (&optional prefix
)
274 (calc-algebraic-entry prefix t
))
277 (defun calc-algebraic-entry (&optional prefix auto
)
280 (let ((calc-language (if prefix nil calc-language
))
281 (math-expr-opers (if prefix
(math-standard-ops) (math-expr-ops))))
282 (calc-alg-entry (and auto
(char-to-string last-command-event
))))))
284 (defvar calc-alg-entry-history nil
285 "History for algebraic entry.")
288 (defun calc-alg-entry (&optional initial prompt
)
289 (let* ((sel-mode nil
)
290 (calc-dollar-values (mapcar 'calc-get-stack-element
291 (nthcdr calc-stack-top calc-stack
)))
294 (alg-exp (calc-do-alg-entry initial prompt t
'calc-alg-entry-history
)))
295 (if (stringp alg-exp
)
298 (calc-alg-edit alg-exp
))
299 (let* ((calc-simplify-mode (if (eq last-command-event ?\C-j
)
302 (nvals (mapcar 'calc-normalize alg-exp
)))
304 (calc-record (if (featurep 'calc-ext
) (car alg-exp
) (car nvals
))
306 (calc-pop-push-record-list calc-dollar-used
307 (and (not (equal (car alg-exp
)
312 (setq alg-exp
(cdr alg-exp
)
314 calc-dollar-used
0)))
315 (calc-handle-whys))))
317 (defvar calc-alg-ent-map
318 (let ((map (make-sparse-keymap)))
319 (set-keymap-parent map minibuffer-local-map
)
320 (define-key map
"'" 'calcAlg-previous
)
321 (define-key map
"`" 'calcAlg-edit
)
322 (define-key map
"\C-m" 'calcAlg-enter
)
323 (define-key map
"\C-j" 'calcAlg-enter
)
325 "The keymap used for algebraic entry.")
327 (defvar calc-alg-ent-esc-map
328 (let ((map (make-keymap))
330 (set-keymap-parent map esc-map
)
332 (define-key map
(vector i
) 'calcAlg-escape
)
335 "The keymap used for escapes in algebraic entry.")
337 (defvar calc-alg-exp
)
340 (defun calc-do-alg-entry (&optional initial prompt no-normalize history
)
341 (let* ((calc-buffer (current-buffer))
342 (blink-matching-check-function 'calcAlg-blink-matching-check
)
343 (calc-alg-exp 'error
))
344 (define-key calc-alg-ent-map
"\e" nil
)
345 (if (eq calc-algebraic-mode
'total
)
346 (define-key calc-alg-ent-map
"\e" calc-alg-ent-esc-map
)
347 (define-key calc-alg-ent-map
"\e+" 'calcAlg-plus-minus
)
348 (define-key calc-alg-ent-map
"\em" 'calcAlg-mod
)
349 (define-key calc-alg-ent-map
"\e=" 'calcAlg-equals
)
350 (define-key calc-alg-ent-map
"\e\r" 'calcAlg-equals
)
351 (define-key calc-alg-ent-map
"\ep" 'previous-history-element
)
352 (define-key calc-alg-ent-map
"\en" 'next-history-element
)
353 (define-key calc-alg-ent-map
"\e%" 'self-insert-command
))
354 (setq calc-aborted-prefix nil
)
355 (let ((buf (read-from-minibuffer (or prompt
"Algebraic: ")
357 calc-alg-ent-map nil history
)))
358 (when (eq calc-alg-exp
'error
)
359 (when (eq (car-safe (setq calc-alg-exp
(math-read-exprs buf
))) 'error
)
360 (setq calc-alg-exp nil
)))
361 (setq calc-aborted-prefix
"alg'")
363 (and calc-alg-exp
(setq calc-alg-exp
(mapcar 'calc-normalize calc-alg-exp
))))
366 (defun calcAlg-plus-minus ()
368 (if (calc-minibuffer-contains ".* \\'")
372 (defun calcAlg-mod ()
374 (if (not (calc-minibuffer-contains ".* \\'"))
376 (if (calc-minibuffer-contains ".* mod +\\'")
377 (if calc-previous-modulo
378 (insert (math-format-flat-expr calc-previous-modulo
0))
382 (defun calcAlg-previous ()
384 (if (calc-minibuffer-contains "\\'")
385 (previous-history-element 1)
388 (defun calcAlg-equals ()
392 (if (consp calc-alg-exp
)
393 (progn (setq prefix-arg
(length calc-alg-exp
))
394 (calc-unread-command ?
=)))))
396 (defun calcAlg-escape ()
398 (calc-unread-command)
401 (use-local-map calc-mode-map
))
404 (defvar calc-plain-entry nil
)
405 (defun calcAlg-edit ()
407 (if (or (not calc-plain-entry
)
408 (calc-minibuffer-contains
409 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
411 (setq calc-alg-exp
(minibuffer-contents))
416 (defun calcAlg-enter ()
418 (let* ((str (minibuffer-contents))
419 (exp (and (> (length str
) 0)
420 (with-current-buffer calc-buffer
421 (math-read-exprs str
)))))
422 (if (eq (car-safe exp
) 'error
)
424 (goto-char (minibuffer-prompt-end))
425 (forward-char (nth 1 exp
))
427 (calc-temp-minibuffer-message
428 (concat " [" (or (nth 2 exp
) "Error") "]"))
429 (calc-clear-unread-commands))
430 (setq calc-alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
435 (defun calcAlg-blink-matching-check (leftpt rightpt
)
436 (let ((rightchar (char-before rightpt
))
437 (leftchar (if leftpt
(char-after leftpt
))))
439 (or (and (= rightchar ?\
))
441 (and (= rightchar ?\
])
445 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
446 ;; [2..5) perfectly valid!
448 (blink-matching-check-mismatch leftpt rightpt
))))
451 (defun calc-alg-digit-entry ()
453 (cond ((eq last-command-event ?e
)
454 (if (> calc-number-radix
14) (format "%d.^" calc-number-radix
) "1e"))
455 ((eq last-command-event ?
#) (format "%d#" calc-number-radix
))
456 ((eq last-command-event ?_
) "-")
457 ((eq last-command-event ?
@) "0@ ")
458 (t (char-to-string last-command-event
)))))
460 ;; The variable calc-digit-value is initially declared in calc.el,
461 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
462 (defvar calc-digit-value
)
465 (defun calcDigit-algebraic ()
467 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
469 (setq calc-digit-value
(minibuffer-contents))
473 (defun calcDigit-edit ()
475 (calc-unread-command)
476 (setq calc-digit-value
(minibuffer-contents))
480 ;;; Algebraic expression parsing. [Public]
482 (defvar math-read-replacement-list
484 ("±" "+/-") ; plus or minus
485 ("×" "*") ; multiplication sign
486 ("÷" ":") ; division sign
487 ("−" "-") ; subtraction sign
488 ("∕" "/") ; division sign
489 ("∗" "*") ; asterisk multiplication
490 ("∞" "inf") ; infinity symbol
545 "A list whose elements (old new) indicate replacements to make
546 in Calc algebraic input.")
548 (defvar math-read-superscripts
549 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
550 "A string consisting of the superscripts allowed by Calc.")
552 (defvar math-read-subscripts
553 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
554 "A string consisting of the subscripts allowed by Calc.")
557 (defun math-read-preprocess-string (str)
558 "Replace some substrings of STR by Calc equivalents."
560 (replace-regexp-in-string (concat "[" math-read-superscripts
"]+")
563 (replace-regexp-in-string (concat "[" math-read-subscripts
"]+")
565 (let ((rep-list math-read-replacement-list
))
568 (replace-regexp-in-string (nth 0 (car rep-list
))
569 (nth 1 (car rep-list
)) str
))
570 (setq rep-list
(cdr rep-list
))))
573 ;; The next few variables are local to math-read-exprs (and math-read-expr
574 ;; in calc-ext.el), but are set in functions they call.
576 (defvar math-exp-pos
)
577 (defvar math-exp-str
)
578 (defvar math-exp-old-pos
)
579 (defvar math-exp-token
)
580 (defvar math-exp-keep-spaces
)
581 (defvar math-expr-data
)
584 (defun math-read-exprs (math-exp-str)
585 (let ((math-exp-pos 0)
587 (math-exp-keep-spaces nil
)
588 math-exp-token math-expr-data
)
589 (setq math-exp-str
(math-read-preprocess-string math-exp-str
))
590 (if (memq calc-language calc-lang-allow-percentsigns
)
591 (setq math-exp-str
(math-remove-percentsigns math-exp-str
)))
592 (if calc-language-input-filter
593 (setq math-exp-str
(funcall calc-language-input-filter math-exp-str
)))
594 (while (setq math-exp-token
595 (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str
))
597 (concat (substring math-exp-str
0 math-exp-token
) "\\dots"
598 (substring math-exp-str
(+ math-exp-token
2)))))
599 (math-build-parse-table)
601 (let ((val (catch 'syntax
(math-read-expr-list))))
603 (list 'error math-exp-old-pos val
)
604 (if (equal math-exp-token
'end
)
606 (list 'error math-exp-old-pos
"Syntax error"))))))
609 (defun math-read-expr-list ()
610 (let* ((math-exp-keep-spaces nil
)
611 (val (list (math-read-expr-level 0)))
613 (while (equal math-expr-data
",")
615 (let ((rest (list (math-read-expr-level 0))))
620 (defvar calc-user-parse-table nil
)
621 (defvar calc-last-main-parse-table nil
)
622 (defvar calc-last-user-lang-parse-table nil
)
623 (defvar calc-last-lang-parse-table nil
)
624 (defvar calc-user-tokens nil
)
625 (defvar calc-user-token-chars nil
)
627 (defvar math-toks nil
628 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
631 (defun math-build-parse-table ()
632 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
633 (ltab (cdr (assq calc-language calc-user-parse-tables
)))
634 (lltab (get calc-language
'math-parse-table
)))
635 (or (and (eq mtab calc-last-main-parse-table
)
636 (eq ltab calc-last-user-lang-parse-table
)
637 (eq lltab calc-last-lang-parse-table
))
638 (let ((p (append mtab ltab lltab
))
640 (setq calc-user-parse-table p
)
641 (setq calc-user-token-chars nil
)
643 (math-find-user-tokens (car (car p
)))
645 (setq calc-user-tokens
(mapconcat 'identity
646 (sort (mapcar 'car math-toks
)
647 (function (lambda (x y
)
651 calc-last-main-parse-table mtab
652 calc-last-user-lang-parse-table ltab
653 calc-last-lang-parse-table lltab
)))))
656 (defun math-find-user-tokens (p)
658 (cond ((and (stringp (car p
))
659 (or (> (length (car p
)) 1) (equal (car p
) "$")
660 (equal (car p
) "\""))
661 (string-match "[^a-zA-Zα-ωΑ-Ω0-9]" (car p
)))
662 (let ((s (regexp-quote (car p
))))
663 (if (string-match "\\`[a-zA-Zα-ωΑ-Ω0-9]" s
)
664 (setq s
(concat "\\<" s
)))
665 (if (string-match "[a-zA-Zα-ωΑ-Ω0-9]\\'" s
)
666 (setq s
(concat s
"\\>")))
667 (or (assoc s math-toks
)
669 (setq math-toks
(cons (list s
) math-toks
))
670 (or (memq (aref (car p
) 0) calc-user-token-chars
)
671 (setq calc-user-token-chars
672 (cons (aref (car p
) 0)
673 calc-user-token-chars
)))))))
675 (math-find-user-tokens (nth 1 (car p
)))
676 (or (eq (car (car p
)) '\?)
677 (math-find-user-tokens (nth 2 (car p
))))))
681 (defun math-read-token ()
682 (if (>= math-exp-pos
(length math-exp-str
))
683 (setq math-exp-old-pos math-exp-pos
685 math-expr-data
"\000")
687 (ch (aref math-exp-str math-exp-pos
)))
688 (setq math-exp-old-pos math-exp-pos
)
689 (cond ((memq ch
'(32 10 9))
690 (setq math-exp-pos
(1+ math-exp-pos
))
691 (if math-exp-keep-spaces
692 (setq math-exp-token
'space
695 ((and (memq ch calc-user-token-chars
)
696 (let ((case-fold-search nil
))
698 calc-user-tokens math-exp-str math-exp-pos
)
700 (setq math-exp-token
'punc
701 math-expr-data
(math-match-substring math-exp-str
0)
702 math-exp-pos
(match-end 0)))
703 ((or (and (>= ch ?a
) (<= ch ?z
))
704 (and (>= ch ?A
) (<= ch ?Z
))
705 (and (>= ch ?α
) (<= ch ?ω
))
706 (and (>= ch ?Α
) (<= ch ?Ω
)))
709 ((and (memq calc-language calc-lang-allow-underscores
)
710 (memq calc-language calc-lang-allow-percentsigns
))
711 "[a-zA-Zα-ωΑ-Ω0-9_'#]*")
712 ((memq calc-language calc-lang-allow-underscores
)
713 "[a-zA-Zα-ωΑ-Ω0-9_#]*")
714 (t "[a-zA-Zα-ωΑ-Ω0-9'#]*"))
715 math-exp-str math-exp-pos
)
716 (setq math-exp-token
'symbol
717 math-exp-pos
(match-end 0)
718 math-expr-data
(math-restore-dashes
719 (math-match-substring math-exp-str
0)))
720 (if (setq adfn
(get calc-language
'math-lang-adjust-words
))
722 ((or (and (>= ch ?
0) (<= ch ?
9))
724 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos
)
727 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos
)
729 (or (eq math-exp-pos
0)
730 (and (not (memq calc-language
731 calc-lang-allow-underscores
))
732 (eq (string-match "[^])}\"a-zA-Zα-ωΑ-Ω0-9'$]_"
733 math-exp-str
(1- math-exp-pos
))
734 (1- math-exp-pos
))))))
735 (or (and (memq calc-language calc-lang-c-type-hex
)
736 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos
))
737 (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\(0*\\([2-9]\\|1[0-4]\\)\\(#[#]?\\|\\^\\^\\)[0-9a-dA-D.]+[eE][-+_]?[0-9]+\\|0*\\([2-9]\\|[0-2][0-9]\\|3[0-6]\\)\\(#[#]?\\|\\^\\^\\)[0-9a-zA-Zα-ωΑ-Ω:.]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?"
738 math-exp-str math-exp-pos
))
739 (setq math-exp-token
'number
740 math-expr-data
(math-match-substring math-exp-str
0)
741 math-exp-pos
(match-end 0)))
743 (assq ch
(get calc-language
'math-lang-read-symbol
)))
747 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
749 (setq math-expr-data
(- (string-to-number (math-match-substring
751 (string-match "\\$+" math-exp-str math-exp-pos
)
752 (setq math-expr-data
(- (match-end 0) (match-beginning 0))))
753 (setq math-exp-token
'dollar
754 math-exp-pos
(match-end 0)))
756 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
758 (setq math-expr-data
(string-to-number
759 (math-match-substring math-exp-str
1))
760 math-exp-pos
(match-end 0))
761 (setq math-expr-data
1
762 math-exp-pos
(1+ math-exp-pos
)))
763 (setq math-exp-token
'hash
))
764 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
765 math-exp-str math-exp-pos
)
767 (setq math-exp-token
'punc
768 math-expr-data
(math-match-substring math-exp-str
0)
769 math-exp-pos
(match-end 0)))
771 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
772 math-exp-str math-exp-pos
))
773 (setq math-exp-token
'string
774 math-expr-data
(math-match-substring math-exp-str
1)
775 math-exp-pos
(match-end 0)))
776 ((and (setq adfn
(get calc-language
'math-lang-read
))
778 (eval (nth 1 adfn
))))
779 ((eq (string-match "%%.*$" math-exp-str math-exp-pos
) math-exp-pos
)
780 (setq math-exp-pos
(match-end 0))
783 (if (setq adfn
(assq ch
(get calc-language
'math-punc-table
)))
784 (setq ch
(cdr adfn
)))
785 (setq math-exp-token
'punc
786 math-expr-data
(char-to-string ch
)
787 math-exp-pos
(1+ math-exp-pos
)))))))
789 (defconst math-alg-inequalities
790 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
791 calcFunc-eq calcFunc-neq
))
793 (defun math-read-expr-level (exp-prec &optional exp-term
)
794 (let* ((math-expr-opers (math-expr-ops))
795 (x (math-read-factor))
798 (while (and (or (and calc-user-parse-table
799 (setq op
(calc-check-user-syntax x exp-prec
))
801 op
'("2x" ident
999999 -
1)))
802 (and (setq op
(assoc math-expr-data math-expr-opers
))
804 (or (and (setq op2
(assoc
806 (cdr (memq op math-expr-opers
))))
807 (eq (= (nth 3 op
) -
1)
809 (eq (= (nth 3 op2
) -
1)
810 (not (math-factor-after)))
813 (and (or (eq (nth 2 op
) -
1)
814 (memq math-exp-token
'(symbol number dollar hash
))
815 (equal math-expr-data
"(")
816 (and (equal math-expr-data
"[")
819 'math-function-open
) "["))
820 (not (and math-exp-keep-spaces
821 (eq (car-safe x
) 'vec
)))))
822 (or (not (setq op
(assoc math-expr-data math-expr-opers
)))
824 (or (not calc-user-parse-table
)
825 (not (eq math-exp-token
'symbol
))
826 (let ((p calc-user-parse-table
))
829 (car (car (car p
)))))
831 (nth 1 (car (car p
)))
835 (setq op
(assoc "2x" math-expr-opers
))))
836 (not (and exp-term
(equal math-expr-data exp-term
)))
837 (>= (nth 2 op
) exp-prec
))
838 (if (not (equal (car op
) "2x"))
840 (and (memq (nth 1 op
) '(sdev mod
))
842 (setq x
(cond ((consp (nth 1 op
))
843 (funcall (car (nth 1 op
)) x op
))
845 (if (eq (nth 1 op
) 'ident
)
847 (if (eq (nth 1 op
) 'closing
)
848 (if (eq (nth 2 op
) exp-prec
)
852 (throw 'syntax
"Mismatched delimiters"))
853 (list (nth 1 op
) x
))))
855 (memq (nth 1 op
) math-alg-inequalities
)
856 (memq (car-safe x
) math-alg-inequalities
))
858 (math-composite-inequalities x op
))
861 (math-read-expr-level (nth 3 op
) exp-term
))))
865 ;; calc-arg-values is defined in calc-ext.el, but is used here.
866 (defvar calc-arg-values
)
869 (defun calc-check-user-syntax (&optional x prec
)
870 (let ((p calc-user-parse-table
)
875 (setq rule
(car (car p
)))
877 (and (integerp (car rule
))
879 (equal math-expr-data
880 (car (setq rule
(cdr rule
)))))
881 (equal math-expr-data
(car rule
)))))
882 (let ((save-exp-pos math-exp-pos
)
883 (save-exp-old-pos math-exp-old-pos
)
884 (save-exp-token math-exp-token
)
885 (save-exp-data math-expr-data
))
887 (setq matches
(calc-match-user-syntax rule
))))
894 (setq matches
(cons x matches
)))
895 (setq match
(cdr (car p
)))
896 (while (and (eq (car-safe match
)
898 (= (length match
) 3))
899 (setq conds
(append (math-flatten-lands
902 match
(nth 1 match
)))
903 (while (and conds match
)
905 (cond ((eq (car-safe (car conds
))
907 (setq temp
(car conds
))
908 (or (= (length temp
) 3)
909 (and (= (length temp
) 2)
910 (eq (car-safe (nth 1 temp
))
912 (= (length (nth 1 temp
)) 3)
913 (setq temp
(nth 1 temp
)))
921 args
(cons (nth 1 temp
)
923 ((and (eq (car-safe (car conds
))
925 (= (length (car conds
)) 3))
926 (setq temp
(calcFunc-vmatches
930 (nth 2 (car conds
))))
933 (while (setq temp
(cdr temp
))
934 (setq matches
(cons (nth 2 (car temp
))
936 args
(cons (nth 1 (car temp
))
939 (or (math-is-true (math-simplify
944 (setq conds
(cdr conds
)))
946 (not (setq match
(math-multi-subst
947 match args matches
)))
948 (setq math-exp-old-pos save-exp-old-pos
949 math-exp-token save-exp-token
950 math-expr-data save-exp-data
951 math-exp-pos save-exp-pos
)))))))
956 (defun calc-match-user-syntax (p &optional term
)
958 (save-exp-pos math-exp-pos
)
959 (save-exp-old-pos math-exp-old-pos
)
960 (save-exp-token math-exp-token
)
961 (save-exp-data math-expr-data
)
964 (cond ((stringp (car p
))
965 (and (equal math-expr-data
(car p
))
970 (and (setq m
(catch 'syntax
971 (math-read-expr-level
974 (if (consp (nth 1 p
))
975 (car (nth 1 (nth 1 p
)))
979 (setq matches
(nconc matches
(list m
)))))
980 ((eq (car (car p
)) '\?)
981 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
986 (cons 'vec
(and (listp m
) m
))))))
987 (or (listp m
) (not (nth 2 (car p
)))
988 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
989 (eq math-exp-token
'end
)))
991 (setq m
(calc-match-user-syntax (nth 1 (car p
))
992 (car (nth 2 (car p
)))))
994 (let ((vec (cons 'vec m
))
997 (setq opos math-exp-pos
998 mm
(calc-match-user-syntax
1001 (car (nth 2 (car p
))))))
1002 (> math-exp-pos opos
))
1003 (setq vec
(nconc vec mm
)))
1004 (setq matches
(nconc matches
(list vec
))))
1005 (and (eq (car (car p
)) '*)
1006 (setq matches
(nconc matches
(list '(vec)))))))))
1009 (setq math-exp-pos save-exp-pos
1010 math-exp-old-pos save-exp-old-pos
1011 math-exp-token save-exp-token
1012 math-expr-data save-exp-data
1017 (defun math-remove-dashes (x)
1018 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
1020 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
1023 (defun math-remove-percentsigns (x)
1024 (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x
)
1025 (math-remove-percentsigns
1026 (concat (math-match-substring x
1) "o'o" (math-match-substring x
2)))
1029 (defun math-restore-dashes (x)
1030 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
1031 (math-restore-dashes
1032 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
1035 (defun math-restore-placeholders (x)
1036 "Replace placeholders by the proper characters in the symbol x.
1037 This includes `#' for `_' and `'' for `%'.
1038 If the current Calc language does not use placeholders, return nil."
1039 (if (or (memq calc-language calc-lang-allow-underscores
)
1040 (memq calc-language calc-lang-allow-percentsigns
))
1041 (let ((sx (symbol-name x
)))
1042 (when (memq calc-language calc-lang-allow-percentsigns
)
1044 (setq sx
(math-to-percentsigns sx
)))
1045 (if (memq calc-language calc-lang-allow-underscores
)
1046 (setq sx
(math-string-restore-underscores sx
)))
1049 (defun math-string-restore-underscores (x)
1050 "Replace pound signs by underscores in the string x."
1051 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
1052 (math-string-restore-underscores
1053 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
1057 (defun math-read-if (cond op
)
1058 (let ((then (math-read-expr-level 0)))
1059 (or (equal math-expr-data
":")
1060 (throw 'syntax
"Expected ':'"))
1062 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
1064 (defun math-factor-after ()
1065 (let ((math-exp-pos math-exp-pos
)
1066 math-exp-old-pos math-exp-token math-expr-data
)
1068 (or (memq math-exp-token
'(number symbol dollar hash string
))
1069 (and (assoc math-expr-data
'(("-") ("+") ("!") ("|") ("/")))
1070 (assoc (concat "u" math-expr-data
) math-expr-opers
))
1071 (eq (nth 2 (assoc math-expr-data math-expr-opers
)) -
1)
1072 (assoc math-expr-data
'(("(") ("[") ("{"))))))
1074 (defun math-read-factor ()
1075 (let ((math-expr-opers (math-expr-ops))
1077 (cond ((eq math-exp-token
'number
)
1078 (let ((num (math-read-number math-expr-data
)))
1081 (setq math-exp-old-pos math-exp-pos
)
1082 (throw 'syntax
"Bad format")))
1084 (if (and math-read-expr-quotes
1088 ((and calc-user-parse-table
1089 (setq op
(calc-check-user-syntax)))
1091 ((or (equal math-expr-data
"-")
1092 (equal math-expr-data
"+")
1093 (equal math-expr-data
"!")
1094 (equal math-expr-data
"|")
1095 (equal math-expr-data
"/"))
1096 (setq math-expr-data
(concat "u" math-expr-data
))
1098 ((and (setq op
(assoc math-expr-data math-expr-opers
))
1100 (if (consp (nth 1 op
))
1101 (funcall (car (nth 1 op
)) op
)
1103 (let ((val (math-read-expr-level (nth 3 op
))))
1104 (cond ((eq (nth 1 op
) 'ident
)
1106 ((and (Math-numberp val
)
1107 (equal (car op
) "u-"))
1109 (t (list (nth 1 op
) val
))))))
1110 ((eq math-exp-token
'symbol
)
1111 (let ((sym (intern math-expr-data
)))
1113 (if (equal math-expr-data calc-function-open
)
1114 (let ((f (assq sym math-expr-function-mapping
)))
1117 (funcall (car (cdr f
)) f sym
)
1118 (let ((args (if (or (equal math-expr-data calc-function-close
)
1119 (eq math-exp-token
'end
))
1121 (math-read-expr-list))))
1122 (if (not (or (equal math-expr-data calc-function-close
)
1123 (eq math-exp-token
'end
)))
1124 (throw 'syntax
"Expected `)'"))
1126 (if (and (memq calc-language
1127 calc-lang-parens-are-subscripts
)
1130 (let ((calc-matrix-mode 'scalar
))
1135 (symbol-name sym
)))))))
1136 (math-parse-fortran-subscr sym args
)
1139 (and (= (aref (symbol-name sym
) 0) ?
\\)
1140 (< (prefix-numeric-value calc-language-option
)
1142 (setq sym
(intern (substring (symbol-name sym
)
1144 (or (string-match "-" (symbol-name sym
))
1147 (symbol-name sym
))))))
1149 (if math-read-expr-quotes
1151 (let ((val (list 'var
1152 (intern (math-remove-dashes
1154 (if (string-match "-" (symbol-name sym
))
1156 (intern (concat "var-"
1157 (symbol-name sym
)))))))
1159 (assq (nth 1 val
) math-expr-variable-mapping
)
1160 (assq (math-restore-placeholders (nth 1 val
))
1161 math-expr-variable-mapping
))))
1162 (and v
(setq val
(if (consp (cdr v
))
1163 (funcall (car (cdr v
)) v val
)
1166 (substring (symbol-name (cdr v
))
1169 (while (and (memq calc-language
1170 calc-lang-brackets-are-subscripts
)
1171 (equal math-expr-data
"["))
1173 (let ((el (math-read-expr-list)))
1175 (setq val
(append (list 'calcFunc-subscr val
)
1177 (setq el
(cdr el
))))
1178 (if (equal math-expr-data
"]")
1180 (throw 'syntax
"Expected ']'")))
1182 ((eq math-exp-token
'dollar
)
1183 (let ((abs (if (> math-expr-data
0) math-expr-data
(- math-expr-data
))))
1184 (if (>= (length calc-dollar-values
) abs
)
1185 (let ((num math-expr-data
))
1187 (setq calc-dollar-used
(max calc-dollar-used num
))
1188 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1189 (throw 'syntax
(if calc-dollar-values
1191 "$'s not allowed in this context")))))
1192 ((eq math-exp-token
'hash
)
1193 (or calc-hashes-used
1194 (throw 'syntax
"#'s not allowed in this context"))
1196 (if (<= math-expr-data
(length calc-arg-values
))
1197 (let ((num math-expr-data
))
1199 (setq calc-hashes-used
(max calc-hashes-used num
))
1200 (nth (1- num
) calc-arg-values
))
1201 (throw 'syntax
"Too many # arguments")))
1202 ((equal math-expr-data
"(")
1203 (let* ((exp (let ((math-exp-keep-spaces nil
))
1205 (if (or (equal math-expr-data
"\\dots")
1206 (equal math-expr-data
"\\ldots"))
1207 '(neg (var inf var-inf
))
1208 (math-read-expr-level 0)))))
1209 (let ((math-exp-keep-spaces nil
))
1211 ((equal math-expr-data
",")
1214 (let ((exp2 (math-read-expr-level 0)))
1216 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1217 (math-normalize (list 'cplx exp exp2
))
1218 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1219 ((equal math-expr-data
";")
1222 (let ((exp2 (math-read-expr-level 0)))
1223 (setq exp
(if (and exp2
(Math-realp exp
)
1225 (math-normalize (list 'polar exp exp2
))
1230 (math-to-radians-2 exp2
)
1231 '(var i var-i
)))))))))
1232 ((or (equal math-expr-data
"\\dots")
1233 (equal math-expr-data
"\\ldots"))
1236 (let ((exp2 (if (or (equal math-expr-data
")")
1237 (equal math-expr-data
"]")
1238 (eq math-exp-token
'end
))
1240 (math-read-expr-level 0))))
1243 (if (equal math-expr-data
")") 0 1)
1246 (if (not (or (equal math-expr-data
")")
1247 (and (equal math-expr-data
"]") (eq (car-safe exp
) 'intv
))
1248 (eq math-exp-token
'end
)))
1249 (throw 'syntax
"Expected `)'"))
1252 ((eq math-exp-token
'string
)
1255 ((equal math-expr-data
"[")
1257 (math-read-brackets t
"]"))
1258 ((equal math-expr-data
"{")
1260 (math-read-brackets nil
"}"))
1261 ((equal math-expr-data
"<")
1263 (math-read-angle-brackets))
1264 (t (throw 'syntax
"Expected a number")))))
1266 (provide 'calc-aent
)
1270 ;; generated-autoload-file: "calc-loaddefs.el"
1273 ;;; calc-aent.el ends here