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