]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-lang.el
(math-function-table, math-oper-table, math-variable-table):
[gnu-emacs] / lisp / calc / calc-lang.el
1 ;;; calc-lang.el --- calc language functions
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4
5 ;; Author: David 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-ext.el.
30
31 (require 'calc-ext)
32 (require 'calc-macs)
33
34 ;;; Alternate entry/display languages.
35
36 (defun calc-set-language (lang &optional option no-refresh)
37 (setq math-expr-opers (or (get lang 'math-oper-table) math-standard-opers)
38 math-expr-function-mapping (get lang 'math-function-table)
39 math-expr-special-function-mapping (get lang 'math-special-function-table)
40 math-expr-variable-mapping (get lang 'math-variable-table)
41 calc-language-input-filter (get lang 'math-input-filter)
42 calc-language-output-filter (get lang 'math-output-filter)
43 calc-vector-brackets (or (get lang 'math-vector-brackets) "[]")
44 calc-complex-format (get lang 'math-complex-format)
45 calc-radix-formatter (get lang 'math-radix-formatter)
46 calc-function-open (or (get lang 'math-function-open) "(")
47 calc-function-close (or (get lang 'math-function-close) ")"))
48 (if no-refresh
49 (setq calc-language lang
50 calc-language-option option)
51 (calc-change-mode '(calc-language calc-language-option)
52 (list lang option) t)))
53
54 (defun calc-normal-language ()
55 (interactive)
56 (calc-wrapper
57 (calc-set-language nil)
58 (message "Normal language mode")))
59
60 (defun calc-flat-language ()
61 (interactive)
62 (calc-wrapper
63 (calc-set-language 'flat)
64 (message "Flat language mode (all stack entries shown on one line)")))
65
66 (defun calc-big-language ()
67 (interactive)
68 (calc-wrapper
69 (calc-set-language 'big)
70 (message "\"Big\" language mode")))
71
72 (defun calc-unformatted-language ()
73 (interactive)
74 (calc-wrapper
75 (calc-set-language 'unform)
76 (message "Unformatted language mode")))
77
78
79 (defun calc-c-language ()
80 (interactive)
81 (calc-wrapper
82 (calc-set-language 'c)
83 (message "`C' language mode")))
84
85 (put 'c 'math-oper-table
86 '( ( "u+" ident -1 1000 )
87 ( "u-" neg -1 1000 )
88 ( "u!" calcFunc-lnot -1 1000 )
89 ( "~" calcFunc-not -1 1000 )
90 ( "*" * 190 191 )
91 ( "/" / 190 191 )
92 ( "%" % 190 191 )
93 ( "+" + 180 181 )
94 ( "-" - 180 181 )
95 ( "<<" calcFunc-lsh 170 171 )
96 ( ">>" calcFunc-rsh 170 171 )
97 ( "<" calcFunc-lt 160 161 )
98 ( ">" calcFunc-gt 160 161 )
99 ( "<=" calcFunc-leq 160 161 )
100 ( ">=" calcFunc-geq 160 161 )
101 ( "==" calcFunc-eq 150 151 )
102 ( "!=" calcFunc-neq 150 151 )
103 ( "&" calcFunc-and 140 141 )
104 ( "^" calcFunc-xor 131 130 )
105 ( "|" calcFunc-or 120 121 )
106 ( "&&" calcFunc-land 110 111 )
107 ( "||" calcFunc-lor 100 101 )
108 ( "?" (math-read-if) 91 90 )
109 ( "!!!" calcFunc-pnot -1 88 )
110 ( "&&&" calcFunc-pand 85 86 )
111 ( "|||" calcFunc-por 75 76 )
112 ( "=" calcFunc-assign 51 50 )
113 ( ":=" calcFunc-assign 51 50 )
114 ( "::" calcFunc-condition 45 46 ))) ; should support full assignments
115
116 (put 'c 'math-function-table
117 '( ( acos . calcFunc-arccos )
118 ( acosh . calcFunc-arccosh )
119 ( asin . calcFunc-arcsin )
120 ( asinh . calcFunc-arcsinh )
121 ( atan . calcFunc-arctan )
122 ( atan2 . calcFunc-arctan2 )
123 ( atanh . calcFunc-arctanh )))
124
125 (put 'c 'math-variable-table
126 '( ( M_PI . var-pi )
127 ( M_E . var-e )))
128
129 (put 'c 'math-vector-brackets "{}")
130
131 (put 'c 'math-radix-formatter
132 (function (lambda (r s)
133 (if (= r 16) (format "0x%s" s)
134 (if (= r 8) (format "0%s" s)
135 (format "%d#%s" r s))))))
136
137
138 (defun calc-pascal-language (n)
139 (interactive "P")
140 (calc-wrapper
141 (and n (setq n (prefix-numeric-value n)))
142 (calc-set-language 'pascal n)
143 (message (if (and n (/= n 0))
144 (if (> n 0)
145 "Pascal language mode (all uppercase)"
146 "Pascal language mode (all lowercase)")
147 "Pascal language mode"))))
148
149 (put 'pascal 'math-oper-table
150 '( ( "not" calcFunc-lnot -1 1000 )
151 ( "*" * 190 191 )
152 ( "/" / 190 191 )
153 ( "and" calcFunc-and 190 191 )
154 ( "div" calcFunc-idiv 190 191 )
155 ( "mod" % 190 191 )
156 ( "u+" ident -1 185 )
157 ( "u-" neg -1 185 )
158 ( "+" + 180 181 )
159 ( "-" - 180 181 )
160 ( "or" calcFunc-or 180 181 )
161 ( "xor" calcFunc-xor 180 181 )
162 ( "shl" calcFunc-lsh 180 181 )
163 ( "shr" calcFunc-rsh 180 181 )
164 ( "in" calcFunc-in 160 161 )
165 ( "<" calcFunc-lt 160 161 )
166 ( ">" calcFunc-gt 160 161 )
167 ( "<=" calcFunc-leq 160 161 )
168 ( ">=" calcFunc-geq 160 161 )
169 ( "=" calcFunc-eq 160 161 )
170 ( "<>" calcFunc-neq 160 161 )
171 ( "!!!" calcFunc-pnot -1 85 )
172 ( "&&&" calcFunc-pand 80 81 )
173 ( "|||" calcFunc-por 75 76 )
174 ( ":=" calcFunc-assign 51 50 )
175 ( "::" calcFunc-condition 45 46 )))
176
177 (put 'pascal 'math-input-filter 'calc-input-case-filter)
178 (put 'pascal 'math-output-filter 'calc-output-case-filter)
179
180 (put 'pascal 'math-radix-formatter
181 (function (lambda (r s)
182 (if (= r 16) (format "$%s" s)
183 (format "%d#%s" r s)))))
184
185 (defun calc-input-case-filter (str)
186 (cond ((or (null calc-language-option) (= calc-language-option 0))
187 str)
188 (t
189 (downcase str))))
190
191 (defun calc-output-case-filter (str)
192 (cond ((or (null calc-language-option) (= calc-language-option 0))
193 str)
194 ((> calc-language-option 0)
195 (upcase str))
196 (t
197 (downcase str))))
198
199
200 (defun calc-fortran-language (n)
201 (interactive "P")
202 (calc-wrapper
203 (and n (setq n (prefix-numeric-value n)))
204 (calc-set-language 'fortran n)
205 (message (if (and n (/= n 0))
206 (if (> n 0)
207 "FORTRAN language mode (all uppercase)"
208 "FORTRAN language mode (all lowercase)")
209 "FORTRAN language mode"))))
210
211 (put 'fortran 'math-oper-table
212 '( ( "u/" (math-parse-fortran-vector) -1 1 )
213 ( "/" (math-parse-fortran-vector-end) 1 -1 )
214 ( "**" ^ 201 200 )
215 ( "u+" ident -1 191 )
216 ( "u-" neg -1 191 )
217 ( "*" * 190 191 )
218 ( "/" / 190 191 )
219 ( "+" + 180 181 )
220 ( "-" - 180 181 )
221 ( ".LT." calcFunc-lt 160 161 )
222 ( ".GT." calcFunc-gt 160 161 )
223 ( ".LE." calcFunc-leq 160 161 )
224 ( ".GE." calcFunc-geq 160 161 )
225 ( ".EQ." calcFunc-eq 160 161 )
226 ( ".NE." calcFunc-neq 160 161 )
227 ( ".NOT." calcFunc-lnot -1 121 )
228 ( ".AND." calcFunc-land 110 111 )
229 ( ".OR." calcFunc-lor 100 101 )
230 ( "!!!" calcFunc-pnot -1 85 )
231 ( "&&&" calcFunc-pand 80 81 )
232 ( "|||" calcFunc-por 75 76 )
233 ( "=" calcFunc-assign 51 50 )
234 ( ":=" calcFunc-assign 51 50 )
235 ( "::" calcFunc-condition 45 46 )))
236
237 (put 'fortran 'math-vector-brackets "//")
238
239 (put 'fortran 'math-function-table
240 '( ( acos . calcFunc-arccos )
241 ( acosh . calcFunc-arccosh )
242 ( aimag . calcFunc-im )
243 ( aint . calcFunc-ftrunc )
244 ( asin . calcFunc-arcsin )
245 ( asinh . calcFunc-arcsinh )
246 ( atan . calcFunc-arctan )
247 ( atan2 . calcFunc-arctan2 )
248 ( atanh . calcFunc-arctanh )
249 ( conjg . calcFunc-conj )
250 ( log . calcFunc-ln )
251 ( nint . calcFunc-round )
252 ( real . calcFunc-re )))
253
254 (put 'fortran 'math-input-filter 'calc-input-case-filter)
255 (put 'fortran 'math-output-filter 'calc-output-case-filter)
256
257 ;; The next few variables are local to math-read-exprs in calc-aent.el
258 ;; and math-read-expr in calc-ext.el, but are set in functions they call.
259
260 (defvar math-exp-token)
261 (defvar math-expr-data)
262 (defvar math-exp-old-pos)
263
264 (defvar math-parsing-fortran-vector nil)
265 (defun math-parse-fortran-vector (op)
266 (let ((math-parsing-fortran-vector '(end . "\000")))
267 (prog1
268 (math-read-brackets t "]")
269 (setq math-exp-token (car math-parsing-fortran-vector)
270 math-expr-data (cdr math-parsing-fortran-vector)))))
271
272 (defun math-parse-fortran-vector-end (x op)
273 (if math-parsing-fortran-vector
274 (progn
275 (setq math-parsing-fortran-vector (cons math-exp-token math-expr-data)
276 math-exp-token 'end
277 math-expr-data "\000")
278 x)
279 (throw 'syntax "Unmatched closing `/'")))
280
281 (defun math-parse-fortran-subscr (sym args)
282 (setq sym (math-build-var-name sym))
283 (while args
284 (setq sym (list 'calcFunc-subscr sym (car args))
285 args (cdr args)))
286 sym)
287
288
289 (defun calc-tex-language (n)
290 (interactive "P")
291 (calc-wrapper
292 (and n (setq n (prefix-numeric-value n)))
293 (calc-set-language 'tex n)
294 (message (if (and n (/= n 0))
295 (if (> n 0)
296 "TeX language mode with \\hbox{func}(\\hbox{var})"
297 "TeX language mode with \\func{\\hbox{var}}")
298 "TeX language mode"))))
299
300 (defun calc-latex-language (n)
301 (interactive "P")
302 (calc-wrapper
303 (and n (setq n (prefix-numeric-value n)))
304 (calc-set-language 'latex n)
305 (cond ((not n)
306 (message "LaTeX language mode"))
307 ((= n 0)
308 (message "LaTeX language mode with multiline matrices"))
309 ((= n 1)
310 (message "LaTeX language mode with \\text{func}(\\text{var})"))
311 ((> n 1)
312 (message
313 "LaTeX language mode with \\text{func}(\\text{var}) and multiline matrices"))
314 ((= n -1)
315 (message "LaTeX language mode with \\func(\\text{var})"))
316 ((< n -1)
317 (message
318 "LaTeX language mode with \\func(\\text{var}) and multiline matrices")))))
319
320 (put 'tex 'math-oper-table
321 '( ( "u+" ident -1 1000 )
322 ( "u-" neg -1 1000 )
323 ( "\\hat" calcFunc-hat -1 950 )
324 ( "\\check" calcFunc-check -1 950 )
325 ( "\\tilde" calcFunc-tilde -1 950 )
326 ( "\\acute" calcFunc-acute -1 950 )
327 ( "\\grave" calcFunc-grave -1 950 )
328 ( "\\dot" calcFunc-dot -1 950 )
329 ( "\\ddot" calcFunc-dotdot -1 950 )
330 ( "\\breve" calcFunc-breve -1 950 )
331 ( "\\bar" calcFunc-bar -1 950 )
332 ( "\\vec" calcFunc-Vec -1 950 )
333 ( "\\underline" calcFunc-under -1 950 )
334 ( "u|" calcFunc-abs -1 0 )
335 ( "|" closing 0 -1 )
336 ( "\\lfloor" calcFunc-floor -1 0 )
337 ( "\\rfloor" closing 0 -1 )
338 ( "\\lceil" calcFunc-ceil -1 0 )
339 ( "\\rceil" closing 0 -1 )
340 ( "\\pm" sdev 300 300 )
341 ( "!" calcFunc-fact 210 -1 )
342 ( "^" ^ 201 200 )
343 ( "_" calcFunc-subscr 201 200 )
344 ( "\\times" * 191 190 )
345 ( "*" * 191 190 )
346 ( "2x" * 191 190 )
347 ( "+" + 180 181 )
348 ( "-" - 180 181 )
349 ( "\\over" / 170 171 )
350 ( "/" / 170 171 )
351 ( "\\choose" calcFunc-choose 170 171 )
352 ( "\\mod" % 170 171 )
353 ( "<" calcFunc-lt 160 161 )
354 ( ">" calcFunc-gt 160 161 )
355 ( "\\leq" calcFunc-leq 160 161 )
356 ( "\\geq" calcFunc-geq 160 161 )
357 ( "=" calcFunc-eq 160 161 )
358 ( "\\neq" calcFunc-neq 160 161 )
359 ( "\\ne" calcFunc-neq 160 161 )
360 ( "\\lnot" calcFunc-lnot -1 121 )
361 ( "\\land" calcFunc-land 110 111 )
362 ( "\\lor" calcFunc-lor 100 101 )
363 ( "?" (math-read-if) 91 90 )
364 ( "!!!" calcFunc-pnot -1 85 )
365 ( "&&&" calcFunc-pand 80 81 )
366 ( "|||" calcFunc-por 75 76 )
367 ( "\\gets" calcFunc-assign 51 50 )
368 ( ":=" calcFunc-assign 51 50 )
369 ( "::" calcFunc-condition 45 46 )
370 ( "\\to" calcFunc-evalto 40 41 )
371 ( "\\to" calcFunc-evalto 40 -1 )
372 ( "=>" calcFunc-evalto 40 41 )
373 ( "=>" calcFunc-evalto 40 -1 )))
374
375 (put 'tex 'math-function-table
376 '( ( \\arccos . calcFunc-arccos )
377 ( \\arcsin . calcFunc-arcsin )
378 ( \\arctan . calcFunc-arctan )
379 ( \\arg . calcFunc-arg )
380 ( \\cos . calcFunc-cos )
381 ( \\cosh . calcFunc-cosh )
382 ( \\det . calcFunc-det )
383 ( \\exp . calcFunc-exp )
384 ( \\gcd . calcFunc-gcd )
385 ( \\ln . calcFunc-ln )
386 ( \\log . calcFunc-log10 )
387 ( \\max . calcFunc-max )
388 ( \\min . calcFunc-min )
389 ( \\tan . calcFunc-tan )
390 ( \\sin . calcFunc-sin )
391 ( \\sinh . calcFunc-sinh )
392 ( \\sqrt . calcFunc-sqrt )
393 ( \\tanh . calcFunc-tanh )
394 ( \\phi . calcFunc-totient )
395 ( \\mu . calcFunc-moebius )))
396
397 (put 'tex 'math-variable-table
398 '( ( \\pi . var-pi )
399 ( \\infty . var-inf )
400 ( \\infty . var-uinf )
401 ( \\phi . var-phi )
402 ( \\gamma . var-gamma )
403 ( \\sum . (math-parse-tex-sum calcFunc-sum) )
404 ( \\prod . (math-parse-tex-sum calcFunc-prod) )))
405
406 (put 'tex 'math-complex-format 'i)
407
408 (defun math-parse-tex-sum (f val)
409 (let (low high save)
410 (or (equal math-expr-data "_") (throw 'syntax "Expected `_'"))
411 (math-read-token)
412 (setq save math-exp-old-pos)
413 (setq low (math-read-factor))
414 (or (eq (car-safe low) 'calcFunc-eq)
415 (progn
416 (setq math-exp-old-pos (1+ save))
417 (throw 'syntax "Expected equation")))
418 (or (equal math-expr-data "^") (throw 'syntax "Expected `^'"))
419 (math-read-token)
420 (setq high (math-read-factor))
421 (list (nth 2 f) (math-read-factor) (nth 1 low) (nth 2 low) high)))
422
423 (defun math-tex-input-filter (str) ; allow parsing of 123\,456\,789.
424 (while (string-match "[0-9]\\\\,[0-9]" str)
425 (setq str (concat (substring str 0 (1+ (match-beginning 0)))
426 (substring str (1- (match-end 0))))))
427 str)
428 (put 'tex 'math-input-filter 'math-tex-input-filter)
429
430 (put 'latex 'math-oper-table
431 (append (get 'tex 'math-oper-table)
432 '(( "\\Hat" calcFunc-Hat -1 950 )
433 ( "\\Check" calcFunc-Check -1 950 )
434 ( "\\Tilde" calcFunc-Tilde -1 950 )
435 ( "\\Acute" calcFunc-Acute -1 950 )
436 ( "\\Grave" calcFunc-Grave -1 950 )
437 ( "\\Dot" calcFunc-Dot -1 950 )
438 ( "\\Ddot" calcFunc-Dotdot -1 950 )
439 ( "\\Breve" calcFunc-Breve -1 950 )
440 ( "\\Bar" calcFunc-Bar -1 950 )
441 ( "\\Vec" calcFunc-VEC -1 950 )
442 ( "\\dddot" calcFunc-dddot -1 950 )
443 ( "\\ddddot" calcFunc-ddddot -1 950 )
444 ( "\div" / 170 171 )
445 ( "\\le" calcFunc-leq 160 161 )
446 ( "\\leqq" calcFunc-leq 160 161 )
447 ( "\\leqsland" calcFunc-leq 160 161 )
448 ( "\\ge" calcFunc-geq 160 161 )
449 ( "\\geqq" calcFunc-geq 160 161 )
450 ( "\\geqslant" calcFunc-geq 160 161 )
451 ( "=" calcFunc-eq 160 161 )
452 ( "\\neq" calcFunc-neq 160 161 )
453 ( "\\ne" calcFunc-neq 160 161 )
454 ( "\\lnot" calcFunc-lnot -1 121 )
455 ( "\\land" calcFunc-land 110 111 )
456 ( "\\lor" calcFunc-lor 100 101 )
457 ( "?" (math-read-if) 91 90 )
458 ( "!!!" calcFunc-pnot -1 85 )
459 ( "&&&" calcFunc-pand 80 81 )
460 ( "|||" calcFunc-por 75 76 )
461 ( "\\gets" calcFunc-assign 51 50 )
462 ( ":=" calcFunc-assign 51 50 )
463 ( "::" calcFunc-condition 45 46 )
464 ( "\\to" calcFunc-evalto 40 41 )
465 ( "\\to" calcFunc-evalto 40 -1 )
466 ( "=>" calcFunc-evalto 40 41 )
467 ( "=>" calcFunc-evalto 40 -1 ))))
468
469 (put 'latex 'math-function-table
470 (append
471 (get 'tex 'math-function-table)
472 '(( \\frac . (math-latex-parse-frac /))
473 ( \\tfrac . (math-latex-parse-frac /))
474 ( \\dfrac . (math-latex-parse-frac /))
475 ( \\binom . (math-latex-parse-frac calcFunc-choose))
476 ( \\tbinom . (math-latex-parse-frac calcFunc-choose))
477 ( \\dbinom . (math-latex-parse-frac calcFunc-choose))
478 ( \\phi . calcFunc-totient )
479 ( \\mu . calcFunc-moebius ))))
480
481 (put 'latex 'math-special-function-table
482 '((/ . (math-latex-print-frac "\\frac"))
483 (calcFunc-choose . (math-latex-print-frac "\\binom"))))
484
485 (put 'latex 'math-variable-table
486 (get 'tex 'math-variable-table))
487
488 (put 'latex 'math-complex-format 'i)
489
490 (defun math-latex-parse-frac (f val)
491 (let (numer denom)
492 (setq args (math-read-expr-list))
493 (math-read-token)
494 (setq margs (math-read-factor))
495 (list (nth 2 f) (car args) margs)))
496
497 (defun math-latex-print-frac (a fn)
498 (list 'horiz (nth 1 fn) "{" (math-compose-expr (nth 1 a) -1)
499 "}{"
500 (math-compose-expr (nth 2 a) -1)
501 "}"))
502
503 (put 'latex 'math-input-filter 'math-tex-input-filter)
504
505 (defun calc-eqn-language (n)
506 (interactive "P")
507 (calc-wrapper
508 (calc-set-language 'eqn)
509 (message "Eqn language mode")))
510
511 (put 'eqn 'math-oper-table
512 '( ( "u+" ident -1 1000 )
513 ( "u-" neg -1 1000 )
514 ( "prime" (math-parse-eqn-prime) 950 -1 )
515 ( "prime" calcFunc-Prime 950 -1 )
516 ( "dot" calcFunc-dot 950 -1 )
517 ( "dotdot" calcFunc-dotdot 950 -1 )
518 ( "hat" calcFunc-hat 950 -1 )
519 ( "tilde" calcFunc-tilde 950 -1 )
520 ( "vec" calcFunc-Vec 950 -1 )
521 ( "dyad" calcFunc-dyad 950 -1 )
522 ( "bar" calcFunc-bar 950 -1 )
523 ( "under" calcFunc-under 950 -1 )
524 ( "sub" calcFunc-subscr 931 930 )
525 ( "sup" ^ 921 920 )
526 ( "sqrt" calcFunc-sqrt -1 910 )
527 ( "over" / 900 901 )
528 ( "u|" calcFunc-abs -1 0 )
529 ( "|" closing 0 -1 )
530 ( "left floor" calcFunc-floor -1 0 )
531 ( "right floor" closing 0 -1 )
532 ( "left ceil" calcFunc-ceil -1 0 )
533 ( "right ceil" closing 0 -1 )
534 ( "+-" sdev 300 300 )
535 ( "!" calcFunc-fact 210 -1 )
536 ( "times" * 191 190 )
537 ( "*" * 191 190 )
538 ( "2x" * 191 190 )
539 ( "/" / 180 181 )
540 ( "%" % 180 181 )
541 ( "+" + 170 171 )
542 ( "-" - 170 171 )
543 ( "<" calcFunc-lt 160 161 )
544 ( ">" calcFunc-gt 160 161 )
545 ( "<=" calcFunc-leq 160 161 )
546 ( ">=" calcFunc-geq 160 161 )
547 ( "=" calcFunc-eq 160 161 )
548 ( "==" calcFunc-eq 160 161 )
549 ( "!=" calcFunc-neq 160 161 )
550 ( "u!" calcFunc-lnot -1 121 )
551 ( "&&" calcFunc-land 110 111 )
552 ( "||" calcFunc-lor 100 101 )
553 ( "?" (math-read-if) 91 90 )
554 ( "!!!" calcFunc-pnot -1 85 )
555 ( "&&&" calcFunc-pand 80 81 )
556 ( "|||" calcFunc-por 75 76 )
557 ( "<-" calcFunc-assign 51 50 )
558 ( ":=" calcFunc-assign 51 50 )
559 ( "::" calcFunc-condition 45 46 )
560 ( "->" calcFunc-evalto 40 41 )
561 ( "->" calcFunc-evalto 40 -1 )
562 ( "=>" calcFunc-evalto 40 41 )
563 ( "=>" calcFunc-evalto 40 -1 )))
564
565 (put 'eqn 'math-function-table
566 '( ( arc\ cos . calcFunc-arccos )
567 ( arc\ cosh . calcFunc-arccosh )
568 ( arc\ sin . calcFunc-arcsin )
569 ( arc\ sinh . calcFunc-arcsinh )
570 ( arc\ tan . calcFunc-arctan )
571 ( arc\ tanh . calcFunc-arctanh )
572 ( GAMMA . calcFunc-gamma )
573 ( phi . calcFunc-totient )
574 ( mu . calcFunc-moebius )
575 ( matrix . (math-parse-eqn-matrix) )))
576
577 (put 'eqn 'math-variable-table
578 '( ( inf . var-uinf )))
579
580 (put 'eqn 'math-complex-format 'i)
581
582 (defun math-parse-eqn-matrix (f sym)
583 (let ((vec nil))
584 (while (assoc math-expr-data '(("ccol") ("lcol") ("rcol")))
585 (math-read-token)
586 (or (equal math-expr-data calc-function-open)
587 (throw 'syntax "Expected `{'"))
588 (math-read-token)
589 (setq vec (cons (cons 'vec (math-read-expr-list)) vec))
590 (or (equal math-expr-data calc-function-close)
591 (throw 'syntax "Expected `}'"))
592 (math-read-token))
593 (or (equal math-expr-data calc-function-close)
594 (throw 'syntax "Expected `}'"))
595 (math-read-token)
596 (math-transpose (cons 'vec (nreverse vec)))))
597
598 (defun math-parse-eqn-prime (x sym)
599 (if (eq (car-safe x) 'var)
600 (if (equal math-expr-data calc-function-open)
601 (progn
602 (math-read-token)
603 (let ((args (if (or (equal math-expr-data calc-function-close)
604 (eq math-exp-token 'end))
605 nil
606 (math-read-expr-list))))
607 (if (not (or (equal math-expr-data calc-function-close)
608 (eq math-exp-token 'end)))
609 (throw 'syntax "Expected `)'"))
610 (math-read-token)
611 (cons (intern (format "calcFunc-%s'" (nth 1 x))) args)))
612 (list 'var
613 (intern (concat (symbol-name (nth 1 x)) "'"))
614 (intern (concat (symbol-name (nth 2 x)) "'"))))
615 (list 'calcFunc-Prime x)))
616
617
618 (defun calc-mathematica-language ()
619 (interactive)
620 (calc-wrapper
621 (calc-set-language 'math)
622 (message "Mathematica language mode")))
623
624 (put 'math 'math-oper-table
625 '( ( "[[" (math-read-math-subscr) 250 -1 )
626 ( "!" calcFunc-fact 210 -1 )
627 ( "!!" calcFunc-dfact 210 -1 )
628 ( "^" ^ 201 200 )
629 ( "u+" ident -1 197 )
630 ( "u-" neg -1 197 )
631 ( "/" / 195 196 )
632 ( "*" * 190 191 )
633 ( "2x" * 190 191 )
634 ( "+" + 180 181 )
635 ( "-" - 180 181 )
636 ( "<" calcFunc-lt 160 161 )
637 ( ">" calcFunc-gt 160 161 )
638 ( "<=" calcFunc-leq 160 161 )
639 ( ">=" calcFunc-geq 160 161 )
640 ( "==" calcFunc-eq 150 151 )
641 ( "!=" calcFunc-neq 150 151 )
642 ( "u!" calcFunc-lnot -1 121 )
643 ( "&&" calcFunc-land 110 111 )
644 ( "||" calcFunc-lor 100 101 )
645 ( "!!!" calcFunc-pnot -1 85 )
646 ( "&&&" calcFunc-pand 80 81 )
647 ( "|||" calcFunc-por 75 76 )
648 ( ":=" calcFunc-assign 51 50 )
649 ( "=" calcFunc-assign 51 50 )
650 ( "->" calcFunc-assign 51 50 )
651 ( ":>" calcFunc-assign 51 50 )
652 ( "::" calcFunc-condition 45 46 )
653 ))
654
655 (put 'math 'math-function-table
656 '( ( Abs . calcFunc-abs )
657 ( ArcCos . calcFunc-arccos )
658 ( ArcCosh . calcFunc-arccosh )
659 ( ArcSin . calcFunc-arcsin )
660 ( ArcSinh . calcFunc-arcsinh )
661 ( ArcTan . calcFunc-arctan )
662 ( ArcTanh . calcFunc-arctanh )
663 ( Arg . calcFunc-arg )
664 ( Binomial . calcFunc-choose )
665 ( Ceiling . calcFunc-ceil )
666 ( Conjugate . calcFunc-conj )
667 ( Cos . calcFunc-cos )
668 ( Cosh . calcFunc-cosh )
669 ( D . calcFunc-deriv )
670 ( Dt . calcFunc-tderiv )
671 ( Det . calcFunc-det )
672 ( Exp . calcFunc-exp )
673 ( EulerPhi . calcFunc-totient )
674 ( Floor . calcFunc-floor )
675 ( Gamma . calcFunc-gamma )
676 ( GCD . calcFunc-gcd )
677 ( If . calcFunc-if )
678 ( Im . calcFunc-im )
679 ( Inverse . calcFunc-inv )
680 ( Integrate . calcFunc-integ )
681 ( Join . calcFunc-vconcat )
682 ( LCM . calcFunc-lcm )
683 ( Log . calcFunc-ln )
684 ( Max . calcFunc-max )
685 ( Min . calcFunc-min )
686 ( Mod . calcFunc-mod )
687 ( MoebiusMu . calcFunc-moebius )
688 ( Random . calcFunc-random )
689 ( Round . calcFunc-round )
690 ( Re . calcFunc-re )
691 ( Sign . calcFunc-sign )
692 ( Sin . calcFunc-sin )
693 ( Sinh . calcFunc-sinh )
694 ( Sqrt . calcFunc-sqrt )
695 ( Tan . calcFunc-tan )
696 ( Tanh . calcFunc-tanh )
697 ( Transpose . calcFunc-trn )
698 ( Length . calcFunc-vlen )
699 ))
700
701 (put 'math 'math-variable-table
702 '( ( I . var-i )
703 ( Pi . var-pi )
704 ( E . var-e )
705 ( GoldenRatio . var-phi )
706 ( EulerGamma . var-gamma )
707 ( Infinity . var-inf )
708 ( ComplexInfinity . var-uinf )
709 ( Indeterminate . var-nan )
710 ))
711
712 (put 'math 'math-vector-brackets "{}")
713 (put 'math 'math-complex-format 'I)
714 (put 'math 'math-function-open "[")
715 (put 'math 'math-function-close "]")
716
717 (put 'math 'math-radix-formatter
718 (function (lambda (r s) (format "%d^^%s" r s))))
719
720 (defun math-read-math-subscr (x op)
721 (let ((idx (math-read-expr-level 0)))
722 (or (and (equal math-expr-data "]")
723 (progn
724 (math-read-token)
725 (equal math-expr-data "]")))
726 (throw 'syntax "Expected ']]'"))
727 (math-read-token)
728 (list 'calcFunc-subscr x idx)))
729
730
731 (defun calc-maple-language ()
732 (interactive)
733 (calc-wrapper
734 (calc-set-language 'maple)
735 (message "Maple language mode")))
736
737 (put 'maple 'math-oper-table
738 '( ( "matrix" ident -1 300 )
739 ( "MATRIX" ident -1 300 )
740 ( "!" calcFunc-fact 210 -1 )
741 ( "^" ^ 201 200 )
742 ( "**" ^ 201 200 )
743 ( "u+" ident -1 197 )
744 ( "u-" neg -1 197 )
745 ( "/" / 191 192 )
746 ( "*" * 191 192 )
747 ( "intersect" calcFunc-vint 191 192 )
748 ( "+" + 180 181 )
749 ( "-" - 180 181 )
750 ( "union" calcFunc-vunion 180 181 )
751 ( "minus" calcFunc-vdiff 180 181 )
752 ( "mod" % 170 170 )
753 ( ".." (math-read-maple-dots) 165 165 )
754 ( "\\dots" (math-read-maple-dots) 165 165 )
755 ( "<" calcFunc-lt 160 160 )
756 ( ">" calcFunc-gt 160 160 )
757 ( "<=" calcFunc-leq 160 160 )
758 ( ">=" calcFunc-geq 160 160 )
759 ( "=" calcFunc-eq 160 160 )
760 ( "<>" calcFunc-neq 160 160 )
761 ( "not" calcFunc-lnot -1 121 )
762 ( "and" calcFunc-land 110 111 )
763 ( "or" calcFunc-lor 100 101 )
764 ( "!!!" calcFunc-pnot -1 85 )
765 ( "&&&" calcFunc-pand 80 81 )
766 ( "|||" calcFunc-por 75 76 )
767 ( ":=" calcFunc-assign 51 50 )
768 ( "::" calcFunc-condition 45 46 )
769 ))
770
771 (put 'maple 'math-function-table
772 '( ( bernoulli . calcFunc-bern )
773 ( binomial . calcFunc-choose )
774 ( diff . calcFunc-deriv )
775 ( GAMMA . calcFunc-gamma )
776 ( ifactor . calcFunc-prfac )
777 ( igcd . calcFunc-gcd )
778 ( ilcm . calcFunc-lcm )
779 ( int . calcFunc-integ )
780 ( modp . % )
781 ( irem . % )
782 ( iquo . calcFunc-idiv )
783 ( isprime . calcFunc-prime )
784 ( length . calcFunc-vlen )
785 ( member . calcFunc-in )
786 ( crossprod . calcFunc-cross )
787 ( inverse . calcFunc-inv )
788 ( trace . calcFunc-tr )
789 ( transpose . calcFunc-trn )
790 ( vectdim . calcFunc-vlen )
791 ))
792
793 (put 'maple 'math-variable-table
794 '( ( I . var-i )
795 ( Pi . var-pi )
796 ( E . var-e )
797 ( infinity . var-inf )
798 ( infinity . var-uinf )
799 ( infinity . var-nan )
800 ))
801
802 (put 'maple 'math-complex-format 'I)
803
804 (defun math-read-maple-dots (x op)
805 (list 'intv 3 x (math-read-expr-level (nth 3 op))))
806
807
808 ;; The variable math-read-big-lines is local to math-read-big-expr in
809 ;; calc-ext.el, but is used by math-read-big-rec, math-read-big-char,
810 ;; math-read-big-emptyp, math-read-big-error and math-read-big-balance,
811 ;; which are called (directly and indirectly) by math-read-big-expr.
812 ;; It is also local to math-read-big-bigp in calc-ext.el, which calls
813 ;; math-read-big-balance.
814 (defvar math-read-big-lines)
815
816 ;; The variables math-read-big-baseline and math-read-big-h2 are
817 ;; local to math-read-big-expr in calc-ext.el, but used by
818 ;; math-read-big-rec.
819 (defvar math-read-big-baseline)
820 (defvar math-read-big-h2)
821
822 ;; The variables math-rb-h1, math-rb-h2, math-rb-v1 and math-rb-v2
823 ;; are local to math-read-big-rec, but are used by math-read-big-char,
824 ;; math-read-big-emptyp and math-read-big-balance which are called by
825 ;; math-read-big-rec.
826 ;; math-rb-h2 is also local to math-read-big-bigp in calc-ext.el,
827 ;; which calls math-read-big-balance.
828 (defvar math-rb-h1)
829 (defvar math-rb-h2)
830 (defvar math-rb-v1)
831 (defvar math-rb-v2)
832
833 (defun math-read-big-rec (math-rb-h1 math-rb-v1 math-rb-h2 math-rb-v2
834 &optional baseline prec short)
835 (or prec (setq prec 0))
836
837 ;; Clip whitespace above or below.
838 (while (and (< math-rb-v1 math-rb-v2)
839 (math-read-big-emptyp math-rb-h1 math-rb-v1 math-rb-h2 (1+ math-rb-v1)))
840 (setq math-rb-v1 (1+ math-rb-v1)))
841 (while (and (< math-rb-v1 math-rb-v2)
842 (math-read-big-emptyp math-rb-h1 (1- math-rb-v2) math-rb-h2 math-rb-v2))
843 (setq math-rb-v2 (1- math-rb-v2)))
844
845 ;; If formula is a single line high, normal parser can handle it.
846 (if (<= math-rb-v2 (1+ math-rb-v1))
847 (if (or (<= math-rb-v2 math-rb-v1)
848 (> math-rb-h1 (length (setq math-rb-v2
849 (nth math-rb-v1 math-read-big-lines)))))
850 (math-read-big-error math-rb-h1 math-rb-v1)
851 (setq math-read-big-baseline math-rb-v1
852 math-read-big-h2 math-rb-h2
853 math-rb-v2 (nth math-rb-v1 math-read-big-lines)
854 math-rb-h2 (math-read-expr
855 (substring math-rb-v2 math-rb-h1
856 (min math-rb-h2 (length math-rb-v2)))))
857 (if (eq (car-safe math-rb-h2) 'error)
858 (math-read-big-error (+ math-rb-h1 (nth 1 math-rb-h2))
859 math-rb-v1 (nth 2 math-rb-h2))
860 math-rb-h2))
861
862 ;; Clip whitespace at left or right.
863 (while (and (< math-rb-h1 math-rb-h2)
864 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) math-rb-v2))
865 (setq math-rb-h1 (1+ math-rb-h1)))
866 (while (and (< math-rb-h1 math-rb-h2)
867 (math-read-big-emptyp (1- math-rb-h2) math-rb-v1 math-rb-h2 math-rb-v2))
868 (setq math-rb-h2 (1- math-rb-h2)))
869
870 ;; Scan to find widest left-justified "----" in the region.
871 (let* ((widest nil)
872 (widest-h2 0)
873 (lines-v1 (nthcdr math-rb-v1 math-read-big-lines))
874 (p lines-v1)
875 (v math-rb-v1)
876 (other-v nil)
877 other-char line len h)
878 (while (< v math-rb-v2)
879 (setq line (car p)
880 len (min math-rb-h2 (length line)))
881 (and (< math-rb-h1 len)
882 (/= (aref line math-rb-h1) ?\ )
883 (if (and (= (aref line math-rb-h1) ?\-)
884 ;; Make sure it's not a minus sign.
885 (or (and (< (1+ math-rb-h1) len)
886 (= (aref line (1+ math-rb-h1)) ?\-))
887 (/= (math-read-big-char math-rb-h1 (1- v)) ?\ )
888 (/= (math-read-big-char math-rb-h1 (1+ v)) ?\ )))
889 (progn
890 (setq h math-rb-h1)
891 (while (and (< (setq h (1+ h)) len)
892 (= (aref line h) ?\-)))
893 (if (> h widest-h2)
894 (setq widest v
895 widest-h2 h)))
896 (or other-v (setq other-v v other-char (aref line math-rb-h1)))))
897 (setq v (1+ v)
898 p (cdr p)))
899
900 (cond ((not (setq v other-v))
901 (math-read-big-error math-rb-h1 math-rb-v1)) ; Should never happen!
902
903 ;; Quotient.
904 (widest
905 (setq h widest-h2
906 v widest)
907 (let ((num (math-read-big-rec math-rb-h1 math-rb-v1 h v))
908 (den (math-read-big-rec math-rb-h1 (1+ v) h math-rb-v2)))
909 (setq p (if (and (math-integerp num) (math-integerp den))
910 (math-make-frac num den)
911 (list '/ num den)))))
912
913 ;; Big radical sign.
914 ((= other-char ?\\)
915 (or (= (math-read-big-char (1+ math-rb-h1) v) ?\|)
916 (math-read-big-error (1+ math-rb-h1) v "Malformed root sign"))
917 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
918 (while (= (math-read-big-char (1+ math-rb-h1) (setq v (1- v))) ?\|))
919 (or (= (math-read-big-char (setq h (+ math-rb-h1 2)) v) ?\_)
920 (math-read-big-error h v "Malformed root sign"))
921 (while (= (math-read-big-char (setq h (1+ h)) v) ?\_))
922 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
923 (math-read-big-emptyp math-rb-h1 (1+ other-v) h math-rb-v2 nil t)
924 (setq p (list 'calcFunc-sqrt (math-read-big-rec
925 (+ math-rb-h1 2) (1+ v)
926 h (1+ other-v) baseline))
927 v math-read-big-baseline))
928
929 ;; Small radical sign.
930 ((and (= other-char ?V)
931 (= (math-read-big-char (1+ math-rb-h1) (1- v)) ?\_))
932 (setq h (1+ math-rb-h1))
933 (math-read-big-emptyp math-rb-h1 math-rb-v1 h (1- v) nil t)
934 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
935 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
936 (while (= (math-read-big-char (setq h (1+ h)) (1- v)) ?\_))
937 (setq p (list 'calcFunc-sqrt (math-read-big-rec
938 (1+ math-rb-h1) v h (1+ v) t))
939 v math-read-big-baseline))
940
941 ;; Binomial coefficient.
942 ((and (= other-char ?\()
943 (= (math-read-big-char (1+ math-rb-h1) v) ?\ )
944 (= (string-match "( *)" (nth v math-read-big-lines)
945 math-rb-h1) math-rb-h1))
946 (setq h (match-end 0))
947 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
948 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
949 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
950 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
951 (setq p (list 'calcFunc-choose
952 (math-read-big-rec (1+ math-rb-h1) math-rb-v1 (1- h) v)
953 (math-read-big-rec (1+ math-rb-h1) (1+ v)
954 (1- h) math-rb-v2))))
955
956 ;; Minus sign.
957 ((= other-char ?\-)
958 (setq p (list 'neg (math-read-big-rec (1+ math-rb-h1) math-rb-v1
959 math-rb-h2 math-rb-v2 v 250 t))
960 v math-read-big-baseline
961 h math-read-big-h2))
962
963 ;; Parentheses.
964 ((= other-char ?\()
965 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
966 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
967 (setq h (math-read-big-balance (1+ math-rb-h1) v "(" t))
968 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
969 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
970 (let ((sep (math-read-big-char (1- h) v))
971 hmid)
972 (if (= sep ?\.)
973 (setq h (1+ h)))
974 (if (= sep ?\])
975 (math-read-big-error (1- h) v "Expected `)'"))
976 (if (= sep ?\))
977 (setq p (math-read-big-rec
978 (1+ math-rb-h1) math-rb-v1 (1- h) math-rb-v2 v))
979 (setq hmid (math-read-big-balance h v "(")
980 p (list p
981 (math-read-big-rec h math-rb-v1 (1- hmid) math-rb-v2 v))
982 h hmid)
983 (cond ((= sep ?\.)
984 (setq p (cons 'intv (cons (if (= (math-read-big-char
985 (1- h) v)
986 ?\))
987 0 1)
988 p))))
989 ((= (math-read-big-char (1- h) v) ?\])
990 (math-read-big-error (1- h) v "Expected `)'"))
991 ((= sep ?\,)
992 (or (and (math-realp (car p)) (math-realp (nth 1 p)))
993 (math-read-big-error
994 math-rb-h1 v "Complex components must be real"))
995 (setq p (cons 'cplx p)))
996 ((= sep ?\;)
997 (or (and (math-realp (car p)) (math-anglep (nth 1 p)))
998 (math-read-big-error
999 math-rb-h1 v "Complex components must be real"))
1000 (setq p (cons 'polar p)))))))
1001
1002 ;; Matrix.
1003 ((and (= other-char ?\[)
1004 (or (= (math-read-big-char (setq h math-rb-h1) (1+ v)) ?\[)
1005 (= (math-read-big-char (setq h (1+ h)) v) ?\[)
1006 (and (= (math-read-big-char h v) ?\ )
1007 (= (math-read-big-char (setq h (1+ h)) v) ?\[)))
1008 (= (math-read-big-char h (1+ v)) ?\[))
1009 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1010 (let ((vtop v)
1011 (hleft h)
1012 (hright nil))
1013 (setq p nil)
1014 (while (progn
1015 (setq h (math-read-big-balance (1+ hleft) v "["))
1016 (if hright
1017 (or (= h hright)
1018 (math-read-big-error hright v "Expected `]'"))
1019 (setq hright h))
1020 (setq p (cons (math-read-big-rec
1021 hleft v h (1+ v)) p))
1022 (and (memq (math-read-big-char h v) '(?\ ?\,))
1023 (= (math-read-big-char hleft (1+ v)) ?\[)))
1024 (setq v (1+ v)))
1025 (or (= hleft math-rb-h1)
1026 (progn
1027 (if (= (math-read-big-char h v) ?\ )
1028 (setq h (1+ h)))
1029 (and (= (math-read-big-char h v) ?\])
1030 (setq h (1+ h))))
1031 (math-read-big-error (1- h) v "Expected `]'"))
1032 (if (= (math-read-big-char h vtop) ?\,)
1033 (setq h (1+ h)))
1034 (math-read-big-emptyp math-rb-h1 (1+ v) (1- h) math-rb-v2 nil t)
1035 (setq v (+ vtop (/ (- v vtop) 2))
1036 p (cons 'vec (nreverse p)))))
1037
1038 ;; Square brackets.
1039 ((= other-char ?\[)
1040 (math-read-big-emptyp math-rb-h1 math-rb-v1 (1+ math-rb-h1) v nil t)
1041 (math-read-big-emptyp math-rb-h1 (1+ v) (1+ math-rb-h1) math-rb-v2 nil t)
1042 (setq p nil
1043 h (1+ math-rb-h1))
1044 (while (progn
1045 (setq widest (math-read-big-balance h v "[" t))
1046 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1047 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1048 (setq p (cons (math-read-big-rec
1049 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1050 h widest)
1051 (= (math-read-big-char (1- h) v) ?\,)))
1052 (setq widest (math-read-big-char (1- h) v))
1053 (if (or (memq widest '(?\; ?\)))
1054 (and (eq widest ?\.) (cdr p)))
1055 (math-read-big-error (1- h) v "Expected `]'"))
1056 (if (= widest ?\.)
1057 (setq h (1+ h)
1058 widest (math-read-big-balance h v "[")
1059 p (nconc p (list (math-read-big-rec
1060 h math-rb-v1 (1- widest) math-rb-v2 v)))
1061 h widest
1062 p (cons 'intv (cons (if (= (math-read-big-char (1- h) v)
1063 ?\])
1064 3 2)
1065 p)))
1066 (setq p (cons 'vec (nreverse p)))))
1067
1068 ;; Date form.
1069 ((= other-char ?\<)
1070 (setq line (nth v math-read-big-lines))
1071 (string-match ">" line math-rb-h1)
1072 (setq h (match-end 0))
1073 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1074 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1075 (setq p (math-read-big-rec math-rb-h1 v h (1+ v) v)))
1076
1077 ;; Variable name or function call.
1078 ((or (and (>= other-char ?a) (<= other-char ?z))
1079 (and (>= other-char ?A) (<= other-char ?Z)))
1080 (setq line (nth v math-read-big-lines))
1081 (string-match "\\([a-zA-Z'_]+\\) *" line math-rb-h1)
1082 (setq h (match-end 1)
1083 widest (match-end 0)
1084 p (math-match-substring line 1))
1085 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1086 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)
1087 (if (= (math-read-big-char widest v) ?\()
1088 (progn
1089 (setq line (if (string-match "-" p)
1090 (intern p)
1091 (intern (concat "calcFunc-" p)))
1092 h (1+ widest)
1093 p nil)
1094 (math-read-big-emptyp widest math-rb-v1 h v nil t)
1095 (math-read-big-emptyp widest (1+ v) h math-rb-v2 nil t)
1096 (while (progn
1097 (setq widest (math-read-big-balance h v "(" t))
1098 (math-read-big-emptyp (1- h) math-rb-v1 h v nil t)
1099 (math-read-big-emptyp (1- h) (1+ v) h math-rb-v2 nil t)
1100 (setq p (cons (math-read-big-rec
1101 h math-rb-v1 (1- widest) math-rb-v2 v) p)
1102 h widest)
1103 (= (math-read-big-char (1- h) v) ?\,)))
1104 (or (= (math-read-big-char (1- h) v) ?\))
1105 (math-read-big-error (1- h) v "Expected `)'"))
1106 (setq p (cons line (nreverse p))))
1107 (setq p (list 'var
1108 (intern (math-remove-dashes p))
1109 (if (string-match "-" p)
1110 (intern p)
1111 (intern (concat "var-" p)))))))
1112
1113 ;; Number.
1114 (t
1115 (setq line (nth v math-read-big-lines))
1116 (or (= (string-match "_?\\([0-9]+.?0*@ *\\)?\\([0-9]+.?0*' *\\)?\\([0-9]+\\(#\\|\\^\\^\\)[0-9a-zA-Z:]+\\|[0-9]+:[0-9:]+\\|[0-9.]+\\([eE][-+_]?[0-9]+\\)?\"?\\)?" line math-rb-h1) math-rb-h1)
1117 (math-read-big-error h v "Expected a number"))
1118 (setq h (match-end 0)
1119 p (math-read-number (math-match-substring line 0)))
1120 (math-read-big-emptyp math-rb-h1 math-rb-v1 h v nil t)
1121 (math-read-big-emptyp math-rb-h1 (1+ v) h math-rb-v2 nil t)))
1122
1123 ;; Now left term is bounded by math-rb-h1, math-rb-v1, h, math-rb-v2;
1124 ;; baseline = v.
1125 (if baseline
1126 (or (= v baseline)
1127 (math-read-big-error math-rb-h1 v "Inconsistent baseline in formula"))
1128 (setq baseline v))
1129
1130 ;; Look for superscripts or subscripts.
1131 (setq line (nth baseline math-read-big-lines)
1132 len (min math-rb-h2 (length line))
1133 widest h)
1134 (while (and (< widest len)
1135 (= (aref line widest) ?\ ))
1136 (setq widest (1+ widest)))
1137 (and (>= widest len) (setq widest math-rb-h2))
1138 (if (math-read-big-emptyp h v widest math-rb-v2)
1139 (if (math-read-big-emptyp h math-rb-v1 widest v)
1140 (setq h widest)
1141 (setq p (list '^ p (math-read-big-rec h math-rb-v1 widest v))
1142 h widest))
1143 (if (math-read-big-emptyp h math-rb-v1 widest v)
1144 (setq p (list 'calcFunc-subscr p
1145 (math-read-big-rec h v widest math-rb-v2))
1146 h widest)))
1147
1148 ;; Look for an operator name and grab additional terms.
1149 (while (and (< h len)
1150 (if (setq widest (and (math-read-big-emptyp
1151 h math-rb-v1 (1+ h) v)
1152 (math-read-big-emptyp
1153 h (1+ v) (1+ h) math-rb-v2)
1154 (string-match "<=\\|>=\\|\\+/-\\|!=\\|&&\\|||\\|:=\\|=>\\|." line h)
1155 (assoc (math-match-substring line 0)
1156 math-standard-opers)))
1157 (and (>= (nth 2 widest) prec)
1158 (setq h (match-end 0)))
1159 (and (not (eq (string-match ",\\|;\\|\\.\\.\\|)\\|\\]\\|:" line h)
1160 h))
1161 (setq widest '("2x" * 196 195)))))
1162 (cond ((eq (nth 3 widest) -1)
1163 (setq p (list (nth 1 widest) p)))
1164 ((equal (car widest) "?")
1165 (let ((y (math-read-big-rec h math-rb-v1 math-rb-h2
1166 math-rb-v2 baseline nil t)))
1167 (or (= (math-read-big-char math-read-big-h2 baseline) ?\:)
1168 (math-read-big-error math-read-big-h2 baseline "Expected `:'"))
1169 (setq p (list (nth 1 widest) p y
1170 (math-read-big-rec
1171 (1+ math-read-big-h2) math-rb-v1 math-rb-h2 math-rb-v2
1172 baseline (nth 3 widest) t))
1173 h math-read-big-h2)))
1174 (t
1175 (setq p (list (nth 1 widest) p
1176 (math-read-big-rec h math-rb-v1 math-rb-h2 math-rb-v2
1177 baseline (nth 3 widest) t))
1178 h math-read-big-h2))))
1179
1180 ;; Return all relevant information to caller.
1181 (setq math-read-big-baseline baseline
1182 math-read-big-h2 h)
1183 (or short (= math-read-big-h2 math-rb-h2)
1184 (math-read-big-error h baseline))
1185 p)))
1186
1187 (defun math-read-big-char (h v)
1188 (or (and (>= h math-rb-h1)
1189 (< h math-rb-h2)
1190 (>= v math-rb-v1)
1191 (< v math-rb-v2)
1192 (let ((line (nth v math-read-big-lines)))
1193 (and line
1194 (< h (length line))
1195 (aref line h))))
1196 ?\ ))
1197
1198 (defun math-read-big-emptyp (eh1 ev1 eh2 ev2 &optional what error)
1199 (and (< ev1 math-rb-v1) (setq ev1 math-rb-v1))
1200 (and (< eh1 math-rb-h1) (setq eh1 math-rb-h1))
1201 (and (> ev2 math-rb-v2) (setq ev2 math-rb-v2))
1202 (and (> eh2 math-rb-h2) (setq eh2 math-rb-h2))
1203 (or what (setq what ?\ ))
1204 (let ((p (nthcdr ev1 math-read-big-lines))
1205 h)
1206 (while (and (< ev1 ev2)
1207 (progn
1208 (setq h (min eh2 (length (car p))))
1209 (while (and (>= (setq h (1- h)) eh1)
1210 (= (aref (car p) h) what)))
1211 (and error (>= h eh1)
1212 (math-read-big-error h ev1 (if (stringp error)
1213 error
1214 "Whitespace expected")))
1215 (< h eh1)))
1216 (setq ev1 (1+ ev1)
1217 p (cdr p)))
1218 (>= ev1 ev2)))
1219
1220 ;; math-read-big-err-msg is local to math-read-big-expr in calc-ext.el,
1221 ;; but is used by math-read-big-error which is called (indirectly) by
1222 ;; math-read-big-expr.
1223 (defvar math-read-big-err-msg)
1224
1225 (defun math-read-big-error (h v &optional msg)
1226 (let ((pos 0)
1227 (p math-read-big-lines))
1228 (while (> v 0)
1229 (setq pos (+ pos 1 (length (car p)))
1230 p (cdr p)
1231 v (1- v)))
1232 (setq h (+ pos (min h (length (car p))))
1233 math-read-big-err-msg (list 'error h (or msg "Syntax error")))
1234 (throw 'syntax nil)))
1235
1236 (defun math-read-big-balance (h v what &optional commas)
1237 (let* ((line (nth v math-read-big-lines))
1238 (len (min math-rb-h2 (length line)))
1239 (count 1))
1240 (while (> count 0)
1241 (if (>= h len)
1242 (if what
1243 (math-read-big-error nil v (format "Unmatched `%s'" what))
1244 (setq count 0))
1245 (if (memq (aref line h) '(?\( ?\[))
1246 (setq count (1+ count))
1247 (if (if (and commas (= count 1))
1248 (or (memq (aref line h) '(?\) ?\] ?\, ?\;))
1249 (and (eq (aref line h) ?\.)
1250 (< (1+ h) len)
1251 (eq (aref line (1+ h)) ?\.)))
1252 (memq (aref line h) '(?\) ?\])))
1253 (setq count (1- count))))
1254 (setq h (1+ h))))
1255 h))
1256
1257 (provide 'calc-lang)
1258
1259 ;;; arch-tag: 483bfe15-f290-4fef-bb7d-ce65be687f2e
1260 ;;; calc-lang.el ends here