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 (defmvar *alphabet
* (list #\_
#\%
))
18 (defmvar *whitespace-chars
*
19 '(#\tab
#\space
#\linefeed
#\return
#\page
#\newline
20 #+(or (and unicode
(not lispworks
))
21 sb-unicode openmcl-unicode-strings
) #\no-break_space
))
25 (or (alpha-char-p n
) #+gcl
(>= (char-code n
) 128)
26 (member n
*alphabet
*))))
28 (defun ascii-numberp (num)
29 (and (characterp num
) (char<= num
#\
9) (char>= num
#\
0)))
31 (defvar *parse-window
* nil
)
32 (defvar *parse-stream
* () "input stream for Maxima parser")
33 (defvar *parse-stream-eof
* -
1 "EOF value for Maxima parser")
34 (defvar *parse-tyi
* nil
)
36 (defvar *mread-prompt
* nil
"prompt used by `mread'")
37 (defvar *mread-eof-obj
* () "Bound by `mread' for use by `mread-raw'")
38 (defvar *current-line-info
* nil
)
40 (defvar *parse-string-input-stream
* ;; reference to the input stream
41 (let ((stream (make-string-input-stream ""))) ;; used by parse-string
42 (close stream
) ;; in share/stringroc/eval_string.lisp
43 stream
)) ;; (see also add-lineinfo below)
45 (defmvar $report_synerr_line t
"If T, report line number where syntax error occurs; otherwise, report FILE-POSITION of error.")
46 (defmvar $report_synerr_info t
"If T, report the syntax error details from all sources; otherwise, only report details from standard-input.")
48 (defun mread-synerr (format-string &rest l
)
49 (let ((fp (and (not (eq *parse-stream
* *standard-input
*))
50 (file-position *parse-stream
*)))
51 (file (and (not (eq *parse-stream
* *standard-input
*))
52 (cadr *current-line-info
*))))
53 (flet ((line-number ()
54 ;; Fix me: Neither batch nor load track the line number
55 ;; correctly. batch, via dbm-read, does not track the
56 ;; line number at all (a bug?).
58 ;; Find the line number by jumping to the start of file
59 ;; and reading line-by-line til we reach the current
61 (cond ((and fp
(file-position *parse-stream
* 0))
62 (do ((l (read-line *parse-stream
* nil nil
) (read-line *parse-stream
* nil nil
))
64 (p (file-position *parse-stream
*) (file-position *parse-stream
*))
66 ((or (null p
) (>= p fp
))
70 (let ((n (get '*parse-window
* 'length
))
72 (loop for i from
(1- n
) downto
(- n
20)
73 while
(setq ch
(nth i
*parse-window
*))
75 (cond ((char= ch
#\newline
)
76 (return-from column some
))
81 (print-invert-case (stripdollar x
)))
83 (maybe-invert-string-case x
))
86 (case (and file $report_synerr_line
)
88 ;; print the file, line and column information
89 (let ((line+column
(line-number)))
90 (format t
"~&~a:~a:~a:" file
(car line
+column
) (cdr line
+column
))))
92 ;; if file=nil, then print a fresh line only; otherwise print
93 ;; file and character location
94 (format t
"~&~:[~;~:*~a:~a:~]" file fp
)))
95 (format t
(intl:gettext
"incorrect syntax: "))
96 (apply 'format t format-string
(mapcar #'printer l
))
97 (cond ((or $report_synerr_info
(eql *parse-stream
* *standard-input
*))
98 (let ((some (column)))
99 (format t
"~%~{~c~}~%~vt^" some
(- (length some
) 2))
100 (read-line *parse-stream
* nil nil
))))
103 (throw-macsyma-top))))
105 (defun tyi-parse-int (stream eof
)
107 (progn (setq *parse-window
* (make-list 25))
108 (setf (get '*parse-window
* 'length
) (length *parse-window
*))
109 (nconc *parse-window
* *parse-window
*)))
110 (let ((tem (tyi stream eof
)))
111 (setf (car *parse-window
*) tem
*parse-window
*
112 (cdr *parse-window
*))
113 (if (eql tem
#\newline
)
117 (defun *mread-prompt
* (out-stream char
)
118 (declare (ignore char
))
119 (format out-stream
"~&~A" *mread-prompt
*))
121 (defun aliaslookup (op)
123 (or (get op
'alias
) op
)
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;;;;; The Input Scanner ;;;;;
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 (defun gobble-whitespace ()
135 (do ((ch (parse-tyipeek) (parse-tyipeek)))
136 ((not (member ch
*whitespace-chars
*)))
139 (defun read-command-token (obj)
141 (read-command-token-aux obj
))
143 (defun safe-assoc (item lis
)
144 "maclisp would not complain about (car 3), it gives nil"
147 (equal (car v
) item
))
151 ;; list contains an atom, only check
152 ;; (parser-assoc 1 '(2 1 3)) ==>(1 3)
153 ;; (parser-assoc 1 '(2 (1 4) 3)) ==>(1 4)
155 (defun parser-assoc (c lis
)
158 (cond ((consp (car v
))
164 ;; we need to be able to unparse-tyi an arbitrary number of
165 ;; characters, since if you do
166 ;; PREFIX("ABCDEFGH");
167 ;; then ABCDEFGA should read as a symbol.
168 ;; 99% of the time we don't have to unparse-tyi, and so there will
172 (let ((tem *parse-tyi
*))
174 (tyi-parse-int *parse-stream
* *parse-stream-eof
*))
176 (setq *parse-tyi
* nil
)
179 (setq *parse-tyi
* (cdr tem
))
182 ;; read one character but leave it there. so next parse-tyi gets it
183 (defun parse-tyipeek ()
184 (let ((tem *parse-tyi
*))
186 (setq *parse-tyi
* (tyi-parse-int *parse-stream
* *parse-stream-eof
*)))
190 ;; push characters back on the stream
191 (defun unparse-tyi (c)
192 (let ((tem *parse-tyi
*))
195 (setq *parse-tyi
* (cons c tem
)))))
197 ;;I know that the tradition says there should be no comments
198 ;;in tricky code in maxima. However the operator parsing
199 ;;gave me a bit of trouble. It was incorrect because
200 ;;it could not handle things produced by the extensions
201 ;;the following was broken for prefixes
203 (defun read-command-token-aux (obj)
206 (lis (if (eql ch
*parse-stream-eof
*)
208 (parser-assoc ch obj
))))
213 (cond ((atom (cadr lis
))
214 ;; INFIX("ABC"); puts into macsyma-operators
215 ;;something like: (#\A #\B #\C (ANS $abc))
216 ;; ordinary things are like:
217 ;; (#\< (ANS $<) (#\= (ANS $<=)))
218 ;; where if you fail at the #\< #\X
219 ;; stage, then the previous step was permitted.
220 (setq result
(read-command-token-aux (list (cdr lis
)))))
222 ;; lis something like (#\= (ANS $<=))
223 ;; and this says there are no longer operators
224 ;; starting with this.
226 (and (eql (car (cadr lis
)) 'ans
)
227 ;; When we have an operator, which starts with a
228 ;; literal, we check, if the operator is
229 ;; followed with a whitespace. With this code
230 ;; Maxima parses an expression grad x or grad(x)
231 ;; as (($grad) x) and gradef(x) as (($gradef) x),
232 ;; when grad is defined as a prefix operator.
233 ;; See bug report ID: 2970792.
234 (or (not (alphabetp (cadr (exploden (cadr (cadr lis
))))))
235 (member (parse-tyipeek) *whitespace-chars
*))
238 (let ((res (and (eql (car (cadr lis
)) 'ans
)
240 (com-token (read-command-token-aux (cddr lis
) )))
241 (setq result
(or com-token res
242 (read-command-token-aux (list (cadr lis
))))))))
243 (or result
(unparse-tyi ch
))
246 (defun scan-macsyma-token ()
247 ;; note that only $-ed tokens are GETALIASed.
248 (getalias (implode (cons '#\$
(scan-token t
)))))
250 (defun scan-lisp-token ()
251 (let ((charlist (scan-token nil
)))
254 (mread-synerr "Lisp symbol expected."))))
256 ;; Example: ?mismatch(x+y,x*z,?:from\-end,true); => 3
257 (defun scan-keyword-token ()
258 (let ((charlist (cdr (scan-token nil
))))
260 (let ((*package
* (find-package :keyword
)))
262 (mread-synerr "Lisp keyword expected."))))
264 (defun scan-token (flag)
265 (do ((c (parse-tyipeek) (parse-tyipeek))
267 ((or (eql c
*parse-stream-eof
*)
269 (not (or (digit-char-p c
(max 10 *read-base
*))
272 (nreverse (or l
(list (parse-tyi))))) ; Read at least one char ...
273 (when (char= (parse-tyi) #\\ )
274 (setq c
(parse-tyi)))
277 (defun scan-lisp-string () (scan-string))
278 (defun scan-macsyma-string () (scan-string))
280 (defun scan-string (&optional init
)
281 (let ((buf (make-array 50 :element-type
' #.
(array-element-type "a")
282 :fill-pointer
0 :adjustable t
)))
284 (vector-push-extend init buf
))
285 (do ((c (parse-tyipeek) (parse-tyipeek)))
286 ((cond ((eql c
*parse-stream-eof
*))
290 (if (char= (parse-tyi) #\\ )
291 (setq c
(parse-tyi)))
292 (vector-push-extend c buf
))))
294 (defun readlist (lis)
295 (read-from-string (coerce lis
'string
)))
297 ;; These variables control how we convert bfloat inputs to the
298 ;; internal bfloat representation. These variables should probably go
299 ;; away after some testing.
300 (defmvar $fast_bfloat_conversion t
301 "Use fast, but possibly inaccurate conversion")
302 (defmvar $fast_bfloat_threshold
100000.
303 "Exponents larger than this (in absolute value) will use the fast
304 conversion instead of the accurate conversion")
305 (defvar *fast-bfloat-extra-bits
* 0)
307 ;; Here is a test routine to test the fast bfloat conversion
309 (defun test-make-number (&optional
(n 1000))
312 (flet ((digit-list (n)
313 (coerce (format nil
"~D" n
) 'list
)))
315 ;; Generate a random number with 30 fraction digits and an
317 (push (digit-list (random 10)) numlist
)
318 (push '(#\.
) numlist
)
319 (push (digit-list (random (expt 10 30))) numlist
)
320 (push '(#\B
) numlist
)
321 (push (if (zerop (random 2)) '(#\
+) '(#\-
)) numlist
)
322 (push (digit-list (+ $fast_bfloat_threshold
323 (random $fast_bfloat_threshold
)))
325 ;; Convert using accurate and fast methods and compare the
327 (let ((true (let (($fast_bfloat_conversion nil
))
328 (make-number (copy-list numlist
))))
329 (fast (let (($fast_bfloat_conversion t
))
330 (make-number (copy-list numlist
)))))
331 (format t
"Test ~3A: " k
)
332 (map nil
#'(lambda (x)
337 (unless (equalp true fast
)
339 (format t
"NUM: ~A~% TRUE: ~S~% FAST: ~S~%"
340 (reverse numlist
) true fast
))))))
341 (format t
"~D failures in ~D tests (~F%)~%"
342 failures n
(* 100 failures
(/ (float n
))))))
345 ;; WARNING: MAKE-NUMBER destructively modifies it argument! Should we
347 (defun make-number (data)
348 (setq data
(nreverse data
))
349 ;; Maxima really wants to read in any number as a flonum
350 ;; (except when we have a bigfloat, of course!). So convert exponent
351 ;; markers to the flonum-exponent-marker.
352 (let ((marker (car (nth 3 data
))))
353 (unless (eql marker flonum-exponent-marker
)
354 (when (member marker
'(#\E
#\F
#\S
#\D
#\L
#+cmu
#\W
))
355 (setf (nth 3 data
) (list flonum-exponent-marker
)))))
356 (if (not (equal (nth 3 data
) '(#\B
)))
357 (readlist (apply #'append data
))
360 (int-part (readlist (or (first data
) '(#\
0))))
361 (frac-part (readlist (or (third data
) '(#\
0))))
362 (frac-len (length (third data
)))
363 (exp-sign (first (fifth data
)))
364 (exp (readlist (sixth data
))))
365 (if (and $fast_bfloat_conversion
366 (> (abs exp
) $fast_bfloat_threshold
))
367 ;; Exponent is large enough that we don't want to do exact
368 ;; rational arithmetic. Instead we do bfloat arithmetic.
369 ;; For example, 1.234b1000 is converted by computing
370 ;; bfloat(1234)*10b0^(1000-3). Extra precision is used
371 ;; during the bfloat computations.
372 (let* ((extra-prec (+ *fast-bfloat-extra-bits
* (ceiling (log exp
2e0
))))
373 (fpprec (+ fpprec extra-prec
))
374 (mant (+ (* int-part
(expt 10 frac-len
)) frac-part
))
375 (bf-mant (bcons (intofp mant
)))
376 (p (power (bcons (intofp 10))
377 (- (if (char= exp-sign
#\-
)
381 ;; Compute the product using extra precision. This
382 ;; helps to get the last bit correct (but not
383 ;; always). If we didn't do this, then bf-mant and
384 ;; p would be rounded to the target precision and
385 ;; then the product is rounded again. Doing it
386 ;; this way, we still have 3 roundings, but bf-mant
387 ;; and p aren't rounded too soon.
388 (result (mul bf-mant p
)))
389 (let ((fpprec (- fpprec extra-prec
)))
390 ;; Now round the product back to the desired precision.
392 ;; For bigfloats, turn them into rational numbers then
393 ;; convert to bigfloat. Fix for the 0.25b0 # 2.5b-1 bug.
394 ;; Richard J. Fateman posted this fix to the Maxima list
395 ;; on 10 October 2005. Without this fix, some tests in
396 ;; rtestrationalize will fail. Used with permission.
397 (let ((ratio (* (+ int-part
(* frac-part
(expt 10 (- frac-len
))))
398 (expt 10 (if (char= exp-sign
#\-
)
401 ($bfloat
(cl-rat-to-maxima ratio
)))))))
403 ;; Richard J. Fateman wrote the big float to rational code and the function
406 (defun cl-rat-to-maxima (x)
409 (list '(rat simp
) (numerator x
) (denominator x
))))
411 (defun scan-digits (data continuation? continuation
&optional exponent-p
)
412 (do ((c (parse-tyipeek) (parse-tyipeek))
414 ((not (and (characterp c
) (digit-char-p c
(max 10.
*read-base
*))))
415 (cond ((member c continuation?
)
416 (funcall continuation
(list* (ncons (char-upcase
420 ((and (null l
) exponent-p
)
421 ;; We're trying to parse the exponent part of a number,
422 ;; and we didn't get a value after the exponent marker.
424 (mread-synerr "parser: incomplete number; missing exponent?"))
426 (make-number (cons (nreverse l
) data
)))))
429 (defun scan-number-after-dot (data)
430 (scan-digits data
'(#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
) #'scan-number-exponent
))
432 (defun scan-number-exponent (data)
433 (push (ncons (if (or (char= (parse-tyipeek) #\
+)
434 (char= (parse-tyipeek) #\-
))
438 (scan-digits data
() () t
))
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442 ;;;;; The Expression Parser ;;;;;
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
448 ;;; Based on a theory of parsing presented in: ;;;
450 ;;; Pratt, Vaughan R., ``Top Down Operator Precedence,'' ;;;
451 ;;; ACM Symposium on Principles of Programming Languages ;;;
452 ;;; Boston, MA; October, 1973. ;;;
454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
456 ;;; Implementation Notes ....
458 ;;; JPG Chars like ^A, ^B, ... get left around after interrupts and
459 ;;; should be thrown away by the scanner if not used as editting
462 ;;; KMP There is RBP stuff in DISPLA, too. Probably this sort of
463 ;;; data should all be in one place somewhere.
465 ;;; KMP Maybe the parser and/or scanner could use their own GC scheme
466 ;;; to recycle conses used in scan/parse from line to line which
467 ;;; really ought not be getting dynamically discarded and reconsed.
468 ;;; Alternatively, we could call RECLAIM explicitly on certain
469 ;;; pieces of structure which get used over and over. A
470 ;;; local-reclaim abstraction may want to be developed since this
471 ;;; stuff will always be needed, really. On small-address-space
472 ;;; machines, this could be overridden when the last DYNAMALLOC
473 ;;; GC barrier were passed (indicating that space was at a premium
474 ;;; -- in such case, real RECLAIM would be more economical -- or
475 ;;; would the code to control that be larger than the area locked
478 ;;; KMP GJC has a MAKE-EVALUATOR type package which could probably
479 ;;; replace the CALL-IF-POSSIBLE stuff used here.
480 ;;; [So it was written, so it was done. -gjc]
482 ;;; KMP DEFINE-SYMBOL and KILL-OPERATOR need to be redefined.
483 ;;; Probably these shouldn't be defined in this file anyway.
485 ;;; KMP The relationship of thisfile to SYNEX needs to be thought
486 ;;; out more carefully.
488 ;;; GJC Need macros for declaring INFIX, PREFIX, etc ops
490 ;;; GJC You know, PARSE-NARY isn't really needed it seems, since
491 ;;; the SIMPLIFIER makes the conversion of
492 ;;; ((MTIMES) ((MTIMES) A B) C) => ((MTIMES) A B C)
493 ;;; I bet you could get make "*" infix and nobody would
496 ;;; The following terms may be useful in deciphering this code:
498 ;;; NUD -- NUll left Denotation (op has nothing to its left (prefix))
499 ;;; LED -- LEft Denotation (op has something to left (postfix or infix))
501 ;;; LBP -- Left Binding Power (the stickiness to the left)
502 ;;; RBP -- Right Binding Power (the stickiness to the right)
507 (defvar scan-buffered-token
(list nil
)
508 "put-back buffer for scanner, a state-variable of the reader")
510 (defun peek-one-token ()
511 (peek-one-token-g nil nil
))
513 (defun peek-one-token-g (eof-ok? eof-obj
)
515 ((car scan-buffered-token
)
516 (cdr scan-buffered-token
))
517 (t (rplacd scan-buffered-token
(scan-one-token-g eof-ok? eof-obj
))
518 (cdr (rplaca scan-buffered-token t
)))))
520 (defun scan-one-token ()
521 (scan-one-token-g nil nil
))
523 (defun scan-one-token-g (eof-ok? eof-obj
)
524 (declare (special macsyma-operators
))
525 (cond ((car scan-buffered-token
)
526 (rplaca scan-buffered-token
())
527 (cdr scan-buffered-token
))
528 ((read-command-token macsyma-operators
))
530 (let ((test (parse-tyipeek)))
531 (cond ((eql test
*parse-stream-eof
*)
534 (maxima-error (intl:gettext
"parser: end of file while scanning expression."))))
537 (cond ((char= (parse-tyipeek) #\
*)
540 (scan-one-token-g eof-ok? eof-obj
))
542 ((eql test
#\.
) (parse-tyi) ; Read the dot
543 (if (digit-char-p (parse-tyipeek) 10.
)
544 (scan-number-after-dot (list (ncons #\.
) nil
))
548 (scan-macsyma-string))
551 (cond ((char= (parse-tyipeek) #\")
554 ((char= (parse-tyipeek) #\
:)
555 (scan-keyword-token))
559 (if (digit-char-p test
10.
)
560 (scan-number-before-dot ())
561 (scan-macsyma-token))))))))
563 ;; nested comments are permitted.
564 (defun gobble-comment ()
568 (setq c
(parse-tyipeek))
570 (cond ((= depth
0) (return t
)))
571 (cond ((eql c
*parse-stream-eof
*)
572 (error (intl:gettext
"parser: end of file in comment.")))
574 (cond ((char= (parse-tyipeek) #\
/)
577 (cond ((= depth
0) (return t
)))
580 (cond ((char= (parse-tyipeek) #\
*)
581 (incf depth
) (parse-tyi)
586 (defun scan-number-rest (data)
587 (let ((c (caar data
)))
590 (scan-number-after-dot data
))
591 ((member c
'(#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
))
592 ;; Dot missing but found exponent marker. Fake it.
593 (setf data
(push (ncons #\.
) (rest data
)))
594 (push (ncons #\
0) data
)
595 (push (ncons c
) data
)
596 (scan-number-exponent data
)))))
598 (defun scan-number-before-dot (data)
599 (scan-digits data
'(#\.
#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
)
603 ;; "First character" and "Pop character"
605 (defmacro first-c
() '(peek-one-token))
606 (defmacro pop-c
() '(scan-one-token))
608 (defun mstringp (x) (stringp x
)) ;; OBSOLETE. PRESERVE FOR SAKE OF POSSIBLE CALLS FROM NON-MAXIMA CODE !!
610 (defun inherit-propl (op-to op-from getl
)
611 (let ((propl (getl op-from getl
)))
613 (progn (remprop op-to
(car propl
))
614 (putprop op-to
(cadr propl
) (car propl
)))
616 (maxima-error "has no ~a properties. ~a ~a" getl op-from
'wrng-type-arg
)
621 ;;; (LED <op> <left>)
623 ;;; <op> is the name of the operator which was just popped.
624 ;;; <left> is the stuff to the left of the operator in the LED case.
628 #+gcl
(eval compile load
)
629 #-gcl
(:execute
:compile-toplevel
:load-toplevel
)
630 (defmacro def-nud-equiv
(op equiv
)
631 (list 'putprop
(list 'quote op
) (list 'function equiv
)
634 (defmacro nud-propl
() ''(nud))
636 (defmacro def-nud-fun
(op-name op-l . body
)
637 (list* 'defun-prop
(list* op-name
'nud
'nil
) op-l body
))
639 (defmacro def-led-equiv
(op equiv
)
640 (list 'putprop
(list 'quote op
) (list 'function equiv
)
643 (defmacro led-propl
() ''(led))
645 (defmacro def-led-fun
(op-name op-l . body
)
646 (list* 'defun-prop
(list* op-name
'led
'nil
) op-l body
)))
649 (let ((tem (and (symbolp op
) (getl op
'(nud)))) res
)
653 (mread-synerr "~A is not a prefix operator" (mopstrip op
))
655 (funcall (cadr tem
) op
)))
658 (defun led-call (op l
)
659 (let ((tem (and (symbolp op
) (getl op
'(led)))) res
)
662 (mread-synerr "~A is not an infix operator" (mopstrip op
))
663 (funcall (cadr tem
) op l
)))
666 ;;; (DEF-NUD (op lbp rbp) bvl . body)
668 ;;; Defines a procedure for parsing OP as a prefix operator.
670 ;;; OP should be the name of the symbol as a string or symbol.
671 ;;; LBP is an optional left binding power for the operator.
672 ;;; RBP is an optional right binding power for the operator.
673 ;;; BVL must contain exactly one variable, which the compiler will not
674 ;;; complain about if unused, since it will rarely be of use anyway.
675 ;;; It will get bound to the operator being parsed.
676 ;;; lispm:Optional args not allowed in release 5 allowed, necessary afterwards..
678 (defmacro def-nud
((op . lbp-rbp
) bvl . body
)
679 (let (( lbp
(nth 0 lbp-rbp
))
680 ( rbp
(nth 1 lbp-rbp
)))
681 `(progn ,(make-parser-fun-def op
'nud bvl body
)
682 (set-lbp-and-rbp ',op
',lbp
',rbp
))))
684 (defun set-lbp-and-rbp (op lbp rbp
)
685 (cond ((not (consp op
))
686 (let ((existing-lbp (get op
'lbp
))
687 (existing-rbp (get op
'rbp
)))
688 (cond ((not lbp
) ;; ignore omitted arg
691 (putprop op lbp
'lbp
))
692 ((not (equal existing-lbp lbp
))
693 (maxima-error "Incompatible LBP's defined for this operator ~a" op
)))
694 (cond ((not rbp
) ;; ignore omitted arg
697 (putprop op rbp
'rbp
))
698 ((not (equal existing-rbp rbp
))
699 (maxima-error "Incompatible RBP's defined for this operator ~a" op
)))))
701 (mapcar #'(lambda (x) (set-lbp-and-rbp x lbp rbp
))
704 ;;; (DEF-LED (op lbp rbp) bvl . body)
706 ;;; Defines a procedure for parsing OP as an infix or postfix operator.
708 ;;; OP should be the name of the symbol as a string or symbol.
709 ;;; LBP is an optional left binding power for the operator.
710 ;;; RBP is an optional right binding power for the operator.
711 ;;; BVL must contain exactly two variables, the first of which the compiler
712 ;;; will not complain about if unused, since it will rarely be of use
713 ;;; anyway. Arg1 will get bound to the operator being parsed. Arg2 will
714 ;;; get bound to the parsed structure which was to the left of Arg1.
717 (defmacro def-led
((op . lbp-rbp
) bvl . body
)
718 (let (( lbp
(nth 0 lbp-rbp
))
719 ( rbp
(nth 1 lbp-rbp
)))
720 `(progn ,(make-parser-fun-def op
'led bvl body
)
721 (set-lbp-and-rbp ',op
',lbp
',rbp
))))
723 (defmacro def-collisions
(op &rest alist
)
724 (let ((keys (do ((i 1 (ash i
1))
725 (lis alist
(cdr lis
))
726 (nl () (cons (cons (caar lis
) i
) nl
)))
729 (defprop ,op
,(let nil
730 (copy-tree keys
)) keys
)
731 ,@(mapcar #'(lambda (data)
732 `(defprop ,(car data
)
733 ,(do ((i 0 (logior i
(cdr (assoc (car lis
) keys
:test
#'eq
))))
734 (lis (cdr data
) (cdr lis
)))
740 (defun collision-lookup (op active-bitmask key-bitmask
)
741 (let ((result (logand active-bitmask key-bitmask
)))
742 (if (not (zerop result
))
743 (do ((l (get op
'keys
) (cdr l
)))
744 ((null l
) (parse-bug-err 'collision-check
))
745 (if (not (zerop (logand result
(cdar l
))))
746 (return (caar l
)))))))
748 (defun collision-check (op active-bitmask key
)
749 (let ((key-bitmask (get key op
)))
750 (if (not key-bitmask
)
751 (mread-synerr "~A is an unknown keyword in a ~A statement."
752 (mopstrip key
) (mopstrip op
)))
753 (let ((collision (collision-lookup op active-bitmask key-bitmask
)))
755 (if (eq collision key
)
756 (mread-synerr "This ~A's ~A slot is already filled."
759 (mread-synerr "A ~A cannot have a ~A with a ~A field."
762 (mopstrip collision
))))
763 (logior (cdr (assoc key
(get op
'keys
) :test
#'eq
)) active-bitmask
))))
765 ;;;; Data abstraction
767 ;;; LBP = Left Binding Power
769 ;;; (LBP <op>) - reads an operator's Left Binding Power
770 ;;; (DEF-LBP <op> <val>) - defines an operator's Left Binding Power
772 (defun lbp (lex) (cond ((safe-get lex
'lbp
)) (t 200.
)))
774 (defmacro def-lbp
(sym val
) `(defprop ,sym
,val lbp
))
776 ;;; RBP = Right Binding Power
778 ;;; (RBP <op>) - reads an operator's Right Binding Power
779 ;;; (DEF-RBP <op> <val>) - defines an operator's Right Binding Power
781 (defun rbp (lex) (cond ((safe-get lex
'rbp
)) (t 200.
)))
783 (defmacro def-rbp
(sym val
) `(defprop ,sym
,val rbp
))
785 (defmacro def-match
(x m
) `(defprop ,x
,m match
))
787 ;;; POS = Part of Speech!
794 (defun lpos (op) (cond ((safe-get op
'lpos
)) (t '$any
)))
795 (defun rpos (op) (cond ((safe-get op
'rpos
)) (t '$any
)))
796 (defun pos (op) (cond ((safe-get op
'pos
)) (t '$any
)))
798 (defmacro def-pos
(op pos
) `(defprop ,op
,pos pos
))
799 (defmacro def-rpos
(op pos
) `(defprop ,op
,pos rpos
))
800 (defmacro def-lpos
(op pos
) `(defprop ,op
,pos lpos
))
804 (defun mheader (op) (add-lineinfo (or (safe-get op
'mheader
) (ncons op
))))
806 (defmacro def-mheader
(op header
) `(defprop ,op
,header mheader
))
809 (defmvar $parsewindow
10.
810 "The maximum number of 'lexical tokens' that are printed out on
811 each side of the error-point when a syntax (parsing) MAXIMA-ERROR occurs. This
812 option is especially useful on slow terminals. Setting it to -1 causes the
813 entire input string to be printed out when an MAXIMA-ERROR occurs."
817 ;;;; Misplaced definitions
819 (defmacro def-operatorp
()
820 `(defun operatorp (lex)
821 (and (symbolp lex
) (getl lex
'(,@(nud-propl) ,@(led-propl))))))
825 (defmacro def-operatorp1
()
826 ;Defmfun -- used by SYNEX if not others.
827 `(defun operatorp1 (lex)
828 ;; Referenced outside of package: OP-SETUP, DECLARE1
829 ;; Use for truth value only, not for return-value.
830 (and (symbolp lex
) (getl lex
'(lbp rbp
,@(nud-propl) ,@(led-propl))))))
834 ;;;; The Macsyma Parser
836 ;;; (MREAD) with arguments compatible with losing maclisp READ style.
838 ;;; Returns a parsed form of tokens read from stream.
840 ;;; If you want rubout processing, be sure to call some stream which knows
841 ;;; about such things. Also, I'm figuring that the PROMPT will be
842 ;;; an attribute of the stream which somebody can hack before calling
843 ;;; MREAD if he wants to.
846 ;;Important for lispm rubout handler
847 (defun mread (&rest read-args
)
850 (and *parse-window
* (setf (car *parse-window
*) nil
851 *parse-window
* (cdr *parse-window
*)))
852 (princ *mread-prompt
*)
854 (apply 'mread-raw read-args
)))
856 (defun mread-prompter (stream char
)
857 (declare (special *mread-prompt-internal
*)
860 (princ *mread-prompt-internal
* stream
))
862 ;; input can look like:
865 (defun mread-raw (*parse-stream
* &optional
*mread-eof-obj
*)
866 (let ((scan-buffered-token (list nil
))
868 (if (eq scan-buffered-token
;; a handly unique object for the EQ test.
869 (peek-one-token-g t scan-buffered-token
))
872 (input (parse '$any
0.
) (parse '$any
0.
)))
876 ;force a separate line info structure
877 (setf *current-line-info
* nil
)
878 (return (list (mheader (pop-c))
879 (if labels
(cons (mheader '|$
[|
) (nreverse labels
)))
885 (mread-synerr "Invalid && tag. Tag must be a symbol")))
887 (parse-bug-err 'mread-raw
)))))))
889 ;;; (PARSE <mode> <rbp>)
891 ;;; This will parse an expression containing operators which have a higher
892 ;;; left binding power than <rbp>, returning as soon as an operator of
893 ;;; lesser or equal binding power is seen. The result will be in the given
894 ;;; mode (which allows some control over the class of result expected).
895 ;;; Modes used are as follows:
896 ;;; $ANY = Match any type of expression
897 ;;; $CLAUSE = Match only boolean expressions (or $ANY)
898 ;;; $EXPR = Match only mathematical expressions (or $ANY)
899 ;;; If a mismatched mode occurs, a syntax error will be flagged. Eg,
900 ;;; this is why "X^A*B" parses but "X^A and B" does not. X^A is a $EXPR
901 ;;; and not coercible to a $CLAUSE. See CONVERT.
903 ;;; <mode> is the required mode of the result.
904 ;;; <rbp> is the right binding power to use for the parse. When an
905 ;;; LED-type operator is seen with a lower left binding power
906 ;;; than <rbp>, this parse returns what it's seen so far rather
907 ;;; than calling that operator.
910 (defun parse (mode rbp
)
911 (do ((left (nud-call (pop-c)) ; Envoke the null left denotation
912 (led-call (pop-c) left
))) ; and keep calling LED ops as needed
913 ((>= rbp
(lbp (first-c))) ; Until next op lbp too low
914 (convert left mode
)))) ; in which case, return stuff seen
916 ;;; (PARSE-PREFIX <op>)
918 ;;; Parses prefix forms -- eg, -X or NOT FOO.
920 ;;; This should be the NUD property on an operator. It fires after <op>
921 ;;; has been seen. It parses forward looking for one more expression
922 ;;; according to its right binding power, returning
923 ;;; ( <mode> . ((<op>) <arg1>) )
925 (defun parse-prefix (op)
926 (list (pos op
) ; Operator mode
927 (mheader op
) ; Standard Macsyma expression header
928 (parse (rpos op
) (rbp op
)))) ; Convert single argument for use
930 ;;; (PARSE-POSTFIX <op> <left>)
932 ;;; Parses postfix forms. eg, X!.
934 ;;; This should be the LED property of an operator. It fires after <left>
935 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
936 ;;; ( <mode> . ((<op>) <arg1>) )
938 (defun parse-postfix (op l
)
939 (list (pos op
) ; Operator's mode
940 (mheader op
) ; Standard Macsyma expression header
941 (convert l
(lpos op
)))) ; Convert single argument for use
943 ;;; (PARSE-INFIX <op> <left>)
945 ;;; Parses infix (non-nary) forms. eg, 5 mod 3.
947 ;;; This should be the led property of an operator. It fires after <left>
948 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
949 ;;; ( <mode> . ((<op>) <arg1> <arg2>) )
951 (defun parse-infix (op l
)
952 (list (pos op
) ; Operator's mode
953 (mheader op
) ; Standard Macsyma expression header
954 (convert l
(lpos op
)) ; Convert arg1 for immediate use
955 (parse (rpos op
) (rbp op
)))) ; Look for an arg2
957 ;;; (PARSE-NARY <op> <left>)
959 ;;; Parses nary forms. Eg, form1*form2*... or form1+form2+...
960 ;;; This should be the LED property on an operator. It fires after <op>
961 ;;; has been seen, accumulating and returning
962 ;;; ( <mode> . ((<op>) <arg1> <arg2> ...) )
964 ;;; <op> is the being parsed.
965 ;;; <left> is the stuff that has been seen to the left of <op> which
966 ;;; rightly belongs to <op> on the basis of parse precedence rules.
968 (defun parse-nary (op l
)
969 (list* (pos op
) ; Operator's mode
970 (mheader op
) ; Normal Macsyma operator header
971 (convert l
(lpos op
)) ; Check type-match of arg1
972 (prsnary op
(lpos op
) (lbp op
)))) ; Search for other args
974 ;;; (PARSE-MATCHFIX <lop>)
976 ;;; Parses matchfix forms. eg, [form1,form2,...] or (form1,form2,...)
978 ;;; This should be the NUD property on an operator. It fires after <op>
979 ;;; has been seen. It parses <lop><form1>,<form2>,...<rop> returning
980 ;;; ( <mode> . ((<lop>) <form1> <form2> ...) ).
982 (defun parse-matchfix (op)
983 (list* (pos op
) ; Operator's mode
984 (mheader op
) ; Normal Macsyma operator header
985 (prsmatch (safe-get op
'match
) (lpos op
)))) ; Search for matchfixed forms
987 ;;; (PARSE-NOFIX <op>)
989 ;;; Parses an operator of no args. eg, @+X where @ designates a function
990 ;;; call (eg, @() is implicitly stated by the lone symbol @.)
992 ;;; This should be a NUD property on an operator which takes no args.
993 ;;; It immediately returns ( <mode> . ((<op>)) ).
995 ;;; <op> is the name of the operator.
997 ;;; Note: This is not used by default and probably shouldn't be used by
998 ;;; someone who doesn't know what he's doing. Example lossage. If @ is
999 ;;; a nofix op, then @(3,4) parses, but parses as "@"()(3,4) would -- ie,
1000 ;;; to ((MQAPPLY) (($@)) 3 4) which is perhaps not what the user will expect.
1002 (defun parse-nofix (op) (list (pos op
) (mheader op
)))
1004 ;;; (PRSNARY <op> <mode> <rbp>)
1006 ;;; Parses an nary operator tail Eg, ...form2+form3+... or ...form2*form3*...
1008 ;;; Expects to be entered after the leading form and the first call to an
1009 ;;; nary operator has been seen and popped. Returns a list of parsed forms
1010 ;;; which belong to that operator. Eg, for X+Y+Z; this should be called
1011 ;;; after the first + is popped. Returns (Y Z) and leaves the ; token
1012 ;;; in the parser scan buffer.
1014 ;;; <op> is the nary operator in question.
1015 ;;; <rbp> is (LBP <op>) and is provided for efficiency. It is for use in
1016 ;;; recursive parses as a binding power to parse for.
1017 ;;; <mode> is the name of the mode that each form must be.
1019 (defun prsnary (op mode rbp
)
1020 (do ((nl (list (parse mode rbp
)) ; Get at least one form
1021 (cons (parse mode rbp
) nl
))) ; and keep getting forms
1022 ((not (eq op
(first-c))) ; until a parse pops on a new op
1023 (nreverse nl
)) ; at which time return forms
1024 (pop-c))) ; otherwise pop op
1026 ;;; (PRSMATCH <match> <mode>)
1028 ;;; Parses a matchfix sequence. Eg, [form1,form2,...] or (form1,form2,...)
1029 ;;; Expects to be entered after the leading token is the popped (ie, at the
1030 ;;; point where the parse of form1 will begin). Returns (form1 form2 ...).
1032 ;;; <match> is the token to look for as a matchfix character.
1033 ;;; <mode> is the name of the mode that each form must be.
1035 (defun prsmatch (match mode
) ; Parse for matchfix char
1036 (cond ((eq match
(first-c)) (pop-c) nil
) ; If immediate match, ()
1038 (do ((nl (list (parse mode
10.
)) ; Get first element
1039 (cons (parse mode
10.
) nl
))) ; and Keep adding elements
1040 ((eq match
(first-c)) ; Until we hit the match.
1041 (pop-c) ; Throw away match.
1042 (nreverse nl
)) ; Put result back in order
1043 (if (eq '|$
,|
(first-c)) ; If not end, look for ","
1044 (pop-c) ; and pop it if it's there
1045 (mread-synerr "Missing ~A" ; or give an error message.
1046 (mopstrip match
)))))))
1048 ;;; (CONVERT <exp> <mode>)
1050 ;;; Parser coercion function.
1052 ;;; <exp> should have the form ( <expressionmode> . <expression> )
1053 ;;; <mode> is the target mode.
1055 ;;; If <expressionmode> and <mode> are compatible, returns <expression>.
1057 (defun convert (item mode
)
1058 (if (or (eq mode
(car item
)) ; If modes match exactly
1059 (eq '$any mode
) ; or target is $ANY
1060 (eq '$any
(car item
))) ; or input is $ANY
1061 (cdr item
) ; then return expression
1062 (mread-synerr "Found ~A expression where ~A expression expected"
1063 (get (car item
) 'english
)
1064 (get mode
'english
))))
1066 (defprop $any
"untyped" english
)
1067 (defprop $clause
"logical" english
)
1068 (defprop $expr
"algebraic" english
)
1070 ;;;; Parser Error Diagnostics
1072 ;; Call this for random user-generated parse errors
1074 (defun parse-err () (mread-synerr "Syntax error"))
1076 ;; Call this for random internal parser lossage (eg, code that shouldn't
1079 (defun parse-bug-err (op)
1081 "Parser bug in ~A. Please report this to the Maxima maintainers,~
1082 ~%including the characters you just typed which caused the error. Thanks."
1085 ;;; Random shared error messages
1087 (defun delim-err (op)
1088 (mread-synerr "Illegal use of delimiter ~A" (mopstrip op
)))
1090 (defun erb-err (op l
) l
;Ignored
1091 (mread-synerr "Too many ~A's" (mopstrip op
)))
1093 (defun premterm-err (op)
1094 (mread-synerr "Premature termination of input at ~A."
1097 ;;;; Operator Specific Data
1099 (def-nud-equiv |$
]| delim-err
)
1100 (def-led-equiv |$
]| erb-err
)
1103 (def-nud-equiv |$
[| parse-matchfix
)
1104 (def-match |$
[| |$
]|
)
1107 (def-mheader |$
[|
(mlist))
1109 (def-lpos |$
[| $any
)
1112 (def-led (|$
[|
200.
) (op left
)
1113 (setq left
(convert left
'$any
))
1114 (if (numberp left
) (parse-err)) ; number[...] invalid
1115 (let ((header (if (atom left
)
1116 (add-lineinfo (list (amperchk left
) 'array
))
1117 (add-lineinfo '(mqapply array
))))
1118 (right (prsmatch '|$
]|
'$any
))) ; get sublist in RIGHT
1119 (cond ((null right
) ; 1 subscript minimum
1120 (mread-synerr "No subscripts given"))
1121 ((atom left
) ; atom[...]
1122 (setq right
(cons header
1124 (cons '$any
(aliaslookup right
)))
1126 (cons '$any
(cons header
1127 (cons left right
)))))))
1130 (def-nud-equiv |$
)| delim-err
)
1131 (def-led-equiv |$
)| erb-err
)
1134 (def-mheader |$
(|
(mprogn))
1136 ;; KMP: This function optimizes out (exp) into just exp.
1137 ;; This is useful for mathy expressions, but obnoxious for non-mathy
1138 ;; expressions. I think DISPLA should be made smart about such things,
1139 ;; but probably the (...) should be carried around in the internal
1140 ;; representation. This would make things like BUILDQ much easier to
1142 ;; GJC: CGOL has the same behavior, so users tend to write extensions
1143 ;; to the parser rather than write Macros per se. The transformation
1144 ;; "(EXP)" ==> "EXP" is done by the evaluator anyway, the problem
1145 ;; comes inside quoted expressions. There are many other problems with
1146 ;; the "QUOTE" concept however.
1148 (def-nud (|$
(|
200.
) (op)
1149 (let ((right)(hdr (mheader '|$
(|
))) ; make mheader first for lineinfo
1150 (cond ((eq '|$
)|
(first-c)) (parse-err)) ; () is illegal
1151 ((or (null (setq right
(prsmatch '|$
)|
'$any
))) ; No args to MPROGN??
1152 (cdr right
)) ; More than one arg.
1153 (when (suspicious-mprogn-p right
)
1154 (mtell (intl:gettext
"warning: parser: I'll let it stand, but (...) doesn't recognize local variables.~%"))
1155 (mtell (intl:gettext
"warning: parser: did you mean to say: block(~M, ...) ?~%") (car right
)))
1156 (cons '$any
(cons hdr right
))) ; Return an MPROGN
1157 (t (cons '$any
(car right
)))))) ; Optimize out MPROGN
1159 (defun suspicious-mprogn-p (right)
1160 ;; Look for a Maxima list of symbols or assignments to symbols.
1161 (and ($listp
(car right
))
1162 (every #'(lambda (e) (or (symbolp e
)
1163 (and (consp e
) (eq (caar e
) 'msetq
) (symbolp (second e
)))))
1164 (rest (car right
)))))
1166 (def-led (|$
(|
200.
) (op left
)
1167 (setq left
(convert left
'$any
)) ;De-reference LEFT
1168 (if (numberp left
) (parse-err)) ;number(...) illegal
1169 (let ((hdr (and (atom left
)(mheader (amperchk left
))))
1170 (r (prsmatch '|$
)|
'$any
)) ;Get arglist in R
1172 (cons '$any
;Result is type $ANY
1173 (cond ((atom left
) ;If atom(...) =>
1174 (cons hdr r
)) ;(($atom) exp . args)
1175 (t ;Else exp(...) =>
1176 (cons '(mqapply) (cons left r
))))))) ;((MQAPPLY) op . args)
1178 (def-mheader |$
'|
(mquote))
1180 (def-nud (|$
'|
) (op)
1182 (cond ((eq '|$
(|
(first-c))
1183 (list '$any
(mheader '|$
'|
) (parse '$any
190.
)))
1184 ((or (atom (setq right
(parse '$any
190.
)))
1185 (member (caar right
) '(mquote mlist $set mprog mprogn lambda
) :test
#'eq
))
1186 (list '$any
(mheader '|$
'|
) right
))
1187 ((eq 'mqapply
(caar right
))
1188 (cond ((eq (caaadr right
) 'lambda
)
1189 (list '$any
(mheader '|$
'|
) right
))
1190 (t (rplaca (cdr right
)
1191 (cons (cons ($nounify
(caaadr right
))
1194 (cons '$any right
))))
1195 (t (cons '$any
(cons (cons ($nounify
(caar right
)) (cdar right
))
1198 (def-nud (|$
''|
) (op)
1201 (cond ((eq '|$
(|
(first-c)) (meval (parse '$any
190.
)))
1202 ((atom (setq right
(parse '$any
190.
))) (meval1 right
))
1203 ((eq 'mqapply
(caar right
))
1205 (cons (cons ($verbify
(caaadr right
)) (cdaadr right
))
1208 (t (cons (cons ($verbify
(caar right
)) (cdar right
))
1211 (def-led-equiv |$
:| parse-infix
)
1215 (def-rpos |$
:| $any
)
1216 (def-lpos |$
:| $any
)
1217 (def-mheader |$
:|
(msetq))
1219 (def-led-equiv |$
::| parse-infix
)
1220 (def-lbp |$
::|
180.
)
1222 (def-pos |$
::| $any
)
1223 (def-rpos |$
::| $any
)
1224 (def-lpos |$
::| $any
)
1225 (def-mheader |$
::|
(mset))
1227 (def-led-equiv |$
:=| parse-infix
)
1228 (def-lbp |$
:=|
180.
)
1230 (def-pos |$
:=| $any
)
1231 (def-rpos |$
:=| $any
)
1232 (def-lpos |$
:=| $any
)
1233 (def-mheader |$
:=|
(mdefine))
1235 (def-led-equiv |$
::=| parse-infix
)
1236 (def-lbp |$
::=|
180.
)
1237 (def-rbp |$
::=|
20.
)
1238 (def-pos |$
::=| $any
)
1239 (def-rpos |$
::=| $any
)
1240 (def-lpos |$
::=| $any
)
1241 (def-mheader |$
::=|
(mdefmacro))
1243 (def-led-equiv |$
!| parse-postfix
)
1246 (def-pos |$
!| $expr
)
1247 (def-lpos |$
!| $expr
)
1249 (def-mheader |$
!|
(mfactorial))
1251 (def-mheader |$
!!|
($genfact
))
1253 (def-led (|$
!!|
160.
) (op left
)
1256 (convert left
'$expr
)
1257 (list (mheader '$
/) (convert left
'$expr
) 2)
1262 (def-pos |$^| $expr
)
1263 (def-lpos |$^| $expr
)
1264 (def-rpos |$^| $expr
)
1265 (def-mheader |$^|
(mexpt))
1267 (def-led ((|$^| |$^^|
)) (op left
)
1269 (aliaslookup (list (mheader op
)
1270 (convert left
(lpos op
))
1271 (parse (rpos op
) (rbp op
))))))
1273 (mapc #'(lambda (prop) ; Make $** like $^
1274 (let ((propval (get '$^ prop
)))
1275 (if propval
(putprop '$
** propval prop
))))
1276 '(lbp rbp pos rpos lpos mheader
))
1278 (inherit-propl '$
** '$^
(led-propl))
1280 (def-lbp |$^^|
140.
)
1281 (def-rbp |$^^|
139.
)
1282 (def-pos |$^^| $expr
)
1283 (def-lpos |$^^| $expr
)
1284 (def-rpos |$^^| $expr
)
1285 (def-mheader |$^^|
(mncexpt))
1287 ;; note y^^4.z gives an error because it scans the number 4 together with
1288 ;; the trailing '.' as a decimal place. I think the error is correct.
1289 (def-led-equiv |$.| parse-infix
)
1292 (def-pos |$.| $expr
)
1293 (def-lpos |$.| $expr
)
1294 (def-rpos |$.| $expr
)
1295 (def-mheader |$.|
(mnctimes))
1297 ;; Copy properties to noun operator.
1298 (setf (get '%mnctimes
'op
) (get 'mnctimes
'op
))
1300 (def-led-equiv |$
*| parse-nary
)
1303 (def-pos |$
*| $expr
)
1305 (def-lpos |$
*| $expr
)
1306 (def-mheader |$
*|
(mtimes))
1308 (def-led-equiv $
/ parse-infix
)
1314 (def-mheader $
/ (mquotient))
1316 (def-nud-equiv |$
+| parse-prefix
)
1318 (def-rbp |$
+|
134.
) ; Value increased from 100 to 134 (DK 02/2010).
1319 (def-pos |$
+| $expr
)
1320 (def-rpos |$
+| $expr
)
1322 (def-mheader |$
+|
(mplus))
1324 (def-led ((|$
+| |$-|
) 100.
) (op left
)
1325 (setq left
(convert left
'$expr
))
1326 (do ((nl (list (if (eq op
'$-
)
1327 (list (mheader '$-
) (parse '$expr
100.
))
1328 (parse '$expr
100.
))
1330 (cons (parse '$expr
100.
) nl
)))
1331 ((not (member (first-c) '($
+ $-
) :test
#'eq
))
1332 (list* '$expr
(mheader '$
+) (nreverse nl
)))
1333 (if (eq (first-c) '$
+) (pop-c))))
1335 (def-nud-equiv |$-| parse-prefix
)
1338 (def-pos |$-| $expr
)
1339 (def-rpos |$-| $expr
)
1341 (def-mheader |$-|
(mminus))
1343 (def-led-equiv |$
=| parse-infix
)
1346 (def-pos |$
=| $clause
)
1347 (def-rpos |$
=| $expr
)
1348 (def-lpos |$
=| $expr
)
1349 (def-mheader |$
=|
(mequal))
1351 (def-led-equiv |$
#| parse-infix
)
1354 (def-pos |$
#| $clause
)
1355 (def-rpos |$
#| $expr
)
1356 (def-lpos |$
#| $expr
)
1357 (def-mheader |$
#|
(mnotequal))
1359 (def-led-equiv |$
>| parse-infix
)
1362 (def-pos |$
>| $clause
)
1363 (def-rpos |$
>| $expr
)
1364 (def-lpos |$
>| $expr
)
1365 (def-mheader |$
>|
(mgreaterp))
1367 (def-led-equiv |$
>=| parse-infix
)
1370 (def-pos |$
>=| $clause
)
1371 (def-rpos |$
>=| $expr
)
1372 (def-lpos |$
>=| $expr
)
1373 (def-mheader |$
>=|
(mgeqp))
1375 (def-led-equiv |$
<| parse-infix
)
1378 (def-pos |$
<| $clause
)
1379 (def-rpos |$
<| $expr
)
1380 (def-lpos |$
<| $expr
)
1381 (def-mheader |$
<|
(mlessp))
1383 (def-led-equiv |$
<=| parse-infix
)
1386 (def-pos |$
<=| $clause
)
1387 (def-rpos |$
<=| $expr
)
1388 (def-lpos |$
<=| $expr
)
1389 (def-mheader |$
<=|
(mleqp))
1391 (def-nud-equiv $not parse-prefix
)
1394 (def-pos $not $clause
)
1395 (def-rpos $not $clause
)
1396 (def-lpos $not $clause
)
1397 (def-mheader $not
(mnot))
1399 (def-led-equiv $and parse-nary
)
1402 (def-pos $and $clause
)
1404 (def-lpos $and $clause
)
1405 (def-mheader $and
(mand))
1407 (def-led-equiv $or parse-nary
)
1410 (def-pos $or $clause
)
1412 (def-lpos $or $clause
)
1413 (def-mheader $or
(mor))
1415 (def-led-equiv |$
,| parse-nary
)
1420 (def-lpos |$
,| $any
)
1421 (def-mheader |$
,|
($ev
))
1423 (def-nud-equiv $then delim-err
)
1427 (def-nud-equiv $else delim-err
)
1431 (def-nud-equiv $elseif delim-err
)
1432 (def-lbp $elseif
5.
)
1433 (def-rbp $elseif
45.
)
1434 (def-pos $elseif $any
)
1435 (def-rpos $elseif $clause
)
1437 ;No LBP - Default as high as possible
1440 (def-rpos $if $clause
)
1442 (def-mheader $if
(mcond))
1447 (parse-condition op
)))
1449 (defun parse-condition (op)
1450 (list* (parse (rpos op
) (rbp op
))
1451 (if (eq (first-c) '$then
)
1452 (parse '$any
(rbp (pop-c)))
1453 (mread-synerr "Missing `then'"))
1455 (($else
) (list t
(parse '$any
(rbp (pop-c)))))
1456 (($elseif
) (parse-condition (pop-c)))
1457 (t (list t
'$false
)))))
1459 (def-mheader $do
(mdo))
1461 (defun parse-$do
(lex &aux
(left (make-mdo)))
1462 (setf (car left
) (mheader 'mdo
))
1463 (do ((op lex
(pop-c)) (active-bitmask 0))
1465 (if (eq op
'|$
:|
) (setq op
'$from
))
1466 (setq active-bitmask
(collision-check '$do active-bitmask op
))
1467 (let ((data (parse (rpos op
) (rbp op
))))
1469 ($do
(setf (mdo-body left
) data
) (return (cons '$any left
)))
1470 ($for
(setf (mdo-for left
) data
))
1471 ($from
(setf (mdo-from left
) data
))
1472 ($in
(setf (mdo-op left
) 'mdoin
)
1473 (setf (mdo-from left
) data
))
1474 ($step
(setf (mdo-step left
) data
))
1475 ($next
(setf (mdo-next left
) data
))
1476 ($thru
(setf (mdo-thru left
) data
))
1479 (setq data
(list (mheader '$not
) data
)))
1480 (setf (mdo-unless left
)
1481 (if (null (mdo-unless left
))
1483 (list (mheader '$or
) data
(mdo-unless left
)))))
1484 (t (parse-bug-err '$do
))))))
1491 (def-lbp $unless
25.
)
1492 (def-lbp $while
25.
)
1495 (def-nud-equiv $for parse-$do
)
1496 (def-nud-equiv $from parse-$do
)
1497 (def-nud-equiv $step parse-$do
)
1498 (def-nud-equiv $next parse-$do
)
1499 (def-nud-equiv $thru parse-$do
)
1500 (def-nud-equiv $unless parse-$do
)
1501 (def-nud-equiv $while parse-$do
)
1502 (def-nud-equiv $do parse-$do
)
1511 (def-rbp $unless
45.
)
1512 (def-rbp $while
45.
)
1515 (def-rpos $for $any
)
1516 (def-rpos $from $any
)
1517 (def-rpos $step $expr
)
1518 (def-rpos $next $any
)
1519 (def-rpos $thru $expr
)
1520 (def-rpos $unless $clause
)
1521 (def-rpos $while $clause
)
1527 ($from .
($in $from
))
1528 ($in .
($in $from $step $next
))
1529 ($step .
($in $step $next
))
1530 ($next .
($in $step $next
))
1531 ($thru .
($in $thru
)) ;$IN didn't used to get checked for
1535 (def-mheader |$$|
(nodisplayinput))
1536 (def-nud-equiv |$$| premterm-err
)
1538 ;No RBP, POS, RPOS, RBP, or MHEADER
1540 (def-mheader |$
;| (displayinput))
1541 (def-nud-equiv |$
;| premterm-err)
1543 ;No RBP, POS, RPOS, RBP, or MHEADER
1545 (def-nud-equiv |$
&&| delim-err
)
1549 ;; kludge interface function to allow the use of lisp PRINC in places.
1550 (cond ((null x
) 'false
)
1551 ((or (eq x t
) (eq x
't
)) 'true
)
1554 (or (get x
'reversealias
)
1555 (let ((name (symbol-name x
)))
1556 (if (member (char name
0) '(#\$
#\%
) :test
#'char
=)
1561 (define-initial-symbols
1562 ;; * Note: /. is looked for explicitly rather than
1563 ;; existing in this chart. The reason is that
1564 ;; it serves a dual role (as a decimal point) and
1565 ;; must be special-cased.
1567 ;; Same for // because of the /* ... */ handling
1570 |
+| |-| |
*| |^| |
<| |
=| |
>| |
(| |
)| |
[| |
]| |
,|
1571 |
:| |
!| |
#| |
'| |;| |$| |
&|
1573 |
**| |^^| |
:=| |
::| |
!!| |
<=| |
>=| |
''| |
&&|
1578 ;; !! FOLLOWING MOVED HERE FROM MLISP.LISP (DEFSTRUCT STUFF)
1579 ;; !! SEE NOTE THERE
1582 ;;; User extensibility:
1583 (defmfun $prefix
(operator &optional
(rbp 180.
)
1586 (def-operator operator pos
() () rbp rpos
() t
1587 '(nud . parse-prefix
) 'msize-prefix
'dimension-prefix
() )
1590 (defmfun $postfix
(operator &optional
(lbp 180.
)
1593 (def-operator operator pos lbp lpos
() () t
()
1594 '(led . parse-postfix
) 'msize-postfix
'dimension-postfix
() )
1597 (defmfun $infix
(operator &optional
(lbp 180.
)
1602 (def-operator operator pos lbp lpos rbp rpos t t
1603 '(led . parse-infix
) 'msize-infix
'dimension-infix
() )
1606 (defmfun $nary
(operator &optional
(bp 180.
)
1609 (def-operator operator pos bp argpos bp
() t t
1610 '(led . parse-nary
) 'msize-nary
'dimension-nary
() )
1613 (defmfun $matchfix
(operator
1614 match
&optional
(argpos '$any
)
1616 ;shouldn't MATCH be optional?
1617 (def-operator operator pos
() argpos
() () () ()
1618 '(nud . parse-matchfix
) 'msize-matchfix
'dimension-match match
)
1621 (defmfun $nofix
(operator &optional
(pos '$any
))
1622 (def-operator operator pos
() () () () () ()
1623 '(nud . parse-nofix
) 'msize-nofix
'dimension-nofix
() )
1626 ;;; (DEF-OPERATOR op pos lbp lpos rbp rpos sp1 sp2
1627 ;;; parse-data grind-fn dim-fn match)
1628 ;;; OP is the operator name.
1629 ;;; POS is its ``part of speech.''
1630 ;;; LBP is its ``left binding power.''
1631 ;;; LPOS is the part of speech of the arguments to its left, or of all.
1632 ;;; arguments for NARY and MATCHFIX.
1633 ;;; RBP is its ``right binding power.''
1634 ;;; RPOS is the part of speech of the argument to its right.
1635 ;;; SP1 says if the DISSYM property needs a space on the right.
1636 ;;; SP2 says if the DISSYM property needs a space on the left.
1637 ;;; PARSE-DATA is (prop . fn) -- parser prop name dotted with function name
1638 ;;; GRIND-FN is the grinder function for the operator.
1639 ;;; DIM-FN is the dimension function for the operator.
1640 ;;; PARSEPROP is the property name to use for parsing. One of LED or NUD.
1641 ;;; MATCH if non-(), ignores SP1 and SP2. Should be the match symbol.
1642 ;;; sets OP up as matchfix with MATCH.
1644 ;;; For more complete descriptions of these naming conventions, see
1645 ;;; the comments in GRAM package, which describe them in reasonable detail.
1647 (defun def-operator (op pos lbp lpos rbp rpos sp1 sp2
1648 parse-data grind-fn dim-fn match
)
1650 (if (or (and rbp
(not (integerp (setq x rbp
))))
1651 (and lbp
(not (integerp (setq x lbp
)))))
1652 (merror (intl:gettext
"syntax extension: binding powers must be integers; found: ~M") x
))
1653 (if (stringp op
) (setq op
(define-symbol op
)))
1655 (let ((noun ($nounify op
))
1656 (dissym (cdr (exploden op
))))
1659 (setq dissym
(append (if sp1
'(#\space
)) dissym
(if sp2
'(#\space
)))))
1660 (t (if (stringp match
) (setq match
(define-symbol match
)))
1662 (putprop op match
'match
)
1663 (putprop match
5.
'lbp
)
1664 (setq dissym
(cons dissym
(cdr (exploden match
))))))
1665 (putprop op pos
'pos
)
1666 (putprop op
(cdr parse-data
) (car parse-data
))
1667 (putprop op grind-fn
'grind
)
1668 (putprop op dim-fn
'dimension
)
1669 (putprop noun dim-fn
'dimension
)
1670 (putprop op dissym
'dissym
)
1671 (putprop noun dissym
'dissym
)
1673 (putprop op rbp
'rbp
)
1674 (putprop noun rbp
'rbp
))
1676 (putprop op lbp
'lbp
)
1677 (putprop noun lbp
'lbp
))
1678 (when lpos
(putprop op lpos
'lpos
))
1679 (when rpos
(putprop op rpos
'rpos
))
1682 (defun op-setup (op)
1683 (declare (special *mopl
*))
1684 (let ((dummy (or (get op
'op
)
1685 (coerce (string* op
) 'string
))))
1686 (putprop op dummy
'op
)
1688 (if (and (operatorp1 op
) (not (member dummy
(cdr $props
) :test
#'eq
)))
1689 (push dummy
*mopl
*))
1690 (add2lnc dummy $props
)))
1692 (defun kill-operator (op)
1695 (noun-form ($nounify op
)))
1696 ;; Refuse to kill an operator which appears on *BUILTIN-$PROPS*.
1697 (unless (member opr
*builtin-$props
* :test
#'equal
)
1698 (undefine-symbol opr
)
1701 (mapc #'(lambda (x) (remprop op x
))
1702 '(nud nud-expr nud-subr
; NUD info
1703 led led-expr led-subr
; LED info
1704 lbp rbp
; Binding power info
1705 lpos rpos pos
; Part-Of-Speech info
1706 grind dimension dissym
; Display info
1707 op
)) ; Operator info
1708 (mapc #'(lambda (x) (remprop noun-form x
))
1709 '(dimension dissym lbp rbp
)))))
1713 ;; the functions get-instream etc.. are all defined in
1714 ;; gcl lsp/debug.lsp
1715 ;; they are all generic common lisp and could be used by
1716 ;; any Common lisp implementation.
1721 (line 0 :type fixnum
)
1725 (defvar *stream-alist
* nil
)
1728 (defun stream-name (path)
1730 (car (errset (namestring (pathname path
))))))
1733 (defun instream-name (instr)
1734 (or (instream-stream-name instr
)
1735 (stream-name (instream-stream instr
))))
1737 ;; (closedp stream) checks if a stream is closed.
1738 ;; how to do this in common lisp!!
1742 #+never-clean-up-dont-know-how-to-close
1743 (dolist (v *stream-alist
*)
1744 (if (closedp (instream-stream v
))
1745 (setq *stream-alist
* (delete v
*stream-alist
*)))))
1748 (defun get-instream (str)
1749 (or (dolist (v *stream-alist
*)
1750 (cond ((eq str
(instream-stream v
))
1753 (errset (setq name
(namestring str
)))
1754 (car (setq *stream-alist
*
1755 (cons (make-instream :stream str
:stream-name name
)
1756 *stream-alist
*))))))
1758 (defun newline (str)
1759 (incf (instream-line (get-instream str
)))
1762 (defun find-stream (stream)
1763 (dolist (v *stream-alist
*)
1764 (cond ((eq stream
(instream-stream v
))
1768 (defun add-lineinfo (lis)
1770 (eq *parse-stream
* *parse-string-input-stream
*) ;; avoid consing *parse-string-input-stream*
1771 ;; via get-instream to *stream-alist*
1772 (and (eq *parse-window
* *standard-input
*)
1773 (not (find-stream *parse-stream
*)) ))
1775 (let* ((st (get-instream *parse-stream
*))
1776 (n (instream-line st
))
1777 (nam (instream-name st
)))
1778 (or nam
(return-from add-lineinfo lis
))
1779 (setq *current-line-info
*
1780 (cond ((eq (cadr *current-line-info
*) nam
)
1781 (cond ((eql (car *current-line-info
*) n
)
1782 *current-line-info
*)
1783 (t (cons n
(cdr *current-line-info
*)))))
1784 (t (list n nam
'src
))))
1785 (cond ((null (cdr lis
))
1786 (list (car lis
) *current-line-info
*))
1787 (t (append lis
(list *current-line-info
*)))))))
1789 ;; Remove debugging stuff.
1790 ;; STRIP-LINEINFO does not modify EXPR.
1792 (defun strip-lineinfo (expr)
1793 (if (atom expr
) expr
1794 (cons (strip-lineinfo-op (car expr
)) (mapcar #'strip-lineinfo
(cdr expr
)))))
1796 ;; If something in the operator looks like debugging stuff, remove it.
1797 ;; It is assumed here that debugging stuff is a list comprising an integer and a string
1798 ;; (and maybe other stuff, which is ignored).
1800 (defun strip-lineinfo-op (maxima-op)
1801 (remove-if #'(lambda (x) (and (consp x
) (integerp (first x
)) (stringp (second x
)))) maxima-op
))