1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1981 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module nparse
)
15 (load-macsyma-macros defcal mopers
)
17 (defvar *ascii-space-chars-for-maxima
* '(#\tab
#\space
#\linefeed
#\return
#\page
#\newline
))
19 (defvar *unicode-space-chars-for-maxima
*
20 #-
(or unicode sb-unicode openmcl-unicode-strings abcl
(and allegro ics
)) nil
21 #+(or unicode sb-unicode openmcl-unicode-strings abcl
(and allegro ics
))
22 ;; Adapted from the list given by: https://jkorpela.fi/chars/spaces.html
23 ;; omitting SPACE, OGHAM SPACE MARK, MONGOLIAN VOWEL SEPARATOR, IDEOGRAPHIC SPACE,
24 ;; and ZERO WIDTH NO-BREAK SPACE.
26 #.
(code-char #x00A0
) ;; NO-BREAK SPACE
27 #.
(code-char #x2000
) ;; EN QUAD
28 #.
(code-char #x2001
) ;; EM QUAD
29 #.
(code-char #x2002
) ;; EN SPACE
30 #.
(code-char #x2003
) ;; EM SPACE
31 #.
(code-char #x2004
) ;; THREE-PER-EM SPACE
32 #.
(code-char #x2005
) ;; FOUR-PER-EM SPACE
33 #.
(code-char #x2006
) ;; SIX-PER-EM SPACE
34 #.
(code-char #x2007
) ;; FIGURE SPACE
35 #.
(code-char #x2008
) ;; PUNCTUATION SPACE
36 #.
(code-char #x2009
) ;; THIN SPACE
37 #.
(code-char #x200A
) ;; HAIR SPACE
38 #.
(code-char #x200B
) ;; ZERO WIDTH SPACE
39 #.
(code-char #x202F
) ;; NARROW NO-BREAK SPACE
40 #.
(code-char #x205F
) ;; MEDIUM MATHEMATICAL SPACE
43 (defmvar *whitespace-chars
* (append *ascii-space-chars-for-maxima
* *unicode-space-chars-for-maxima
*))
45 (defmvar *alphabet
* (list #\_
#\%
))
49 (or (alpha-char-p n
) #+gcl
(>= (char-code n
) 128)
50 (member n
*alphabet
*))))
52 (defun ascii-numberp (num)
53 (and (characterp num
) (char<= num
#\
9) (char>= num
#\
0)))
55 (defvar *parse-window
* nil
)
56 (defvar *parse-stream
* () "input stream for Maxima parser")
57 (defvar *parse-stream-eof
* -
1 "EOF value for Maxima parser")
58 (defvar *parse-tyi
* nil
)
60 (defvar *mread-prompt
* nil
"prompt used by `mread'")
61 (defvar *mread-eof-obj
* () "Bound by `mread' for use by `mread-raw'")
62 (defvar *current-line-info
* nil
)
64 (defvar *parse-string-input-stream
* ;; reference to the input stream
65 (let ((stream (make-string-input-stream ""))) ;; used by parse-string
66 (close stream
) ;; in share/stringroc/eval_string.lisp
67 stream
)) ;; (see also add-lineinfo below)
69 (defmvar $report_synerr_line t
"If T, report line number where syntax error occurs; otherwise, report FILE-POSITION of error.")
70 (defmvar $report_synerr_info t
"If T, report the syntax error details from all sources; otherwise, only report details from standard-input.")
72 (defun mread-synerr (format-string &rest l
)
73 (let ((fp (and (not (eq *parse-stream
* *standard-input
*))
74 (file-position *parse-stream
*)))
75 (file (and (not (eq *parse-stream
* *standard-input
*))
76 (cadr *current-line-info
*))))
77 (flet ((line-number ()
78 ;; Fix me: Neither batch nor load track the line number
79 ;; correctly. batch, via dbm-read, does not track the
80 ;; line number at all (a bug?).
82 ;; Find the line number by jumping to the start of file
83 ;; and reading line-by-line til we reach the current
85 (cond ((and fp
(file-position *parse-stream
* 0))
86 (do ((l (read-line *parse-stream
* nil nil
) (read-line *parse-stream
* nil nil
))
88 (p (file-position *parse-stream
*) (file-position *parse-stream
*))
90 ((or (null p
) (>= p fp
))
94 (let ((n (get '*parse-window
* 'length
))
96 (loop for i from
(1- n
) downto
(- n
20)
97 while
(setq ch
(nth i
*parse-window
*))
99 (cond ((or (eql ch
*parse-stream-eof
*)
100 (char= ch
#\newline
))
101 (return-from column some
))
106 (print-invert-case (stripdollar x
)))
108 (maybe-invert-string-case x
))
111 (case (and file $report_synerr_line
)
113 ;; print the file, line and column information
114 (let ((line+column
(line-number)))
115 (format t
"~&~a:~a:~a:" file
(car line
+column
) (cdr line
+column
))))
117 ;; if file=nil, then print a fresh line only; otherwise print
118 ;; file and character location
119 (format t
"~&~:[~;~:*~a:~a:~]" file fp
)))
120 (format t
(intl:gettext
"incorrect syntax: "))
121 (apply 'format t format-string
(mapcar #'printer l
))
122 (cond ((or $report_synerr_info
(eql *parse-stream
* *standard-input
*))
123 (let ((some (column)))
124 (format t
"~%~{~c~}~%~vt^" some
(max 0 (- (length some
) 2)))
125 (read-line *parse-stream
* nil nil
))))
128 (throw-macsyma-top))))
130 (defun tyi-parse-int (stream eof
)
132 (progn (setq *parse-window
* (make-list 25))
133 (setf (get '*parse-window
* 'length
) (length *parse-window
*))
134 (nconc *parse-window
* *parse-window
*)))
135 (let ((tem (tyi stream eof
)))
136 (setf (car *parse-window
*) tem
*parse-window
*
137 (cdr *parse-window
*))
138 (if (eql tem
#\newline
)
142 (defun *mread-prompt
* (out-stream char
)
143 (declare (ignore char
))
144 (format out-stream
"~&~A" *mread-prompt
*))
146 (defun aliaslookup (op)
148 (or (get op
'alias
) op
)
153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;;;;; The Input Scanner ;;;;;
157 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
159 (defun gobble-whitespace ()
160 (do ((ch (parse-tyipeek) (parse-tyipeek)))
161 ((not (member ch
*whitespace-chars
*)))
164 (defun read-command-token (obj)
166 (read-command-token-aux obj
))
168 (defun safe-assoc (item lis
)
169 "maclisp would not complain about (car 3), it gives nil"
172 (equal (car v
) item
))
176 ;; list contains an atom, only check
177 ;; (parser-assoc 1 '(2 1 3)) ==>(1 3)
178 ;; (parser-assoc 1 '(2 (1 4) 3)) ==>(1 4)
180 (defun parser-assoc (c lis
)
183 (cond ((consp (car v
))
189 ;; we need to be able to unparse-tyi an arbitrary number of
190 ;; characters, since if you do
191 ;; PREFIX("ABCDEFGH");
192 ;; then ABCDEFGA should read as a symbol.
193 ;; 99% of the time we don't have to unparse-tyi, and so there will
197 (let ((tem *parse-tyi
*))
199 (tyi-parse-int *parse-stream
* *parse-stream-eof
*))
201 (setq *parse-tyi
* nil
)
204 (setq *parse-tyi
* (cdr tem
))
207 ;; read one character but leave it there. so next parse-tyi gets it
208 (defun parse-tyipeek ()
209 (let ((tem *parse-tyi
*))
211 (setq *parse-tyi
* (tyi-parse-int *parse-stream
* *parse-stream-eof
*)))
215 ;; push characters back on the stream
216 (defun unparse-tyi (c)
217 (let ((tem *parse-tyi
*))
220 (setq *parse-tyi
* (cons c tem
)))))
222 ;;I know that the tradition says there should be no comments
223 ;;in tricky code in maxima. However the operator parsing
224 ;;gave me a bit of trouble. It was incorrect because
225 ;;it could not handle things produced by the extensions
226 ;;the following was broken for prefixes
228 (defun read-command-token-aux (obj)
231 (lis (if (eql ch
*parse-stream-eof
*)
233 (parser-assoc ch obj
))))
238 (cond ((atom (cadr lis
))
239 ;; INFIX("ABC"); puts into macsyma-operators
240 ;;something like: (#\A #\B #\C (ANS $abc))
241 ;; ordinary things are like:
242 ;; (#\< (ANS $<) (#\= (ANS $<=)))
243 ;; where if you fail at the #\< #\X
244 ;; stage, then the previous step was permitted.
245 (setq result
(read-command-token-aux (list (cdr lis
)))))
247 ;; lis something like (#\= (ANS $<=))
248 ;; and this says there are no longer operators
249 ;; starting with this.
251 (and (eql (car (cadr lis
)) 'ans
)
252 ;; When we have an operator, which starts with a
253 ;; literal, we check, if the operator is
254 ;; followed with a whitespace. With this code
255 ;; Maxima parses an expression grad x or grad(x)
256 ;; as (($grad) x) and gradef(x) as (($gradef) x),
257 ;; when grad is defined as a prefix operator.
258 ;; See bug report ID: 2970792.
259 (or (not (alphabetp (cadr (exploden (cadr (cadr lis
))))))
260 (member (parse-tyipeek) *whitespace-chars
*))
263 (let ((res (and (eql (car (cadr lis
)) 'ans
)
265 (com-token (read-command-token-aux (cddr lis
) )))
266 (setq result
(or com-token res
267 (read-command-token-aux (list (cadr lis
))))))))
268 (or result
(unparse-tyi ch
))
271 (defun scan-macsyma-token ()
272 ;; note that only $-ed tokens are GETALIASed.
273 (getalias (implode (cons '#\$
(scan-token t
)))))
275 (defun scan-lisp-token ()
276 (let ((charlist (scan-token nil
)))
279 (mread-synerr "Lisp symbol expected."))))
281 ;; Example: ?mismatch(x+y,x*z,?:from\-end,true); => 3
282 (defun scan-keyword-token ()
283 (let ((charlist (cdr (scan-token nil
))))
285 (let ((*package
* (find-package :keyword
)))
287 (mread-synerr "Lisp keyword expected."))))
289 (defun scan-token (flag)
290 (do ((c (parse-tyipeek) (parse-tyipeek))
292 ((or (eql c
*parse-stream-eof
*)
294 (not (or (digit-char-p c
(max 10 *read-base
*))
297 (nreverse (or l
(list (parse-tyi))))) ; Read at least one char ...
298 (when (char= (parse-tyi) #\\ )
299 (setq c
(parse-tyi)))
302 (defun scan-lisp-string () (scan-string))
303 (defun scan-macsyma-string () (scan-string))
305 (defun scan-string (&optional init
)
306 (let ((buf (make-array 50 :element-type
' #.
(array-element-type "a")
307 :fill-pointer
0 :adjustable t
)))
309 (vector-push-extend init buf
))
310 (do ((c (parse-tyipeek) (parse-tyipeek)))
311 ((cond ((eql c
*parse-stream-eof
*))
315 (if (char= (parse-tyi) #\\ )
316 (setq c
(parse-tyi)))
317 (vector-push-extend c buf
))))
319 (defun readlist (lis)
320 (read-from-string (coerce lis
'string
)))
322 ;; These variables control how we convert bfloat inputs to the
323 ;; internal bfloat representation. These variables should probably go
324 ;; away after some testing.
325 (defmvar $fast_bfloat_conversion t
326 "Use fast, but possibly inaccurate conversion")
327 (defmvar $fast_bfloat_threshold
100000.
328 "Exponents larger than this (in absolute value) will use the fast
329 conversion instead of the accurate conversion")
330 (defvar *fast-bfloat-extra-bits
* 0)
332 ;; Here is a test routine to test the fast bfloat conversion
334 (defun test-make-number (&optional
(n 1000))
337 (flet ((digit-list (n)
338 (coerce (format nil
"~D" n
) 'list
)))
340 ;; Generate a random number with 30 fraction digits and an
342 (push (digit-list (random 10)) numlist
)
343 (push '(#\.
) numlist
)
344 (push (digit-list (random (expt 10 30))) numlist
)
345 (push '(#\B
) numlist
)
346 (push (if (zerop (random 2)) '(#\
+) '(#\-
)) numlist
)
347 (push (digit-list (+ $fast_bfloat_threshold
348 (random $fast_bfloat_threshold
)))
350 ;; Convert using accurate and fast methods and compare the
352 (let ((true (let (($fast_bfloat_conversion nil
))
353 (make-number (copy-list numlist
))))
354 (fast (let (($fast_bfloat_conversion t
))
355 (make-number (copy-list numlist
)))))
356 (format t
"Test ~3A: " k
)
357 (map nil
#'(lambda (x)
362 (unless (equalp true fast
)
364 (format t
"NUM: ~A~% TRUE: ~S~% FAST: ~S~%"
365 (reverse numlist
) true fast
))))))
366 (format t
"~D failures in ~D tests (~F%)~%"
367 failures n
(* 100 failures
(/ (float n
))))))
370 ;; WARNING: MAKE-NUMBER destructively modifies it argument! Should we
372 (defun make-number (data)
373 (setq data
(nreverse data
))
374 ;; Maxima really wants to read in any number as a flonum
375 ;; (except when we have a bigfloat, of course!). So convert exponent
376 ;; markers to the flonum-exponent-marker.
377 (let ((marker (car (nth 3 data
))))
378 (unless (eql marker flonum-exponent-marker
)
379 (when (member marker
'(#\E
#\F
#\S
#\D
#\L
#+cmu
#\W
))
380 (setf (nth 3 data
) (list flonum-exponent-marker
)))))
381 (if (not (equal (nth 3 data
) '(#\B
)))
382 (readlist (apply #'append data
))
385 (int-part (readlist (or (first data
) '(#\
0))))
386 (frac-part (readlist (or (third data
) '(#\
0))))
387 (frac-len (length (third data
)))
388 (exp-sign (first (fifth data
)))
389 (exp (readlist (sixth data
))))
390 (if (and $fast_bfloat_conversion
391 (> (abs exp
) $fast_bfloat_threshold
))
392 ;; Exponent is large enough that we don't want to do exact
393 ;; rational arithmetic. Instead we do bfloat arithmetic.
394 ;; For example, 1.234b1000 is converted by computing
395 ;; bfloat(1234)*10b0^(1000-3). Extra precision is used
396 ;; during the bfloat computations.
397 (let* ((extra-prec (+ *fast-bfloat-extra-bits
* (ceiling (log exp
2e0
))))
398 (fpprec (+ fpprec extra-prec
))
399 (mant (+ (* int-part
(expt 10 frac-len
)) frac-part
))
400 (bf-mant (bcons (intofp mant
)))
401 (p (power (bcons (intofp 10))
402 (- (if (char= exp-sign
#\-
)
406 ;; Compute the product using extra precision. This
407 ;; helps to get the last bit correct (but not
408 ;; always). If we didn't do this, then bf-mant and
409 ;; p would be rounded to the target precision and
410 ;; then the product is rounded again. Doing it
411 ;; this way, we still have 3 roundings, but bf-mant
412 ;; and p aren't rounded too soon.
413 (result (mul bf-mant p
)))
414 (let ((fpprec (- fpprec extra-prec
)))
415 ;; Now round the product back to the desired precision.
417 ;; For bigfloats, turn them into rational numbers then
418 ;; convert to bigfloat. Fix for the 0.25b0 # 2.5b-1 bug.
419 ;; Richard J. Fateman posted this fix to the Maxima list
420 ;; on 10 October 2005. Without this fix, some tests in
421 ;; rtestrationalize will fail. Used with permission.
422 (let ((ratio (* (+ int-part
(* frac-part
(expt 10 (- frac-len
))))
423 (expt 10 (if (char= exp-sign
#\-
)
426 ($bfloat
(cl-rat-to-maxima ratio
)))))))
428 ;; Richard J. Fateman wrote the big float to rational code and the function
431 (defun cl-rat-to-maxima (x)
434 (list '(rat simp
) (numerator x
) (denominator x
))))
436 (defun scan-digits (data continuation? continuation
&optional exponent-p
)
437 (do ((c (parse-tyipeek) (parse-tyipeek))
439 ((not (and (characterp c
) (digit-char-p c
(max 10.
*read-base
*))))
440 (cond ((member c continuation?
)
441 (funcall continuation
(list* (ncons (char-upcase
445 ((and (null l
) exponent-p
)
446 ;; We're trying to parse the exponent part of a number,
447 ;; and we didn't get a value after the exponent marker.
449 (mread-synerr "parser: incomplete number; missing exponent?"))
451 (make-number (cons (nreverse l
) data
)))))
454 (defun scan-number-after-dot (data)
455 (scan-digits data
'(#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
) #'scan-number-exponent
))
457 (defun scan-number-exponent (data)
458 (push (ncons (if (or (char= (parse-tyipeek) #\
+)
459 (char= (parse-tyipeek) #\-
))
463 (scan-digits data
() () t
))
465 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
467 ;;;;; The Expression Parser ;;;;;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
471 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
473 ;;; Based on a theory of parsing presented in: ;;;
475 ;;; Pratt, Vaughan R., ``Top Down Operator Precedence,'' ;;;
476 ;;; ACM Symposium on Principles of Programming Languages ;;;
477 ;;; Boston, MA; October, 1973. ;;;
479 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
481 ;;; Implementation Notes ....
483 ;;; JPG Chars like ^A, ^B, ... get left around after interrupts and
484 ;;; should be thrown away by the scanner if not used as editting
487 ;;; KMP There is RBP stuff in DISPLA, too. Probably this sort of
488 ;;; data should all be in one place somewhere.
490 ;;; KMP Maybe the parser and/or scanner could use their own GC scheme
491 ;;; to recycle conses used in scan/parse from line to line which
492 ;;; really ought not be getting dynamically discarded and reconsed.
493 ;;; Alternatively, we could call RECLAIM explicitly on certain
494 ;;; pieces of structure which get used over and over. A
495 ;;; local-reclaim abstraction may want to be developed since this
496 ;;; stuff will always be needed, really. On small-address-space
497 ;;; machines, this could be overridden when the last DYNAMALLOC
498 ;;; GC barrier were passed (indicating that space was at a premium
499 ;;; -- in such case, real RECLAIM would be more economical -- or
500 ;;; would the code to control that be larger than the area locked
503 ;;; KMP GJC has a MAKE-EVALUATOR type package which could probably
504 ;;; replace the CALL-IF-POSSIBLE stuff used here.
505 ;;; [So it was written, so it was done. -gjc]
507 ;;; KMP DEFINE-SYMBOL and KILL-OPERATOR need to be redefined.
508 ;;; Probably these shouldn't be defined in this file anyway.
510 ;;; KMP The relationship of thisfile to SYNEX needs to be thought
511 ;;; out more carefully.
513 ;;; GJC Need macros for declaring INFIX, PREFIX, etc ops
515 ;;; GJC You know, PARSE-NARY isn't really needed it seems, since
516 ;;; the SIMPLIFIER makes the conversion of
517 ;;; ((MTIMES) ((MTIMES) A B) C) => ((MTIMES) A B C)
518 ;;; I bet you could get make "*" infix and nobody would
521 ;;; The following terms may be useful in deciphering this code:
523 ;;; NUD -- NUll left Denotation (op has nothing to its left (prefix))
524 ;;; LED -- LEft Denotation (op has something to left (postfix or infix))
526 ;;; LBP -- Left Binding Power (the stickiness to the left)
527 ;;; RBP -- Right Binding Power (the stickiness to the right)
532 (defvar scan-buffered-token
(list nil
)
533 "put-back buffer for scanner, a state-variable of the reader")
535 (defun peek-one-token ()
536 (peek-one-token-g nil nil
))
538 (defun peek-one-token-g (eof-ok? eof-obj
)
540 ((car scan-buffered-token
)
541 (cdr scan-buffered-token
))
542 (t (rplacd scan-buffered-token
(scan-one-token-g eof-ok? eof-obj
))
543 (cdr (rplaca scan-buffered-token t
)))))
545 (defun scan-one-token ()
546 (scan-one-token-g nil nil
))
548 (defun scan-one-token-g (eof-ok? eof-obj
)
549 (declare (special macsyma-operators
))
550 (cond ((car scan-buffered-token
)
551 (rplaca scan-buffered-token
())
552 (cdr scan-buffered-token
))
553 ((read-command-token macsyma-operators
))
555 (let ((test (parse-tyipeek)))
556 (cond ((eql test
*parse-stream-eof
*)
559 (mread-synerr (intl:gettext
"end of file while scanning expression."))))
562 (cond ((char= (parse-tyipeek) #\
*)
565 (scan-one-token-g eof-ok? eof-obj
))
567 ((eql test
#\.
) (parse-tyi) ; Read the dot
568 (if (digit-char-p (parse-tyipeek) 10.
)
569 (scan-number-after-dot (list (ncons #\.
) nil
))
573 (scan-macsyma-string))
576 (cond ((char= (parse-tyipeek) #\")
579 ((char= (parse-tyipeek) #\
:)
580 (scan-keyword-token))
584 (if (digit-char-p test
10.
)
585 (scan-number-before-dot ())
586 (scan-macsyma-token))))))))
588 ;; nested comments are permitted.
589 (defun gobble-comment ()
593 (setq c
(parse-tyipeek))
595 (cond ((= depth
0) (return t
)))
596 (cond ((eql c
*parse-stream-eof
*)
597 (mread-synerr (intl:gettext
"end of file in comment.")))
599 (cond ((char= (parse-tyipeek) #\
/)
602 (cond ((= depth
0) (return t
)))
605 (cond ((char= (parse-tyipeek) #\
*)
606 (incf depth
) (parse-tyi)
611 (defun scan-number-rest (data)
612 (let ((c (caar data
)))
615 (scan-number-after-dot data
))
616 ((member c
'(#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
))
617 ;; Dot missing but found exponent marker. Fake it.
618 (setf data
(push (ncons #\.
) (rest data
)))
619 (push (ncons #\
0) data
)
620 (push (ncons c
) data
)
621 (scan-number-exponent data
)))))
623 (defun scan-number-before-dot (data)
624 (scan-digits data
'(#\.
#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
)
628 ;; "First character" and "Pop character"
630 (defmacro first-c
() '(peek-one-token))
631 (defmacro pop-c
() '(scan-one-token))
633 (defun mstringp (x) (stringp x
)) ;; OBSOLETE. PRESERVE FOR SAKE OF POSSIBLE CALLS FROM NON-MAXIMA CODE !!
635 (defun inherit-propl (op-to op-from getl
)
636 (let ((propl (getl op-from getl
)))
638 (progn (remprop op-to
(car propl
))
639 (putprop op-to
(cadr propl
) (car propl
)))
641 (maxima-error "has no ~a properties. ~a ~a" getl op-from
'wrng-type-arg
)
646 ;;; (LED <op> <left>)
648 ;;; <op> is the name of the operator which was just popped.
649 ;;; <left> is the stuff to the left of the operator in the LED case.
653 #+gcl
(eval compile load
)
654 #-gcl
(:execute
:compile-toplevel
:load-toplevel
)
655 (defmacro def-nud-equiv
(op equiv
)
656 (list 'putprop
(list 'quote op
) (list 'function equiv
)
659 (defmacro nud-propl
() ''(nud))
661 (defmacro def-nud-fun
(op-name op-l . body
)
662 (list* 'defun-prop
(list* op-name
'nud
'nil
) op-l body
))
664 (defmacro def-led-equiv
(op equiv
)
665 (list 'putprop
(list 'quote op
) (list 'function equiv
)
668 (defmacro led-propl
() ''(led))
670 (defmacro def-led-fun
(op-name op-l . body
)
671 (list* 'defun-prop
(list* op-name
'led
'nil
) op-l body
)))
674 (let ((tem (and (symbolp op
) (getl op
'(nud)))) res
)
678 (mread-synerr "~A is not a prefix operator" (mopstrip op
))
680 (funcall (cadr tem
) op
)))
683 (defun led-call (op l
)
684 (let ((tem (and (symbolp op
) (getl op
'(led)))) res
)
687 (mread-synerr "~A is not an infix operator" (mopstrip op
))
688 (funcall (cadr tem
) op l
)))
691 ;;; (DEF-NUD (op lbp rbp) bvl . body)
693 ;;; Defines a procedure for parsing OP as a prefix operator.
695 ;;; OP should be the name of the symbol as a string or symbol.
696 ;;; LBP is an optional left binding power for the operator.
697 ;;; RBP is an optional right binding power for the operator.
698 ;;; BVL must contain exactly one variable, which the compiler will not
699 ;;; complain about if unused, since it will rarely be of use anyway.
700 ;;; It will get bound to the operator being parsed.
701 ;;; lispm:Optional args not allowed in release 5 allowed, necessary afterwards..
703 (defmacro def-nud
((op . lbp-rbp
) bvl . body
)
704 (let (( lbp
(nth 0 lbp-rbp
))
705 ( rbp
(nth 1 lbp-rbp
)))
706 `(progn ,(make-parser-fun-def op
'nud bvl body
)
707 (set-lbp-and-rbp ',op
',lbp
',rbp
))))
709 (defun set-lbp-and-rbp (op lbp rbp
)
710 (cond ((not (consp op
))
711 (let ((existing-lbp (get op
'lbp
))
712 (existing-rbp (get op
'rbp
)))
713 (cond ((not lbp
) ;; ignore omitted arg
716 (putprop op lbp
'lbp
))
717 ((not (equal existing-lbp lbp
))
718 (maxima-error "Incompatible LBP's defined for this operator ~a" op
)))
719 (cond ((not rbp
) ;; ignore omitted arg
722 (putprop op rbp
'rbp
))
723 ((not (equal existing-rbp rbp
))
724 (maxima-error "Incompatible RBP's defined for this operator ~a" op
)))))
726 (mapcar #'(lambda (x) (set-lbp-and-rbp x lbp rbp
))
729 ;;; (DEF-LED (op lbp rbp) bvl . body)
731 ;;; Defines a procedure for parsing OP as an infix or postfix operator.
733 ;;; OP should be the name of the symbol as a string or symbol.
734 ;;; LBP is an optional left binding power for the operator.
735 ;;; RBP is an optional right binding power for the operator.
736 ;;; BVL must contain exactly two variables, the first of which the compiler
737 ;;; will not complain about if unused, since it will rarely be of use
738 ;;; anyway. Arg1 will get bound to the operator being parsed. Arg2 will
739 ;;; get bound to the parsed structure which was to the left of Arg1.
742 (defmacro def-led
((op . lbp-rbp
) bvl . body
)
743 (let (( lbp
(nth 0 lbp-rbp
))
744 ( rbp
(nth 1 lbp-rbp
)))
745 `(progn ,(make-parser-fun-def op
'led bvl body
)
746 (set-lbp-and-rbp ',op
',lbp
',rbp
))))
748 (defmacro def-collisions
(op &rest alist
)
749 (let ((keys (do ((i 1 (ash i
1))
750 (lis alist
(cdr lis
))
751 (nl () (cons (cons (caar lis
) i
) nl
)))
754 (defprop ,op
,(let nil
755 (copy-tree keys
)) keys
)
756 ,@(mapcar #'(lambda (data)
757 `(defprop ,(car data
)
758 ,(do ((i 0 (logior i
(cdr (assoc (car lis
) keys
:test
#'eq
))))
759 (lis (cdr data
) (cdr lis
)))
765 (defun collision-lookup (op active-bitmask key-bitmask
)
766 (let ((result (logand active-bitmask key-bitmask
)))
767 (if (not (zerop result
))
768 (do ((l (get op
'keys
) (cdr l
)))
769 ((null l
) (parse-bug-err 'collision-check
))
770 (if (not (zerop (logand result
(cdar l
))))
771 (return (caar l
)))))))
773 (defun collision-check (op active-bitmask key
)
774 (let ((key-bitmask (get key op
)))
775 (if (not key-bitmask
)
776 (mread-synerr "~A is an unknown keyword in a ~A statement."
777 (mopstrip key
) (mopstrip op
)))
778 (let ((collision (collision-lookup op active-bitmask key-bitmask
)))
780 (if (eq collision key
)
781 (mread-synerr "This ~A's ~A slot is already filled."
784 (mread-synerr "A ~A cannot have a ~A with a ~A field."
787 (mopstrip collision
))))
788 (logior (cdr (assoc key
(get op
'keys
) :test
#'eq
)) active-bitmask
))))
790 ;;;; Data abstraction
792 ;;; LBP = Left Binding Power
794 ;;; (LBP <op>) - reads an operator's Left Binding Power
795 ;;; (DEF-LBP <op> <val>) - defines an operator's Left Binding Power
797 (defun lbp (lex) (cond ((safe-get lex
'lbp
)) (t 200.
)))
799 (defmacro def-lbp
(sym val
) `(defprop ,sym
,val lbp
))
801 ;;; RBP = Right Binding Power
803 ;;; (RBP <op>) - reads an operator's Right Binding Power
804 ;;; (DEF-RBP <op> <val>) - defines an operator's Right Binding Power
806 (defun rbp (lex) (cond ((safe-get lex
'rbp
)) (t 200.
)))
808 (defmacro def-rbp
(sym val
) `(defprop ,sym
,val rbp
))
810 (defmacro def-match
(x m
) `(defprop ,x
,m match
))
812 ;;; POS = Part of Speech!
819 (defun lpos (op) (cond ((safe-get op
'lpos
)) (t '$any
)))
820 (defun rpos (op) (cond ((safe-get op
'rpos
)) (t '$any
)))
821 (defun pos (op) (cond ((safe-get op
'pos
)) (t '$any
)))
823 (defmacro def-pos
(op pos
) `(defprop ,op
,pos pos
))
824 (defmacro def-rpos
(op pos
) `(defprop ,op
,pos rpos
))
825 (defmacro def-lpos
(op pos
) `(defprop ,op
,pos lpos
))
829 (defun mheader (op) (add-lineinfo (or (safe-get op
'mheader
) (ncons op
))))
831 (defmacro def-mheader
(op header
) `(defprop ,op
,header mheader
))
834 (defmvar $parsewindow
10.
835 "The maximum number of 'lexical tokens' that are printed out on
836 each side of the error-point when a syntax (parsing) MAXIMA-ERROR occurs. This
837 option is especially useful on slow terminals. Setting it to -1 causes the
838 entire input string to be printed out when an MAXIMA-ERROR occurs."
842 ;;;; Misplaced definitions
844 (defmacro def-operatorp
()
845 `(defun operatorp (lex)
846 (and (symbolp lex
) (getl lex
'(,@(nud-propl) ,@(led-propl))))))
850 (defmacro def-operatorp1
()
851 ;Defmfun -- used by SYNEX if not others.
852 `(defun operatorp1 (lex)
853 ;; Referenced outside of package: OP-SETUP, DECLARE1
854 ;; Use for truth value only, not for return-value.
855 (and (symbolp lex
) (getl lex
'(lbp rbp
,@(nud-propl) ,@(led-propl))))))
859 ;;;; The Macsyma Parser
861 ;;; (MREAD) with arguments compatible with losing maclisp READ style.
863 ;;; Returns a parsed form of tokens read from stream.
865 ;;; If you want rubout processing, be sure to call some stream which knows
866 ;;; about such things. Also, I'm figuring that the PROMPT will be
867 ;;; an attribute of the stream which somebody can hack before calling
868 ;;; MREAD if he wants to.
871 ;;Important for lispm rubout handler
872 (defun mread (&rest read-args
)
875 (and *parse-window
* (setf (car *parse-window
*) nil
876 *parse-window
* (cdr *parse-window
*)))
877 (princ *mread-prompt
*)
879 (apply 'mread-raw read-args
)))
881 (defun mread-prompter (stream char
)
882 (declare (special *mread-prompt-internal
*)
885 (princ *mread-prompt-internal
* stream
))
887 ;; input can look like:
890 (defun mread-raw (*parse-stream
* &optional
*mread-eof-obj
*)
891 (let ((scan-buffered-token (list nil
))
893 (if (eq scan-buffered-token
;; a handly unique object for the EQ test.
894 (peek-one-token-g t scan-buffered-token
))
897 (input (parse '$any
0.
) (parse '$any
0.
)))
901 ;force a separate line info structure
902 (setf *current-line-info
* nil
)
903 (return (list (mheader (pop-c))
904 (if labels
(cons (mheader '|$
[|
) (nreverse labels
)))
910 (mread-synerr "Invalid && tag. Tag must be a symbol")))
912 (parse-bug-err 'mread-raw
)))))))
914 ;;; (PARSE <mode> <rbp>)
916 ;;; This will parse an expression containing operators which have a higher
917 ;;; left binding power than <rbp>, returning as soon as an operator of
918 ;;; lesser or equal binding power is seen. The result will be in the given
919 ;;; mode (which allows some control over the class of result expected).
920 ;;; Modes used are as follows:
921 ;;; $ANY = Match any type of expression
922 ;;; $CLAUSE = Match only boolean expressions (or $ANY)
923 ;;; $EXPR = Match only mathematical expressions (or $ANY)
924 ;;; If a mismatched mode occurs, a syntax error will be flagged. Eg,
925 ;;; this is why "X^A*B" parses but "X^A and B" does not. X^A is a $EXPR
926 ;;; and not coercible to a $CLAUSE. See CONVERT.
928 ;;; <mode> is the required mode of the result.
929 ;;; <rbp> is the right binding power to use for the parse. When an
930 ;;; LED-type operator is seen with a lower left binding power
931 ;;; than <rbp>, this parse returns what it's seen so far rather
932 ;;; than calling that operator.
935 (defun parse (mode rbp
)
936 (do ((left (nud-call (pop-c)) ; Envoke the null left denotation
937 (led-call (pop-c) left
))) ; and keep calling LED ops as needed
938 ((>= rbp
(lbp (first-c))) ; Until next op lbp too low
939 (convert left mode
)))) ; in which case, return stuff seen
941 ;;; (PARSE-PREFIX <op>)
943 ;;; Parses prefix forms -- eg, -X or NOT FOO.
945 ;;; This should be the NUD property on an operator. It fires after <op>
946 ;;; has been seen. It parses forward looking for one more expression
947 ;;; according to its right binding power, returning
948 ;;; ( <mode> . ((<op>) <arg1>) )
950 (defun parse-prefix (op)
951 (list (pos op
) ; Operator mode
952 (mheader op
) ; Standard Macsyma expression header
953 (parse (rpos op
) (rbp op
)))) ; Convert single argument for use
955 ;;; (PARSE-POSTFIX <op> <left>)
957 ;;; Parses postfix forms. eg, X!.
959 ;;; This should be the LED property of an operator. It fires after <left>
960 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
961 ;;; ( <mode> . ((<op>) <arg1>) )
963 (defun parse-postfix (op l
)
964 (list (pos op
) ; Operator's mode
965 (mheader op
) ; Standard Macsyma expression header
966 (convert l
(lpos op
)))) ; Convert single argument for use
968 ;;; (PARSE-INFIX <op> <left>)
970 ;;; Parses infix (non-nary) forms. eg, 5 mod 3.
972 ;;; This should be the led property of an operator. It fires after <left>
973 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
974 ;;; ( <mode> . ((<op>) <arg1> <arg2>) )
976 (defun parse-infix (op l
)
977 (list (pos op
) ; Operator's mode
978 (mheader op
) ; Standard Macsyma expression header
979 (convert l
(lpos op
)) ; Convert arg1 for immediate use
980 (parse (rpos op
) (rbp op
)))) ; Look for an arg2
982 ;;; (PARSE-NARY <op> <left>)
984 ;;; Parses nary forms. Eg, form1*form2*... or form1+form2+...
985 ;;; This should be the LED property on an operator. It fires after <op>
986 ;;; has been seen, accumulating and returning
987 ;;; ( <mode> . ((<op>) <arg1> <arg2> ...) )
989 ;;; <op> is the being parsed.
990 ;;; <left> is the stuff that has been seen to the left of <op> which
991 ;;; rightly belongs to <op> on the basis of parse precedence rules.
993 (defun parse-nary (op l
)
994 (list* (pos op
) ; Operator's mode
995 (mheader op
) ; Normal Macsyma operator header
996 (convert l
(lpos op
)) ; Check type-match of arg1
997 (prsnary op
(lpos op
) (lbp op
)))) ; Search for other args
999 ;;; (PARSE-MATCHFIX <lop>)
1001 ;;; Parses matchfix forms. eg, [form1,form2,...] or (form1,form2,...)
1003 ;;; This should be the NUD property on an operator. It fires after <op>
1004 ;;; has been seen. It parses <lop><form1>,<form2>,...<rop> returning
1005 ;;; ( <mode> . ((<lop>) <form1> <form2> ...) ).
1007 (defun parse-matchfix (op)
1008 (list* (pos op
) ; Operator's mode
1009 (mheader op
) ; Normal Macsyma operator header
1010 (prsmatch (safe-get op
'match
) (lpos op
)))) ; Search for matchfixed forms
1012 ;;; (PARSE-NOFIX <op>)
1014 ;;; Parses an operator of no args. eg, @+X where @ designates a function
1015 ;;; call (eg, @() is implicitly stated by the lone symbol @.)
1017 ;;; This should be a NUD property on an operator which takes no args.
1018 ;;; It immediately returns ( <mode> . ((<op>)) ).
1020 ;;; <op> is the name of the operator.
1022 ;;; Note: This is not used by default and probably shouldn't be used by
1023 ;;; someone who doesn't know what he's doing. Example lossage. If @ is
1024 ;;; a nofix op, then @(3,4) parses, but parses as "@"()(3,4) would -- ie,
1025 ;;; to ((MQAPPLY) (($@)) 3 4) which is perhaps not what the user will expect.
1027 (defun parse-nofix (op) (list (pos op
) (mheader op
)))
1029 ;;; (PRSNARY <op> <mode> <rbp>)
1031 ;;; Parses an nary operator tail Eg, ...form2+form3+... or ...form2*form3*...
1033 ;;; Expects to be entered after the leading form and the first call to an
1034 ;;; nary operator has been seen and popped. Returns a list of parsed forms
1035 ;;; which belong to that operator. Eg, for X+Y+Z; this should be called
1036 ;;; after the first + is popped. Returns (Y Z) and leaves the ; token
1037 ;;; in the parser scan buffer.
1039 ;;; <op> is the nary operator in question.
1040 ;;; <rbp> is (LBP <op>) and is provided for efficiency. It is for use in
1041 ;;; recursive parses as a binding power to parse for.
1042 ;;; <mode> is the name of the mode that each form must be.
1044 (defun prsnary (op mode rbp
)
1045 (do ((nl (list (parse mode rbp
)) ; Get at least one form
1046 (cons (parse mode rbp
) nl
))) ; and keep getting forms
1047 ((not (eq op
(first-c))) ; until a parse pops on a new op
1048 (nreverse nl
)) ; at which time return forms
1049 (pop-c))) ; otherwise pop op
1051 ;;; (PRSMATCH <match> <mode>)
1053 ;;; Parses a matchfix sequence. Eg, [form1,form2,...] or (form1,form2,...)
1054 ;;; Expects to be entered after the leading token is the popped (ie, at the
1055 ;;; point where the parse of form1 will begin). Returns (form1 form2 ...).
1057 ;;; <match> is the token to look for as a matchfix character.
1058 ;;; <mode> is the name of the mode that each form must be.
1060 (defun prsmatch (match mode
) ; Parse for matchfix char
1061 (cond ((eq match
(first-c)) (pop-c) nil
) ; If immediate match, ()
1063 (do ((nl (list (parse mode
10.
)) ; Get first element
1064 (cons (parse mode
10.
) nl
))) ; and Keep adding elements
1065 ((eq match
(first-c)) ; Until we hit the match.
1066 (pop-c) ; Throw away match.
1067 (nreverse nl
)) ; Put result back in order
1068 (if (eq '|$
,|
(first-c)) ; If not end, look for ","
1069 (pop-c) ; and pop it if it's there
1070 (mread-synerr "Missing ~A" ; or give an error message.
1071 (mopstrip match
)))))))
1073 ;;; (CONVERT <exp> <mode>)
1075 ;;; Parser coercion function.
1077 ;;; <exp> should have the form ( <expressionmode> . <expression> )
1078 ;;; <mode> is the target mode.
1080 ;;; If <expressionmode> and <mode> are compatible, returns <expression>.
1082 (defun convert (item mode
)
1083 (if (or (eq mode
(car item
)) ; If modes match exactly
1084 (eq '$any mode
) ; or target is $ANY
1085 (eq '$any
(car item
))) ; or input is $ANY
1086 (cdr item
) ; then return expression
1087 (mread-synerr "Found ~A expression where ~A expression expected"
1088 (get (car item
) 'english
)
1089 (get mode
'english
))))
1091 (defprop $any
"untyped" english
)
1092 (defprop $clause
"logical" english
)
1093 (defprop $expr
"algebraic" english
)
1095 ;;;; Parser Error Diagnostics
1097 ;; Call this for random user-generated parse errors
1099 (defun parse-err () (mread-synerr "Syntax error"))
1101 ;; Call this for random internal parser lossage (eg, code that shouldn't
1104 (defun parse-bug-err (op)
1106 "Parser bug in ~A. Please report this to the Maxima maintainers,~
1107 ~%including the characters you just typed which caused the error. Thanks."
1110 ;;; Random shared error messages
1112 (defun delim-err (op)
1113 (mread-synerr "Illegal use of delimiter ~A" (mopstrip op
)))
1115 (defun erb-err (op l
) l
;Ignored
1116 (mread-synerr "Too many ~A's" (mopstrip op
)))
1118 (defun premterm-err (op)
1119 (mread-synerr "Premature termination of input at ~A."
1122 ;;;; Operator Specific Data
1124 (def-nud-equiv |$
]| delim-err
)
1125 (def-led-equiv |$
]| erb-err
)
1128 (def-nud-equiv |$
[| parse-matchfix
)
1129 (def-match |$
[| |$
]|
)
1132 (def-mheader |$
[|
(mlist))
1134 (def-lpos |$
[| $any
)
1137 (def-led (|$
[|
200.
) (op left
)
1138 (setq left
(convert left
'$any
))
1139 (if (numberp left
) (parse-err)) ; number[...] invalid
1140 (let ((header (if (atom left
)
1141 (add-lineinfo (list (amperchk left
) 'array
))
1142 (add-lineinfo '(mqapply array
))))
1143 (right (prsmatch '|$
]|
'$any
))) ; get sublist in RIGHT
1144 (cond ((null right
) ; 1 subscript minimum
1145 (mread-synerr "No subscripts given"))
1146 ((atom left
) ; atom[...]
1147 (setq right
(cons header
1149 (cons '$any
(aliaslookup right
)))
1151 (cons '$any
(cons header
1152 (cons left right
)))))))
1155 (def-nud-equiv |$
)| delim-err
)
1156 (def-led-equiv |$
)| erb-err
)
1159 (def-mheader |$
(|
(mprogn))
1161 ;; KMP: This function optimizes out (exp) into just exp.
1162 ;; This is useful for mathy expressions, but obnoxious for non-mathy
1163 ;; expressions. I think DISPLA should be made smart about such things,
1164 ;; but probably the (...) should be carried around in the internal
1165 ;; representation. This would make things like BUILDQ much easier to
1167 ;; GJC: CGOL has the same behavior, so users tend to write extensions
1168 ;; to the parser rather than write Macros per se. The transformation
1169 ;; "(EXP)" ==> "EXP" is done by the evaluator anyway, the problem
1170 ;; comes inside quoted expressions. There are many other problems with
1171 ;; the "QUOTE" concept however.
1173 (def-nud (|$
(|
200.
) (op)
1174 (let ((right)(hdr (mheader '|$
(|
))) ; make mheader first for lineinfo
1175 (cond ((eq '|$
)|
(first-c)) (parse-err)) ; () is illegal
1176 ((or (null (setq right
(prsmatch '|$
)|
'$any
))) ; No args to MPROGN??
1177 (cdr right
)) ; More than one arg.
1178 (when (suspicious-mprogn-p right
)
1179 (mtell (intl:gettext
"warning: parser: I'll let it stand, but (...) doesn't recognize local variables.~%"))
1180 (mtell (intl:gettext
"warning: parser: did you mean to say: block(~M, ...) ?~%") (car right
)))
1181 (cons '$any
(cons hdr right
))) ; Return an MPROGN
1182 (t (cons '$any
(car right
)))))) ; Optimize out MPROGN
1184 (defun suspicious-mprogn-p (right)
1185 ;; Look for a Maxima list of symbols or assignments to symbols.
1186 (and ($listp
(car right
))
1187 (every #'(lambda (e) (or (symbolp e
)
1188 (and (consp e
) (eq (caar e
) 'msetq
) (symbolp (second e
)))))
1189 (rest (car right
)))))
1191 (def-led (|$
(|
200.
) (op left
)
1192 (setq left
(convert left
'$any
)) ;De-reference LEFT
1193 (if (numberp left
) (parse-err)) ;number(...) illegal
1194 (let ((hdr (and (atom left
)(mheader (amperchk left
))))
1195 (r (prsmatch '|$
)|
'$any
)) ;Get arglist in R
1197 (cons '$any
;Result is type $ANY
1198 (cond ((atom left
) ;If atom(...) =>
1199 (cons hdr r
)) ;(($atom) exp . args)
1200 (t ;Else exp(...) =>
1201 (cons '(mqapply) (cons left r
))))))) ;((MQAPPLY) op . args)
1203 (def-mheader |$
'|
(mquote))
1205 (def-nud (|$
'|
) (op)
1207 (cond ((eq '|$
(|
(first-c))
1208 (list '$any
(mheader '|$
'|
) (parse '$any
190.
)))
1209 ((or (atom (setq right
(parse '$any
190.
)))
1210 (member (caar right
) '(mquote mlist $set mprog mprogn lambda
) :test
#'eq
))
1211 (list '$any
(mheader '|$
'|
) right
))
1212 ((eq 'mqapply
(caar right
))
1213 (cond ((eq (caaadr right
) 'lambda
)
1214 (list '$any
(mheader '|$
'|
) right
))
1215 (t (rplaca (cdr right
)
1216 (cons (cons ($nounify
(caaadr right
))
1219 (cons '$any right
))))
1220 (t (cons '$any
(cons (cons ($nounify
(caar right
)) (cdar right
))
1223 (def-nud (|$
''|
) (op)
1226 (cond ((eq '|$
(|
(first-c)) (meval (parse '$any
190.
)))
1227 ((atom (setq right
(parse '$any
190.
))) (meval1 right
))
1228 ((eq 'mqapply
(caar right
))
1230 (cons (cons ($verbify
(caaadr right
)) (cdaadr right
))
1233 (t (cons (cons ($verbify
(caar right
)) (cdar right
))
1236 (def-led-equiv |$
:| parse-infix
)
1240 (def-rpos |$
:| $any
)
1241 (def-lpos |$
:| $any
)
1242 (def-mheader |$
:|
(msetq))
1244 (def-led-equiv |$
::| parse-infix
)
1245 (def-lbp |$
::|
180.
)
1247 (def-pos |$
::| $any
)
1248 (def-rpos |$
::| $any
)
1249 (def-lpos |$
::| $any
)
1250 (def-mheader |$
::|
(mset))
1252 (def-led-equiv |$
:=| parse-infix
)
1253 (def-lbp |$
:=|
180.
)
1255 (def-pos |$
:=| $any
)
1256 (def-rpos |$
:=| $any
)
1257 (def-lpos |$
:=| $any
)
1258 (def-mheader |$
:=|
(mdefine))
1260 (def-led-equiv |$
::=| parse-infix
)
1261 (def-lbp |$
::=|
180.
)
1262 (def-rbp |$
::=|
20.
)
1263 (def-pos |$
::=| $any
)
1264 (def-rpos |$
::=| $any
)
1265 (def-lpos |$
::=| $any
)
1266 (def-mheader |$
::=|
(mdefmacro))
1268 (def-led-equiv |$
!| parse-postfix
)
1271 (def-pos |$
!| $expr
)
1272 (def-lpos |$
!| $expr
)
1274 (def-mheader |$
!|
(mfactorial))
1276 (def-mheader |$
!!|
($genfact
))
1278 (def-led (|$
!!|
160.
) (op left
)
1281 (convert left
'$expr
)
1282 (list (mheader '$
/) (convert left
'$expr
) 2)
1287 (def-pos |$^| $expr
)
1288 (def-lpos |$^| $expr
)
1289 (def-rpos |$^| $expr
)
1290 (def-mheader |$^|
(mexpt))
1292 (def-led ((|$^| |$^^|
)) (op left
)
1294 (aliaslookup (list (mheader op
)
1295 (convert left
(lpos op
))
1296 (parse (rpos op
) (rbp op
))))))
1298 (mapc #'(lambda (prop) ; Make $** like $^
1299 (let ((propval (get '$^ prop
)))
1300 (if propval
(putprop '$
** propval prop
))))
1301 '(lbp rbp pos rpos lpos mheader
))
1303 (inherit-propl '$
** '$^
(led-propl))
1305 (def-lbp |$^^|
140.
)
1306 (def-rbp |$^^|
139.
)
1307 (def-pos |$^^| $expr
)
1308 (def-lpos |$^^| $expr
)
1309 (def-rpos |$^^| $expr
)
1310 (def-mheader |$^^|
(mncexpt))
1312 ;; note y^^4.z gives an error because it scans the number 4 together with
1313 ;; the trailing '.' as a decimal place. I think the error is correct.
1314 (def-led-equiv |$.| parse-infix
)
1317 (def-pos |$.| $expr
)
1318 (def-lpos |$.| $expr
)
1319 (def-rpos |$.| $expr
)
1320 (def-mheader |$.|
(mnctimes))
1322 ;; Copy properties to noun operator.
1323 (setf (get '%mnctimes
'op
) (get 'mnctimes
'op
))
1325 (def-led-equiv |$
*| parse-nary
)
1328 (def-pos |$
*| $expr
)
1330 (def-lpos |$
*| $expr
)
1331 (def-mheader |$
*|
(mtimes))
1333 (def-led-equiv $
/ parse-infix
)
1339 (def-mheader $
/ (mquotient))
1341 (def-nud-equiv |$
+| parse-prefix
)
1343 (def-rbp |$
+|
134.
) ; Value increased from 100 to 134 (DK 02/2010).
1344 (def-pos |$
+| $expr
)
1345 (def-rpos |$
+| $expr
)
1347 (def-mheader |$
+|
(mplus))
1349 (def-led ((|$
+| |$-|
) 100.
) (op left
)
1350 (setq left
(convert left
'$expr
))
1351 (do ((nl (list (if (eq op
'$-
)
1352 (list (mheader '$-
) (parse '$expr
100.
))
1353 (parse '$expr
100.
))
1355 (cons (parse '$expr
100.
) nl
)))
1356 ((not (member (first-c) '($
+ $-
) :test
#'eq
))
1357 (list* '$expr
(mheader '$
+) (nreverse nl
)))
1358 (if (eq (first-c) '$
+) (pop-c))))
1360 (def-nud-equiv |$-| parse-prefix
)
1363 (def-pos |$-| $expr
)
1364 (def-rpos |$-| $expr
)
1366 (def-mheader |$-|
(mminus))
1368 (def-led-equiv |$
=| parse-infix
)
1371 (def-pos |$
=| $clause
)
1372 (def-rpos |$
=| $expr
)
1373 (def-lpos |$
=| $expr
)
1374 (def-mheader |$
=|
(mequal))
1376 (def-led-equiv |$
#| parse-infix
)
1379 (def-pos |$
#| $clause
)
1380 (def-rpos |$
#| $expr
)
1381 (def-lpos |$
#| $expr
)
1382 (def-mheader |$
#|
(mnotequal))
1384 (def-led-equiv |$
>| parse-infix
)
1387 (def-pos |$
>| $clause
)
1388 (def-rpos |$
>| $expr
)
1389 (def-lpos |$
>| $expr
)
1390 (def-mheader |$
>|
(mgreaterp))
1392 (def-led-equiv |$
>=| parse-infix
)
1395 (def-pos |$
>=| $clause
)
1396 (def-rpos |$
>=| $expr
)
1397 (def-lpos |$
>=| $expr
)
1398 (def-mheader |$
>=|
(mgeqp))
1400 (def-led-equiv |$
<| parse-infix
)
1403 (def-pos |$
<| $clause
)
1404 (def-rpos |$
<| $expr
)
1405 (def-lpos |$
<| $expr
)
1406 (def-mheader |$
<|
(mlessp))
1408 (def-led-equiv |$
<=| parse-infix
)
1411 (def-pos |$
<=| $clause
)
1412 (def-rpos |$
<=| $expr
)
1413 (def-lpos |$
<=| $expr
)
1414 (def-mheader |$
<=|
(mleqp))
1416 (def-nud-equiv $not parse-prefix
)
1419 (def-pos $not $clause
)
1420 (def-rpos $not $clause
)
1421 (def-lpos $not $clause
)
1422 (def-mheader $not
(mnot))
1424 (def-led-equiv $and parse-nary
)
1427 (def-pos $and $clause
)
1429 (def-lpos $and $clause
)
1430 (def-mheader $and
(mand))
1432 (def-led-equiv $or parse-nary
)
1435 (def-pos $or $clause
)
1437 (def-lpos $or $clause
)
1438 (def-mheader $or
(mor))
1440 (def-led-equiv |$
,| parse-nary
)
1445 (def-lpos |$
,| $any
)
1446 (def-mheader |$
,|
($ev
))
1448 (def-nud-equiv $then delim-err
)
1452 (def-nud-equiv $else delim-err
)
1456 (def-nud-equiv $elseif delim-err
)
1457 (def-lbp $elseif
5.
)
1458 (def-rbp $elseif
45.
)
1459 (def-pos $elseif $any
)
1460 (def-rpos $elseif $clause
)
1462 ;No LBP - Default as high as possible
1465 (def-rpos $if $clause
)
1467 (def-mheader $if
(mcond))
1472 (parse-condition op
)))
1474 (defun parse-condition (op)
1475 (list* (parse (rpos op
) (rbp op
))
1476 (if (eq (first-c) '$then
)
1477 (parse '$any
(rbp (pop-c)))
1478 (mread-synerr "Missing `then'"))
1480 (($else
) (list t
(parse '$any
(rbp (pop-c)))))
1481 (($elseif
) (parse-condition (pop-c)))
1482 (t (list t
'$false
)))))
1484 (def-mheader $do
(mdo))
1486 (defun parse-$do
(lex &aux
(left (make-mdo)))
1487 (setf (car left
) (mheader 'mdo
))
1488 (do ((op lex
(pop-c)) (active-bitmask 0))
1490 (if (eq op
'|$
:|
) (setq op
'$from
))
1491 (setq active-bitmask
(collision-check '$do active-bitmask op
))
1492 (let ((data (parse (rpos op
) (rbp op
))))
1494 ($do
(setf (mdo-body left
) data
) (return (cons '$any left
)))
1495 ($for
(setf (mdo-for left
) data
))
1496 ($from
(setf (mdo-from left
) data
))
1497 ($in
(setf (mdo-op left
) 'mdoin
)
1498 (setf (mdo-from left
) data
))
1499 ($step
(setf (mdo-step left
) data
))
1500 ($next
(setf (mdo-next left
) data
))
1501 ($thru
(setf (mdo-thru left
) data
))
1504 (setq data
(list (mheader '$not
) data
)))
1505 (setf (mdo-unless left
)
1506 (if (null (mdo-unless left
))
1508 (list (mheader '$or
) data
(mdo-unless left
)))))
1509 (t (parse-bug-err '$do
))))))
1516 (def-lbp $unless
25.
)
1517 (def-lbp $while
25.
)
1520 (def-nud-equiv $for parse-$do
)
1521 (def-nud-equiv $from parse-$do
)
1522 (def-nud-equiv $step parse-$do
)
1523 (def-nud-equiv $next parse-$do
)
1524 (def-nud-equiv $thru parse-$do
)
1525 (def-nud-equiv $unless parse-$do
)
1526 (def-nud-equiv $while parse-$do
)
1527 (def-nud-equiv $do parse-$do
)
1536 (def-rbp $unless
45.
)
1537 (def-rbp $while
45.
)
1540 (def-rpos $for $any
)
1541 (def-rpos $from $any
)
1542 (def-rpos $step $expr
)
1543 (def-rpos $next $any
)
1544 (def-rpos $thru $expr
)
1545 (def-rpos $unless $clause
)
1546 (def-rpos $while $clause
)
1552 ($from .
($in $from
))
1553 ($in .
($in $from $step $next
))
1554 ($step .
($in $step $next
))
1555 ($next .
($in $step $next
))
1556 ($thru .
($in $thru
)) ;$IN didn't used to get checked for
1560 (def-mheader |$$|
(nodisplayinput))
1561 (def-nud-equiv |$$| premterm-err
)
1563 ;No RBP, POS, RPOS, RBP, or MHEADER
1565 (def-mheader |$
;| (displayinput))
1566 (def-nud-equiv |$
;| premterm-err)
1568 ;No RBP, POS, RPOS, RBP, or MHEADER
1570 (def-nud-equiv |$
&&| delim-err
)
1574 ;; kludge interface function to allow the use of lisp PRINC in places.
1575 (cond ((null x
) 'false
)
1576 ((or (eq x t
) (eq x
't
)) 'true
)
1579 (or (get x
'reversealias
)
1580 (let ((name (symbol-name x
)))
1581 (if (member (char name
0) '(#\$
#\%
) :test
#'char
=)
1586 (define-initial-symbols
1587 ;; * Note: /. is looked for explicitly rather than
1588 ;; existing in this chart. The reason is that
1589 ;; it serves a dual role (as a decimal point) and
1590 ;; must be special-cased.
1592 ;; Same for // because of the /* ... */ handling
1595 |
+| |-| |
*| |^| |
<| |
=| |
>| |
(| |
)| |
[| |
]| |
,|
1596 |
:| |
!| |
#| |
'| |;| |$| |
&|
1598 |
**| |^^| |
:=| |
::| |
!!| |
<=| |
>=| |
''| |
&&|
1603 ;; !! FOLLOWING MOVED HERE FROM MLISP.LISP (DEFSTRUCT STUFF)
1604 ;; !! SEE NOTE THERE
1607 ;;; User extensibility:
1608 (defmfun $prefix
(operator &optional
(rbp 180.
)
1611 (def-operator operator pos
() () rbp rpos
() t
1612 '(nud . parse-prefix
) 'msize-prefix
'dimension-prefix
() )
1615 (defmfun $postfix
(operator &optional
(lbp 180.
)
1618 (def-operator operator pos lbp lpos
() () t
()
1619 '(led . parse-postfix
) 'msize-postfix
'dimension-postfix
() )
1622 (defmfun $infix
(operator &optional
(lbp 180.
)
1627 (def-operator operator pos lbp lpos rbp rpos t t
1628 '(led . parse-infix
) 'msize-infix
'dimension-infix
() )
1631 (defmfun $nary
(operator &optional
(bp 180.
)
1634 (def-operator operator pos bp argpos bp
() t t
1635 '(led . parse-nary
) 'msize-nary
'dimension-nary
() )
1638 (defmfun $matchfix
(operator
1639 match
&optional
(argpos '$any
)
1641 ;shouldn't MATCH be optional?
1642 (def-operator operator pos
() argpos
() () () ()
1643 '(nud . parse-matchfix
) 'msize-matchfix
'dimension-match match
)
1646 (defmfun $nofix
(operator &optional
(pos '$any
))
1647 (def-operator operator pos
() () () () () ()
1648 '(nud . parse-nofix
) 'msize-nofix
'dimension-nofix
() )
1651 ;;; (DEF-OPERATOR op pos lbp lpos rbp rpos sp1 sp2
1652 ;;; parse-data grind-fn dim-fn match)
1653 ;;; OP is the operator name.
1654 ;;; POS is its ``part of speech.''
1655 ;;; LBP is its ``left binding power.''
1656 ;;; LPOS is the part of speech of the arguments to its left, or of all.
1657 ;;; arguments for NARY and MATCHFIX.
1658 ;;; RBP is its ``right binding power.''
1659 ;;; RPOS is the part of speech of the argument to its right.
1660 ;;; SP1 says if the DISSYM property needs a space on the right.
1661 ;;; SP2 says if the DISSYM property needs a space on the left.
1662 ;;; PARSE-DATA is (prop . fn) -- parser prop name dotted with function name
1663 ;;; GRIND-FN is the grinder function for the operator.
1664 ;;; DIM-FN is the dimension function for the operator.
1665 ;;; PARSEPROP is the property name to use for parsing. One of LED or NUD.
1666 ;;; MATCH if non-(), ignores SP1 and SP2. Should be the match symbol.
1667 ;;; sets OP up as matchfix with MATCH.
1669 ;;; For more complete descriptions of these naming conventions, see
1670 ;;; the comments in GRAM package, which describe them in reasonable detail.
1672 (defun def-operator (op pos lbp lpos rbp rpos sp1 sp2
1673 parse-data grind-fn dim-fn match
)
1675 (if (or (and rbp
(not (integerp (setq x rbp
))))
1676 (and lbp
(not (integerp (setq x lbp
)))))
1677 (merror (intl:gettext
"syntax extension: binding powers must be integers; found: ~M") x
))
1678 (if (stringp op
) (setq op
(define-symbol op
)))
1680 (let ((noun ($nounify op
))
1681 (dissym (cdr (exploden op
))))
1684 (setq dissym
(append (if sp1
'(#\space
)) dissym
(if sp2
'(#\space
)))))
1685 (t (if (stringp match
) (setq match
(define-symbol match
)))
1687 (putprop op match
'match
)
1688 (putprop match
5.
'lbp
)
1689 (setq dissym
(cons dissym
(cdr (exploden match
))))))
1690 (putprop op pos
'pos
)
1691 (putprop op
(cdr parse-data
) (car parse-data
))
1692 (putprop op grind-fn
'grind
)
1693 (putprop op dim-fn
'dimension
)
1694 (putprop noun dim-fn
'dimension
)
1695 (putprop op dissym
'dissym
)
1696 (putprop noun dissym
'dissym
)
1698 (putprop op rbp
'rbp
)
1699 (putprop noun rbp
'rbp
))
1701 (putprop op lbp
'lbp
)
1702 (putprop noun lbp
'lbp
))
1703 (when lpos
(putprop op lpos
'lpos
))
1704 (when rpos
(putprop op rpos
'rpos
))
1707 (defun op-setup (op)
1708 (declare (special *mopl
*))
1709 (let ((dummy (or (get op
'op
)
1710 (coerce (string* op
) 'string
))))
1711 (putprop op dummy
'op
)
1713 (if (and (operatorp1 op
) (not (member dummy
(cdr $props
) :test
#'eq
)))
1714 (push dummy
*mopl
*))
1715 (add2lnc dummy $props
)))
1717 (defun kill-operator (op)
1720 (noun-form ($nounify op
)))
1721 ;; Refuse to kill an operator which appears on *BUILTIN-$PROPS*.
1722 (unless (member opr
*builtin-$props
* :test
#'equal
)
1723 (undefine-symbol opr
)
1726 (mapc #'(lambda (x) (remprop op x
))
1727 '(nud nud-expr nud-subr
; NUD info
1728 led led-expr led-subr
; LED info
1729 lbp rbp
; Binding power info
1730 lpos rpos pos
; Part-Of-Speech info
1731 grind dimension dissym
; Display info
1732 op
)) ; Operator info
1733 (mapc #'(lambda (x) (remprop noun-form x
))
1734 '(dimension dissym lbp rbp
)))))
1738 ;; the functions get-instream etc.. are all defined in
1739 ;; gcl lsp/debug.lsp
1740 ;; they are all generic common lisp and could be used by
1741 ;; any Common lisp implementation.
1746 (line 0 :type fixnum
)
1750 (defvar *stream-alist
* nil
)
1753 (defun stream-name (path)
1755 (car (errset (namestring (pathname path
))))))
1758 (defun instream-name (instr)
1759 (or (instream-stream-name instr
)
1760 (stream-name (instream-stream instr
))))
1762 ;; (closedp stream) checks if a stream is closed.
1763 ;; how to do this in common lisp!!
1767 #+never-clean-up-dont-know-how-to-close
1768 (dolist (v *stream-alist
*)
1769 (if (closedp (instream-stream v
))
1770 (setq *stream-alist
* (delete v
*stream-alist
*)))))
1773 (defun get-instream (str)
1774 (or (dolist (v *stream-alist
*)
1775 (cond ((eq str
(instream-stream v
))
1778 (errset (setq name
(namestring str
)))
1779 (car (setq *stream-alist
*
1780 (cons (make-instream :stream str
:stream-name name
)
1781 *stream-alist
*))))))
1783 (defun newline (str)
1784 (incf (instream-line (get-instream str
)))
1787 (defun find-stream (stream)
1788 (dolist (v *stream-alist
*)
1789 (cond ((eq stream
(instream-stream v
))
1793 (defun add-lineinfo (lis)
1795 (eq *parse-stream
* *parse-string-input-stream
*) ;; avoid consing *parse-string-input-stream*
1796 ;; via get-instream to *stream-alist*
1797 (and (eq *parse-window
* *standard-input
*)
1798 (not (find-stream *parse-stream
*)) ))
1800 (let* ((st (get-instream *parse-stream
*))
1801 (n (instream-line st
))
1802 (nam (instream-name st
)))
1803 (or nam
(return-from add-lineinfo lis
))
1804 (setq *current-line-info
*
1805 (cond ((eq (cadr *current-line-info
*) nam
)
1806 (cond ((eql (car *current-line-info
*) n
)
1807 *current-line-info
*)
1808 (t (cons n
(cdr *current-line-info
*)))))
1809 (t (list n nam
'src
))))
1810 (cond ((null (cdr lis
))
1811 (list (car lis
) *current-line-info
*))
1812 (t (append lis
(list *current-line-info
*)))))))
1814 ;; Remove debugging stuff.
1815 ;; STRIP-LINEINFO does not modify EXPR.
1817 (defun strip-lineinfo (expr)
1818 (if (or (atom expr
) (specrepp expr
))
1820 (cons (strip-lineinfo-op (car expr
)) (mapcar #'strip-lineinfo
(cdr expr
)))))
1822 ;; If something in the operator looks like debugging stuff, remove it.
1823 ;; It is assumed here that debugging stuff is a list comprising an integer and a string
1824 ;; (and maybe other stuff, which is ignored).
1826 (defun strip-lineinfo-op (maxima-op)
1827 (remove-if #'(lambda (x) (and (consp x
) (integerp (first x
)) (stringp (second x
)))) maxima-op
))