]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-aent.el
(calc-sec, calc-csc, calc-cot, calc-sech, calc-csch, calc-coth)
[gnu-emacs] / lisp / calc / calc-aent.el
1 ;;; calc-aent.el --- algebraic entry functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
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
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; This file is autoloaded from calc.el.
30
31 (require 'calc)
32 (require 'calc-macs)
33
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)
50 (if (and (not (featurep 'calc-ext))
51 calc-previous-alg-entry
52 (string-match
53 "\\`[-0-9._+*/^() ]+\\'"
54 calc-previous-alg-entry))
55 (calc-normalize x)
56 (require 'calc-ext)
57 (math-evaluate-expr x))))
58 entry)))
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))
63 (require 'calc-ext)
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)))
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 ")")))
89 (if (and (< (length buf) (frame-width)) (= (length entry) 1)
90 (featurep 'calc-ext))
91 (let ((long (concat (math-format-value (car entry) 1000)
92 " => " buf)))
93 (if (<= (length long) (- (frame-width) 8))
94 (setq buf long))))
95 (calc-handle-whys)
96 (message "Result: %s" buf)))
97 (if (eq last-command-char 10)
98 (insert shortbuf)
99 (kill-new shortbuf)))))
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)
146 (require 'calc-ext)
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)
207 (require 'calc-ext)
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)))
237 buf)))))))))
238
239 (defvar calc-eval-error nil
240 "Determines how calc handles errors.
241 NIL means return a list containing the character position of error.
242 STRING means return error message as string rather than list.
243 T means abort and give an error message.")
244
245 (defun calc-eval-error (msg)
246 (if calc-eval-error
247 (if (eq calc-eval-error 'string)
248 (nth 1 msg)
249 (error "%s" (nth 1 msg)))
250 msg))
251
252
253 ;;;; Reading an expression in algebraic form.
254
255 (defun calc-auto-algebraic-entry (&optional prefix)
256 (interactive "P")
257 (calc-algebraic-entry prefix t))
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)))
264 (calc-alg-entry (and auto (char-to-string last-command-char))))))
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
275 (require 'calc-ext)
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
282 (calc-record (if (featurep 'calc-ext) (car alg-exp) (car nvals))
283 "alg'")
284 (calc-pop-push-record-list calc-dollar-used
285 (and (not (equal (car alg-exp)
286 (car nvals)))
287 (featurep 'calc-ext)
288 "")
289 (list (car nvals)))
290 (setq alg-exp (cdr alg-exp)
291 nvals (cdr nvals)
292 calc-dollar-used 0)))
293 (calc-handle-whys))))
294
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
301 (defvar calc-alg-exp)
302
303 (defun calc-do-alg-entry (&optional initial prompt no-normalize)
304 (let* ((calc-buffer (current-buffer))
305 (blink-paren-function 'calcAlg-blink-matching-open)
306 (calc-alg-exp 'error))
307 (unless calc-alg-ent-map
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)
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)
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)))
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)))
333 (setq calc-aborted-prefix "alg'")
334 (or no-normalize
335 (and calc-alg-exp (setq calc-alg-exp (mapcar 'calc-normalize calc-alg-exp))))
336 calc-alg-exp)))
337
338 (defun calcAlg-plus-minus ()
339 (interactive)
340 (if (calc-minibuffer-contains ".* \\'")
341 (insert "+/- ")
342 (insert " +/- ")))
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))
352 (insert "mod ")))
353
354 (defun calcAlg-previous ()
355 (interactive)
356 (if (calc-minibuffer-contains "\\'")
357 (if calc-previous-alg-entry
358 (insert calc-previous-alg-entry)
359 (beep))
360 (insert "'")))
361
362 (defun calcAlg-equals ()
363 (interactive)
364 (unwind-protect
365 (calcAlg-enter)
366 (if (consp calc-alg-exp)
367 (progn (setq prefix-arg (length calc-alg-exp))
368 (calc-unread-command ?=)))))
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))
376 (calcAlg-enter))
377
378 (defvar calc-plain-entry nil)
379 (defun calcAlg-edit ()
380 (interactive)
381 (if (or (not calc-plain-entry)
382 (calc-minibuffer-contains
383 "\\`\\([^\"]*\"[^\"]*\"\\)*[^\"]*\"[^\"]*\\'"))
384 (insert "`")
385 (setq calc-alg-exp (minibuffer-contents))
386 (and (> (length calc-alg-exp) 0) (setq calc-previous-alg-entry calc-alg-exp))
387 (exit-minibuffer)))
388
389 (defvar calc-buffer)
390
391 (defun calcAlg-enter ()
392 (interactive)
393 (let* ((str (minibuffer-contents))
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
400 (goto-char (minibuffer-prompt-end))
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))
406 (setq calc-alg-exp (if (calc-minibuffer-contains "\\` *\\[ *\\'")
407 '((incomplete vec))
408 exp))
409 (and (> (length str) 0) (setq calc-previous-alg-entry str))
410 (exit-minibuffer))))
411
412 (defun calcAlg-blink-matching-open ()
413 (let ((rightpt (point))
414 (leftpt nil)
415 (rightchar (preceding-char))
416 leftchar
417 rightsyntax
418 leftsyntax)
419 (save-excursion
420 (condition-case ()
421 (setq leftpt (scan-sexps rightpt -1)
422 leftchar (char-after leftpt))
423 (error nil)))
424 (if (and leftpt
425 (or (and (= rightchar ?\))
426 (= leftchar ?\[))
427 (and (= rightchar ?\])
428 (= leftchar ?\()))
429 (save-excursion
430 (goto-char leftpt)
431 (looking-at ".+\\(\\.\\.\\|\\\\dots\\|\\\\ldots\\)")))
432 (let ((leftsaved (aref (syntax-table) leftchar))
433 (rightsaved (aref (syntax-table) rightchar)))
434 (unwind-protect
435 (progn
436 (cond ((= leftchar ?\[)
437 (aset (syntax-table) leftchar (cons 4 ?\)))
438 (aset (syntax-table) rightchar (cons 5 ?\[)))
439 (t
440 (aset (syntax-table) leftchar (cons 4 ?\]))
441 (aset (syntax-table) rightchar (cons 5 ?\())))
442 (blink-matching-open))
443 (aset (syntax-table) leftchar leftsaved)
444 (aset (syntax-table) rightchar rightsaved)))
445 (blink-matching-open))))
446
447 (defun calc-alg-digit-entry ()
448 (calc-alg-entry
449 (cond ((eq last-command-char ?e)
450 (if (> calc-number-radix 14) (format "%d.^" calc-number-radix) "1e"))
451 ((eq last-command-char ?#) (format "%d#" calc-number-radix))
452 ((eq last-command-char ?_) "-")
453 ((eq last-command-char ?@) "0@ ")
454 (t (char-to-string last-command-char)))))
455
456 ;; The variable calc-digit-value is initially declared in calc.el,
457 ;; but can be set by calcDigit-algebraic and calcDigit-edit.
458 (defvar calc-digit-value)
459
460 (defun calcDigit-algebraic ()
461 (interactive)
462 (if (calc-minibuffer-contains ".*[@oh] *[^'m ]+[^'m]*\\'")
463 (calcDigit-key)
464 (setq calc-digit-value (minibuffer-contents))
465 (exit-minibuffer)))
466
467 (defun calcDigit-edit ()
468 (interactive)
469 (calc-unread-command)
470 (setq calc-digit-value (minibuffer-contents))
471 (exit-minibuffer))
472
473
474 ;;; Algebraic expression parsing. [Public]
475
476 (defvar math-read-replacement-list
477 '(;; Misc symbols
478 ("±" "+/-") ; plus or minus
479 ("×" "*") ; multiplication sign
480 ("÷" ":") ; division sign
481 ("−" "-") ; subtraction sign
482 ("∕" "/") ; division sign
483 ("∗" "*") ; asterisk multiplication
484 ("∞" "inf") ; infinity symbol
485 ("≤" "<=")
486 ("≥" ">=")
487 ("≦" "<=")
488 ("≧" ">=")
489 ;; fractions
490 ("¼" "(1:4)") ; 1/4
491 ("½" "(1:2)") ; 1/2
492 ("¾" "(3:4)") ; 3/4
493 ("⅓" "(1:3)") ; 1/3
494 ("⅔" "(2:3)") ; 2/3
495 ("⅕" "(1:5)") ; 1/5
496 ("⅖" "(2:5)") ; 2/5
497 ("⅗" "(3:5)") ; 3/5
498 ("⅘" "(4:5)") ; 4/5
499 ("⅙" "(1:6)") ; 1/6
500 ("⅚" "(5:6)") ; 5/6
501 ("⅛" "(1:8)") ; 1/8
502 ("⅜" "(3:8)") ; 3/8
503 ("⅝" "(5:8)") ; 5/8
504 ("⅞" "(7:8)") ; 7/8
505 ("⅟" "1:") ; 1/...
506 ;; superscripts
507 ("⁰" "0") ; 0
508 ("¹" "1") ; 1
509 ("²" "2") ; 2
510 ("³" "3") ; 3
511 ("⁴" "4") ; 4
512 ("⁵" "5") ; 5
513 ("⁶" "6") ; 6
514 ("⁷" "7") ; 7
515 ("⁸" "8") ; 8
516 ("⁹" "9") ; 9
517 ("⁺" "+") ; +
518 ("⁻" "-") ; -
519 ("⁽" "(") ; (
520 ("⁾" ")") ; )
521 ("ⁿ" "n") ; n
522 ("ⁱ" "i")) ; i
523 "A list whose elements (old new) indicate replacements to make
524 in Calc algebraic input.")
525
526 (defvar math-read-superscripts
527 "⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁽⁾ⁿⁱ" ; 0123456789+-()ni
528 "A string consisting of the superscripts allowed by Calc.")
529
530 (defun math-read-preprocess-string (str)
531 "Replace some substrings of STR by Calc equivalents."
532 (setq str
533 (replace-regexp-in-string (concat "[" math-read-superscripts "]+")
534 "^(\\&)" str))
535 (let ((rep-list math-read-replacement-list))
536 (while rep-list
537 (setq str
538 (replace-regexp-in-string (nth 0 (car rep-list))
539 (nth 1 (car rep-list)) str))
540 (setq rep-list (cdr rep-list))))
541 str)
542
543 ;; The next few variables are local to math-read-exprs (and math-read-expr
544 ;; in calc-ext.el), but are set in functions they call.
545
546 (defvar math-exp-pos)
547 (defvar math-exp-str)
548 (defvar math-exp-old-pos)
549 (defvar math-exp-token)
550 (defvar math-exp-keep-spaces)
551 (defvar math-expr-data)
552
553 (defun math-read-exprs (math-exp-str)
554 (let ((math-exp-pos 0)
555 (math-exp-old-pos 0)
556 (math-exp-keep-spaces nil)
557 math-exp-token math-expr-data)
558 (setq math-exp-str (math-read-preprocess-string math-exp-str))
559 (if calc-language-input-filter
560 (setq math-exp-str (funcall calc-language-input-filter math-exp-str)))
561 (while (setq math-exp-token (string-match "\\.\\.\\([^.]\\|.[^.]\\)" math-exp-str))
562 (setq math-exp-str (concat (substring math-exp-str 0 math-exp-token) "\\dots"
563 (substring math-exp-str (+ math-exp-token 2)))))
564 (math-build-parse-table)
565 (math-read-token)
566 (let ((val (catch 'syntax (math-read-expr-list))))
567 (if (stringp val)
568 (list 'error math-exp-old-pos val)
569 (if (equal math-exp-token 'end)
570 val
571 (list 'error math-exp-old-pos "Syntax error"))))))
572
573 (defun math-read-expr-list ()
574 (let* ((math-exp-keep-spaces nil)
575 (val (list (math-read-expr-level 0)))
576 (last val))
577 (while (equal math-expr-data ",")
578 (math-read-token)
579 (let ((rest (list (math-read-expr-level 0))))
580 (setcdr last rest)
581 (setq last rest)))
582 val))
583
584 (defvar calc-user-parse-table nil)
585 (defvar calc-last-main-parse-table nil)
586 (defvar calc-last-lang-parse-table nil)
587 (defvar calc-user-tokens nil)
588 (defvar calc-user-token-chars nil)
589
590 (defvar math-toks nil
591 "Tokens to pass between math-build-parse-table and math-find-user-tokens.")
592
593 (defun math-build-parse-table ()
594 (let ((mtab (cdr (assq nil calc-user-parse-tables)))
595 (ltab (cdr (assq calc-language calc-user-parse-tables))))
596 (or (and (eq mtab calc-last-main-parse-table)
597 (eq ltab calc-last-lang-parse-table))
598 (let ((p (append mtab ltab))
599 (math-toks nil))
600 (setq calc-user-parse-table p)
601 (setq calc-user-token-chars nil)
602 (while p
603 (math-find-user-tokens (car (car p)))
604 (setq p (cdr p)))
605 (setq calc-user-tokens (mapconcat 'identity
606 (sort (mapcar 'car math-toks)
607 (function (lambda (x y)
608 (> (length x)
609 (length y)))))
610 "\\|")
611 calc-last-main-parse-table mtab
612 calc-last-lang-parse-table ltab)))))
613
614 (defun math-find-user-tokens (p)
615 (while p
616 (cond ((and (stringp (car p))
617 (or (> (length (car p)) 1) (equal (car p) "$")
618 (equal (car p) "\""))
619 (string-match "[^a-zA-Z0-9]" (car p)))
620 (let ((s (regexp-quote (car p))))
621 (if (string-match "\\`[a-zA-Z0-9]" s)
622 (setq s (concat "\\<" s)))
623 (if (string-match "[a-zA-Z0-9]\\'" s)
624 (setq s (concat s "\\>")))
625 (or (assoc s math-toks)
626 (progn
627 (setq math-toks (cons (list s) math-toks))
628 (or (memq (aref (car p) 0) calc-user-token-chars)
629 (setq calc-user-token-chars
630 (cons (aref (car p) 0)
631 calc-user-token-chars)))))))
632 ((consp (car p))
633 (math-find-user-tokens (nth 1 (car p)))
634 (or (eq (car (car p)) '\?)
635 (math-find-user-tokens (nth 2 (car p))))))
636 (setq p (cdr p))))
637
638 (defun math-read-token ()
639 (if (>= math-exp-pos (length math-exp-str))
640 (setq math-exp-old-pos math-exp-pos
641 math-exp-token 'end
642 math-expr-data "\000")
643 (let ((ch (aref math-exp-str math-exp-pos)))
644 (setq math-exp-old-pos math-exp-pos)
645 (cond ((memq ch '(32 10 9))
646 (setq math-exp-pos (1+ math-exp-pos))
647 (if math-exp-keep-spaces
648 (setq math-exp-token 'space
649 math-expr-data " ")
650 (math-read-token)))
651 ((and (memq ch calc-user-token-chars)
652 (let ((case-fold-search nil))
653 (eq (string-match calc-user-tokens math-exp-str math-exp-pos)
654 math-exp-pos)))
655 (setq math-exp-token 'punc
656 math-expr-data (math-match-substring math-exp-str 0)
657 math-exp-pos (match-end 0)))
658 ((or (and (>= ch ?a) (<= ch ?z))
659 (and (>= ch ?A) (<= ch ?Z)))
660 (string-match (if (memq calc-language '(c fortran pascal maple))
661 "[a-zA-Z0-9_#]*"
662 "[a-zA-Z0-9'#]*")
663 math-exp-str math-exp-pos)
664 (setq math-exp-token 'symbol
665 math-exp-pos (match-end 0)
666 math-expr-data (math-restore-dashes
667 (math-match-substring math-exp-str 0)))
668 (if (eq calc-language 'eqn)
669 (let ((code (assoc math-expr-data math-eqn-ignore-words)))
670 (cond ((null code))
671 ((null (cdr code))
672 (math-read-token))
673 ((consp (nth 1 code))
674 (math-read-token)
675 (if (assoc math-expr-data (cdr code))
676 (setq math-expr-data (format "%s %s"
677 (car code) math-expr-data))))
678 ((eq (nth 1 code) 'punc)
679 (setq math-exp-token 'punc
680 math-expr-data (nth 2 code)))
681 (t
682 (math-read-token)
683 (math-read-token))))))
684 ((or (and (>= ch ?0) (<= ch ?9))
685 (and (eq ch '?\.)
686 (eq (string-match "\\.[0-9]" math-exp-str math-exp-pos)
687 math-exp-pos))
688 (and (eq ch '?_)
689 (eq (string-match "_\\.?[0-9]" math-exp-str math-exp-pos)
690 math-exp-pos)
691 (or (eq math-exp-pos 0)
692 (and (memq calc-language '(nil flat big unform
693 tex latex eqn))
694 (eq (string-match "[^])}\"a-zA-Z0-9'$]_"
695 math-exp-str (1- math-exp-pos))
696 (1- math-exp-pos))))))
697 (or (and (eq calc-language 'c)
698 (string-match "0[xX][0-9a-fA-F]+" math-exp-str math-exp-pos))
699 (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]+\\)?\"?\\)?"
700 math-exp-str math-exp-pos))
701 (setq math-exp-token 'number
702 math-expr-data (math-match-substring math-exp-str 0)
703 math-exp-pos (match-end 0)))
704 ((eq ch ?\$)
705 (if (and (eq calc-language 'pascal)
706 (eq (string-match
707 "\\(\\$[0-9a-fA-F]+\\)\\($\\|[^0-9a-zA-Z]\\)"
708 math-exp-str math-exp-pos)
709 math-exp-pos))
710 (setq math-exp-token 'number
711 math-expr-data (math-match-substring math-exp-str 1)
712 math-exp-pos (match-end 1))
713 (if (eq (string-match "\\$\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
714 math-exp-pos)
715 (setq math-expr-data (- (string-to-int (math-match-substring
716 math-exp-str 1))))
717 (string-match "\\$+" math-exp-str math-exp-pos)
718 (setq math-expr-data (- (match-end 0) (match-beginning 0))))
719 (setq math-exp-token 'dollar
720 math-exp-pos (match-end 0))))
721 ((eq ch ?\#)
722 (if (eq (string-match "#\\([1-9][0-9]*\\)" math-exp-str math-exp-pos)
723 math-exp-pos)
724 (setq math-expr-data (string-to-int
725 (math-match-substring math-exp-str 1))
726 math-exp-pos (match-end 0))
727 (setq math-expr-data 1
728 math-exp-pos (1+ math-exp-pos)))
729 (setq math-exp-token 'hash))
730 ((eq (string-match "~=\\|<=\\|>=\\|<>\\|/=\\|\\+/-\\|\\\\dots\\|\\\\ldots\\|\\*\\*\\|<<\\|>>\\|==\\|!=\\|&&&\\||||\\|!!!\\|&&\\|||\\|!!\\|:=\\|::\\|=>"
731 math-exp-str math-exp-pos)
732 math-exp-pos)
733 (setq math-exp-token 'punc
734 math-expr-data (math-match-substring math-exp-str 0)
735 math-exp-pos (match-end 0)))
736 ((and (eq ch ?\")
737 (string-match "\\(\"\\([^\"\\]\\|\\\\.\\)*\\)\\(\"\\|\\'\\)"
738 math-exp-str math-exp-pos))
739 (if (eq calc-language 'eqn)
740 (progn
741 (setq math-exp-str (copy-sequence math-exp-str))
742 (aset math-exp-str (match-beginning 1) ?\{)
743 (if (< (match-end 1) (length math-exp-str))
744 (aset math-exp-str (match-end 1) ?\}))
745 (math-read-token))
746 (setq math-exp-token 'string
747 math-expr-data (math-match-substring math-exp-str 1)
748 math-exp-pos (match-end 0))))
749 ((and (= ch ?\\) (eq calc-language 'tex)
750 (< math-exp-pos (1- (length math-exp-str))))
751 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
752 math-exp-str math-exp-pos)
753 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
754 math-exp-str math-exp-pos))
755 (setq math-exp-token 'symbol
756 math-exp-pos (match-end 0)
757 math-expr-data (math-restore-dashes
758 (math-match-substring math-exp-str 1)))
759 (let ((code (assoc math-expr-data math-latex-ignore-words)))
760 (cond ((null code))
761 ((null (cdr code))
762 (math-read-token))
763 ((eq (nth 1 code) 'punc)
764 (setq math-exp-token 'punc
765 math-expr-data (nth 2 code)))
766 ((and (eq (nth 1 code) 'mat)
767 (string-match " *{" math-exp-str math-exp-pos))
768 (setq math-exp-pos (match-end 0)
769 math-exp-token 'punc
770 math-expr-data "[")
771 (let ((right (string-match "}" math-exp-str math-exp-pos)))
772 (and right
773 (setq math-exp-str (copy-sequence math-exp-str))
774 (aset math-exp-str right ?\])))))))
775 ((and (= ch ?\\) (eq calc-language 'latex)
776 (< math-exp-pos (1- (length math-exp-str))))
777 (or (string-match "\\\\hbox *{\\([a-zA-Z0-9]+\\)}"
778 math-exp-str math-exp-pos)
779 (string-match "\\\\text *{\\([a-zA-Z0-9]+\\)}"
780 math-exp-str math-exp-pos)
781 (string-match "\\(\\\\\\([a-zA-Z]+\\|[^a-zA-Z]\\)\\)"
782 math-exp-str math-exp-pos))
783 (setq math-exp-token 'symbol
784 math-exp-pos (match-end 0)
785 math-expr-data (math-restore-dashes
786 (math-match-substring math-exp-str 1)))
787 (let ((code (assoc math-expr-data math-tex-ignore-words)))
788 (cond ((null code))
789 ((null (cdr code))
790 (math-read-token))
791 ((eq (nth 1 code) 'punc)
792 (setq math-exp-token 'punc
793 math-expr-data (nth 2 code)))
794 ((and (eq (nth 1 code) 'begenv)
795 (string-match " *{\\([^}]*\\)}" math-exp-str math-exp-pos))
796 (setq math-exp-pos (match-end 0)
797 envname (match-string 1 math-exp-str)
798 math-exp-token 'punc
799 math-expr-data "[")
800 (cond ((or (string= envname "matrix")
801 (string= envname "bmatrix")
802 (string= envname "smallmatrix")
803 (string= envname "pmatrix"))
804 (if (setq j (string-match (concat "\\\\end{" envname "}")
805 math-exp-str math-exp-pos))
806 (setq math-exp-str
807 (replace-match "]" t t math-exp-str))
808 (error "%s" (concat "No closing \\end{" envname "}"))))))
809 ((and (eq (nth 1 code) 'mat)
810 (string-match " *{" math-exp-str math-exp-pos))
811 (setq math-exp-pos (match-end 0)
812 math-exp-token 'punc
813 math-expr-data "[")
814 (let ((right (string-match "}" math-exp-str math-exp-pos)))
815 (and right
816 (setq math-exp-str (copy-sequence math-exp-str))
817 (aset math-exp-str right ?\])))))))
818 ((and (= ch ?\.) (eq calc-language 'fortran)
819 (eq (string-match "\\.[a-zA-Z][a-zA-Z][a-zA-Z]?\\."
820 math-exp-str math-exp-pos) math-exp-pos))
821 (setq math-exp-token 'punc
822 math-expr-data (upcase (math-match-substring math-exp-str 0))
823 math-exp-pos (match-end 0)))
824 ((and (eq calc-language 'math)
825 (eq (string-match "\\[\\[\\|->\\|:>" math-exp-str math-exp-pos)
826 math-exp-pos))
827 (setq math-exp-token 'punc
828 math-expr-data (math-match-substring math-exp-str 0)
829 math-exp-pos (match-end 0)))
830 ((and (eq calc-language 'eqn)
831 (eq (string-match "->\\|<-\\|+-\\|\\\\dots\\|~\\|\\^"
832 math-exp-str math-exp-pos)
833 math-exp-pos))
834 (setq math-exp-token 'punc
835 math-expr-data (math-match-substring math-exp-str 0)
836 math-exp-pos (match-end 0))
837 (and (eq (string-match "\\\\dots\\." math-exp-str math-exp-pos)
838 math-exp-pos)
839 (setq math-exp-pos (match-end 0)))
840 (if (memq (aref math-expr-data 0) '(?~ ?^))
841 (math-read-token)))
842 ((eq (string-match "%%.*$" math-exp-str math-exp-pos) math-exp-pos)
843 (setq math-exp-pos (match-end 0))
844 (math-read-token))
845 (t
846 (if (and (eq ch ?\{) (memq calc-language '(tex latex eqn)))
847 (setq ch ?\())
848 (if (and (eq ch ?\}) (memq calc-language '(tex latex eqn)))
849 (setq ch ?\)))
850 (if (and (eq ch ?\&) (memq calc-language '(tex latex)))
851 (setq ch ?\,))
852 (setq math-exp-token 'punc
853 math-expr-data (char-to-string ch)
854 math-exp-pos (1+ math-exp-pos)))))))
855
856 (defconst math-alg-inequalities
857 '(calcFunc-lt calcFunc-gt calcFunc-leq calcFunc-geq
858 calcFunc-eq calcFunc-neq))
859
860 (defun math-read-expr-level (exp-prec &optional exp-term)
861 (let* ((x (math-read-factor)) (first t) op op2)
862 (while (and (or (and calc-user-parse-table
863 (setq op (calc-check-user-syntax x exp-prec))
864 (setq x op
865 op '("2x" ident 999999 -1)))
866 (and (setq op (assoc math-expr-data math-expr-opers))
867 (/= (nth 2 op) -1)
868 (or (and (setq op2 (assoc
869 math-expr-data
870 (cdr (memq op math-expr-opers))))
871 (eq (= (nth 3 op) -1)
872 (/= (nth 3 op2) -1))
873 (eq (= (nth 3 op2) -1)
874 (not (math-factor-after)))
875 (setq op op2))
876 t))
877 (and (or (eq (nth 2 op) -1)
878 (memq math-exp-token '(symbol number dollar hash))
879 (equal math-expr-data "(")
880 (and (equal math-expr-data "[")
881 (not (eq calc-language 'math))
882 (not (and math-exp-keep-spaces
883 (eq (car-safe x) 'vec)))))
884 (or (not (setq op (assoc math-expr-data math-expr-opers)))
885 (/= (nth 2 op) -1))
886 (or (not calc-user-parse-table)
887 (not (eq math-exp-token 'symbol))
888 (let ((p calc-user-parse-table))
889 (while (and p
890 (or (not (integerp
891 (car (car (car p)))))
892 (not (equal
893 (nth 1 (car (car p)))
894 math-expr-data))))
895 (setq p (cdr p)))
896 (not p)))
897 (setq op (assoc "2x" math-expr-opers))))
898 (not (and exp-term (equal math-expr-data exp-term)))
899 (>= (nth 2 op) exp-prec))
900 (if (not (equal (car op) "2x"))
901 (math-read-token))
902 (and (memq (nth 1 op) '(sdev mod))
903 (require 'calc-ext))
904 (setq x (cond ((consp (nth 1 op))
905 (funcall (car (nth 1 op)) x op))
906 ((eq (nth 3 op) -1)
907 (if (eq (nth 1 op) 'ident)
908 x
909 (if (eq (nth 1 op) 'closing)
910 (if (eq (nth 2 op) exp-prec)
911 (progn
912 (setq exp-prec 1000)
913 x)
914 (throw 'syntax "Mismatched delimiters"))
915 (list (nth 1 op) x))))
916 ((and (not first)
917 (memq (nth 1 op) math-alg-inequalities)
918 (memq (car-safe x) math-alg-inequalities))
919 (require 'calc-ext)
920 (math-composite-inequalities x op))
921 (t (list (nth 1 op)
922 x
923 (math-read-expr-level (nth 3 op) exp-term))))
924 first nil))
925 x))
926
927 (defun calc-check-user-syntax (&optional x prec)
928 (let ((p calc-user-parse-table)
929 (matches nil)
930 match rule)
931 (while (and p
932 (or (not (progn
933 (setq rule (car (car p)))
934 (if x
935 (and (integerp (car rule))
936 (>= (car rule) prec)
937 (equal math-expr-data
938 (car (setq rule (cdr rule)))))
939 (equal math-expr-data (car rule)))))
940 (let ((save-exp-pos math-exp-pos)
941 (save-exp-old-pos math-exp-old-pos)
942 (save-exp-token math-exp-token)
943 (save-exp-data math-expr-data))
944 (or (not (listp
945 (setq matches (calc-match-user-syntax rule))))
946 (let ((args (progn
947 (require 'calc-ext)
948 calc-arg-values))
949 (conds nil)
950 temp)
951 (if x
952 (setq matches (cons x matches)))
953 (setq match (cdr (car p)))
954 (while (and (eq (car-safe match)
955 'calcFunc-condition)
956 (= (length match) 3))
957 (setq conds (append (math-flatten-lands
958 (nth 2 match))
959 conds)
960 match (nth 1 match)))
961 (while (and conds match)
962 (require 'calc-ext)
963 (cond ((eq (car-safe (car conds))
964 'calcFunc-let)
965 (setq temp (car conds))
966 (or (= (length temp) 3)
967 (and (= (length temp) 2)
968 (eq (car-safe (nth 1 temp))
969 'calcFunc-assign)
970 (= (length (nth 1 temp)) 3)
971 (setq temp (nth 1 temp)))
972 (setq match nil))
973 (setq matches (cons
974 (math-normalize
975 (math-multi-subst
976 (nth 2 temp)
977 args matches))
978 matches)
979 args (cons (nth 1 temp)
980 args)))
981 ((and (eq (car-safe (car conds))
982 'calcFunc-matches)
983 (= (length (car conds)) 3))
984 (setq temp (calcFunc-vmatches
985 (math-multi-subst
986 (nth 1 (car conds))
987 args matches)
988 (nth 2 (car conds))))
989 (if (eq temp 0)
990 (setq match nil)
991 (while (setq temp (cdr temp))
992 (setq matches (cons (nth 2 (car temp))
993 matches)
994 args (cons (nth 1 (car temp))
995 args)))))
996 (t
997 (or (math-is-true (math-simplify
998 (math-multi-subst
999 (car conds)
1000 args matches)))
1001 (setq match nil))))
1002 (setq conds (cdr conds)))
1003 (if match
1004 (not (setq match (math-multi-subst
1005 match args matches)))
1006 (setq math-exp-old-pos save-exp-old-pos
1007 math-exp-token save-exp-token
1008 math-expr-data save-exp-data
1009 math-exp-pos save-exp-pos)))))))
1010 (setq p (cdr p)))
1011 (and p match)))
1012
1013 (defun calc-match-user-syntax (p &optional term)
1014 (let ((matches nil)
1015 (save-exp-pos math-exp-pos)
1016 (save-exp-old-pos math-exp-old-pos)
1017 (save-exp-token math-exp-token)
1018 (save-exp-data math-expr-data)
1019 m)
1020 (while (and p
1021 (cond ((stringp (car p))
1022 (and (equal math-expr-data (car p))
1023 (progn
1024 (math-read-token)
1025 t)))
1026 ((integerp (car p))
1027 (and (setq m (catch 'syntax
1028 (math-read-expr-level
1029 (car p)
1030 (if (cdr p)
1031 (if (consp (nth 1 p))
1032 (car (nth 1 (nth 1 p)))
1033 (nth 1 p))
1034 term))))
1035 (not (stringp m))
1036 (setq matches (nconc matches (list m)))))
1037 ((eq (car (car p)) '\?)
1038 (setq m (calc-match-user-syntax (nth 1 (car p))))
1039 (or (nth 2 (car p))
1040 (setq matches
1041 (nconc matches
1042 (list
1043 (cons 'vec (and (listp m) m))))))
1044 (or (listp m) (not (nth 2 (car p)))
1045 (not (eq (aref (car (nth 2 (car p))) 0) ?\$))
1046 (eq math-exp-token 'end)))
1047 (t
1048 (setq m (calc-match-user-syntax (nth 1 (car p))
1049 (car (nth 2 (car p)))))
1050 (if (listp m)
1051 (let ((vec (cons 'vec m))
1052 opos mm)
1053 (while (and (listp
1054 (setq opos math-exp-pos
1055 mm (calc-match-user-syntax
1056 (or (nth 2 (car p))
1057 (nth 1 (car p)))
1058 (car (nth 2 (car p))))))
1059 (> math-exp-pos opos))
1060 (setq vec (nconc vec mm)))
1061 (setq matches (nconc matches (list vec))))
1062 (and (eq (car (car p)) '*)
1063 (setq matches (nconc matches (list '(vec)))))))))
1064 (setq p (cdr p)))
1065 (if p
1066 (setq math-exp-pos save-exp-pos
1067 math-exp-old-pos save-exp-old-pos
1068 math-exp-token save-exp-token
1069 math-expr-data save-exp-data
1070 matches "Failed"))
1071 matches))
1072
1073 (defun math-remove-dashes (x)
1074 (if (string-match "\\`\\(.*\\)-\\(.*\\)\\'" x)
1075 (math-remove-dashes
1076 (concat (math-match-substring x 1) "#" (math-match-substring x 2)))
1077 x))
1078
1079 (defun math-restore-dashes (x)
1080 (if (string-match "\\`\\(.*\\)[#_]\\(.*\\)\\'" x)
1081 (math-restore-dashes
1082 (concat (math-match-substring x 1) "-" (math-match-substring x 2)))
1083 x))
1084
1085 (defun math-read-if (cond op)
1086 (let ((then (math-read-expr-level 0)))
1087 (or (equal math-expr-data ":")
1088 (throw 'syntax "Expected ':'"))
1089 (math-read-token)
1090 (list 'calcFunc-if cond then (math-read-expr-level (nth 3 op)))))
1091
1092 (defun math-factor-after ()
1093 (let ((math-exp-pos math-exp-pos)
1094 math-exp-old-pos math-exp-token math-expr-data)
1095 (math-read-token)
1096 (or (memq math-exp-token '(number symbol dollar hash string))
1097 (and (assoc math-expr-data '(("-") ("+") ("!") ("|") ("/")))
1098 (assoc (concat "u" math-expr-data) math-expr-opers))
1099 (eq (nth 2 (assoc math-expr-data math-expr-opers)) -1)
1100 (assoc math-expr-data '(("(") ("[") ("{"))))))
1101
1102 (defun math-read-factor ()
1103 (let (op)
1104 (cond ((eq math-exp-token 'number)
1105 (let ((num (math-read-number math-expr-data)))
1106 (if (not num)
1107 (progn
1108 (setq math-exp-old-pos math-exp-pos)
1109 (throw 'syntax "Bad format")))
1110 (math-read-token)
1111 (if (and math-read-expr-quotes
1112 (consp num))
1113 (list 'quote num)
1114 num)))
1115 ((and calc-user-parse-table
1116 (setq op (calc-check-user-syntax)))
1117 op)
1118 ((or (equal math-expr-data "-")
1119 (equal math-expr-data "+")
1120 (equal math-expr-data "!")
1121 (equal math-expr-data "|")
1122 (equal math-expr-data "/"))
1123 (setq math-expr-data (concat "u" math-expr-data))
1124 (math-read-factor))
1125 ((and (setq op (assoc math-expr-data math-expr-opers))
1126 (eq (nth 2 op) -1))
1127 (if (consp (nth 1 op))
1128 (funcall (car (nth 1 op)) op)
1129 (math-read-token)
1130 (let ((val (math-read-expr-level (nth 3 op))))
1131 (cond ((eq (nth 1 op) 'ident)
1132 val)
1133 ((and (Math-numberp val)
1134 (equal (car op) "u-"))
1135 (math-neg val))
1136 (t (list (nth 1 op) val))))))
1137 ((eq math-exp-token 'symbol)
1138 (let ((sym (intern math-expr-data)))
1139 (math-read-token)
1140 (if (equal math-expr-data calc-function-open)
1141 (let ((f (assq sym math-expr-function-mapping)))
1142 (math-read-token)
1143 (if (consp (cdr f))
1144 (funcall (car (cdr f)) f sym)
1145 (let ((args (if (or (equal math-expr-data calc-function-close)
1146 (eq math-exp-token 'end))
1147 nil
1148 (math-read-expr-list))))
1149 (if (not (or (equal math-expr-data calc-function-close)
1150 (eq math-exp-token 'end)))
1151 (throw 'syntax "Expected `)'"))
1152 (math-read-token)
1153 (if (and (eq calc-language 'fortran) args
1154 (require 'calc-ext)
1155 (let ((calc-matrix-mode 'scalar))
1156 (math-known-matrixp
1157 (list 'var sym
1158 (intern
1159 (concat "var-"
1160 (symbol-name sym)))))))
1161 (math-parse-fortran-subscr sym args)
1162 (if f
1163 (setq sym (cdr f))
1164 (and (= (aref (symbol-name sym) 0) ?\\)
1165 (< (prefix-numeric-value calc-language-option)
1166 0)
1167 (setq sym (intern (substring (symbol-name sym)
1168 1))))
1169 (or (string-match "-" (symbol-name sym))
1170 (setq sym (intern
1171 (concat "calcFunc-"
1172 (symbol-name sym))))))
1173 (cons sym args)))))
1174 (if math-read-expr-quotes
1175 sym
1176 (let ((val (list 'var
1177 (intern (math-remove-dashes
1178 (symbol-name sym)))
1179 (if (string-match "-" (symbol-name sym))
1180 sym
1181 (intern (concat "var-"
1182 (symbol-name sym)))))))
1183 (let ((v (assq (nth 1 val) math-expr-variable-mapping)))
1184 (and v (setq val (if (consp (cdr v))
1185 (funcall (car (cdr v)) v val)
1186 (list 'var
1187 (intern
1188 (substring (symbol-name (cdr v))
1189 4))
1190 (cdr v))))))
1191 (while (and (memq calc-language '(c pascal maple))
1192 (equal math-expr-data "["))
1193 (math-read-token)
1194 (setq val (append (list 'calcFunc-subscr val)
1195 (math-read-expr-list)))
1196 (if (equal math-expr-data "]")
1197 (math-read-token)
1198 (throw 'syntax "Expected ']'")))
1199 val)))))
1200 ((eq math-exp-token 'dollar)
1201 (let ((abs (if (> math-expr-data 0) math-expr-data (- math-expr-data))))
1202 (if (>= (length calc-dollar-values) abs)
1203 (let ((num math-expr-data))
1204 (math-read-token)
1205 (setq calc-dollar-used (max calc-dollar-used num))
1206 (math-check-complete (nth (1- abs) calc-dollar-values)))
1207 (throw 'syntax (if calc-dollar-values
1208 "Too many $'s"
1209 "$'s not allowed in this context")))))
1210 ((eq math-exp-token 'hash)
1211 (or calc-hashes-used
1212 (throw 'syntax "#'s not allowed in this context"))
1213 (require 'calc-ext)
1214 (if (<= math-expr-data (length calc-arg-values))
1215 (let ((num math-expr-data))
1216 (math-read-token)
1217 (setq calc-hashes-used (max calc-hashes-used num))
1218 (nth (1- num) calc-arg-values))
1219 (throw 'syntax "Too many # arguments")))
1220 ((equal math-expr-data "(")
1221 (let* ((exp (let ((math-exp-keep-spaces nil))
1222 (math-read-token)
1223 (if (or (equal math-expr-data "\\dots")
1224 (equal math-expr-data "\\ldots"))
1225 '(neg (var inf var-inf))
1226 (math-read-expr-level 0)))))
1227 (let ((math-exp-keep-spaces nil))
1228 (cond
1229 ((equal math-expr-data ",")
1230 (progn
1231 (math-read-token)
1232 (let ((exp2 (math-read-expr-level 0)))
1233 (setq exp
1234 (if (and exp2 (Math-realp exp) (Math-realp exp2))
1235 (math-normalize (list 'cplx exp exp2))
1236 (list '+ exp (list '* exp2 '(var i var-i))))))))
1237 ((equal math-expr-data ";")
1238 (progn
1239 (math-read-token)
1240 (let ((exp2 (math-read-expr-level 0)))
1241 (setq exp (if (and exp2 (Math-realp exp)
1242 (Math-anglep exp2))
1243 (math-normalize (list 'polar exp exp2))
1244 (require 'calc-ext)
1245 (list '* exp
1246 (list 'calcFunc-exp
1247 (list '*
1248 (math-to-radians-2 exp2)
1249 '(var i var-i)))))))))
1250 ((or (equal math-expr-data "\\dots")
1251 (equal math-expr-data "\\ldots"))
1252 (progn
1253 (math-read-token)
1254 (let ((exp2 (if (or (equal math-expr-data ")")
1255 (equal math-expr-data "]")
1256 (eq math-exp-token 'end))
1257 '(var inf var-inf)
1258 (math-read-expr-level 0))))
1259 (setq exp
1260 (list 'intv
1261 (if (equal math-expr-data ")") 0 1)
1262 exp
1263 exp2)))))))
1264 (if (not (or (equal math-expr-data ")")
1265 (and (equal math-expr-data "]") (eq (car-safe exp) 'intv))
1266 (eq math-exp-token 'end)))
1267 (throw 'syntax "Expected `)'"))
1268 (math-read-token)
1269 exp))
1270 ((eq math-exp-token 'string)
1271 (require 'calc-ext)
1272 (math-read-string))
1273 ((equal math-expr-data "[")
1274 (require 'calc-ext)
1275 (math-read-brackets t "]"))
1276 ((equal math-expr-data "{")
1277 (require 'calc-ext)
1278 (math-read-brackets nil "}"))
1279 ((equal math-expr-data "<")
1280 (require 'calc-ext)
1281 (math-read-angle-brackets))
1282 (t (throw 'syntax "Expected a number")))))
1283
1284 (provide 'calc-aent)
1285
1286 ;;; arch-tag: 5599e45d-e51e-44bb-9a20-9f4ed8c96c32
1287 ;;; calc-aent.el ends here