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