1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 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-digit-start-entry
"calc" ())
34 (declare-function calc-refresh-evaltos
"calc-ext" (&optional which-var
))
35 (declare-function calc-execute-kbd-macro
"calc-prog" (mac arg
&rest prefix
))
36 (declare-function math-is-true
"calc-ext" (expr))
37 (declare-function calc-explain-why
"calc-stuff" (why &optional more
))
38 (declare-function calc-alg-edit
"calc-yank" (str))
39 (declare-function math-composite-inequalities
"calc-prog" (x op
))
40 (declare-function math-flatten-lands
"calc-rewr" (expr))
41 (declare-function math-multi-subst
"calc-map" (expr olds news
))
42 (declare-function calcFunc-vmatches
"calc-rewr" (expr pat
))
43 (declare-function math-simplify
"calc-alg" (top-expr))
44 (declare-function math-known-matrixp
"calc-arith" (a))
45 (declare-function math-parse-fortran-subscr
"calc-lang" (sym args
))
46 (declare-function math-to-radians-2
"calc-math" (a &optional force-symbolic
))
47 (declare-function math-read-string
"calc-ext" ())
48 (declare-function math-read-brackets
"calc-vec" (space-sep math-rb-close
))
49 (declare-function math-read-angle-brackets
"calc-forms" ())
50 (declare-function math-to-percentsigns
"calccomp" (x))
52 (defvar calc-quick-calc-history nil
53 "The history list for quick-calc.")
56 (defun calc-do-quick-calc (&optional insert
)
59 (if (eq major-mode
'calc-mode
)
60 (calc-algebraic-entry t
)
64 (let* ((calc-command-flags nil
)
65 (calc-dollar-values calc-quick-prev-results
)
67 (enable-recursive-minibuffers t
)
68 (calc-language (if (memq calc-language
'(nil big
))
70 (entry (calc-do-alg-entry "" "Quick calc: " t
'calc-quick-calc-history
))
71 (alg-exp (mapcar 'math-evaluate-expr entry
)))
72 (when (and (= (length alg-exp
) 1)
73 (eq (car-safe (car alg-exp
)) 'calcFunc-assign
)
74 (= (length (car alg-exp
)) 3)
75 (eq (car-safe (nth 1 (car alg-exp
))) 'var
))
76 (set (nth 2 (nth 1 (car alg-exp
))) (nth 2 (car alg-exp
)))
77 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp
))))
78 (setq alg-exp
(list (nth 2 (car alg-exp
)))))
79 (setq calc-quick-prev-results alg-exp
80 buf
(mapconcat (function (lambda (x)
81 (math-format-value x
1000)))
85 (if (and (= (length alg-exp
) 1)
86 (memq (car-safe (car alg-exp
)) '(nil bigpos bigneg
))
88 (= calc-number-radix
10))
89 (setq buf
(concat buf
" ("
90 (let ((calc-number-radix 16))
91 (math-format-value (car alg-exp
) 1000))
93 (let ((calc-number-radix 8))
94 (math-format-value (car alg-exp
) 1000))
96 (let ((calc-number-radix 2))
97 (math-format-value (car alg-exp
) 1000))
98 (if (and (integerp (car alg-exp
))
100 (< (car alg-exp
) 127))
101 (format ", \"%c\"" (car alg-exp
))
104 (if (and (< (length buf
) (frame-width)) (= (length entry
) 1)
105 (featurep 'calc-ext
))
106 (let ((long (concat (math-format-value (car entry
) 1000)
108 (if (<= (length long
) (- (frame-width) 8))
111 (message "Result: %s" buf
)))
113 (eq last-command-event
10))
115 (kill-new shortbuf
)))))
118 (defun calc-do-calc-eval (str separator args
)
124 ((and (consp str
) (not (symbolp (car str
))))
125 (let ((calc-language nil
)
126 (math-expr-opers (math-standard-ops))
127 (calc-internal-prec 12)
129 (calc-symbolic-mode nil
)
130 (calc-matrix-mode nil
)
131 (calc-angle-mode 'deg
)
132 (calc-number-radix 10)
133 (calc-twos-complement-mode nil
)
134 (calc-leading-zeros nil
)
135 (calc-group-digits nil
)
136 (calc-point-char ".")
137 (calc-frac-format '(":" nil
))
138 (calc-prefer-frac nil
)
139 (calc-hms-format "%s@ %s' %s\"")
140 (calc-date-format '((H ":" mm C SS pp
" ")
141 Www
" " Mmm
" " D
", " YYYY
))
142 (calc-float-format '(float 0))
143 (calc-full-float-format '(float 0))
144 (calc-complex-format nil
)
145 (calc-matrix-just nil
)
146 (calc-full-vectors t
)
147 (calc-break-vectors nil
)
148 (calc-vector-commas ",")
149 (calc-vector-brackets "[]")
150 (calc-matrix-brackets '(R O
))
151 (calc-complex-mode 'cplx
)
152 (calc-infinite-mode nil
)
153 (calc-display-strings nil
)
154 (calc-simplify-mode nil
)
155 (calc-display-working-message 'lots
)
158 (set (car strp
) (nth 1 strp
))
159 (setq strp
(cdr (cdr strp
))))
160 (calc-do-calc-eval (car str
) separator args
)))
161 ((eq separator
'eval
)
163 ((eq separator
'macro
)
165 (let* ((calc-buffer (current-buffer))
166 (calc-window (get-buffer-window calc-buffer
))
167 (save-window (selected-window)))
171 (select-window calc-window
)
172 (calc-execute-kbd-macro str nil
(car args
)))
173 (and (window-point save-window
)
174 (select-window save-window
)))
175 (save-window-excursion
176 (select-window (get-largest-window))
177 (switch-to-buffer calc-buffer
)
178 (calc-execute-kbd-macro str nil
(car args
)))))
181 (or (not (integerp str
))
183 (calc-pop (min str
(calc-stack-size))))
188 (<= str
(calc-stack-size))
189 (math-format-value (calc-top-n str
(car args
)) 1000)))
190 ((eq separator
'rawtop
)
193 (<= str
(calc-stack-size))
194 (calc-top-n str
(car args
))))
196 (let* ((calc-command-flags nil
)
198 (calc-language (if (memq calc-language
'(nil big
))
199 'flat calc-language
))
200 (calc-dollar-values (mapcar
205 (setq x
(math-read-exprs x
))
215 (res (if (stringp str
)
216 (math-read-exprs str
)
219 (if (eq (car res
) 'error
)
220 (calc-eval-error (cdr res
))
221 (setq res
(mapcar 'calc-normalize res
))
222 (and (memq 'clear-message calc-command-flags
)
224 (cond ((eq separator
'pred
)
226 (if (= (length res
) 1)
227 (math-is-true (car res
))
228 (calc-eval-error '(0 "Single value expected"))))
230 (if (= (length res
) 1)
232 (calc-eval-error '(0 "Single value expected"))))
233 ((eq separator
'list
)
235 ((memq separator
'(num rawnum
))
236 (if (= (length res
) 1)
237 (if (math-constp (car res
))
238 (if (eq separator
'num
)
239 (math-format-value (car res
) 1000)
244 (calc-explain-why (car calc-next-why
))
245 "Number expected"))))
246 (calc-eval-error '(0 "Single value expected"))))
247 ((eq separator
'push
)
251 (setq buf
(concat buf
252 (and buf
(or separator
", "))
253 (math-format-value (car res
) 1000))
257 (defvar calc-eval-error nil
258 "Determines how calc handles errors.
259 If nil, return a list containing the character position of error.
260 STRING means return error message as string rather than list.
261 The value t means abort and give an error message.")
263 (defun calc-eval-error (msg)
265 (if (eq calc-eval-error
'string
)
267 (error "%s" (nth 1 msg
)))
271 ;;;; Reading an expression in algebraic form.
274 (defun calc-auto-algebraic-entry (&optional prefix
)
276 (calc-algebraic-entry prefix t
))
279 (defun calc-algebraic-entry (&optional prefix auto
)
282 (let ((calc-language (if prefix nil calc-language
))
283 (math-expr-opers (if prefix
(math-standard-ops) (math-expr-ops))))
284 (calc-alg-entry (and auto
(char-to-string last-command-event
))))))
286 (defvar calc-alg-entry-history nil
287 "History for algebraic entry.")
290 (defun calc-alg-entry (&optional initial prompt
)
291 (let* ((calc-dollar-values (mapcar #'calc-get-stack-element
292 (nthcdr calc-stack-top calc-stack
)))
295 (alg-exp (calc-do-alg-entry initial prompt t
'calc-alg-entry-history
)))
296 (if (stringp alg-exp
)
299 (calc-alg-edit alg-exp
))
300 (let* ((calc-simplify-mode (if (eq last-command-event ?\C-j
)
303 (nvals (mapcar 'calc-normalize alg-exp
)))
305 (calc-record (if (featurep 'calc-ext
) (car alg-exp
) (car nvals
))
307 (calc-pop-push-record-list calc-dollar-used
308 (and (not (equal (car alg-exp
)
313 (setq alg-exp
(cdr alg-exp
)
315 calc-dollar-used
0)))
316 (calc-handle-whys))))
318 (defvar calc-alg-ent-map
319 (let ((map (make-sparse-keymap)))
320 (set-keymap-parent map minibuffer-local-map
)
321 (define-key map
"'" 'calcAlg-previous
)
322 (define-key map
"`" 'calcAlg-edit
)
323 (define-key map
"\C-m" 'calcAlg-enter
)
324 (define-key map
"\C-j" 'calcAlg-enter
)
326 "The keymap used for algebraic entry.")
328 (defvar calc-alg-ent-esc-map
329 (let ((map (make-keymap))
331 (set-keymap-parent map esc-map
)
333 (define-key map
(vector i
) 'calcAlg-escape
)
336 "The keymap used for escapes in algebraic entry.")
338 (defvar calc-alg-exp
)
341 (defun calc-do-alg-entry (&optional initial prompt no-normalize history
)
342 (let* ((calc-buffer (current-buffer))
343 (blink-matching-check-function 'calcAlg-blink-matching-check
)
344 (calc-alg-exp 'error
))
345 (define-key calc-alg-ent-map
"\e" nil
)
346 (if (eq calc-algebraic-mode
'total
)
347 (define-key calc-alg-ent-map
"\e" calc-alg-ent-esc-map
)
348 (define-key calc-alg-ent-map
"\e+" 'calcAlg-plus-minus
)
349 (define-key calc-alg-ent-map
"\em" 'calcAlg-mod
)
350 (define-key calc-alg-ent-map
"\e=" 'calcAlg-equals
)
351 (define-key calc-alg-ent-map
"\e\r" 'calcAlg-equals
)
352 (define-key calc-alg-ent-map
"\ep" 'previous-history-element
)
353 (define-key calc-alg-ent-map
"\en" 'next-history-element
)
354 (define-key calc-alg-ent-map
"\e%" 'self-insert-command
))
355 (setq calc-aborted-prefix nil
)
356 (let ((buf (read-from-minibuffer (or prompt
"Algebraic: ")
358 calc-alg-ent-map nil history
)))
359 (when (eq calc-alg-exp
'error
)
360 (when (eq (car-safe (setq calc-alg-exp
(math-read-exprs buf
))) 'error
)
361 (setq calc-alg-exp nil
)))
362 (setq calc-aborted-prefix
"alg'")
364 (and calc-alg-exp
(setq calc-alg-exp
(mapcar 'calc-normalize calc-alg-exp
))))
367 (defun calcAlg-plus-minus ()
369 (if (calc-minibuffer-contains ".* \\'")
373 (defun calcAlg-mod ()
375 (if (not (calc-minibuffer-contains ".* \\'"))
377 (if (calc-minibuffer-contains ".* mod +\\'")
378 (if calc-previous-modulo
379 (insert (math-format-flat-expr calc-previous-modulo
0))
383 (defun calcAlg-previous ()
385 (if (calc-minibuffer-contains "\\'")
386 (previous-history-element 1)
389 (defun calcAlg-equals ()
393 (if (consp calc-alg-exp
)
394 (progn (setq prefix-arg
(length calc-alg-exp
))
395 (calc-unread-command ?
=)))))
397 (defun calcAlg-escape ()
399 (calc-unread-command)
402 (use-local-map calc-mode-map
))
405 (defvar calc-plain-entry nil
)
406 (defun calcAlg-edit ()
408 (if (or (not calc-plain-entry
)
409 (calc-minibuffer-contains
410 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
412 (setq calc-alg-exp
(minibuffer-contents))
417 (defun calcAlg-enter ()
419 (let* ((str (minibuffer-contents))
420 (exp (and (> (length str
) 0)
421 (with-current-buffer calc-buffer
422 (math-read-exprs str
)))))
423 (if (eq (car-safe exp
) 'error
)
425 (goto-char (minibuffer-prompt-end))
426 (forward-char (nth 1 exp
))
428 (calc-temp-minibuffer-message
429 (concat " [" (or (nth 2 exp
) "Error") "]"))
430 (calc-clear-unread-commands))
431 (setq calc-alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
436 (defun calcAlg-blink-matching-check (leftpt rightpt
)
437 (let ((rightchar (char-before rightpt
))
438 (leftchar (if leftpt
(char-after leftpt
))))
440 (or (and (= rightchar ?\
))
442 (and (= rightchar ?\
])
446 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
447 ;; [2..5) perfectly valid!
449 (blink-matching-check-mismatch leftpt rightpt
))))
452 (defun calc-alg-digit-entry ()
454 (calc-digit-start-entry)))
456 ;; The variable calc-digit-value is initially declared in calc.el,
457 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
458 (defvar calc-digit-value
)
461 (defun calcDigit-algebraic ()
463 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
465 (setq calc-digit-value
(minibuffer-contents))
469 (defun calcDigit-edit ()
471 (calc-unread-command)
472 (setq calc-digit-value
(minibuffer-contents))
476 ;;; Algebraic expression parsing. [Public]
478 (defvar math-read-replacement-list
480 ("±" "+/-") ; plus or minus
481 ("×" "*") ; multiplication sign
482 ("÷" ":") ; division sign
483 ("−" "-") ; subtraction sign
484 ("∕" "/") ; division sign
485 ("∗" "*") ; asterisk multiplication
486 ("∞" "inf") ; infinity symbol
541 "A list whose elements (old new) indicate replacements to make
542 in Calc algebraic input.")
544 (defvar math-read-superscripts
545 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
546 "A string consisting of the superscripts allowed by Calc.")
548 (defvar math-read-subscripts
549 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
550 "A string consisting of the subscripts allowed by Calc.")
553 (defun math-read-preprocess-string (str)
554 "Replace some substrings of STR by Calc equivalents."
556 (replace-regexp-in-string (concat "[" math-read-superscripts
"]+")
559 (replace-regexp-in-string (concat "[" math-read-subscripts
"]+")
561 (let ((rep-list math-read-replacement-list
))
564 (replace-regexp-in-string (nth 0 (car rep-list
))
565 (nth 1 (car rep-list
)) str
))
566 (setq rep-list
(cdr rep-list
))))
569 ;; The next few variables are local to math-read-exprs (and math-read-expr
570 ;; in calc-ext.el), but are set in functions they call.
572 (defvar math-exp-pos
)
573 (defvar math-exp-str
)
574 (defvar math-exp-old-pos
)
575 (defvar math-exp-token
)
576 (defvar math-exp-keep-spaces
)
577 (defvar math-expr-data
)
580 (defun math-read-exprs (math-exp-str)
581 (let ((math-exp-pos 0)
583 (math-exp-keep-spaces nil
)
584 math-exp-token math-expr-data
)
585 (setq math-exp-str
(math-read-preprocess-string math-exp-str
))
586 (if (memq calc-language calc-lang-allow-percentsigns
)
587 (setq math-exp-str
(math-remove-percentsigns math-exp-str
)))
588 (if calc-language-input-filter
589 (setq math-exp-str
(funcall calc-language-input-filter math-exp-str
)))
590 (while (setq math-exp-token
591 (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str
))
593 (concat (substring math-exp-str
0 math-exp-token
) "\\dots"
594 (substring math-exp-str
(+ math-exp-token
2)))))
595 (math-build-parse-table)
597 (let ((val (catch 'syntax
(math-read-expr-list))))
599 (list 'error math-exp-old-pos val
)
600 (if (equal math-exp-token
'end
)
602 (list 'error math-exp-old-pos
"Syntax error"))))))
605 (defun math-read-expr-list ()
606 (let* ((math-exp-keep-spaces nil
)
607 (val (list (math-read-expr-level 0)))
609 (while (equal math-expr-data
",")
611 (let ((rest (list (math-read-expr-level 0))))
616 (defvar calc-user-parse-table nil
)
617 (defvar calc-last-main-parse-table nil
)
618 (defvar calc-last-user-lang-parse-table nil
)
619 (defvar calc-last-lang-parse-table nil
)
620 (defvar calc-user-tokens nil
)
621 (defvar calc-user-token-chars nil
)
623 (defvar math-toks nil
624 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
627 (defun math-build-parse-table ()
628 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
629 (ltab (cdr (assq calc-language calc-user-parse-tables
)))
630 (lltab (get calc-language
'math-parse-table
)))
631 (or (and (eq mtab calc-last-main-parse-table
)
632 (eq ltab calc-last-user-lang-parse-table
)
633 (eq lltab calc-last-lang-parse-table
))
634 (let ((p (append mtab ltab lltab
))
636 (setq calc-user-parse-table p
)
637 (setq calc-user-token-chars nil
)
639 (math-find-user-tokens (car (car p
)))
641 (setq calc-user-tokens
(mapconcat 'identity
642 (sort (mapcar 'car math-toks
)
643 (function (lambda (x y
)
647 calc-last-main-parse-table mtab
648 calc-last-user-lang-parse-table ltab
649 calc-last-lang-parse-table lltab
)))))
652 (defun math-find-user-tokens (p)
654 (cond ((and (stringp (car p
))
655 (or (> (length (car p
)) 1) (equal (car p
) "$")
656 (equal (car p
) "\""))
657 (string-match "[^a-zA-Zα-ωΑ-Ω0-9]" (car p
)))
658 (let ((s (regexp-quote (car p
))))
659 (if (string-match "\\`[a-zA-Zα-ωΑ-Ω0-9]" s
)
660 (setq s
(concat "\\<" s
)))
661 (if (string-match "[a-zA-Zα-ωΑ-Ω0-9]\\'" s
)
662 (setq s
(concat s
"\\>")))
663 (or (assoc s math-toks
)
665 (setq math-toks
(cons (list s
) math-toks
))
666 (or (memq (aref (car p
) 0) calc-user-token-chars
)
667 (setq calc-user-token-chars
668 (cons (aref (car p
) 0)
669 calc-user-token-chars
)))))))
671 (math-find-user-tokens (nth 1 (car p
)))
672 (or (eq (car (car p
)) '\?)
673 (math-find-user-tokens (nth 2 (car p
))))))
677 (defun math-read-token ()
678 (if (>= math-exp-pos
(length math-exp-str
))
679 (setq math-exp-old-pos math-exp-pos
681 math-expr-data
"\000")
683 (ch (aref math-exp-str math-exp-pos
)))
684 (setq math-exp-old-pos math-exp-pos
)
685 (cond ((memq ch
'(32 10 9))
686 (setq math-exp-pos
(1+ math-exp-pos
))
687 (if math-exp-keep-spaces
688 (setq math-exp-token
'space
691 ((and (memq ch calc-user-token-chars
)
692 (let ((case-fold-search nil
))
694 calc-user-tokens math-exp-str math-exp-pos
)
696 (setq math-exp-token
'punc
697 math-expr-data
(math-match-substring math-exp-str
0)
698 math-exp-pos
(match-end 0)))
699 ((or (and (>= ch ?a
) (<= ch ?z
))
700 (and (>= ch ?A
) (<= ch ?Z
))
701 (and (>= ch ?α
) (<= ch ?ω
))
702 (and (>= ch ?Α
) (<= ch ?Ω
)))
705 ((and (memq calc-language calc-lang-allow-underscores
)
706 (memq calc-language calc-lang-allow-percentsigns
))
707 "[a-zA-Zα-ωΑ-Ω0-9_'#]*")
708 ((memq calc-language calc-lang-allow-underscores
)
709 "[a-zA-Zα-ωΑ-Ω0-9_#]*")
710 (t "[a-zA-Zα-ωΑ-Ω0-9'#]*"))
711 math-exp-str math-exp-pos
)
712 (setq math-exp-token
'symbol
713 math-exp-pos
(match-end 0)
714 math-expr-data
(math-restore-dashes
715 (math-match-substring math-exp-str
0)))
716 (if (setq adfn
(get calc-language
'math-lang-adjust-words
))
718 ((or (and (>= ch ?
0) (<= ch ?
9))
720 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos
)
723 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos
)
725 (or (eq math-exp-pos
0)
726 (and (not (memq calc-language
727 calc-lang-allow-underscores
))
728 (eq (string-match "[^])}\"a-zA-Zα-ωΑ-Ω0-9'$]_"
729 math-exp-str
(1- math-exp-pos
))
730 (1- math-exp-pos
))))))
731 (or (and (memq calc-language calc-lang-c-type-hex
)
732 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos
))
733 (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]+\\)?\"?\\)?"
734 math-exp-str math-exp-pos
))
735 (setq math-exp-token
'number
736 math-expr-data
(math-match-substring math-exp-str
0)
737 math-exp-pos
(match-end 0)))
739 (assq ch
(get calc-language
'math-lang-read-symbol
)))
743 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
745 (setq math-expr-data
(- (string-to-number (math-match-substring
747 (string-match "\\$+" math-exp-str math-exp-pos
)
748 (setq math-expr-data
(- (match-end 0) (match-beginning 0))))
749 (setq math-exp-token
'dollar
750 math-exp-pos
(match-end 0)))
752 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
754 (setq math-expr-data
(string-to-number
755 (math-match-substring math-exp-str
1))
756 math-exp-pos
(match-end 0))
757 (setq math-expr-data
1
758 math-exp-pos
(1+ math-exp-pos
)))
759 (setq math-exp-token
'hash
))
760 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
761 math-exp-str math-exp-pos
)
763 (setq math-exp-token
'punc
764 math-expr-data
(math-match-substring math-exp-str
0)
765 math-exp-pos
(match-end 0)))
767 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
768 math-exp-str math-exp-pos
))
769 (setq math-exp-token
'string
770 math-expr-data
(math-match-substring math-exp-str
1)
771 math-exp-pos
(match-end 0)))
772 ((and (setq adfn
(get calc-language
'math-lang-read
))
774 (eval (nth 1 adfn
))))
775 ((eq (string-match "%%.*$" math-exp-str math-exp-pos
) math-exp-pos
)
776 (setq math-exp-pos
(match-end 0))
779 (if (setq adfn
(assq ch
(get calc-language
'math-punc-table
)))
780 (setq ch
(cdr adfn
)))
781 (setq math-exp-token
'punc
782 math-expr-data
(char-to-string ch
)
783 math-exp-pos
(1+ math-exp-pos
)))))))
785 (defconst math-alg-inequalities
786 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
787 calcFunc-eq calcFunc-neq
))
789 (defun math-read-expr-level (exp-prec &optional exp-term
)
790 (let* ((math-expr-opers (math-expr-ops))
791 (x (math-read-factor))
794 (while (and (or (and calc-user-parse-table
795 (setq op
(calc-check-user-syntax x exp-prec
))
797 op
'("2x" ident
999999 -
1)))
798 (and (setq op
(assoc math-expr-data math-expr-opers
))
800 (or (and (setq op2
(assoc
802 (cdr (memq op math-expr-opers
))))
803 (eq (= (nth 3 op
) -
1)
805 (eq (= (nth 3 op2
) -
1)
806 (not (math-factor-after)))
809 (and (or (eq (nth 2 op
) -
1)
810 (memq math-exp-token
'(symbol number dollar hash
))
811 (equal math-expr-data
"(")
812 (and (equal math-expr-data
"[")
815 'math-function-open
) "["))
816 (not (and math-exp-keep-spaces
817 (eq (car-safe x
) 'vec
)))))
818 (or (not (setq op
(assoc math-expr-data math-expr-opers
)))
820 (or (not calc-user-parse-table
)
821 (not (eq math-exp-token
'symbol
))
822 (let ((p calc-user-parse-table
))
825 (car (car (car p
)))))
827 (nth 1 (car (car p
)))
831 (setq op
(assoc "2x" math-expr-opers
))))
832 (not (and exp-term
(equal math-expr-data exp-term
)))
833 (>= (nth 2 op
) exp-prec
))
834 (if (not (equal (car op
) "2x"))
836 (and (memq (nth 1 op
) '(sdev mod
))
838 (setq x
(cond ((consp (nth 1 op
))
839 (funcall (car (nth 1 op
)) x op
))
841 (if (eq (nth 1 op
) 'ident
)
843 (if (eq (nth 1 op
) 'closing
)
844 (if (eq (nth 2 op
) exp-prec
)
848 (throw 'syntax
"Mismatched delimiters"))
849 (list (nth 1 op
) x
))))
851 (memq (nth 1 op
) math-alg-inequalities
)
852 (memq (car-safe x
) math-alg-inequalities
))
854 (math-composite-inequalities x op
))
857 (math-read-expr-level (nth 3 op
) exp-term
))))
861 ;; calc-arg-values is defined in calc-ext.el, but is used here.
862 (defvar calc-arg-values
)
865 (defun calc-check-user-syntax (&optional x prec
)
866 (let ((p calc-user-parse-table
)
871 (setq rule
(car (car p
)))
873 (and (integerp (car rule
))
875 (equal math-expr-data
876 (car (setq rule
(cdr rule
)))))
877 (equal math-expr-data
(car rule
)))))
878 (let ((save-exp-pos math-exp-pos
)
879 (save-exp-old-pos math-exp-old-pos
)
880 (save-exp-token math-exp-token
)
881 (save-exp-data math-expr-data
))
883 (setq matches
(calc-match-user-syntax rule
))))
890 (setq matches
(cons x matches
)))
891 (setq match
(cdr (car p
)))
892 (while (and (eq (car-safe match
)
894 (= (length match
) 3))
895 (setq conds
(append (math-flatten-lands
898 match
(nth 1 match
)))
899 (while (and conds match
)
901 (cond ((eq (car-safe (car conds
))
903 (setq temp
(car conds
))
904 (or (= (length temp
) 3)
905 (and (= (length temp
) 2)
906 (eq (car-safe (nth 1 temp
))
908 (= (length (nth 1 temp
)) 3)
909 (setq temp
(nth 1 temp
)))
917 args
(cons (nth 1 temp
)
919 ((and (eq (car-safe (car conds
))
921 (= (length (car conds
)) 3))
922 (setq temp
(calcFunc-vmatches
926 (nth 2 (car conds
))))
929 (while (setq temp
(cdr temp
))
930 (setq matches
(cons (nth 2 (car temp
))
932 args
(cons (nth 1 (car temp
))
935 (or (math-is-true (math-simplify
940 (setq conds
(cdr conds
)))
942 (not (setq match
(math-multi-subst
943 match args matches
)))
944 (setq math-exp-old-pos save-exp-old-pos
945 math-exp-token save-exp-token
946 math-expr-data save-exp-data
947 math-exp-pos save-exp-pos
)))))))
952 (defun calc-match-user-syntax (p &optional term
)
954 (save-exp-pos math-exp-pos
)
955 (save-exp-old-pos math-exp-old-pos
)
956 (save-exp-token math-exp-token
)
957 (save-exp-data math-expr-data
)
960 (cond ((stringp (car p
))
961 (and (equal math-expr-data
(car p
))
966 (and (setq m
(catch 'syntax
967 (math-read-expr-level
970 (if (consp (nth 1 p
))
971 (car (nth 1 (nth 1 p
)))
975 (setq matches
(nconc matches
(list m
)))))
976 ((eq (car (car p
)) '\?)
977 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
982 (cons 'vec
(and (listp m
) m
))))))
983 (or (listp m
) (not (nth 2 (car p
)))
984 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
985 (eq math-exp-token
'end
)))
987 (setq m
(calc-match-user-syntax (nth 1 (car p
))
988 (car (nth 2 (car p
)))))
990 (let ((vec (cons 'vec m
))
993 (setq opos math-exp-pos
994 mm
(calc-match-user-syntax
997 (car (nth 2 (car p
))))))
998 (> math-exp-pos opos
))
999 (setq vec
(nconc vec mm
)))
1000 (setq matches
(nconc matches
(list vec
))))
1001 (and (eq (car (car p
)) '*)
1002 (setq matches
(nconc matches
(list '(vec)))))))))
1005 (setq math-exp-pos save-exp-pos
1006 math-exp-old-pos save-exp-old-pos
1007 math-exp-token save-exp-token
1008 math-expr-data save-exp-data
1013 (defun math-remove-dashes (x)
1014 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
1016 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
1019 (defun math-remove-percentsigns (x)
1020 (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x
)
1021 (math-remove-percentsigns
1022 (concat (math-match-substring x
1) "o'o" (math-match-substring x
2)))
1025 (defun math-restore-dashes (x)
1026 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
1027 (math-restore-dashes
1028 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
1031 (defun math-restore-placeholders (x)
1032 "Replace placeholders by the proper characters in the symbol x.
1033 This includes `#' for `_' and `'' for `%'.
1034 If the current Calc language does not use placeholders, return nil."
1035 (if (or (memq calc-language calc-lang-allow-underscores
)
1036 (memq calc-language calc-lang-allow-percentsigns
))
1037 (let ((sx (symbol-name x
)))
1038 (when (memq calc-language calc-lang-allow-percentsigns
)
1040 (setq sx
(math-to-percentsigns sx
)))
1041 (if (memq calc-language calc-lang-allow-underscores
)
1042 (setq sx
(math-string-restore-underscores sx
)))
1045 (defun math-string-restore-underscores (x)
1046 "Replace pound signs by underscores in the string x."
1047 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
1048 (math-string-restore-underscores
1049 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
1053 (defun math-read-if (cond op
)
1054 (let ((then (math-read-expr-level 0)))
1055 (or (equal math-expr-data
":")
1056 (throw 'syntax
"Expected `:'"))
1058 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
1060 (defun math-factor-after ()
1061 (let ((math-exp-pos math-exp-pos
)
1062 math-exp-old-pos math-exp-token math-expr-data
)
1064 (or (memq math-exp-token
'(number symbol dollar hash string
))
1065 (and (assoc math-expr-data
'(("-") ("+") ("!") ("|") ("/")))
1066 (assoc (concat "u" math-expr-data
) math-expr-opers
))
1067 (eq (nth 2 (assoc math-expr-data math-expr-opers
)) -
1)
1068 (assoc math-expr-data
'(("(") ("[") ("{"))))))
1070 (defun math-read-factor ()
1071 (let ((math-expr-opers (math-expr-ops))
1073 (cond ((eq math-exp-token
'number
)
1074 (let ((num (math-read-number math-expr-data
)))
1077 (setq math-exp-old-pos math-exp-pos
)
1078 (throw 'syntax
"Bad format")))
1080 (if (and math-read-expr-quotes
1084 ((and calc-user-parse-table
1085 (setq op
(calc-check-user-syntax)))
1087 ((or (equal math-expr-data
"-")
1088 (equal math-expr-data
"+")
1089 (equal math-expr-data
"!")
1090 (equal math-expr-data
"|")
1091 (equal math-expr-data
"/"))
1092 (setq math-expr-data
(concat "u" math-expr-data
))
1094 ((and (setq op
(assoc math-expr-data math-expr-opers
))
1096 (if (consp (nth 1 op
))
1097 (funcall (car (nth 1 op
)) op
)
1099 (let ((val (math-read-expr-level (nth 3 op
))))
1100 (cond ((eq (nth 1 op
) 'ident
)
1102 ((and (Math-numberp val
)
1103 (equal (car op
) "u-"))
1105 (t (list (nth 1 op
) val
))))))
1106 ((eq math-exp-token
'symbol
)
1107 (let ((sym (intern math-expr-data
)))
1109 (if (equal math-expr-data calc-function-open
)
1110 (let ((f (assq sym math-expr-function-mapping
)))
1113 (funcall (car (cdr f
)) f sym
)
1114 (let ((args (if (or (equal math-expr-data calc-function-close
)
1115 (eq math-exp-token
'end
))
1117 (math-read-expr-list))))
1118 (if (not (or (equal math-expr-data calc-function-close
)
1119 (eq math-exp-token
'end
)))
1120 (throw 'syntax
"Expected `)'"))
1122 (if (and (memq calc-language
1123 calc-lang-parens-are-subscripts
)
1126 (let ((calc-matrix-mode 'scalar
))
1131 (symbol-name sym
)))))))
1132 (math-parse-fortran-subscr sym args
)
1135 (and (= (aref (symbol-name sym
) 0) ?
\\)
1136 (< (prefix-numeric-value calc-language-option
)
1138 (setq sym
(intern (substring (symbol-name sym
)
1140 (or (string-match "-" (symbol-name sym
))
1143 (symbol-name sym
))))))
1145 (if math-read-expr-quotes
1147 (let ((val (list 'var
1148 (intern (math-remove-dashes
1150 (if (string-match "-" (symbol-name sym
))
1152 (intern (concat "var-"
1153 (symbol-name sym
)))))))
1155 (assq (nth 1 val
) math-expr-variable-mapping
)
1156 (assq (math-restore-placeholders (nth 1 val
))
1157 math-expr-variable-mapping
))))
1158 (and v
(setq val
(if (consp (cdr v
))
1159 (funcall (car (cdr v
)) v val
)
1162 (substring (symbol-name (cdr v
))
1165 (while (and (memq calc-language
1166 calc-lang-brackets-are-subscripts
)
1167 (equal math-expr-data
"["))
1169 (let ((el (math-read-expr-list)))
1171 (setq val
(append (list 'calcFunc-subscr val
)
1173 (setq el
(cdr el
))))
1174 (if (equal math-expr-data
"]")
1176 (throw 'syntax
"Expected `]'")))
1178 ((eq math-exp-token
'dollar
)
1179 (let ((abs (if (> math-expr-data
0) math-expr-data
(- math-expr-data
))))
1180 (if (>= (length calc-dollar-values
) abs
)
1181 (let ((num math-expr-data
))
1183 (setq calc-dollar-used
(max calc-dollar-used num
))
1184 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1185 (throw 'syntax
(if calc-dollar-values
1187 "$'s not allowed in this context")))))
1188 ((eq math-exp-token
'hash
)
1189 (or calc-hashes-used
1190 (throw 'syntax
"#'s not allowed in this context"))
1192 (if (<= math-expr-data
(length calc-arg-values
))
1193 (let ((num math-expr-data
))
1195 (setq calc-hashes-used
(max calc-hashes-used num
))
1196 (nth (1- num
) calc-arg-values
))
1197 (throw 'syntax
"Too many # arguments")))
1198 ((equal math-expr-data
"(")
1199 (let* ((exp (let ((math-exp-keep-spaces nil
))
1201 (if (or (equal math-expr-data
"\\dots")
1202 (equal math-expr-data
"\\ldots"))
1203 '(neg (var inf var-inf
))
1204 (math-read-expr-level 0)))))
1205 (let ((math-exp-keep-spaces nil
))
1207 ((equal math-expr-data
",")
1210 (let ((exp2 (math-read-expr-level 0)))
1212 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1213 (math-normalize (list 'cplx exp exp2
))
1214 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1215 ((equal math-expr-data
";")
1218 (let ((exp2 (math-read-expr-level 0)))
1219 (setq exp
(if (and exp2
(Math-realp exp
)
1221 (math-normalize (list 'polar exp exp2
))
1226 (math-to-radians-2 exp2
)
1227 '(var i var-i
)))))))))
1228 ((or (equal math-expr-data
"\\dots")
1229 (equal math-expr-data
"\\ldots"))
1232 (let ((exp2 (if (or (equal math-expr-data
")")
1233 (equal math-expr-data
"]")
1234 (eq math-exp-token
'end
))
1236 (math-read-expr-level 0))))
1239 (if (equal math-expr-data
")") 0 1)
1242 (if (not (or (equal math-expr-data
")")
1243 (and (equal math-expr-data
"]") (eq (car-safe exp
) 'intv
))
1244 (eq math-exp-token
'end
)))
1245 (throw 'syntax
"Expected `)'"))
1248 ((eq math-exp-token
'string
)
1251 ((equal math-expr-data
"[")
1253 (math-read-brackets t
"]"))
1254 ((equal math-expr-data
"{")
1256 (math-read-brackets nil
"}"))
1257 ((equal math-expr-data
"<")
1259 (math-read-angle-brackets))
1260 (t (throw 'syntax
"Expected a number")))))
1262 (provide 'calc-aent
)
1265 ;; generated-autoload-file: "calc-loaddefs.el"
1268 ;;; calc-aent.el ends here