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