(mouse-1-click-follows-link): Doc fix.
[bpt/emacs.git] / lisp / calc / calc-aent.el
CommitLineData
3132f345
CW
1;;; calc-aent.el --- algebraic entry functions for Calc
2
4db56602 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
3132f345
CW
4
5;; Author: Dave Gillespie <daveg@synaptics.com>
c4446fc2 6;; Maintainer: Jay Belanger <belanger@truman.edu>
136211a9
EZ
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY. No author or distributor
12;; accepts responsibility to anyone for the consequences of using it
13;; or for whether it serves any particular purpose or works at all,
14;; unless he says so in writing. Refer to the GNU Emacs General Public
15;; License for full details.
16
17;; Everyone is granted permission to copy, modify and redistribute
18;; GNU Emacs, but only under the conditions described in the
19;; GNU Emacs General Public License. A copy of this license is
20;; supposed to have been given to you along with GNU Emacs so you
21;; can know your rights and responsibilities. It should be in a
22;; file named COPYING. Among other things, the copyright notice
23;; and this notice must be preserved on all copies.
24
3132f345 25;;; Commentary:
136211a9 26
3132f345 27;;; Code:
136211a9
EZ
28
29;; This file is autoloaded from calc.el.
136211a9 30
5eb3eeda 31(require 'calc)
136211a9
EZ
32(require 'calc-macs)
33
136211a9
EZ
34(defun calc-do-quick-calc ()
35 (calc-check-defines)
36 (if (eq major-mode 'calc-mode)
37 (calc-algebraic-entry t)
38 (let (buf shortbuf)
39 (save-excursion
40 (calc-create-buffer)
41 (let* ((calc-command-flags nil)
42 (calc-dollar-values calc-quick-prev-results)
43 (calc-dollar-used 0)
44 (enable-recursive-minibuffers t)
45 (calc-language (if (memq calc-language '(nil big))
46 'flat calc-language))
47 (entry (calc-do-alg-entry "" "Quick calc: " t))
48 (alg-exp (mapcar (function
49 (lambda (x)
5eb3eeda 50 (if (and (not (featurep 'calc-ext))
136211a9
EZ
51 calc-previous-alg-entry
52 (string-match
53 "\\`[-0-9._+*/^() ]+\\'"
54 calc-previous-alg-entry))
55 (calc-normalize x)
5eb3eeda 56 (require 'calc-ext)
136211a9
EZ
57 (math-evaluate-expr x))))
58 entry)))
3132f345
CW
59 (when (and (= (length alg-exp) 1)
60 (eq (car-safe (car alg-exp)) 'calcFunc-assign)
61 (= (length (car alg-exp)) 3)
62 (eq (car-safe (nth 1 (car alg-exp))) 'var))
5eb3eeda 63 (require 'calc-ext)
3132f345
CW
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)))))
136211a9
EZ
67 (setq calc-quick-prev-results alg-exp
68 buf (mapconcat (function (lambda (x)
69 (math-format-value x 1000)))
70 alg-exp
71 " ")
72 shortbuf buf)
73 (if (and (= (length alg-exp) 1)
74 (memq (car-safe (car alg-exp)) '(nil bigpos bigneg))
75 (< (length buf) 20)
76 (= calc-number-radix 10))
77 (setq buf (concat buf " ("
78 (let ((calc-number-radix 16))
79 (math-format-value (car alg-exp) 1000))
80 ", "
81 (let ((calc-number-radix 8))
82 (math-format-value (car alg-exp) 1000))
83 (if (and (integerp (car alg-exp))
84 (> (car alg-exp) 0)
85 (< (car alg-exp) 127))
86 (format ", \"%c\"" (car alg-exp))
87 "")
88 ")")))
f20be8fe 89 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
5eb3eeda 90 (featurep 'calc-ext))
136211a9
EZ
91 (let ((long (concat (math-format-value (car entry) 1000)
92 " => " buf)))
f20be8fe 93 (if (<= (length long) (- (frame-width) 8))
136211a9
EZ
94 (setq buf long))))
95 (calc-handle-whys)
96 (message "Result: %s" buf)))
97 (if (eq last-command-char 10)
98 (insert shortbuf)
3cedbf72 99 (kill-new shortbuf)))))
136211a9
EZ
100
101(defun calc-do-calc-eval (str separator args)
102 (calc-check-defines)
103 (catch 'calc-error
104 (save-excursion
105 (calc-create-buffer)
106 (cond
107 ((and (consp str) (not (symbolp (car str))))
108 (let ((calc-language nil)
109 (math-expr-opers math-standard-opers)
110 (calc-internal-prec 12)
111 (calc-word-size 32)
112 (calc-symbolic-mode nil)
113 (calc-matrix-mode nil)
114 (calc-angle-mode 'deg)
115 (calc-number-radix 10)
116 (calc-leading-zeros nil)
117 (calc-group-digits nil)
118 (calc-point-char ".")
119 (calc-frac-format '(":" nil))
120 (calc-prefer-frac nil)
121 (calc-hms-format "%s@ %s' %s\"")
122 (calc-date-format '((H ":" mm C SS pp " ")
123 Www " " Mmm " " D ", " YYYY))
124 (calc-float-format '(float 0))
125 (calc-full-float-format '(float 0))
126 (calc-complex-format nil)
127 (calc-matrix-just nil)
128 (calc-full-vectors t)
129 (calc-break-vectors nil)
130 (calc-vector-commas ",")
131 (calc-vector-brackets "[]")
132 (calc-matrix-brackets '(R O))
133 (calc-complex-mode 'cplx)
134 (calc-infinite-mode nil)
135 (calc-display-strings nil)
136 (calc-simplify-mode nil)
137 (calc-display-working-message 'lots)
138 (strp (cdr str)))
139 (while strp
140 (set (car strp) (nth 1 strp))
141 (setq strp (cdr (cdr strp))))
142 (calc-do-calc-eval (car str) separator args)))
143 ((eq separator 'eval)
144 (eval str))
145 ((eq separator 'macro)
5eb3eeda 146 (require 'calc-ext)
136211a9
EZ
147 (let* ((calc-buffer (current-buffer))
148 (calc-window (get-buffer-window calc-buffer))
149 (save-window (selected-window)))
150 (if calc-window
151 (unwind-protect
152 (progn
153 (select-window calc-window)
154 (calc-execute-kbd-macro str nil (car args)))
155 (and (window-point save-window)
156 (select-window save-window)))
157 (save-window-excursion
158 (select-window (get-largest-window))
159 (switch-to-buffer calc-buffer)
160 (calc-execute-kbd-macro str nil (car args)))))
161 nil)
162 ((eq separator 'pop)
163 (or (not (integerp str))
164 (= str 0)
165 (calc-pop (min str (calc-stack-size))))
166 (calc-stack-size))
167 ((eq separator 'top)
168 (and (integerp str)
169 (> str 0)
170 (<= str (calc-stack-size))
171 (math-format-value (calc-top-n str (car args)) 1000)))
172 ((eq separator 'rawtop)
173 (and (integerp str)
174 (> str 0)
175 (<= str (calc-stack-size))
176 (calc-top-n str (car args))))
177 (t
178 (let* ((calc-command-flags nil)
179 (calc-next-why nil)
180 (calc-language (if (memq calc-language '(nil big))
181 'flat calc-language))
182 (calc-dollar-values (mapcar
183 (function
184 (lambda (x)
185 (if (stringp x)
186 (progn
187 (setq x (math-read-exprs x))
188 (if (eq (car-safe x)
189 'error)
190 (throw 'calc-error
191 (calc-eval-error
192 (cdr x)))
193 (car x)))
194 x)))
195 args))
196 (calc-dollar-used 0)
197 (res (if (stringp str)
198 (math-read-exprs str)
199 (list str)))
200 buf)
201 (if (eq (car res) 'error)
202 (calc-eval-error (cdr res))
203 (setq res (mapcar 'calc-normalize res))
204 (and (memq 'clear-message calc-command-flags)
205 (message ""))
206 (cond ((eq separator 'pred)
5eb3eeda 207 (require 'calc-ext)
136211a9
EZ
208 (if (= (length res) 1)
209 (math-is-true (car res))
210 (calc-eval-error '(0 "Single value expected"))))
211 ((eq separator 'raw)
212 (if (= (length res) 1)
213 (car res)
214 (calc-eval-error '(0 "Single value expected"))))
215 ((eq separator 'list)
216 res)
217 ((memq separator '(num rawnum))
218 (if (= (length res) 1)
219 (if (math-constp (car res))
220 (if (eq separator 'num)
221 (math-format-value (car res) 1000)
222 (car res))
223 (calc-eval-error
224 (list 0
225 (if calc-next-why
226 (calc-explain-why (car calc-next-why))
227 "Number expected"))))
228 (calc-eval-error '(0 "Single value expected"))))
229 ((eq separator 'push)
230 (calc-push-list res)
231 nil)
232 (t (while res
233 (setq buf (concat buf
234 (and buf (or separator ", "))
235 (math-format-value (car res) 1000))
236 res (cdr res)))
07ff2bc8 237 buf)))))))))
136211a9 238
d37f2979
JB
239(defvar calc-eval-error nil
240 "Determines how calc handles errors.
241NIL means return a list containing the character position of error.
242STRING means return error message as string rather than list.
243T means abort and give an error message.")
244
136211a9 245(defun calc-eval-error (msg)
d37f2979 246 (if calc-eval-error
136211a9
EZ
247 (if (eq calc-eval-error 'string)
248 (nth 1 msg)
249 (error "%s" (nth 1 msg)))
07ff2bc8 250 msg))
136211a9
EZ
251
252
253;;;; Reading an expression in algebraic form.
254
255(defun calc-auto-algebraic-entry (&optional prefix)
256 (interactive "P")
07ff2bc8 257 (calc-algebraic-entry prefix t))
136211a9
EZ
258
259(defun calc-algebraic-entry (&optional prefix auto)
260 (interactive "P")
261 (calc-wrapper
262 (let ((calc-language (if prefix nil calc-language))
263 (math-expr-opers (if prefix math-standard-opers math-expr-opers)))
07ff2bc8 264 (calc-alg-entry (and auto (char-to-string last-command-char))))))
136211a9
EZ
265
266(defun calc-alg-entry (&optional initial prompt)
267 (let* ((sel-mode nil)
268 (calc-dollar-values (mapcar 'calc-get-stack-element
269 (nthcdr calc-stack-top calc-stack)))
270 (calc-dollar-used 0)
271 (calc-plain-entry t)
272 (alg-exp (calc-do-alg-entry initial prompt t)))
273 (if (stringp alg-exp)
274 (progn
5eb3eeda 275 (require 'calc-ext)
136211a9
EZ
276 (calc-alg-edit alg-exp))
277 (let* ((calc-simplify-mode (if (eq last-command-char ?\C-j)
278 'none
279 calc-simplify-mode))
280 (nvals (mapcar 'calc-normalize alg-exp)))
281 (while alg-exp
5eb3eeda 282 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
136211a9
EZ
283 "alg'")
284 (calc-pop-push-record-list calc-dollar-used
285 (and (not (equal (car alg-exp)
286 (car nvals)))
5eb3eeda 287 (featurep 'calc-ext)
136211a9
EZ
288 "")
289 (list (car nvals)))
290 (setq alg-exp (cdr alg-exp)
291 nvals (cdr nvals)
292 calc-dollar-used 0)))
07ff2bc8 293 (calc-handle-whys))))
136211a9 294
1480882c
JB
295(defvar calc-alg-ent-map nil
296 "The keymap used for algebraic entry.")
297
298(defvar calc-alg-ent-esc-map nil
299 "The keymap used for escapes in algebraic entry.")
300
3cedbf72
JB
301(defvar calc-alg-exp)
302
136211a9
EZ
303(defun calc-do-alg-entry (&optional initial prompt no-normalize)
304 (let* ((calc-buffer (current-buffer))
4db56602 305 (blink-paren-function 'calcAlg-blink-matching-open)
3cedbf72 306 (calc-alg-exp 'error))
1480882c 307 (unless calc-alg-ent-map
136211a9
EZ
308 (setq calc-alg-ent-map (copy-keymap minibuffer-local-map))
309 (define-key calc-alg-ent-map "'" 'calcAlg-previous)
310 (define-key calc-alg-ent-map "`" 'calcAlg-edit)
311 (define-key calc-alg-ent-map "\C-m" 'calcAlg-enter)
312 (define-key calc-alg-ent-map "\C-j" 'calcAlg-enter)
f1625eaa
JB
313 (let ((i 33))
314 (setq calc-alg-ent-esc-map (copy-keymap esc-map))
315 (while (< i 127)
316 (aset (nth 1 calc-alg-ent-esc-map) i 'calcAlg-escape)
317 (setq i (1+ i)))))
318 (define-key calc-alg-ent-map "\e" nil)
136211a9
EZ
319 (if (eq calc-algebraic-mode 'total)
320 (define-key calc-alg-ent-map "\e" calc-alg-ent-esc-map)
321 (define-key calc-alg-ent-map "\ep" 'calcAlg-plus-minus)
322 (define-key calc-alg-ent-map "\em" 'calcAlg-mod)
323 (define-key calc-alg-ent-map "\e=" 'calcAlg-equals)
324 (define-key calc-alg-ent-map "\e\r" 'calcAlg-equals)
325 (define-key calc-alg-ent-map "\e%" 'self-insert-command))
326 (setq calc-aborted-prefix nil)
327 (let ((buf (read-from-minibuffer (or prompt "Algebraic: ")
328 (or initial "")
329 calc-alg-ent-map nil)))
3cedbf72
JB
330 (when (eq calc-alg-exp 'error)
331 (when (eq (car-safe (setq calc-alg-exp (math-read-exprs buf))) 'error)
332 (setq calc-alg-exp nil)))
136211a9
EZ
333 (setq calc-aborted-prefix "alg'")
334 (or no-normalize
3cedbf72
JB
335 (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
336 calc-alg-exp)))
136211a9
EZ
337
338(defun calcAlg-plus-minus ()
339 (interactive)
340 (if (calc-minibuffer-contains ".* \\'")
341 (insert "+/- ")
07ff2bc8 342 (insert " +/- ")))
136211a9
EZ
343
344(defun calcAlg-mod ()
345 (interactive)
346 (if (not (calc-minibuffer-contains ".* \\'"))
347 (insert " "))
348 (if (calc-minibuffer-contains ".* mod +\\'")
349 (if calc-previous-modulo
350 (insert (math-format-flat-expr calc-previous-modulo 0))
351 (beep))
07ff2bc8 352 (insert "mod ")))
136211a9
EZ
353
354(defun calcAlg-previous ()
355 (interactive)
aa9208fb 356 (if (calc-minibuffer-contains "\\'")
136211a9
EZ
357 (if calc-previous-alg-entry
358 (insert calc-previous-alg-entry)
359 (beep))
07ff2bc8 360 (insert "'")))
136211a9
EZ
361
362(defun calcAlg-equals ()
363 (interactive)
364 (unwind-protect
365 (calcAlg-enter)
3cedbf72
JB
366 (if (consp calc-alg-exp)
367 (progn (setq prefix-arg (length calc-alg-exp))
07ff2bc8 368 (calc-unread-command ?=)))))
136211a9
EZ
369
370(defun calcAlg-escape ()
371 (interactive)
372 (calc-unread-command)
373 (save-excursion
374 (calc-select-buffer)
375 (use-local-map calc-mode-map))
07ff2bc8 376 (calcAlg-enter))
136211a9 377
3132f345 378(defvar calc-plain-entry nil)
136211a9
EZ
379(defun calcAlg-edit ()
380 (interactive)
381 (if (or (not calc-plain-entry)
382 (calc-minibuffer-contains
383 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
384 (insert "`")
3cedbf72
JB
385 (setq calc-alg-exp (minibuffer-contents))
386 (and (> (length calc-alg-exp) 0) (setq calc-previous-alg-entry calc-alg-exp))
07ff2bc8 387 (exit-minibuffer)))
136211a9 388
d37f2979
JB
389(defvar calc-buffer)
390
136211a9
EZ
391(defun calcAlg-enter ()
392 (interactive)
f20be8fe 393 (let* ((str (minibuffer-contents))
136211a9
EZ
394 (exp (and (> (length str) 0)
395 (save-excursion
396 (set-buffer calc-buffer)
397 (math-read-exprs str)))))
398 (if (eq (car-safe exp) 'error)
399 (progn
f20be8fe 400 (goto-char (minibuffer-prompt-end))
136211a9
EZ
401 (forward-char (nth 1 exp))
402 (beep)
403 (calc-temp-minibuffer-message
404 (concat " [" (or (nth 2 exp) "Error") "]"))
405 (calc-clear-unread-commands))
3cedbf72 406 (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
136211a9
EZ
407 '((incomplete vec))
408 exp))
409 (and (> (length str) 0) (setq calc-previous-alg-entry str))
07ff2bc8 410 (exit-minibuffer))))
136211a9
EZ
411
412(defun calcAlg-blink-matching-open ()
413 (let ((oldpos (point))
414 (blinkpos nil))
415 (save-excursion
416 (condition-case ()
417 (setq blinkpos (scan-sexps oldpos -1))
418 (error nil)))
419 (if (and blinkpos
420 (> oldpos (1+ (point-min)))
421 (or (and (= (char-after (1- oldpos)) ?\))
422 (= (char-after blinkpos) ?\[))
423 (and (= (char-after (1- oldpos)) ?\])
424 (= (char-after blinkpos) ?\()))
425 (save-excursion
426 (goto-char blinkpos)
427 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
428 (let ((saved (aref (syntax-table) (char-after blinkpos))))
429 (unwind-protect
430 (progn
431 (aset (syntax-table) (char-after blinkpos)
432 (+ (logand saved 255)
433 (lsh (char-after (1- oldpos)) 8)))
434 (blink-matching-open))
435 (aset (syntax-table) (char-after blinkpos) saved)))
07ff2bc8 436 (blink-matching-open))))
136211a9
EZ
437
438
439(defun calc-alg-digit-entry ()
a1506d29 440 (calc-alg-entry
136211a9
EZ
441 (cond ((eq last-command-char ?e)
442 (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e"))
443 ((eq last-command-char ?#) (format "%d#" calc-number-radix))
444 ((eq last-command-char ?_) "-")
445 ((eq last-command-char ?@) "0@ ")
07ff2bc8 446 (t (char-to-string last-command-char)))))
136211a9 447
d37f2979
JB
448;; The variable calc-digit-value is initially declared in calc.el,
449;; but can be set by calcDigit-algebraic and calcDigit-edit.
450(defvar calc-digit-value)
451
136211a9
EZ
452(defun calcDigit-algebraic ()
453 (interactive)
454 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
455 (calcDigit-key)
f20be8fe 456 (setq calc-digit-value (minibuffer-contents))
07ff2bc8 457 (exit-minibuffer)))
136211a9
EZ
458
459(defun calcDigit-edit ()
460 (interactive)
461 (calc-unread-command)
f20be8fe 462 (setq calc-digit-value (minibuffer-contents))
07ff2bc8 463 (exit-minibuffer))
136211a9
EZ
464
465
466;;; Algebraic expression parsing. [Public]
467
c59d10e0
JB
468(defvar math-read-replacement-list
469 '(;; Misc symbols
470 ("±" "+/-") ; plus or minus
471 ("×" "*") ; multiplication sign
472 ("÷" ":") ; division sign
473 ("−" "-") ; subtraction sign
474 ("∕" "/") ; division sign
475 ("∗" "*") ; asterisk multiplication
476 ("∞" "inf") ; infinity symbol
477 ("≤" "<=")
478 ("≥" ">=")
479 ("≦" "<=")
480 ("≧" ">=")
481 ;; fractions
482 ("¼" "(1:4)") ; 1/4
483 ("½" "(1:2)") ; 1/2
484 ("¾" "(3:4)") ; 3/4
485 ("⅓" "(1:3)") ; 1/3
486 ("⅔" "(2:3)") ; 2/3
487 ("⅕" "(1:5)") ; 1/5
488 ("⅖" "(2:5)") ; 2/5
489 ("⅗" "(3:5)") ; 3/5
490 ("⅘" "(4:5)") ; 4/5
491 ("⅙" "(1:6)") ; 1/6
492 ("⅚" "(5:6)") ; 5/6
493 ("⅛" "(1:8)") ; 1/8
494 ("⅜" "(3:8)") ; 3/8
495 ("⅝" "(5:8)") ; 5/8
496 ("⅞" "(7:8)") ; 7/8
497 ("⅟" "1:") ; 1/...
498 ;; superscripts
499 ("⁰" "0") ; 0
500 ("¹" "1") ; 1
501 ("²" "2") ; 2
502 ("³" "3") ; 3
503 ("⁴" "4") ; 4
504 ("⁵" "5") ; 5
505 ("⁶" "6") ; 6
506 ("⁷" "7") ; 7
507 ("⁸" "8") ; 8
508 ("⁹" "9") ; 9
509 ("⁺" "+") ; +
510 ("⁻" "-") ; -
511 ("⁽" "(") ; (
512 ("⁾" ")") ; )
513 ("ⁿ" "n") ; n
514 ("ⁱ" "i")) ; i
515 "A list whose elements (old new) indicate replacements to make
516in Calc algebraic input.")
517
518(defvar math-read-superscripts
519 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
520 "A string consisting of the superscripts allowed by Calc.")
521
522(defun math-read-preprocess-string (str)
523 "Replace some substrings of STR by Calc equivalents."
524 (setq str
525 (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
526 "^(\\&)" str))
527 (let ((rep-list math-read-replacement-list))
528 (while rep-list
529 (setq str
530 (replace-regexp-in-string (nth 0 (car rep-list))
531 (nth 1 (car rep-list)) str))
532 (setq rep-list (cdr rep-list))))
533 str)
534
d37f2979
JB
535;; The next few variables are local to math-read-exprs (and math-read-expr
536;; in calc-ext.el), but are set in functions they call.
3cedbf72
JB
537
538(defvar math-exp-pos)
539(defvar math-exp-str)
540(defvar math-exp-old-pos)
541(defvar math-exp-token)
542(defvar math-exp-keep-spaces)
d37f2979 543(defvar math-expr-data)
3cedbf72
JB
544
545(defun math-read-exprs (math-exp-str)
546 (let ((math-exp-pos 0)
547 (math-exp-old-pos 0)
548 (math-exp-keep-spaces nil)
549 math-exp-token math-expr-data)
c4446fc2 550 (setq math-exp-str (math-read-preprocess-string math-exp-str))
136211a9 551 (if calc-language-input-filter
3cedbf72
JB
552 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
553 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
554 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
555 (substring math-exp-str (+ math-exp-token 2)))))
136211a9
EZ
556 (math-build-parse-table)
557 (math-read-token)
558 (let ((val (catch 'syntax (math-read-expr-list))))
559 (if (stringp val)
3cedbf72
JB
560 (list 'error math-exp-old-pos val)
561 (if (equal math-exp-token 'end)
136211a9 562 val
3cedbf72 563 (list 'error math-exp-old-pos "Syntax error"))))))
136211a9
EZ
564
565(defun math-read-expr-list ()
3cedbf72 566 (let* ((math-exp-keep-spaces nil)
136211a9
EZ
567 (val (list (math-read-expr-level 0)))
568 (last val))
abd880c3 569 (while (equal math-expr-data ",")
136211a9
EZ
570 (math-read-token)
571 (let ((rest (list (math-read-expr-level 0))))
572 (setcdr last rest)
573 (setq last rest)))
07ff2bc8 574 val))
136211a9 575
3132f345
CW
576(defvar calc-user-parse-table nil)
577(defvar calc-last-main-parse-table nil)
578(defvar calc-last-lang-parse-table nil)
579(defvar calc-user-tokens nil)
580(defvar calc-user-token-chars nil)
136211a9 581
3cedbf72
JB
582(defvar math-toks nil
583 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
584
136211a9
EZ
585(defun math-build-parse-table ()
586 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
587 (ltab (cdr (assq calc-language calc-user-parse-tables))))
588 (or (and (eq mtab calc-last-main-parse-table)
589 (eq ltab calc-last-lang-parse-table))
590 (let ((p (append mtab ltab))
3cedbf72 591 (math-toks nil))
136211a9
EZ
592 (setq calc-user-parse-table p)
593 (setq calc-user-token-chars nil)
594 (while p
595 (math-find-user-tokens (car (car p)))
596 (setq p (cdr p)))
597 (setq calc-user-tokens (mapconcat 'identity
3cedbf72 598 (sort (mapcar 'car math-toks)
136211a9
EZ
599 (function (lambda (x y)
600 (> (length x)
601 (length y)))))
602 "\\|")
603 calc-last-main-parse-table mtab
07ff2bc8 604 calc-last-lang-parse-table ltab)))))
136211a9 605
3cedbf72 606(defun math-find-user-tokens (p)
136211a9
EZ
607 (while p
608 (cond ((and (stringp (car p))
609 (or (> (length (car p)) 1) (equal (car p) "$")
610 (equal (car p) "\""))
611 (string-match "[^a-zA-Z0-9]" (car p)))
612 (let ((s (regexp-quote (car p))))
613 (if (string-match "\\`[a-zA-Z0-9]" s)
614 (setq s (concat "\\<" s)))
615 (if (string-match "[a-zA-Z0-9]\\'" s)
616 (setq s (concat s "\\>")))
3cedbf72 617 (or (assoc s math-toks)
136211a9 618 (progn
3cedbf72 619 (setq math-toks (cons (list s) math-toks))
136211a9
EZ
620 (or (memq (aref (car p) 0) calc-user-token-chars)
621 (setq calc-user-token-chars
622 (cons (aref (car p) 0)
623 calc-user-token-chars)))))))
624 ((consp (car p))
625 (math-find-user-tokens (nth 1 (car p)))
626 (or (eq (car (car p)) '\?)
627 (math-find-user-tokens (nth 2 (car p))))))
07ff2bc8 628 (setq p (cdr p))))
136211a9
EZ
629
630(defun math-read-token ()
3cedbf72
JB
631 (if (>= math-exp-pos (length math-exp-str))
632 (setq math-exp-old-pos math-exp-pos
633 math-exp-token 'end
abd880c3 634 math-expr-data "\000")
3cedbf72
JB
635 (let ((ch (aref math-exp-str math-exp-pos)))
636 (setq math-exp-old-pos math-exp-pos)
136211a9 637 (cond ((memq ch '(32 10 9))
3cedbf72
JB
638 (setq math-exp-pos (1+ math-exp-pos))
639 (if math-exp-keep-spaces
640 (setq math-exp-token 'space
abd880c3 641 math-expr-data " ")
136211a9
EZ
642 (math-read-token)))
643 ((and (memq ch calc-user-token-chars)
644 (let ((case-fold-search nil))
3cedbf72
JB
645 (eq (string-match calc-user-tokens math-exp-str math-exp-pos)
646 math-exp-pos)))
647 (setq math-exp-token 'punc
648 math-expr-data (math-match-substring math-exp-str 0)
649 math-exp-pos (match-end 0)))
136211a9
EZ
650 ((or (and (>= ch ?a) (<= ch ?z))
651 (and (>= ch ?A) (<= ch ?Z)))
652 (string-match (if (memq calc-language '(c fortran pascal maple))
653 "[a-zA-Z0-9_#]*"
654 "[a-zA-Z0-9'#]*")
3cedbf72
JB
655 math-exp-str math-exp-pos)
656 (setq math-exp-token 'symbol
657 math-exp-pos (match-end 0)
abd880c3 658 math-expr-data (math-restore-dashes
3cedbf72 659 (math-match-substring math-exp-str 0)))
136211a9 660 (if (eq calc-language 'eqn)
abd880c3 661 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
136211a9
EZ
662 (cond ((null code))
663 ((null (cdr code))
664 (math-read-token))
665 ((consp (nth 1 code))
666 (math-read-token)
abd880c3
JB
667 (if (assoc math-expr-data (cdr code))
668 (setq math-expr-data (format "%s %s"
669 (car code) math-expr-data))))
136211a9 670 ((eq (nth 1 code) 'punc)
3cedbf72 671 (setq math-exp-token 'punc
abd880c3 672 math-expr-data (nth 2 code)))
136211a9
EZ
673 (t
674 (math-read-token)
675 (math-read-token))))))
676 ((or (and (>= ch ?0) (<= ch ?9))
677 (and (eq ch '?\.)
3cedbf72
JB
678 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
679 math-exp-pos))
136211a9 680 (and (eq ch '?_)
3cedbf72
JB
681 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
682 math-exp-pos)
683 (or (eq math-exp-pos 0)
136211a9
EZ
684 (and (memq calc-language '(nil flat big unform
685 tex eqn))
686 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
3cedbf72
JB
687 math-exp-str (1- math-exp-pos))
688 (1- math-exp-pos))))))
136211a9 689 (or (and (eq calc-language 'c)
3cedbf72
JB
690 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
691 (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]+\\)?\"?\\)?"
692 math-exp-str math-exp-pos))
693 (setq math-exp-token 'number
694 math-expr-data (math-match-substring math-exp-str 0)
695 math-exp-pos (match-end 0)))
136211a9
EZ
696 ((eq ch ?\$)
697 (if (and (eq calc-language 'pascal)
698 (eq (string-match
699 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
3cedbf72
JB
700 math-exp-str math-exp-pos)
701 math-exp-pos))
702 (setq math-exp-token 'number
703 math-expr-data (math-match-substring math-exp-str 1)
704 math-exp-pos (match-end 1))
705 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
706 math-exp-pos)
abd880c3 707 (setq math-expr-data (- (string-to-int (math-match-substring
3cedbf72
JB
708 math-exp-str 1))))
709 (string-match "\\$+" math-exp-str math-exp-pos)
abd880c3 710 (setq math-expr-data (- (match-end 0) (match-beginning 0))))
3cedbf72
JB
711 (setq math-exp-token 'dollar
712 math-exp-pos (match-end 0))))
136211a9 713 ((eq ch ?\#)
3cedbf72
JB
714 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
715 math-exp-pos)
abd880c3 716 (setq math-expr-data (string-to-int
3cedbf72
JB
717 (math-match-substring math-exp-str 1))
718 math-exp-pos (match-end 0))
abd880c3 719 (setq math-expr-data 1
3cedbf72
JB
720 math-exp-pos (1+ math-exp-pos)))
721 (setq math-exp-token 'hash))
136211a9 722 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
3cedbf72
JB
723 math-exp-str math-exp-pos)
724 math-exp-pos)
725 (setq math-exp-token 'punc
726 math-expr-data (math-match-substring math-exp-str 0)
727 math-exp-pos (match-end 0)))
136211a9 728 ((and (eq ch ?\")
3cedbf72
JB
729 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
730 math-exp-str math-exp-pos))
136211a9
EZ
731 (if (eq calc-language 'eqn)
732 (progn
3cedbf72
JB
733 (setq math-exp-str (copy-sequence math-exp-str))
734 (aset math-exp-str (match-beginning 1) ?\{)
735 (if (< (match-end 1) (length math-exp-str))
736 (aset math-exp-str (match-end 1) ?\}))
136211a9 737 (math-read-token))
3cedbf72
JB
738 (setq math-exp-token 'string
739 math-expr-data (math-match-substring math-exp-str 1)
740 math-exp-pos (match-end 0))))
136211a9 741 ((and (= ch ?\\) (eq calc-language 'tex)
3cedbf72
JB
742 (< math-exp-pos (1- (length math-exp-str))))
743 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
744 math-exp-str math-exp-pos)
745 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
746 math-exp-str math-exp-pos))
747 (setq math-exp-token 'symbol
748 math-exp-pos (match-end 0)
abd880c3 749 math-expr-data (math-restore-dashes
3cedbf72 750 (math-match-substring math-exp-str 1)))
abd880c3 751 (let ((code (assoc math-expr-data math-tex-ignore-words)))
136211a9
EZ
752 (cond ((null code))
753 ((null (cdr code))
754 (math-read-token))
755 ((eq (nth 1 code) 'punc)
3cedbf72 756 (setq math-exp-token 'punc
abd880c3 757 math-expr-data (nth 2 code)))
136211a9 758 ((and (eq (nth 1 code) 'mat)
3cedbf72
JB
759 (string-match " *{" math-exp-str math-exp-pos))
760 (setq math-exp-pos (match-end 0)
761 math-exp-token 'punc
abd880c3 762 math-expr-data "[")
3cedbf72 763 (let ((right (string-match "}" math-exp-str math-exp-pos)))
136211a9 764 (and right
3cedbf72
JB
765 (setq math-exp-str (copy-sequence math-exp-str))
766 (aset math-exp-str right ?\])))))))
136211a9
EZ
767 ((and (= ch ?\.) (eq calc-language 'fortran)
768 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
3cedbf72
JB
769 math-exp-str math-exp-pos) math-exp-pos))
770 (setq math-exp-token 'punc
771 math-expr-data (upcase (math-match-substring math-exp-str 0))
772 math-exp-pos (match-end 0)))
136211a9 773 ((and (eq calc-language 'math)
3cedbf72
JB
774 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
775 math-exp-pos))
776 (setq math-exp-token 'punc
777 math-expr-data (math-match-substring math-exp-str 0)
778 math-exp-pos (match-end 0)))
136211a9
EZ
779 ((and (eq calc-language 'eqn)
780 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
3cedbf72
JB
781 math-exp-str math-exp-pos)
782 math-exp-pos))
783 (setq math-exp-token 'punc
784 math-expr-data (math-match-substring math-exp-str 0)
785 math-exp-pos (match-end 0))
786 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
787 math-exp-pos)
788 (setq math-exp-pos (match-end 0)))
abd880c3 789 (if (memq (aref math-expr-data 0) '(?~ ?^))
136211a9 790 (math-read-token)))
3cedbf72
JB
791 ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
792 (setq math-exp-pos (match-end 0))
136211a9
EZ
793 (math-read-token))
794 (t
795 (if (and (eq ch ?\{) (memq calc-language '(tex eqn)))
796 (setq ch ?\())
797 (if (and (eq ch ?\}) (memq calc-language '(tex eqn)))
798 (setq ch ?\)))
799 (if (and (eq ch ?\&) (eq calc-language 'tex))
800 (setq ch ?\,))
3cedbf72 801 (setq math-exp-token 'punc
abd880c3 802 math-expr-data (char-to-string ch)
3cedbf72 803 math-exp-pos (1+ math-exp-pos)))))))
136211a9 804
d37f2979
JB
805(defconst math-alg-inequalities
806 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
807 calcFunc-eq calcFunc-neq))
136211a9
EZ
808
809(defun math-read-expr-level (exp-prec &optional exp-term)
810 (let* ((x (math-read-factor)) (first t) op op2)
811 (while (and (or (and calc-user-parse-table
812 (setq op (calc-check-user-syntax x exp-prec))
813 (setq x op
814 op '("2x" ident 999999 -1)))
abd880c3 815 (and (setq op (assoc math-expr-data math-expr-opers))
136211a9
EZ
816 (/= (nth 2 op) -1)
817 (or (and (setq op2 (assoc
abd880c3 818 math-expr-data
136211a9
EZ
819 (cdr (memq op math-expr-opers))))
820 (eq (= (nth 3 op) -1)
821 (/= (nth 3 op2) -1))
822 (eq (= (nth 3 op2) -1)
823 (not (math-factor-after)))
824 (setq op op2))
825 t))
826 (and (or (eq (nth 2 op) -1)
3cedbf72 827 (memq math-exp-token '(symbol number dollar hash))
abd880c3
JB
828 (equal math-expr-data "(")
829 (and (equal math-expr-data "[")
136211a9 830 (not (eq calc-language 'math))
3cedbf72 831 (not (and math-exp-keep-spaces
136211a9 832 (eq (car-safe x) 'vec)))))
abd880c3 833 (or (not (setq op (assoc math-expr-data math-expr-opers)))
136211a9
EZ
834 (/= (nth 2 op) -1))
835 (or (not calc-user-parse-table)
3cedbf72 836 (not (eq math-exp-token 'symbol))
136211a9
EZ
837 (let ((p calc-user-parse-table))
838 (while (and p
839 (or (not (integerp
840 (car (car (car p)))))
841 (not (equal
842 (nth 1 (car (car p)))
abd880c3 843 math-expr-data))))
136211a9
EZ
844 (setq p (cdr p)))
845 (not p)))
846 (setq op (assoc "2x" math-expr-opers))))
abd880c3 847 (not (and exp-term (equal math-expr-data exp-term)))
136211a9
EZ
848 (>= (nth 2 op) exp-prec))
849 (if (not (equal (car op) "2x"))
850 (math-read-token))
851 (and (memq (nth 1 op) '(sdev mod))
5eb3eeda 852 (require 'calc-ext))
136211a9
EZ
853 (setq x (cond ((consp (nth 1 op))
854 (funcall (car (nth 1 op)) x op))
855 ((eq (nth 3 op) -1)
856 (if (eq (nth 1 op) 'ident)
857 x
858 (if (eq (nth 1 op) 'closing)
859 (if (eq (nth 2 op) exp-prec)
860 (progn
861 (setq exp-prec 1000)
862 x)
863 (throw 'syntax "Mismatched delimiters"))
864 (list (nth 1 op) x))))
865 ((and (not first)
866 (memq (nth 1 op) math-alg-inequalities)
867 (memq (car-safe x) math-alg-inequalities))
5eb3eeda 868 (require 'calc-ext)
136211a9
EZ
869 (math-composite-inequalities x op))
870 (t (list (nth 1 op)
871 x
872 (math-read-expr-level (nth 3 op) exp-term))))
873 first nil))
07ff2bc8 874 x))
136211a9
EZ
875
876(defun calc-check-user-syntax (&optional x prec)
877 (let ((p calc-user-parse-table)
878 (matches nil)
879 match rule)
880 (while (and p
881 (or (not (progn
882 (setq rule (car (car p)))
883 (if x
884 (and (integerp (car rule))
885 (>= (car rule) prec)
abd880c3 886 (equal math-expr-data
136211a9 887 (car (setq rule (cdr rule)))))
abd880c3 888 (equal math-expr-data (car rule)))))
3cedbf72
JB
889 (let ((save-exp-pos math-exp-pos)
890 (save-exp-old-pos math-exp-old-pos)
891 (save-exp-token math-exp-token)
abd880c3 892 (save-exp-data math-expr-data))
136211a9
EZ
893 (or (not (listp
894 (setq matches (calc-match-user-syntax rule))))
895 (let ((args (progn
5eb3eeda 896 (require 'calc-ext)
136211a9
EZ
897 calc-arg-values))
898 (conds nil)
899 temp)
900 (if x
901 (setq matches (cons x matches)))
902 (setq match (cdr (car p)))
903 (while (and (eq (car-safe match)
904 'calcFunc-condition)
905 (= (length match) 3))
906 (setq conds (append (math-flatten-lands
907 (nth 2 match))
908 conds)
909 match (nth 1 match)))
910 (while (and conds match)
5eb3eeda 911 (require 'calc-ext)
136211a9
EZ
912 (cond ((eq (car-safe (car conds))
913 'calcFunc-let)
914 (setq temp (car conds))
915 (or (= (length temp) 3)
916 (and (= (length temp) 2)
917 (eq (car-safe (nth 1 temp))
918 'calcFunc-assign)
919 (= (length (nth 1 temp)) 3)
920 (setq temp (nth 1 temp)))
921 (setq match nil))
922 (setq matches (cons
923 (math-normalize
924 (math-multi-subst
925 (nth 2 temp)
926 args matches))
927 matches)
928 args (cons (nth 1 temp)
929 args)))
930 ((and (eq (car-safe (car conds))
931 'calcFunc-matches)
932 (= (length (car conds)) 3))
933 (setq temp (calcFunc-vmatches
934 (math-multi-subst
935 (nth 1 (car conds))
936 args matches)
937 (nth 2 (car conds))))
938 (if (eq temp 0)
939 (setq match nil)
940 (while (setq temp (cdr temp))
941 (setq matches (cons (nth 2 (car temp))
942 matches)
943 args (cons (nth 1 (car temp))
944 args)))))
945 (t
946 (or (math-is-true (math-simplify
947 (math-multi-subst
948 (car conds)
949 args matches)))
950 (setq match nil))))
951 (setq conds (cdr conds)))
952 (if match
953 (not (setq match (math-multi-subst
954 match args matches)))
3cedbf72
JB
955 (setq math-exp-old-pos save-exp-old-pos
956 math-exp-token save-exp-token
abd880c3 957 math-expr-data save-exp-data
3cedbf72 958 math-exp-pos save-exp-pos)))))))
136211a9 959 (setq p (cdr p)))
07ff2bc8 960 (and p match)))
136211a9
EZ
961
962(defun calc-match-user-syntax (p &optional term)
963 (let ((matches nil)
3cedbf72
JB
964 (save-exp-pos math-exp-pos)
965 (save-exp-old-pos math-exp-old-pos)
966 (save-exp-token math-exp-token)
967 (save-exp-data math-expr-data)
968 m)
136211a9
EZ
969 (while (and p
970 (cond ((stringp (car p))
abd880c3 971 (and (equal math-expr-data (car p))
136211a9
EZ
972 (progn
973 (math-read-token)
974 t)))
975 ((integerp (car p))
976 (and (setq m (catch 'syntax
977 (math-read-expr-level
978 (car p)
979 (if (cdr p)
980 (if (consp (nth 1 p))
981 (car (nth 1 (nth 1 p)))
982 (nth 1 p))
983 term))))
984 (not (stringp m))
985 (setq matches (nconc matches (list m)))))
986 ((eq (car (car p)) '\?)
987 (setq m (calc-match-user-syntax (nth 1 (car p))))
988 (or (nth 2 (car p))
989 (setq matches
990 (nconc matches
991 (list
992 (cons 'vec (and (listp m) m))))))
993 (or (listp m) (not (nth 2 (car p)))
994 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
3cedbf72 995 (eq math-exp-token 'end)))
136211a9
EZ
996 (t
997 (setq m (calc-match-user-syntax (nth 1 (car p))
998 (car (nth 2 (car p)))))
999 (if (listp m)
1000 (let ((vec (cons 'vec m))
1001 opos mm)
1002 (while (and (listp
3cedbf72 1003 (setq opos math-exp-pos
136211a9
EZ
1004 mm (calc-match-user-syntax
1005 (or (nth 2 (car p))
1006 (nth 1 (car p)))
1007 (car (nth 2 (car p))))))
3cedbf72 1008 (> math-exp-pos opos))
136211a9
EZ
1009 (setq vec (nconc vec mm)))
1010 (setq matches (nconc matches (list vec))))
1011 (and (eq (car (car p)) '*)
1012 (setq matches (nconc matches (list '(vec)))))))))
1013 (setq p (cdr p)))
1014 (if p
3cedbf72
JB
1015 (setq math-exp-pos save-exp-pos
1016 math-exp-old-pos save-exp-old-pos
1017 math-exp-token save-exp-token
abd880c3 1018 math-expr-data save-exp-data
136211a9 1019 matches "Failed"))
07ff2bc8 1020 matches))
136211a9 1021
136211a9
EZ
1022(defun math-remove-dashes (x)
1023 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
1024 (math-remove-dashes
1025 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
07ff2bc8 1026 x))
136211a9
EZ
1027
1028(defun math-restore-dashes (x)
1029 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
1030 (math-restore-dashes
1031 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
07ff2bc8 1032 x))
136211a9
EZ
1033
1034(defun math-read-if (cond op)
1035 (let ((then (math-read-expr-level 0)))
abd880c3 1036 (or (equal math-expr-data ":")
136211a9
EZ
1037 (throw 'syntax "Expected ':'"))
1038 (math-read-token)
07ff2bc8 1039 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
136211a9
EZ
1040
1041(defun math-factor-after ()
3cedbf72
JB
1042 (let ((math-exp-pos math-exp-pos)
1043 math-exp-old-pos math-exp-token math-expr-data)
136211a9 1044 (math-read-token)
3cedbf72 1045 (or (memq math-exp-token '(number symbol dollar hash string))
abd880c3
JB
1046 (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
1047 (assoc (concat "u" math-expr-data) math-expr-opers))
1048 (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
1049 (assoc math-expr-data '(("(") ("[") ("{"))))))
136211a9
EZ
1050
1051(defun math-read-factor ()
1052 (let (op)
3cedbf72 1053 (cond ((eq math-exp-token 'number)
abd880c3 1054 (let ((num (math-read-number math-expr-data)))
136211a9
EZ
1055 (if (not num)
1056 (progn
3cedbf72 1057 (setq math-exp-old-pos math-exp-pos)
136211a9
EZ
1058 (throw 'syntax "Bad format")))
1059 (math-read-token)
1060 (if (and math-read-expr-quotes
1061 (consp num))
1062 (list 'quote num)
1063 num)))
1064 ((and calc-user-parse-table
1065 (setq op (calc-check-user-syntax)))
1066 op)
abd880c3
JB
1067 ((or (equal math-expr-data "-")
1068 (equal math-expr-data "+")
1069 (equal math-expr-data "!")
1070 (equal math-expr-data "|")
1071 (equal math-expr-data "/"))
1072 (setq math-expr-data (concat "u" math-expr-data))
136211a9 1073 (math-read-factor))
abd880c3 1074 ((and (setq op (assoc math-expr-data math-expr-opers))
136211a9
EZ
1075 (eq (nth 2 op) -1))
1076 (if (consp (nth 1 op))
1077 (funcall (car (nth 1 op)) op)
1078 (math-read-token)
1079 (let ((val (math-read-expr-level (nth 3 op))))
1080 (cond ((eq (nth 1 op) 'ident)
1081 val)
1082 ((and (Math-numberp val)
1083 (equal (car op) "u-"))
1084 (math-neg val))
1085 (t (list (nth 1 op) val))))))
3cedbf72 1086 ((eq math-exp-token 'symbol)
abd880c3 1087 (let ((sym (intern math-expr-data)))
136211a9 1088 (math-read-token)
abd880c3 1089 (if (equal math-expr-data calc-function-open)
136211a9
EZ
1090 (let ((f (assq sym math-expr-function-mapping)))
1091 (math-read-token)
1092 (if (consp (cdr f))
1093 (funcall (car (cdr f)) f sym)
abd880c3 1094 (let ((args (if (or (equal math-expr-data calc-function-close)
3cedbf72 1095 (eq math-exp-token 'end))
136211a9
EZ
1096 nil
1097 (math-read-expr-list))))
abd880c3 1098 (if (not (or (equal math-expr-data calc-function-close)
3cedbf72 1099 (eq math-exp-token 'end)))
136211a9
EZ
1100 (throw 'syntax "Expected `)'"))
1101 (math-read-token)
1102 (if (and (eq calc-language 'fortran) args
5eb3eeda 1103 (require 'calc-ext)
136211a9
EZ
1104 (let ((calc-matrix-mode 'scalar))
1105 (math-known-matrixp
1106 (list 'var sym
1107 (intern
1108 (concat "var-"
1109 (symbol-name sym)))))))
1110 (math-parse-fortran-subscr sym args)
1111 (if f
1112 (setq sym (cdr f))
1113 (and (= (aref (symbol-name sym) 0) ?\\)
1114 (< (prefix-numeric-value calc-language-option)
1115 0)
1116 (setq sym (intern (substring (symbol-name sym)
1117 1))))
1118 (or (string-match "-" (symbol-name sym))
1119 (setq sym (intern
1120 (concat "calcFunc-"
1121 (symbol-name sym))))))
1122 (cons sym args)))))
1123 (if math-read-expr-quotes
1124 sym
1125 (let ((val (list 'var
1126 (intern (math-remove-dashes
1127 (symbol-name sym)))
1128 (if (string-match "-" (symbol-name sym))
1129 sym
1130 (intern (concat "var-"
1131 (symbol-name sym)))))))
1132 (let ((v (assq (nth 1 val) math-expr-variable-mapping)))
1133 (and v (setq val (if (consp (cdr v))
1134 (funcall (car (cdr v)) v val)
1135 (list 'var
1136 (intern
1137 (substring (symbol-name (cdr v))
1138 4))
1139 (cdr v))))))
1140 (while (and (memq calc-language '(c pascal maple))
abd880c3 1141 (equal math-expr-data "["))
136211a9
EZ
1142 (math-read-token)
1143 (setq val (append (list 'calcFunc-subscr val)
1144 (math-read-expr-list)))
abd880c3 1145 (if (equal math-expr-data "]")
136211a9
EZ
1146 (math-read-token)
1147 (throw 'syntax "Expected ']'")))
1148 val)))))
3cedbf72 1149 ((eq math-exp-token 'dollar)
abd880c3 1150 (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
136211a9 1151 (if (>= (length calc-dollar-values) abs)
abd880c3 1152 (let ((num math-expr-data))
136211a9
EZ
1153 (math-read-token)
1154 (setq calc-dollar-used (max calc-dollar-used num))
1155 (math-check-complete (nth (1- abs) calc-dollar-values)))
1156 (throw 'syntax (if calc-dollar-values
1157 "Too many $'s"
1158 "$'s not allowed in this context")))))
3cedbf72 1159 ((eq math-exp-token 'hash)
136211a9
EZ
1160 (or calc-hashes-used
1161 (throw 'syntax "#'s not allowed in this context"))
5eb3eeda 1162 (require 'calc-ext)
abd880c3
JB
1163 (if (<= math-expr-data (length calc-arg-values))
1164 (let ((num math-expr-data))
136211a9
EZ
1165 (math-read-token)
1166 (setq calc-hashes-used (max calc-hashes-used num))
1167 (nth (1- num) calc-arg-values))
1168 (throw 'syntax "Too many # arguments")))
abd880c3 1169 ((equal math-expr-data "(")
3cedbf72 1170 (let* ((exp (let ((math-exp-keep-spaces nil))
136211a9 1171 (math-read-token)
abd880c3
JB
1172 (if (or (equal math-expr-data "\\dots")
1173 (equal math-expr-data "\\ldots"))
136211a9
EZ
1174 '(neg (var inf var-inf))
1175 (math-read-expr-level 0)))))
3cedbf72 1176 (let ((math-exp-keep-spaces nil))
136211a9 1177 (cond
abd880c3 1178 ((equal math-expr-data ",")
136211a9
EZ
1179 (progn
1180 (math-read-token)
1181 (let ((exp2 (math-read-expr-level 0)))
1182 (setq exp
1183 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1184 (math-normalize (list 'cplx exp exp2))
1185 (list '+ exp (list '* exp2 '(var i var-i))))))))
abd880c3 1186 ((equal math-expr-data ";")
136211a9
EZ
1187 (progn
1188 (math-read-token)
1189 (let ((exp2 (math-read-expr-level 0)))
1190 (setq exp (if (and exp2 (Math-realp exp)
1191 (Math-anglep exp2))
1192 (math-normalize (list 'polar exp exp2))
5eb3eeda 1193 (require 'calc-ext)
136211a9
EZ
1194 (list '* exp
1195 (list 'calcFunc-exp
1196 (list '*
1197 (math-to-radians-2 exp2)
1198 '(var i var-i)))))))))
abd880c3
JB
1199 ((or (equal math-expr-data "\\dots")
1200 (equal math-expr-data "\\ldots"))
136211a9
EZ
1201 (progn
1202 (math-read-token)
abd880c3
JB
1203 (let ((exp2 (if (or (equal math-expr-data ")")
1204 (equal math-expr-data "]")
3cedbf72 1205 (eq math-exp-token 'end))
136211a9
EZ
1206 '(var inf var-inf)
1207 (math-read-expr-level 0))))
1208 (setq exp
1209 (list 'intv
abd880c3 1210 (if (equal math-expr-data ")") 0 1)
136211a9
EZ
1211 exp
1212 exp2)))))))
abd880c3
JB
1213 (if (not (or (equal math-expr-data ")")
1214 (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
3cedbf72 1215 (eq math-exp-token 'end)))
136211a9
EZ
1216 (throw 'syntax "Expected `)'"))
1217 (math-read-token)
1218 exp))
3cedbf72 1219 ((eq math-exp-token 'string)
5eb3eeda 1220 (require 'calc-ext)
136211a9 1221 (math-read-string))
abd880c3 1222 ((equal math-expr-data "[")
5eb3eeda 1223 (require 'calc-ext)
136211a9 1224 (math-read-brackets t "]"))
abd880c3 1225 ((equal math-expr-data "{")
5eb3eeda 1226 (require 'calc-ext)
136211a9 1227 (math-read-brackets nil "}"))
abd880c3 1228 ((equal math-expr-data "<")
5eb3eeda 1229 (require 'calc-ext)
136211a9 1230 (math-read-angle-brackets))
07ff2bc8 1231 (t (throw 'syntax "Expected a number")))))
136211a9 1232
47233535
JB
1233(provide 'calc-aent)
1234
ab5796a9 1235;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
07ff2bc8 1236;;; calc-aent.el ends here