1 ;;; calc-aent.el --- algebraic entry functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc.el.
33 ;; Declare functions which are defined elsewhere.
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))
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 ()
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
)))
112 (if (eq last-command-char
10)
114 (kill-new shortbuf
)))))
117 (defun calc-do-calc-eval (str separator args
)
123 ((and (consp str
) (not (symbolp (car str
))))
124 (let ((calc-language nil
)
125 (math-expr-opers (math-standard-ops))
126 (calc-internal-prec 12)
128 (calc-symbolic-mode nil
)
129 (calc-matrix-mode nil
)
130 (calc-angle-mode 'deg
)
131 (calc-number-radix 10)
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-char
))))))
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-char ?\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 nil
318 "The keymap used for algebraic entry.")
320 (defvar calc-alg-ent-esc-map nil
321 "The keymap used for escapes in algebraic entry.")
323 (defvar calc-alg-exp
)
326 (defun calc-do-alg-entry (&optional initial prompt no-normalize history
)
327 (let* ((calc-buffer (current-buffer))
328 (blink-paren-function 'calcAlg-blink-matching-open
)
329 (calc-alg-exp 'error
))
330 (unless calc-alg-ent-map
331 (setq calc-alg-ent-map
(copy-keymap minibuffer-local-map
))
332 (define-key calc-alg-ent-map
"'" 'calcAlg-previous
)
333 (define-key calc-alg-ent-map
"`" 'calcAlg-edit
)
334 (define-key calc-alg-ent-map
"\C-m" 'calcAlg-enter
)
335 (define-key calc-alg-ent-map
"\C-j" 'calcAlg-enter
)
337 (setq calc-alg-ent-esc-map
(copy-keymap esc-map
))
339 (aset (nth 1 calc-alg-ent-esc-map
) i
'calcAlg-escape
)
341 (define-key calc-alg-ent-map
"\e" nil
)
342 (if (eq calc-algebraic-mode
'total
)
343 (define-key calc-alg-ent-map
"\e" calc-alg-ent-esc-map
)
344 (define-key calc-alg-ent-map
"\e+" 'calcAlg-plus-minus
)
345 (define-key calc-alg-ent-map
"\em" 'calcAlg-mod
)
346 (define-key calc-alg-ent-map
"\e=" 'calcAlg-equals
)
347 (define-key calc-alg-ent-map
"\e\r" 'calcAlg-equals
)
348 (define-key calc-alg-ent-map
"\ep" 'previous-history-element
)
349 (define-key calc-alg-ent-map
"\en" 'next-history-element
)
350 (define-key calc-alg-ent-map
"\e%" 'self-insert-command
))
351 (setq calc-aborted-prefix nil
)
352 (let ((buf (read-from-minibuffer (or prompt
"Algebraic: ")
354 calc-alg-ent-map nil history
)))
355 (when (eq calc-alg-exp
'error
)
356 (when (eq (car-safe (setq calc-alg-exp
(math-read-exprs buf
))) 'error
)
357 (setq calc-alg-exp nil
)))
358 (setq calc-aborted-prefix
"alg'")
360 (and calc-alg-exp
(setq calc-alg-exp
(mapcar 'calc-normalize calc-alg-exp
))))
363 (defun calcAlg-plus-minus ()
365 (if (calc-minibuffer-contains ".* \\'")
369 (defun calcAlg-mod ()
371 (if (not (calc-minibuffer-contains ".* \\'"))
373 (if (calc-minibuffer-contains ".* mod +\\'")
374 (if calc-previous-modulo
375 (insert (math-format-flat-expr calc-previous-modulo
0))
379 (defun calcAlg-previous ()
381 (if (calc-minibuffer-contains "\\'")
382 (previous-history-element 1)
385 (defun calcAlg-equals ()
389 (if (consp calc-alg-exp
)
390 (progn (setq prefix-arg
(length calc-alg-exp
))
391 (calc-unread-command ?
=)))))
393 (defun calcAlg-escape ()
395 (calc-unread-command)
398 (use-local-map calc-mode-map
))
401 (defvar calc-plain-entry nil
)
402 (defun calcAlg-edit ()
404 (if (or (not calc-plain-entry
)
405 (calc-minibuffer-contains
406 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
408 (setq calc-alg-exp
(minibuffer-contents))
413 (defun calcAlg-enter ()
415 (let* ((str (minibuffer-contents))
416 (exp (and (> (length str
) 0)
418 (set-buffer calc-buffer
)
419 (math-read-exprs str
)))))
420 (if (eq (car-safe exp
) 'error
)
422 (goto-char (minibuffer-prompt-end))
423 (forward-char (nth 1 exp
))
425 (calc-temp-minibuffer-message
426 (concat " [" (or (nth 2 exp
) "Error") "]"))
427 (calc-clear-unread-commands))
428 (setq calc-alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
433 (defun calcAlg-blink-matching-open ()
434 (let ((rightpt (point))
436 (rightchar (preceding-char))
442 (setq leftpt
(scan-sexps rightpt -
1)
443 leftchar
(char-after leftpt
))
446 (or (and (= rightchar ?\
))
448 (and (= rightchar ?\
])
452 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
453 (let ((leftsaved (aref (syntax-table) leftchar
))
454 (rightsaved (aref (syntax-table) rightchar
)))
457 (cond ((= leftchar ?\
[)
458 (aset (syntax-table) leftchar
(cons 4 ?\
)))
459 (aset (syntax-table) rightchar
(cons 5 ?\
[)))
461 (aset (syntax-table) leftchar
(cons 4 ?\
]))
462 (aset (syntax-table) rightchar
(cons 5 ?\
())))
463 (blink-matching-open))
464 (aset (syntax-table) leftchar leftsaved
)
465 (aset (syntax-table) rightchar rightsaved
)))
466 (blink-matching-open))))
469 (defun calc-alg-digit-entry ()
471 (cond ((eq last-command-char ?e
)
472 (if (> calc-number-radix
14) (format "%d.^" calc-number-radix
) "1e"))
473 ((eq last-command-char ?
#) (format "%d#" calc-number-radix
))
474 ((eq last-command-char ?_
) "-")
475 ((eq last-command-char ?
@) "0@ ")
476 (t (char-to-string last-command-char
)))))
478 ;; The variable calc-digit-value is initially declared in calc.el,
479 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
480 (defvar calc-digit-value
)
483 (defun calcDigit-algebraic ()
485 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
487 (setq calc-digit-value
(minibuffer-contents))
491 (defun calcDigit-edit ()
493 (calc-unread-command)
494 (setq calc-digit-value
(minibuffer-contents))
498 ;;; Algebraic expression parsing. [Public]
500 (defvar math-read-replacement-list
502 ("±" "+/-") ; plus or minus
503 ("×" "*") ; multiplication sign
504 ("÷" ":") ; division sign
505 ("−" "-") ; subtraction sign
506 ("∕" "/") ; division sign
507 ("∗" "*") ; asterisk multiplication
508 ("∞" "inf") ; infinity symbol
562 "A list whose elements (old new) indicate replacements to make
563 in Calc algebraic input.")
565 (defvar math-read-superscripts
566 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
567 "A string consisting of the superscripts allowed by Calc.")
569 (defvar math-read-subscripts
570 "₀₁₂₃₄₅₆₇₈₉₊₋₍₎" ; 0123456789+-()
571 "A string consisting of the subscripts allowed by Calc.")
574 (defun math-read-preprocess-string (str)
575 "Replace some substrings of STR by Calc equivalents."
577 (replace-regexp-in-string (concat "[" math-read-superscripts
"]+")
580 (replace-regexp-in-string (concat "[" math-read-subscripts
"]+")
582 (let ((rep-list math-read-replacement-list
))
585 (replace-regexp-in-string (nth 0 (car rep-list
))
586 (nth 1 (car rep-list
)) str
))
587 (setq rep-list
(cdr rep-list
))))
590 ;; The next few variables are local to math-read-exprs (and math-read-expr
591 ;; in calc-ext.el), but are set in functions they call.
593 (defvar math-exp-pos
)
594 (defvar math-exp-str
)
595 (defvar math-exp-old-pos
)
596 (defvar math-exp-token
)
597 (defvar math-exp-keep-spaces
)
598 (defvar math-expr-data
)
601 (defun math-read-exprs (math-exp-str)
602 (let ((math-exp-pos 0)
604 (math-exp-keep-spaces nil
)
605 math-exp-token math-expr-data
)
606 (setq math-exp-str
(math-read-preprocess-string math-exp-str
))
607 (if (memq calc-language calc-lang-allow-percentsigns
)
608 (setq math-exp-str
(math-remove-percentsigns math-exp-str
)))
609 (if calc-language-input-filter
610 (setq math-exp-str
(funcall calc-language-input-filter math-exp-str
)))
611 (while (setq math-exp-token
612 (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str
))
614 (concat (substring math-exp-str
0 math-exp-token
) "\\dots"
615 (substring math-exp-str
(+ math-exp-token
2)))))
616 (math-build-parse-table)
618 (let ((val (catch 'syntax
(math-read-expr-list))))
620 (list 'error math-exp-old-pos val
)
621 (if (equal math-exp-token
'end
)
623 (list 'error math-exp-old-pos
"Syntax error"))))))
626 (defun math-read-expr-list ()
627 (let* ((math-exp-keep-spaces nil
)
628 (val (list (math-read-expr-level 0)))
630 (while (equal math-expr-data
",")
632 (let ((rest (list (math-read-expr-level 0))))
637 (defvar calc-user-parse-table nil
)
638 (defvar calc-last-main-parse-table nil
)
639 (defvar calc-last-user-lang-parse-table nil
)
640 (defvar calc-last-lang-parse-table nil
)
641 (defvar calc-user-tokens nil
)
642 (defvar calc-user-token-chars nil
)
644 (defvar math-toks nil
645 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
648 (defun math-build-parse-table ()
649 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
650 (ltab (cdr (assq calc-language calc-user-parse-tables
)))
651 (lltab (get calc-language
'math-parse-table
)))
652 (or (and (eq mtab calc-last-main-parse-table
)
653 (eq ltab calc-last-user-lang-parse-table
)
654 (eq lltab calc-last-lang-parse-table
))
655 (let ((p (append mtab ltab lltab
))
657 (setq calc-user-parse-table p
)
658 (setq calc-user-token-chars nil
)
660 (math-find-user-tokens (car (car p
)))
662 (setq calc-user-tokens
(mapconcat 'identity
663 (sort (mapcar 'car math-toks
)
664 (function (lambda (x y
)
668 calc-last-main-parse-table mtab
669 calc-last-user-lang-parse-table ltab
670 calc-last-lang-parse-table lltab
)))))
673 (defun math-find-user-tokens (p)
675 (cond ((and (stringp (car p
))
676 (or (> (length (car p
)) 1) (equal (car p
) "$")
677 (equal (car p
) "\""))
678 (string-match "[^a-zA-Z0-9]" (car p
)))
679 (let ((s (regexp-quote (car p
))))
680 (if (string-match "\\`[a-zA-Z0-9]" s
)
681 (setq s
(concat "\\<" s
)))
682 (if (string-match "[a-zA-Z0-9]\\'" s
)
683 (setq s
(concat s
"\\>")))
684 (or (assoc s math-toks
)
686 (setq math-toks
(cons (list s
) math-toks
))
687 (or (memq (aref (car p
) 0) calc-user-token-chars
)
688 (setq calc-user-token-chars
689 (cons (aref (car p
) 0)
690 calc-user-token-chars
)))))))
692 (math-find-user-tokens (nth 1 (car p
)))
693 (or (eq (car (car p
)) '\?)
694 (math-find-user-tokens (nth 2 (car p
))))))
698 (defun math-read-token ()
699 (if (>= math-exp-pos
(length math-exp-str
))
700 (setq math-exp-old-pos math-exp-pos
702 math-expr-data
"\000")
704 (ch (aref math-exp-str math-exp-pos
)))
705 (setq math-exp-old-pos math-exp-pos
)
706 (cond ((memq ch
'(32 10 9))
707 (setq math-exp-pos
(1+ math-exp-pos
))
708 (if math-exp-keep-spaces
709 (setq math-exp-token
'space
712 ((and (memq ch calc-user-token-chars
)
713 (let ((case-fold-search nil
))
715 calc-user-tokens math-exp-str math-exp-pos
)
717 (setq math-exp-token
'punc
718 math-expr-data
(math-match-substring math-exp-str
0)
719 math-exp-pos
(match-end 0)))
720 ((or (and (>= ch ?a
) (<= ch ?z
))
721 (and (>= ch ?A
) (<= ch ?Z
)))
724 ((and (memq calc-language calc-lang-allow-underscores
)
725 (memq calc-language calc-lang-allow-percentsigns
))
727 ((memq calc-language calc-lang-allow-underscores
)
729 (t "[a-zA-Z0-9'#]*"))
730 math-exp-str math-exp-pos
)
731 (setq math-exp-token
'symbol
732 math-exp-pos
(match-end 0)
733 math-expr-data
(math-restore-dashes
734 (math-match-substring math-exp-str
0)))
735 (if (setq adfn
(get calc-language
'math-lang-adjust-words
))
737 ((or (and (>= ch ?
0) (<= ch ?
9))
739 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos
)
742 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos
)
744 (or (eq math-exp-pos
0)
745 (and (not (memq calc-language
746 calc-lang-allow-underscores
))
747 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
748 math-exp-str
(1- math-exp-pos
))
749 (1- math-exp-pos
))))))
750 (or (and (memq calc-language calc-lang-c-type-hex
)
751 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos
))
752 (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]+\\)?\"?\\)?"
753 math-exp-str math-exp-pos
))
754 (setq math-exp-token
'number
755 math-expr-data
(math-match-substring math-exp-str
0)
756 math-exp-pos
(match-end 0)))
758 (assq ch
(get calc-language
'math-lang-read-symbol
)))
762 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
764 (setq math-expr-data
(- (string-to-number (math-match-substring
766 (string-match "\\$+" math-exp-str math-exp-pos
)
767 (setq math-expr-data
(- (match-end 0) (match-beginning 0))))
768 (setq math-exp-token
'dollar
769 math-exp-pos
(match-end 0)))
771 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos
)
773 (setq math-expr-data
(string-to-number
774 (math-match-substring math-exp-str
1))
775 math-exp-pos
(match-end 0))
776 (setq math-expr-data
1
777 math-exp-pos
(1+ math-exp-pos
)))
778 (setq math-exp-token
'hash
))
779 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
780 math-exp-str math-exp-pos
)
782 (setq math-exp-token
'punc
783 math-expr-data
(math-match-substring math-exp-str
0)
784 math-exp-pos
(match-end 0)))
786 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
787 math-exp-str math-exp-pos
))
788 (setq math-exp-token
'string
789 math-expr-data
(math-match-substring math-exp-str
1)
790 math-exp-pos
(match-end 0)))
791 ((and (setq adfn
(get calc-language
'math-lang-read
))
793 (eval (nth 1 adfn
))))
794 ((eq (string-match "%%.*$" math-exp-str math-exp-pos
) math-exp-pos
)
795 (setq math-exp-pos
(match-end 0))
798 (if (setq adfn
(assq ch
(get calc-language
'math-punc-table
)))
799 (setq ch
(cdr adfn
)))
800 (setq math-exp-token
'punc
801 math-expr-data
(char-to-string ch
)
802 math-exp-pos
(1+ math-exp-pos
)))))))
804 (defconst math-alg-inequalities
805 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
806 calcFunc-eq calcFunc-neq
))
808 (defun math-read-expr-level (exp-prec &optional exp-term
)
809 (let* ((math-expr-opers (math-expr-ops))
810 (x (math-read-factor))
813 (while (and (or (and calc-user-parse-table
814 (setq op
(calc-check-user-syntax x exp-prec
))
816 op
'("2x" ident
999999 -
1)))
817 (and (setq op
(assoc math-expr-data math-expr-opers
))
819 (or (and (setq op2
(assoc
821 (cdr (memq op math-expr-opers
))))
822 (eq (= (nth 3 op
) -
1)
824 (eq (= (nth 3 op2
) -
1)
825 (not (math-factor-after)))
828 (and (or (eq (nth 2 op
) -
1)
829 (memq math-exp-token
'(symbol number dollar hash
))
830 (equal math-expr-data
"(")
831 (and (equal math-expr-data
"[")
834 'math-function-open
) "["))
835 (not (and math-exp-keep-spaces
836 (eq (car-safe x
) 'vec
)))))
837 (or (not (setq op
(assoc math-expr-data math-expr-opers
)))
839 (or (not calc-user-parse-table
)
840 (not (eq math-exp-token
'symbol
))
841 (let ((p calc-user-parse-table
))
844 (car (car (car p
)))))
846 (nth 1 (car (car p
)))
850 (setq op
(assoc "2x" math-expr-opers
))))
851 (not (and exp-term
(equal math-expr-data exp-term
)))
852 (>= (nth 2 op
) exp-prec
))
853 (if (not (equal (car op
) "2x"))
855 (and (memq (nth 1 op
) '(sdev mod
))
857 (setq x
(cond ((consp (nth 1 op
))
858 (funcall (car (nth 1 op
)) x op
))
860 (if (eq (nth 1 op
) 'ident
)
862 (if (eq (nth 1 op
) 'closing
)
863 (if (eq (nth 2 op
) exp-prec
)
867 (throw 'syntax
"Mismatched delimiters"))
868 (list (nth 1 op
) x
))))
870 (memq (nth 1 op
) math-alg-inequalities
)
871 (memq (car-safe x
) math-alg-inequalities
))
873 (math-composite-inequalities x op
))
876 (math-read-expr-level (nth 3 op
) exp-term
))))
880 ;; calc-arg-values is defined in calc-ext.el, but is used here.
881 (defvar calc-arg-values
)
884 (defun calc-check-user-syntax (&optional x prec
)
885 (let ((p calc-user-parse-table
)
890 (setq rule
(car (car p
)))
892 (and (integerp (car rule
))
894 (equal math-expr-data
895 (car (setq rule
(cdr rule
)))))
896 (equal math-expr-data
(car rule
)))))
897 (let ((save-exp-pos math-exp-pos
)
898 (save-exp-old-pos math-exp-old-pos
)
899 (save-exp-token math-exp-token
)
900 (save-exp-data math-expr-data
))
902 (setq matches
(calc-match-user-syntax rule
))))
909 (setq matches
(cons x matches
)))
910 (setq match
(cdr (car p
)))
911 (while (and (eq (car-safe match
)
913 (= (length match
) 3))
914 (setq conds
(append (math-flatten-lands
917 match
(nth 1 match
)))
918 (while (and conds match
)
920 (cond ((eq (car-safe (car conds
))
922 (setq temp
(car conds
))
923 (or (= (length temp
) 3)
924 (and (= (length temp
) 2)
925 (eq (car-safe (nth 1 temp
))
927 (= (length (nth 1 temp
)) 3)
928 (setq temp
(nth 1 temp
)))
936 args
(cons (nth 1 temp
)
938 ((and (eq (car-safe (car conds
))
940 (= (length (car conds
)) 3))
941 (setq temp
(calcFunc-vmatches
945 (nth 2 (car conds
))))
948 (while (setq temp
(cdr temp
))
949 (setq matches
(cons (nth 2 (car temp
))
951 args
(cons (nth 1 (car temp
))
954 (or (math-is-true (math-simplify
959 (setq conds
(cdr conds
)))
961 (not (setq match
(math-multi-subst
962 match args matches
)))
963 (setq math-exp-old-pos save-exp-old-pos
964 math-exp-token save-exp-token
965 math-expr-data save-exp-data
966 math-exp-pos save-exp-pos
)))))))
971 (defun calc-match-user-syntax (p &optional term
)
973 (save-exp-pos math-exp-pos
)
974 (save-exp-old-pos math-exp-old-pos
)
975 (save-exp-token math-exp-token
)
976 (save-exp-data math-expr-data
)
979 (cond ((stringp (car p
))
980 (and (equal math-expr-data
(car p
))
985 (and (setq m
(catch 'syntax
986 (math-read-expr-level
989 (if (consp (nth 1 p
))
990 (car (nth 1 (nth 1 p
)))
994 (setq matches
(nconc matches
(list m
)))))
995 ((eq (car (car p
)) '\?)
996 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
1001 (cons 'vec
(and (listp m
) m
))))))
1002 (or (listp m
) (not (nth 2 (car p
)))
1003 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
1004 (eq math-exp-token
'end
)))
1006 (setq m
(calc-match-user-syntax (nth 1 (car p
))
1007 (car (nth 2 (car p
)))))
1009 (let ((vec (cons 'vec m
))
1012 (setq opos math-exp-pos
1013 mm
(calc-match-user-syntax
1016 (car (nth 2 (car p
))))))
1017 (> math-exp-pos opos
))
1018 (setq vec
(nconc vec mm
)))
1019 (setq matches
(nconc matches
(list vec
))))
1020 (and (eq (car (car p
)) '*)
1021 (setq matches
(nconc matches
(list '(vec)))))))))
1024 (setq math-exp-pos save-exp-pos
1025 math-exp-old-pos save-exp-old-pos
1026 math-exp-token save-exp-token
1027 math-expr-data save-exp-data
1032 (defun math-remove-dashes (x)
1033 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
1035 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
1038 (defun math-remove-percentsigns (x)
1039 (if (string-match "\\`\\(.*\\)%\\(.*\\)\\'" x
)
1040 (math-remove-percentsigns
1041 (concat (math-match-substring x
1) "o'o" (math-match-substring x
2)))
1044 (defun math-restore-dashes (x)
1045 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
1046 (math-restore-dashes
1047 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
1050 (defun math-restore-placeholders (x)
1051 "Replace placeholders by the proper characters in the symbol x.
1052 This includes `#' for `_' and `'' for `%'.
1053 If the current Calc language does not use placeholders, return nil."
1054 (if (or (memq calc-language calc-lang-allow-underscores
)
1055 (memq calc-language calc-lang-allow-percentsigns
))
1056 (let ((sx (symbol-name x
)))
1057 (when (memq calc-language calc-lang-allow-percentsigns
)
1059 (setq sx
(math-to-percentsigns sx
)))
1060 (if (memq calc-language calc-lang-allow-underscores
)
1061 (setq sx
(math-string-restore-underscores sx
)))
1064 (defun math-string-restore-underscores (x)
1065 "Replace pound signs by underscores in the string x."
1066 (if (string-match "\\`\\(.*\\)#\\(.*\\)\\'" x
)
1067 (math-string-restore-underscores
1068 (concat (math-match-substring x
1) "_" (math-match-substring x
2)))
1072 (defun math-read-if (cond op
)
1073 (let ((then (math-read-expr-level 0)))
1074 (or (equal math-expr-data
":")
1075 (throw 'syntax
"Expected ':'"))
1077 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
)))))
1079 (defun math-factor-after ()
1080 (let ((math-exp-pos math-exp-pos
)
1081 math-exp-old-pos math-exp-token math-expr-data
)
1083 (or (memq math-exp-token
'(number symbol dollar hash string
))
1084 (and (assoc math-expr-data
'(("-") ("+") ("!") ("|") ("/")))
1085 (assoc (concat "u" math-expr-data
) math-expr-opers
))
1086 (eq (nth 2 (assoc math-expr-data math-expr-opers
)) -
1)
1087 (assoc math-expr-data
'(("(") ("[") ("{"))))))
1089 (defun math-read-factor ()
1090 (let ((math-expr-opers (math-expr-ops))
1092 (cond ((eq math-exp-token
'number
)
1093 (let ((num (math-read-number math-expr-data
)))
1096 (setq math-exp-old-pos math-exp-pos
)
1097 (throw 'syntax
"Bad format")))
1099 (if (and math-read-expr-quotes
1103 ((and calc-user-parse-table
1104 (setq op
(calc-check-user-syntax)))
1106 ((or (equal math-expr-data
"-")
1107 (equal math-expr-data
"+")
1108 (equal math-expr-data
"!")
1109 (equal math-expr-data
"|")
1110 (equal math-expr-data
"/"))
1111 (setq math-expr-data
(concat "u" math-expr-data
))
1113 ((and (setq op
(assoc math-expr-data math-expr-opers
))
1115 (if (consp (nth 1 op
))
1116 (funcall (car (nth 1 op
)) op
)
1118 (let ((val (math-read-expr-level (nth 3 op
))))
1119 (cond ((eq (nth 1 op
) 'ident
)
1121 ((and (Math-numberp val
)
1122 (equal (car op
) "u-"))
1124 (t (list (nth 1 op
) val
))))))
1125 ((eq math-exp-token
'symbol
)
1126 (let ((sym (intern math-expr-data
)))
1128 (if (equal math-expr-data calc-function-open
)
1129 (let ((f (assq sym math-expr-function-mapping
)))
1132 (funcall (car (cdr f
)) f sym
)
1133 (let ((args (if (or (equal math-expr-data calc-function-close
)
1134 (eq math-exp-token
'end
))
1136 (math-read-expr-list))))
1137 (if (not (or (equal math-expr-data calc-function-close
)
1138 (eq math-exp-token
'end
)))
1139 (throw 'syntax
"Expected `)'"))
1141 (if (and (memq calc-language
1142 calc-lang-parens-are-subscripts
)
1145 (let ((calc-matrix-mode 'scalar
))
1150 (symbol-name sym
)))))))
1151 (math-parse-fortran-subscr sym args
)
1154 (and (= (aref (symbol-name sym
) 0) ?
\\)
1155 (< (prefix-numeric-value calc-language-option
)
1157 (setq sym
(intern (substring (symbol-name sym
)
1159 (or (string-match "-" (symbol-name sym
))
1162 (symbol-name sym
))))))
1164 (if math-read-expr-quotes
1166 (let ((val (list 'var
1167 (intern (math-remove-dashes
1169 (if (string-match "-" (symbol-name sym
))
1171 (intern (concat "var-"
1172 (symbol-name sym
)))))))
1174 (assq (nth 1 val
) math-expr-variable-mapping
)
1175 (assq (math-restore-placeholders (nth 1 val
))
1176 math-expr-variable-mapping
))))
1177 (and v
(setq val
(if (consp (cdr v
))
1178 (funcall (car (cdr v
)) v val
)
1181 (substring (symbol-name (cdr v
))
1184 (while (and (memq calc-language
1185 calc-lang-brackets-are-subscripts
)
1186 (equal math-expr-data
"["))
1188 (let ((el (math-read-expr-list)))
1190 (setq val
(append (list 'calcFunc-subscr val
)
1192 (setq el
(cdr el
))))
1193 (if (equal math-expr-data
"]")
1195 (throw 'syntax
"Expected ']'")))
1197 ((eq math-exp-token
'dollar
)
1198 (let ((abs (if (> math-expr-data
0) math-expr-data
(- math-expr-data
))))
1199 (if (>= (length calc-dollar-values
) abs
)
1200 (let ((num math-expr-data
))
1202 (setq calc-dollar-used
(max calc-dollar-used num
))
1203 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1204 (throw 'syntax
(if calc-dollar-values
1206 "$'s not allowed in this context")))))
1207 ((eq math-exp-token
'hash
)
1208 (or calc-hashes-used
1209 (throw 'syntax
"#'s not allowed in this context"))
1211 (if (<= math-expr-data
(length calc-arg-values
))
1212 (let ((num math-expr-data
))
1214 (setq calc-hashes-used
(max calc-hashes-used num
))
1215 (nth (1- num
) calc-arg-values
))
1216 (throw 'syntax
"Too many # arguments")))
1217 ((equal math-expr-data
"(")
1218 (let* ((exp (let ((math-exp-keep-spaces nil
))
1220 (if (or (equal math-expr-data
"\\dots")
1221 (equal math-expr-data
"\\ldots"))
1222 '(neg (var inf var-inf
))
1223 (math-read-expr-level 0)))))
1224 (let ((math-exp-keep-spaces nil
))
1226 ((equal math-expr-data
",")
1229 (let ((exp2 (math-read-expr-level 0)))
1231 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1232 (math-normalize (list 'cplx exp exp2
))
1233 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1234 ((equal math-expr-data
";")
1237 (let ((exp2 (math-read-expr-level 0)))
1238 (setq exp
(if (and exp2
(Math-realp exp
)
1240 (math-normalize (list 'polar exp exp2
))
1245 (math-to-radians-2 exp2
)
1246 '(var i var-i
)))))))))
1247 ((or (equal math-expr-data
"\\dots")
1248 (equal math-expr-data
"\\ldots"))
1251 (let ((exp2 (if (or (equal math-expr-data
")")
1252 (equal math-expr-data
"]")
1253 (eq math-exp-token
'end
))
1255 (math-read-expr-level 0))))
1258 (if (equal math-expr-data
")") 0 1)
1261 (if (not (or (equal math-expr-data
")")
1262 (and (equal math-expr-data
"]") (eq (car-safe exp
) 'intv
))
1263 (eq math-exp-token
'end
)))
1264 (throw 'syntax
"Expected `)'"))
1267 ((eq math-exp-token
'string
)
1270 ((equal math-expr-data
"[")
1272 (math-read-brackets t
"]"))
1273 ((equal math-expr-data
"{")
1275 (math-read-brackets nil
"}"))
1276 ((equal math-expr-data
"<")
1278 (math-read-angle-brackets))
1279 (t (throw 'syntax
"Expected a number")))))
1281 (provide 'calc-aent
)
1284 ;; generated-autoload-file: "calc-loaddefs.el"
1287 ;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1288 ;;; calc-aent.el ends here