1 ;; Calculator for GNU Emacs, part I [calc-aent.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc.el.
28 (eval-when-compile '(require calc-macs
))
30 (defun calc-Need-calc-aent () nil
)
33 (defun calc-do-quick-calc ()
35 (if (eq major-mode
'calc-mode
)
36 (calc-algebraic-entry t
)
40 (let* ((calc-command-flags nil
)
41 (calc-dollar-values calc-quick-prev-results
)
43 (enable-recursive-minibuffers t
)
44 (calc-language (if (memq calc-language
'(nil big
))
46 (entry (calc-do-alg-entry "" "Quick calc: " t
))
47 (alg-exp (mapcar (function
49 (if (and (not calc-extensions-loaded
)
50 calc-previous-alg-entry
52 "\\`[-0-9._+*/^() ]+\\'"
53 calc-previous-alg-entry
))
56 (math-evaluate-expr x
))))
58 (if (and (= (length alg-exp
) 1)
59 (eq (car-safe (car alg-exp
)) 'calcFunc-assign
)
60 (= (length (car alg-exp
)) 3)
61 (eq (car-safe (nth 1 (car alg-exp
))) 'var
))
64 (set (nth 2 (nth 1 (car alg-exp
))) (nth 2 (car alg-exp
)))
65 (calc-refresh-evaltos (nth 2 (nth 1 (car alg-exp
))))
66 (setq alg-exp
(list (nth 2 (car alg-exp
))))))
67 (setq calc-quick-prev-results alg-exp
68 buf
(mapconcat (function (lambda (x)
69 (math-format-value x
1000)))
73 (if (and (= (length alg-exp
) 1)
74 (memq (car-safe (car alg-exp
)) '(nil bigpos bigneg
))
76 (= calc-number-radix
10))
77 (setq buf
(concat buf
" ("
78 (let ((calc-number-radix 16))
79 (math-format-value (car alg-exp
) 1000))
81 (let ((calc-number-radix 8))
82 (math-format-value (car alg-exp
) 1000))
83 (if (and (integerp (car alg-exp
))
85 (< (car alg-exp
) 127))
86 (format ", \"%c\"" (car alg-exp
))
89 (if (and (< (length buf
) (frame-width)) (= (length entry
) 1)
90 calc-extensions-loaded
)
91 (let ((long (concat (math-format-value (car entry
) 1000)
93 (if (<= (length long
) (- (frame-width) 8))
96 (message "Result: %s" buf
)))
97 (if (eq last-command-char
10)
99 (setq kill-ring
(cons shortbuf kill-ring
))
100 (if (> (length kill-ring
) kill-ring-max
)
101 (setcdr (nthcdr (1- kill-ring-max
) kill-ring
) nil
))
102 (setq kill-ring-yank-pointer kill-ring
))))
105 (defun calc-do-calc-eval (str separator args
)
111 ((and (consp str
) (not (symbolp (car str
))))
112 (let ((calc-language nil
)
113 (math-expr-opers math-standard-opers
)
114 (calc-internal-prec 12)
116 (calc-symbolic-mode nil
)
117 (calc-matrix-mode nil
)
118 (calc-angle-mode 'deg
)
119 (calc-number-radix 10)
120 (calc-leading-zeros nil
)
121 (calc-group-digits nil
)
122 (calc-point-char ".")
123 (calc-frac-format '(":" nil
))
124 (calc-prefer-frac nil
)
125 (calc-hms-format "%s@ %s' %s\"")
126 (calc-date-format '((H ":" mm C SS pp
" ")
127 Www
" " Mmm
" " D
", " YYYY
))
128 (calc-float-format '(float 0))
129 (calc-full-float-format '(float 0))
130 (calc-complex-format nil
)
131 (calc-matrix-just nil
)
132 (calc-full-vectors t
)
133 (calc-break-vectors nil
)
134 (calc-vector-commas ",")
135 (calc-vector-brackets "[]")
136 (calc-matrix-brackets '(R O
))
137 (calc-complex-mode 'cplx
)
138 (calc-infinite-mode nil
)
139 (calc-display-strings nil
)
140 (calc-simplify-mode nil
)
141 (calc-display-working-message 'lots
)
144 (set (car strp
) (nth 1 strp
))
145 (setq strp
(cdr (cdr strp
))))
146 (calc-do-calc-eval (car str
) separator args
)))
147 ((eq separator
'eval
)
149 ((eq separator
'macro
)
151 (let* ((calc-buffer (current-buffer))
152 (calc-window (get-buffer-window calc-buffer
))
153 (save-window (selected-window)))
157 (select-window calc-window
)
158 (calc-execute-kbd-macro str nil
(car args
)))
159 (and (window-point save-window
)
160 (select-window save-window
)))
161 (save-window-excursion
162 (select-window (get-largest-window))
163 (switch-to-buffer calc-buffer
)
164 (calc-execute-kbd-macro str nil
(car args
)))))
167 (or (not (integerp str
))
169 (calc-pop (min str
(calc-stack-size))))
174 (<= str
(calc-stack-size))
175 (math-format-value (calc-top-n str
(car args
)) 1000)))
176 ((eq separator
'rawtop
)
179 (<= str
(calc-stack-size))
180 (calc-top-n str
(car args
))))
182 (let* ((calc-command-flags nil
)
184 (calc-language (if (memq calc-language
'(nil big
))
185 'flat calc-language
))
186 (calc-dollar-values (mapcar
191 (setq x
(math-read-exprs x
))
201 (res (if (stringp str
)
202 (math-read-exprs str
)
205 (if (eq (car res
) 'error
)
206 (calc-eval-error (cdr res
))
207 (setq res
(mapcar 'calc-normalize res
))
208 (and (memq 'clear-message calc-command-flags
)
210 (cond ((eq separator
'pred
)
212 (if (= (length res
) 1)
213 (math-is-true (car res
))
214 (calc-eval-error '(0 "Single value expected"))))
216 (if (= (length res
) 1)
218 (calc-eval-error '(0 "Single value expected"))))
219 ((eq separator
'list
)
221 ((memq separator
'(num rawnum
))
222 (if (= (length res
) 1)
223 (if (math-constp (car res
))
224 (if (eq separator
'num
)
225 (math-format-value (car res
) 1000)
230 (calc-explain-why (car calc-next-why
))
231 "Number expected"))))
232 (calc-eval-error '(0 "Single value expected"))))
233 ((eq separator
'push
)
237 (setq buf
(concat buf
238 (and buf
(or separator
", "))
239 (math-format-value (car res
) 1000))
244 (defun calc-eval-error (msg)
245 (if (and (boundp 'calc-eval-error
)
247 (if (eq calc-eval-error
'string
)
249 (error "%s" (nth 1 msg
)))
254 ;;;; Reading an expression in algebraic form.
256 (defun calc-auto-algebraic-entry (&optional prefix
)
258 (calc-algebraic-entry prefix t
)
261 (defun calc-algebraic-entry (&optional prefix auto
)
264 (let ((calc-language (if prefix nil calc-language
))
265 (math-expr-opers (if prefix math-standard-opers math-expr-opers
)))
266 (calc-alg-entry (and auto
(char-to-string last-command-char
)))))
269 (defun calc-alg-entry (&optional initial prompt
)
270 (let* ((sel-mode nil
)
271 (calc-dollar-values (mapcar 'calc-get-stack-element
272 (nthcdr calc-stack-top calc-stack
)))
275 (alg-exp (calc-do-alg-entry initial prompt t
)))
276 (if (stringp alg-exp
)
279 (calc-alg-edit alg-exp
))
280 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j
)
283 (nvals (mapcar 'calc-normalize alg-exp
)))
285 (calc-record (if calc-extensions-loaded
(car alg-exp
) (car nvals
))
287 (calc-pop-push-record-list calc-dollar-used
288 (and (not (equal (car alg-exp
)
290 calc-extensions-loaded
293 (setq alg-exp
(cdr alg-exp
)
295 calc-dollar-used
0)))
299 (defun calc-do-alg-entry (&optional initial prompt no-normalize
)
300 (let* ((calc-buffer (current-buffer))
301 (blink-paren-hook 'calcAlg-blink-matching-open
)
303 (if (boundp 'calc-alg-ent-map
)
305 (setq calc-alg-ent-map
(copy-keymap minibuffer-local-map
))
306 (define-key calc-alg-ent-map
"'" 'calcAlg-previous
)
307 (define-key calc-alg-ent-map
"`" 'calcAlg-edit
)
308 (define-key calc-alg-ent-map
"\C-m" 'calcAlg-enter
)
309 (define-key calc-alg-ent-map
"\C-j" 'calcAlg-enter
)
310 (or calc-emacs-type-19
312 (setq calc-alg-ent-esc-map
(copy-sequence esc-map
))
314 (aset calc-alg-ent-esc-map i
'calcAlg-escape
)
316 (or calc-emacs-type-19
317 (define-key calc-alg-ent-map
"\e" nil
))
318 (if (eq calc-algebraic-mode
'total
)
319 (define-key calc-alg-ent-map
"\e" calc-alg-ent-esc-map
)
320 (define-key calc-alg-ent-map
"\ep" 'calcAlg-plus-minus
)
321 (define-key calc-alg-ent-map
"\em" 'calcAlg-mod
)
322 (define-key calc-alg-ent-map
"\e=" 'calcAlg-equals
)
323 (define-key calc-alg-ent-map
"\e\r" 'calcAlg-equals
)
324 (define-key calc-alg-ent-map
"\e%" 'self-insert-command
))
325 (setq calc-aborted-prefix nil
)
326 (let ((buf (read-from-minibuffer (or prompt
"Algebraic: ")
328 calc-alg-ent-map nil
)))
329 (if (eq alg-exp
'error
)
330 (if (eq (car-safe (setq alg-exp
(math-read-exprs buf
))) 'error
)
332 (setq calc-aborted-prefix
"alg'")
334 (and alg-exp
(setq alg-exp
(mapcar 'calc-normalize alg-exp
))))
338 (defun calcAlg-plus-minus ()
340 (if (calc-minibuffer-contains ".* \\'")
345 (defun calcAlg-mod ()
347 (if (not (calc-minibuffer-contains ".* \\'"))
349 (if (calc-minibuffer-contains ".* mod +\\'")
350 (if calc-previous-modulo
351 (insert (math-format-flat-expr calc-previous-modulo
0))
356 (defun calcAlg-previous ()
358 (if (calc-minibuffer-contains "\\`\\'")
359 (if calc-previous-alg-entry
360 (insert calc-previous-alg-entry
)
365 (defun calcAlg-equals ()
370 (progn (setq prefix-arg
(length alg-exp
))
371 (calc-unread-command ?
=))))
374 (defun calcAlg-escape ()
376 (calc-unread-command)
379 (use-local-map calc-mode-map
))
383 (defun calcAlg-edit ()
385 (if (or (not calc-plain-entry
)
386 (calc-minibuffer-contains
387 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
389 (setq alg-exp
(minibuffer-contents))
390 (and (> (length alg-exp
) 0) (setq calc-previous-alg-entry alg-exp
))
393 (setq calc-plain-entry nil
)
395 (defun calcAlg-enter ()
397 (let* ((str (minibuffer-contents))
398 (exp (and (> (length str
) 0)
400 (set-buffer calc-buffer
)
401 (math-read-exprs str
)))))
402 (if (eq (car-safe exp
) 'error
)
404 (goto-char (minibuffer-prompt-end))
405 (forward-char (nth 1 exp
))
407 (calc-temp-minibuffer-message
408 (concat " [" (or (nth 2 exp
) "Error") "]"))
409 (calc-clear-unread-commands))
410 (setq alg-exp
(if (calc-minibuffer-contains "\\` *\\[ *\\'")
413 (and (> (length str
) 0) (setq calc-previous-alg-entry str
))
417 (defun calcAlg-blink-matching-open ()
418 (let ((oldpos (point))
422 (setq blinkpos
(scan-sexps oldpos -
1))
425 (> oldpos
(1+ (point-min)))
426 (or (and (= (char-after (1- oldpos
)) ?\
))
427 (= (char-after blinkpos
) ?\
[))
428 (and (= (char-after (1- oldpos
)) ?\
])
429 (= (char-after blinkpos
) ?\
()))
432 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
433 (let ((saved (aref (syntax-table) (char-after blinkpos
))))
436 (aset (syntax-table) (char-after blinkpos
)
437 (+ (logand saved
255)
438 (lsh (char-after (1- oldpos
)) 8)))
439 (blink-matching-open))
440 (aset (syntax-table) (char-after blinkpos
) saved
)))
441 (blink-matching-open)))
445 (defun calc-alg-digit-entry ()
447 (cond ((eq last-command-char ?e
)
448 (if (> calc-number-radix
14) (format "%d.^" calc-number-radix
) "1e"))
449 ((eq last-command-char ?
#) (format "%d#" calc-number-radix
))
450 ((eq last-command-char ?_
) "-")
451 ((eq last-command-char ?
@) "0@ ")
452 (t (char-to-string last-command-char
))))
455 (defun calcDigit-algebraic ()
457 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
459 (setq calc-digit-value
(minibuffer-contents))
463 (defun calcDigit-edit ()
465 (calc-unread-command)
466 (setq calc-digit-value
(minibuffer-contents))
471 ;;; Algebraic expression parsing. [Public]
473 (defun math-read-exprs (exp-str)
476 (exp-keep-spaces nil
)
478 (if calc-language-input-filter
479 (setq exp-str
(funcall calc-language-input-filter exp-str
)))
480 (while (setq exp-token
(string-match "\\.\\.\\([^.]\\|.[^.]\\)" exp-str
))
481 (setq exp-str
(concat (substring exp-str
0 exp-token
) "\\dots"
482 (substring exp-str
(+ exp-token
2)))))
483 (math-build-parse-table)
485 (let ((val (catch 'syntax
(math-read-expr-list))))
487 (list 'error exp-old-pos val
)
488 (if (equal exp-token
'end
)
490 (list 'error exp-old-pos
"Syntax error")))))
493 (defun math-read-expr-list ()
494 (let* ((exp-keep-spaces nil
)
495 (val (list (math-read-expr-level 0)))
497 (while (equal exp-data
",")
499 (let ((rest (list (math-read-expr-level 0))))
505 (setq calc-user-parse-table nil
)
506 (setq calc-last-main-parse-table nil
)
507 (setq calc-last-lang-parse-table nil
)
508 (setq calc-user-tokens nil
)
509 (setq calc-user-token-chars nil
)
511 (defun math-build-parse-table ()
512 (let ((mtab (cdr (assq nil calc-user-parse-tables
)))
513 (ltab (cdr (assq calc-language calc-user-parse-tables
))))
514 (or (and (eq mtab calc-last-main-parse-table
)
515 (eq ltab calc-last-lang-parse-table
))
516 (let ((p (append mtab ltab
))
518 (setq calc-user-parse-table p
)
519 (setq calc-user-token-chars nil
)
521 (math-find-user-tokens (car (car p
)))
523 (setq calc-user-tokens
(mapconcat 'identity
524 (sort (mapcar 'car toks
)
525 (function (lambda (x y
)
529 calc-last-main-parse-table mtab
530 calc-last-lang-parse-table ltab
))))
533 (defun math-find-user-tokens (p) ; uses "toks"
535 (cond ((and (stringp (car p
))
536 (or (> (length (car p
)) 1) (equal (car p
) "$")
537 (equal (car p
) "\""))
538 (string-match "[^a-zA-Z0-9]" (car p
)))
539 (let ((s (regexp-quote (car p
))))
540 (if (string-match "\\`[a-zA-Z0-9]" s
)
541 (setq s
(concat "\\<" s
)))
542 (if (string-match "[a-zA-Z0-9]\\'" s
)
543 (setq s
(concat s
"\\>")))
546 (setq toks
(cons (list s
) toks
))
547 (or (memq (aref (car p
) 0) calc-user-token-chars
)
548 (setq calc-user-token-chars
549 (cons (aref (car p
) 0)
550 calc-user-token-chars
)))))))
552 (math-find-user-tokens (nth 1 (car p
)))
553 (or (eq (car (car p
)) '\?)
554 (math-find-user-tokens (nth 2 (car p
))))))
558 (defun math-read-token ()
559 (if (>= exp-pos
(length exp-str
))
560 (setq exp-old-pos exp-pos
563 (let ((ch (aref exp-str exp-pos
)))
564 (setq exp-old-pos exp-pos
)
565 (cond ((memq ch
'(32 10 9))
566 (setq exp-pos
(1+ exp-pos
))
568 (setq exp-token
'space
571 ((and (memq ch calc-user-token-chars
)
572 (let ((case-fold-search nil
))
573 (eq (string-match calc-user-tokens exp-str exp-pos
)
575 (setq exp-token
'punc
576 exp-data
(math-match-substring exp-str
0)
577 exp-pos
(match-end 0)))
578 ((or (and (>= ch ?a
) (<= ch ?z
))
579 (and (>= ch ?A
) (<= ch ?Z
)))
580 (string-match (if (memq calc-language
'(c fortran pascal maple
))
584 (setq exp-token
'symbol
585 exp-pos
(match-end 0)
586 exp-data
(math-restore-dashes
587 (math-match-substring exp-str
0)))
588 (if (eq calc-language
'eqn
)
589 (let ((code (assoc exp-data math-eqn-ignore-words
)))
593 ((consp (nth 1 code
))
595 (if (assoc exp-data
(cdr code
))
596 (setq exp-data
(format "%s %s"
597 (car code
) exp-data
))))
598 ((eq (nth 1 code
) 'punc
)
599 (setq exp-token
'punc
600 exp-data
(nth 2 code
)))
603 (math-read-token))))))
604 ((or (and (>= ch ?
0) (<= ch ?
9))
606 (eq (string-match "\\.[0-9]" exp-str exp-pos
) exp-pos
))
608 (eq (string-match "_\\.?[0-9]" exp-str exp-pos
) exp-pos
)
610 (and (memq calc-language
'(nil flat big unform
612 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
613 exp-str
(1- exp-pos
))
615 (or (and (eq calc-language
'c
)
616 (string-match "0[xX][0-9a-fA-F]+" exp-str exp-pos
))
617 (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]+\\)?\"?\\)?" exp-str exp-pos
))
618 (setq exp-token
'number
619 exp-data
(math-match-substring exp-str
0)
620 exp-pos
(match-end 0)))
622 (if (and (eq calc-language
'pascal
)
624 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
627 (setq exp-token
'number
628 exp-data
(math-match-substring exp-str
1)
629 exp-pos
(match-end 1))
630 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" exp-str exp-pos
)
632 (setq exp-data
(- (string-to-int (math-match-substring
634 (string-match "\\$+" exp-str exp-pos
)
635 (setq exp-data
(- (match-end 0) (match-beginning 0))))
636 (setq exp-token
'dollar
637 exp-pos
(match-end 0))))
639 (if (eq (string-match "#\\([1-9][0-9]*\\)" exp-str exp-pos
)
641 (setq exp-data
(string-to-int
642 (math-match-substring exp-str
1))
643 exp-pos
(match-end 0))
645 exp-pos
(1+ exp-pos
)))
646 (setq exp-token
'hash
))
647 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
650 (setq exp-token
'punc
651 exp-data
(math-match-substring exp-str
0)
652 exp-pos
(match-end 0)))
654 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)" exp-str exp-pos
))
655 (if (eq calc-language
'eqn
)
657 (setq exp-str
(copy-sequence exp-str
))
658 (aset exp-str
(match-beginning 1) ?\
{)
659 (if (< (match-end 1) (length exp-str
))
660 (aset exp-str
(match-end 1) ?\
}))
662 (setq exp-token
'string
663 exp-data
(math-match-substring exp-str
1)
664 exp-pos
(match-end 0))))
665 ((and (= ch ?
\\) (eq calc-language
'tex
)
666 (< exp-pos
(1- (length exp-str
))))
667 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}" exp-str exp-pos
)
668 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)" exp-str exp-pos
))
669 (setq exp-token
'symbol
670 exp-pos
(match-end 0)
671 exp-data
(math-restore-dashes
672 (math-match-substring exp-str
1)))
673 (let ((code (assoc exp-data math-tex-ignore-words
)))
677 ((eq (nth 1 code
) 'punc
)
678 (setq exp-token
'punc
679 exp-data
(nth 2 code
)))
680 ((and (eq (nth 1 code
) 'mat
)
681 (string-match " *{" exp-str exp-pos
))
682 (setq exp-pos
(match-end 0)
685 (let ((right (string-match "}" exp-str exp-pos
)))
687 (setq exp-str
(copy-sequence exp-str
))
688 (aset exp-str right ?\
])))))))
689 ((and (= ch ?\.
) (eq calc-language
'fortran
)
690 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
691 exp-str exp-pos
) exp-pos
))
692 (setq exp-token
'punc
693 exp-data
(upcase (math-match-substring exp-str
0))
694 exp-pos
(match-end 0)))
695 ((and (eq calc-language
'math
)
696 (eq (string-match "\\[\\[\\|->\\|:>" exp-str exp-pos
)
698 (setq exp-token
'punc
699 exp-data
(math-match-substring exp-str
0)
700 exp-pos
(match-end 0)))
701 ((and (eq calc-language
'eqn
)
702 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
705 (setq exp-token
'punc
706 exp-data
(math-match-substring exp-str
0)
707 exp-pos
(match-end 0))
708 (and (eq (string-match "\\\\dots\\." exp-str exp-pos
) exp-pos
)
709 (setq exp-pos
(match-end 0)))
710 (if (memq (aref exp-data
0) '(?~ ?^
))
712 ((eq (string-match "%%.*$" exp-str exp-pos
) exp-pos
)
713 (setq exp-pos
(match-end 0))
716 (if (and (eq ch ?\
{) (memq calc-language
'(tex eqn
)))
718 (if (and (eq ch ?\
}) (memq calc-language
'(tex eqn
)))
720 (if (and (eq ch ?\
&) (eq calc-language
'tex
))
722 (setq exp-token
'punc
723 exp-data
(char-to-string ch
)
724 exp-pos
(1+ exp-pos
))))))
728 (defun math-read-expr-level (exp-prec &optional exp-term
)
729 (let* ((x (math-read-factor)) (first t
) op op2
)
730 (while (and (or (and calc-user-parse-table
731 (setq op
(calc-check-user-syntax x exp-prec
))
733 op
'("2x" ident
999999 -
1)))
734 (and (setq op
(assoc exp-data math-expr-opers
))
736 (or (and (setq op2
(assoc
738 (cdr (memq op math-expr-opers
))))
739 (eq (= (nth 3 op
) -
1)
741 (eq (= (nth 3 op2
) -
1)
742 (not (math-factor-after)))
745 (and (or (eq (nth 2 op
) -
1)
746 (memq exp-token
'(symbol number dollar hash
))
748 (and (equal exp-data
"[")
749 (not (eq calc-language
'math
))
750 (not (and exp-keep-spaces
751 (eq (car-safe x
) 'vec
)))))
752 (or (not (setq op
(assoc exp-data math-expr-opers
)))
754 (or (not calc-user-parse-table
)
755 (not (eq exp-token
'symbol
))
756 (let ((p calc-user-parse-table
))
759 (car (car (car p
)))))
761 (nth 1 (car (car p
)))
765 (setq op
(assoc "2x" math-expr-opers
))))
766 (not (and exp-term
(equal exp-data exp-term
)))
767 (>= (nth 2 op
) exp-prec
))
768 (if (not (equal (car op
) "2x"))
770 (and (memq (nth 1 op
) '(sdev mod
))
772 (setq x
(cond ((consp (nth 1 op
))
773 (funcall (car (nth 1 op
)) x op
))
775 (if (eq (nth 1 op
) 'ident
)
777 (if (eq (nth 1 op
) 'closing
)
778 (if (eq (nth 2 op
) exp-prec
)
782 (throw 'syntax
"Mismatched delimiters"))
783 (list (nth 1 op
) x
))))
785 (memq (nth 1 op
) math-alg-inequalities
)
786 (memq (car-safe x
) math-alg-inequalities
))
788 (math-composite-inequalities x op
))
791 (math-read-expr-level (nth 3 op
) exp-term
))))
796 (defun calc-check-user-syntax (&optional x prec
)
797 (let ((p calc-user-parse-table
)
802 (setq rule
(car (car p
)))
804 (and (integerp (car rule
))
807 (car (setq rule
(cdr rule
)))))
808 (equal exp-data
(car rule
)))))
809 (let ((save-exp-pos exp-pos
)
810 (save-exp-old-pos exp-old-pos
)
811 (save-exp-token exp-token
)
812 (save-exp-data exp-data
))
814 (setq matches
(calc-match-user-syntax rule
))))
821 (setq matches
(cons x matches
)))
822 (setq match
(cdr (car p
)))
823 (while (and (eq (car-safe match
)
825 (= (length match
) 3))
826 (setq conds
(append (math-flatten-lands
829 match
(nth 1 match
)))
830 (while (and conds match
)
832 (cond ((eq (car-safe (car conds
))
834 (setq temp
(car conds
))
835 (or (= (length temp
) 3)
836 (and (= (length temp
) 2)
837 (eq (car-safe (nth 1 temp
))
839 (= (length (nth 1 temp
)) 3)
840 (setq temp
(nth 1 temp
)))
848 args
(cons (nth 1 temp
)
850 ((and (eq (car-safe (car conds
))
852 (= (length (car conds
)) 3))
853 (setq temp
(calcFunc-vmatches
857 (nth 2 (car conds
))))
860 (while (setq temp
(cdr temp
))
861 (setq matches
(cons (nth 2 (car temp
))
863 args
(cons (nth 1 (car temp
))
866 (or (math-is-true (math-simplify
871 (setq conds
(cdr conds
)))
873 (not (setq match
(math-multi-subst
874 match args matches
)))
875 (setq exp-old-pos save-exp-old-pos
876 exp-token save-exp-token
877 exp-data save-exp-data
878 exp-pos save-exp-pos
)))))))
883 (defun calc-match-user-syntax (p &optional term
)
885 (save-exp-pos exp-pos
)
886 (save-exp-old-pos exp-old-pos
)
887 (save-exp-token exp-token
)
888 (save-exp-data exp-data
))
890 (cond ((stringp (car p
))
891 (and (equal exp-data
(car p
))
896 (and (setq m
(catch 'syntax
897 (math-read-expr-level
900 (if (consp (nth 1 p
))
901 (car (nth 1 (nth 1 p
)))
905 (setq matches
(nconc matches
(list m
)))))
906 ((eq (car (car p
)) '\?)
907 (setq m
(calc-match-user-syntax (nth 1 (car p
))))
912 (cons 'vec
(and (listp m
) m
))))))
913 (or (listp m
) (not (nth 2 (car p
)))
914 (not (eq (aref (car (nth 2 (car p
))) 0) ?\$
))
915 (eq exp-token
'end
)))
917 (setq m
(calc-match-user-syntax (nth 1 (car p
))
918 (car (nth 2 (car p
)))))
920 (let ((vec (cons 'vec m
))
924 mm
(calc-match-user-syntax
927 (car (nth 2 (car p
))))))
929 (setq vec
(nconc vec mm
)))
930 (setq matches
(nconc matches
(list vec
))))
931 (and (eq (car (car p
)) '*)
932 (setq matches
(nconc matches
(list '(vec)))))))))
935 (setq exp-pos save-exp-pos
936 exp-old-pos save-exp-old-pos
937 exp-token save-exp-token
938 exp-data save-exp-data
943 (defconst math-alg-inequalities
944 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
945 calcFunc-eq calcFunc-neq
))
947 (defun math-remove-dashes (x)
948 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x
)
950 (concat (math-match-substring x
1) "#" (math-match-substring x
2)))
954 (defun math-restore-dashes (x)
955 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x
)
957 (concat (math-match-substring x
1) "-" (math-match-substring x
2)))
961 (defun math-read-if (cond op
)
962 (let ((then (math-read-expr-level 0)))
963 (or (equal exp-data
":")
964 (throw 'syntax
"Expected ':'"))
966 (list 'calcFunc-if cond then
(math-read-expr-level (nth 3 op
))))
969 (defun math-factor-after ()
970 (let ((exp-pos exp-pos
)
971 exp-old-pos exp-token exp-data
)
973 (or (memq exp-token
'(number symbol dollar hash string
))
974 (and (assoc exp-data
'(("-") ("+") ("!") ("|") ("/")))
975 (assoc (concat "u" exp-data
) math-expr-opers
))
976 (eq (nth 2 (assoc exp-data math-expr-opers
)) -
1)
977 (assoc exp-data
'(("(") ("[") ("{")))))
980 (defun math-read-factor ()
982 (cond ((eq exp-token
'number
)
983 (let ((num (math-read-number exp-data
)))
986 (setq exp-old-pos exp-pos
)
987 (throw 'syntax
"Bad format")))
989 (if (and math-read-expr-quotes
993 ((and calc-user-parse-table
994 (setq op
(calc-check-user-syntax)))
996 ((or (equal exp-data
"-")
1000 (equal exp-data
"/"))
1001 (setq exp-data
(concat "u" exp-data
))
1003 ((and (setq op
(assoc exp-data math-expr-opers
))
1005 (if (consp (nth 1 op
))
1006 (funcall (car (nth 1 op
)) op
)
1008 (let ((val (math-read-expr-level (nth 3 op
))))
1009 (cond ((eq (nth 1 op
) 'ident
)
1011 ((and (Math-numberp val
)
1012 (equal (car op
) "u-"))
1014 (t (list (nth 1 op
) val
))))))
1015 ((eq exp-token
'symbol
)
1016 (let ((sym (intern exp-data
)))
1018 (if (equal exp-data calc-function-open
)
1019 (let ((f (assq sym math-expr-function-mapping
)))
1022 (funcall (car (cdr f
)) f sym
)
1023 (let ((args (if (or (equal exp-data calc-function-close
)
1024 (eq exp-token
'end
))
1026 (math-read-expr-list))))
1027 (if (not (or (equal exp-data calc-function-close
)
1028 (eq exp-token
'end
)))
1029 (throw 'syntax
"Expected `)'"))
1031 (if (and (eq calc-language
'fortran
) args
1033 (let ((calc-matrix-mode 'scalar
))
1038 (symbol-name sym
)))))))
1039 (math-parse-fortran-subscr sym args
)
1042 (and (= (aref (symbol-name sym
) 0) ?
\\)
1043 (< (prefix-numeric-value calc-language-option
)
1045 (setq sym
(intern (substring (symbol-name sym
)
1047 (or (string-match "-" (symbol-name sym
))
1050 (symbol-name sym
))))))
1052 (if math-read-expr-quotes
1054 (let ((val (list 'var
1055 (intern (math-remove-dashes
1057 (if (string-match "-" (symbol-name sym
))
1059 (intern (concat "var-"
1060 (symbol-name sym
)))))))
1061 (let ((v (assq (nth 1 val
) math-expr-variable-mapping
)))
1062 (and v
(setq val
(if (consp (cdr v
))
1063 (funcall (car (cdr v
)) v val
)
1066 (substring (symbol-name (cdr v
))
1069 (while (and (memq calc-language
'(c pascal maple
))
1070 (equal exp-data
"["))
1072 (setq val
(append (list 'calcFunc-subscr val
)
1073 (math-read-expr-list)))
1074 (if (equal exp-data
"]")
1076 (throw 'syntax
"Expected ']'")))
1078 ((eq exp-token
'dollar
)
1079 (let ((abs (if (> exp-data
0) exp-data
(- exp-data
))))
1080 (if (>= (length calc-dollar-values
) abs
)
1081 (let ((num exp-data
))
1083 (setq calc-dollar-used
(max calc-dollar-used num
))
1084 (math-check-complete (nth (1- abs
) calc-dollar-values
)))
1085 (throw 'syntax
(if calc-dollar-values
1087 "$'s not allowed in this context")))))
1088 ((eq exp-token
'hash
)
1089 (or calc-hashes-used
1090 (throw 'syntax
"#'s not allowed in this context"))
1092 (if (<= exp-data
(length calc-arg-values
))
1093 (let ((num exp-data
))
1095 (setq calc-hashes-used
(max calc-hashes-used num
))
1096 (nth (1- num
) calc-arg-values
))
1097 (throw 'syntax
"Too many # arguments")))
1098 ((equal exp-data
"(")
1099 (let* ((exp (let ((exp-keep-spaces nil
))
1101 (if (or (equal exp-data
"\\dots")
1102 (equal exp-data
"\\ldots"))
1103 '(neg (var inf var-inf
))
1104 (math-read-expr-level 0)))))
1105 (let ((exp-keep-spaces nil
))
1107 ((equal exp-data
",")
1110 (let ((exp2 (math-read-expr-level 0)))
1112 (if (and exp2
(Math-realp exp
) (Math-realp exp2
))
1113 (math-normalize (list 'cplx exp exp2
))
1114 (list '+ exp
(list '* exp2
'(var i var-i
))))))))
1115 ((equal exp-data
";")
1118 (let ((exp2 (math-read-expr-level 0)))
1119 (setq exp
(if (and exp2
(Math-realp exp
)
1121 (math-normalize (list 'polar exp exp2
))
1126 (math-to-radians-2 exp2
)
1127 '(var i var-i
)))))))))
1128 ((or (equal exp-data
"\\dots")
1129 (equal exp-data
"\\ldots"))
1132 (let ((exp2 (if (or (equal exp-data
")")
1133 (equal exp-data
"]")
1134 (eq exp-token
'end
))
1136 (math-read-expr-level 0))))
1139 (if (equal exp-data
")") 0 1)
1142 (if (not (or (equal exp-data
")")
1143 (and (equal exp-data
"]") (eq (car-safe exp
) 'intv
))
1144 (eq exp-token
'end
)))
1145 (throw 'syntax
"Expected `)'"))
1148 ((eq exp-token
'string
)
1151 ((equal exp-data
"[")
1153 (math-read-brackets t
"]"))
1154 ((equal exp-data
"{")
1156 (math-read-brackets nil
"}"))
1157 ((equal exp-data
"<")
1159 (math-read-angle-brackets))
1160 (t (throw 'syntax
"Expected a number"))))