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 ((or (eql ch
*parse-stream-eof
*)
77 (return-from column some
))
82 (print-invert-case (stripdollar x
)))
84 (maybe-invert-string-case x
))
87 (case (and file $report_synerr_line
)
89 ;; print the file, line and column information
90 (let ((line+column
(line-number)))
91 (format t
"~&~a:~a:~a:" file
(car line
+column
) (cdr line
+column
))))
93 ;; if file=nil, then print a fresh line only; otherwise print
94 ;; file and character location
95 (format t
"~&~:[~;~:*~a:~a:~]" file fp
)))
96 (format t
(intl:gettext
"incorrect syntax: "))
97 (apply 'format t format-string
(mapcar #'printer l
))
98 (cond ((or $report_synerr_info
(eql *parse-stream
* *standard-input
*))
99 (let ((some (column)))
100 (format t
"~%~{~c~}~%~vt^" some
(max 0 (- (length some
) 2)))
101 (read-line *parse-stream
* nil nil
))))
104 (throw-macsyma-top))))
106 (defun tyi-parse-int (stream eof
)
108 (progn (setq *parse-window
* (make-list 25))
109 (setf (get '*parse-window
* 'length
) (length *parse-window
*))
110 (nconc *parse-window
* *parse-window
*)))
111 (let ((tem (tyi stream eof
)))
112 (setf (car *parse-window
*) tem
*parse-window
*
113 (cdr *parse-window
*))
114 (if (eql tem
#\newline
)
118 (defun *mread-prompt
* (out-stream char
)
119 (declare (ignore char
))
120 (format out-stream
"~&~A" *mread-prompt
*))
122 (defun aliaslookup (op)
124 (or (get op
'alias
) op
)
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;;;;; The Input Scanner ;;;;;
133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
135 (defun gobble-whitespace ()
136 (do ((ch (parse-tyipeek) (parse-tyipeek)))
137 ((not (member ch
*whitespace-chars
*)))
140 (defun read-command-token (obj)
142 (read-command-token-aux obj
))
144 (defun safe-assoc (item lis
)
145 "maclisp would not complain about (car 3), it gives nil"
148 (equal (car v
) item
))
152 ;; list contains an atom, only check
153 ;; (parser-assoc 1 '(2 1 3)) ==>(1 3)
154 ;; (parser-assoc 1 '(2 (1 4) 3)) ==>(1 4)
156 (defun parser-assoc (c lis
)
159 (cond ((consp (car v
))
165 ;; we need to be able to unparse-tyi an arbitrary number of
166 ;; characters, since if you do
167 ;; PREFIX("ABCDEFGH");
168 ;; then ABCDEFGA should read as a symbol.
169 ;; 99% of the time we don't have to unparse-tyi, and so there will
173 (let ((tem *parse-tyi
*))
175 (tyi-parse-int *parse-stream
* *parse-stream-eof
*))
177 (setq *parse-tyi
* nil
)
180 (setq *parse-tyi
* (cdr tem
))
183 ;; read one character but leave it there. so next parse-tyi gets it
184 (defun parse-tyipeek ()
185 (let ((tem *parse-tyi
*))
187 (setq *parse-tyi
* (tyi-parse-int *parse-stream
* *parse-stream-eof
*)))
191 ;; push characters back on the stream
192 (defun unparse-tyi (c)
193 (let ((tem *parse-tyi
*))
196 (setq *parse-tyi
* (cons c tem
)))))
198 ;;I know that the tradition says there should be no comments
199 ;;in tricky code in maxima. However the operator parsing
200 ;;gave me a bit of trouble. It was incorrect because
201 ;;it could not handle things produced by the extensions
202 ;;the following was broken for prefixes
204 (defun read-command-token-aux (obj)
207 (lis (if (eql ch
*parse-stream-eof
*)
209 (parser-assoc ch obj
))))
214 (cond ((atom (cadr lis
))
215 ;; INFIX("ABC"); puts into macsyma-operators
216 ;;something like: (#\A #\B #\C (ANS $abc))
217 ;; ordinary things are like:
218 ;; (#\< (ANS $<) (#\= (ANS $<=)))
219 ;; where if you fail at the #\< #\X
220 ;; stage, then the previous step was permitted.
221 (setq result
(read-command-token-aux (list (cdr lis
)))))
223 ;; lis something like (#\= (ANS $<=))
224 ;; and this says there are no longer operators
225 ;; starting with this.
227 (and (eql (car (cadr lis
)) 'ans
)
228 ;; When we have an operator, which starts with a
229 ;; literal, we check, if the operator is
230 ;; followed with a whitespace. With this code
231 ;; Maxima parses an expression grad x or grad(x)
232 ;; as (($grad) x) and gradef(x) as (($gradef) x),
233 ;; when grad is defined as a prefix operator.
234 ;; See bug report ID: 2970792.
235 (or (not (alphabetp (cadr (exploden (cadr (cadr lis
))))))
236 (member (parse-tyipeek) *whitespace-chars
*))
239 (let ((res (and (eql (car (cadr lis
)) 'ans
)
241 (com-token (read-command-token-aux (cddr lis
) )))
242 (setq result
(or com-token res
243 (read-command-token-aux (list (cadr lis
))))))))
244 (or result
(unparse-tyi ch
))
247 (defun scan-macsyma-token ()
248 ;; note that only $-ed tokens are GETALIASed.
249 (getalias (implode (cons '#\$
(scan-token t
)))))
251 (defun scan-lisp-token ()
252 (let ((charlist (scan-token nil
)))
255 (mread-synerr "Lisp symbol expected."))))
257 ;; Example: ?mismatch(x+y,x*z,?:from\-end,true); => 3
258 (defun scan-keyword-token ()
259 (let ((charlist (cdr (scan-token nil
))))
261 (let ((*package
* (find-package :keyword
)))
263 (mread-synerr "Lisp keyword expected."))))
265 (defun scan-token (flag)
266 (do ((c (parse-tyipeek) (parse-tyipeek))
268 ((or (eql c
*parse-stream-eof
*)
270 (not (or (digit-char-p c
(max 10 *read-base
*))
273 (nreverse (or l
(list (parse-tyi))))) ; Read at least one char ...
274 (when (char= (parse-tyi) #\\ )
275 (setq c
(parse-tyi)))
278 (defun scan-lisp-string () (scan-string))
279 (defun scan-macsyma-string () (scan-string))
281 (defun scan-string (&optional init
)
282 (let ((buf (make-array 50 :element-type
' #.
(array-element-type "a")
283 :fill-pointer
0 :adjustable t
)))
285 (vector-push-extend init buf
))
286 (do ((c (parse-tyipeek) (parse-tyipeek)))
287 ((cond ((eql c
*parse-stream-eof
*))
291 (if (char= (parse-tyi) #\\ )
292 (setq c
(parse-tyi)))
293 (vector-push-extend c buf
))))
295 (defun readlist (lis)
296 (read-from-string (coerce lis
'string
)))
298 ;; These variables control how we convert bfloat inputs to the
299 ;; internal bfloat representation. These variables should probably go
300 ;; away after some testing.
301 (defmvar $fast_bfloat_conversion t
302 "Use fast, but possibly inaccurate conversion")
303 (defmvar $fast_bfloat_threshold
100000.
304 "Exponents larger than this (in absolute value) will use the fast
305 conversion instead of the accurate conversion")
306 (defvar *fast-bfloat-extra-bits
* 0)
308 ;; Here is a test routine to test the fast bfloat conversion
310 (defun test-make-number (&optional
(n 1000))
313 (flet ((digit-list (n)
314 (coerce (format nil
"~D" n
) 'list
)))
316 ;; Generate a random number with 30 fraction digits and an
318 (push (digit-list (random 10)) numlist
)
319 (push '(#\.
) numlist
)
320 (push (digit-list (random (expt 10 30))) numlist
)
321 (push '(#\B
) numlist
)
322 (push (if (zerop (random 2)) '(#\
+) '(#\-
)) numlist
)
323 (push (digit-list (+ $fast_bfloat_threshold
324 (random $fast_bfloat_threshold
)))
326 ;; Convert using accurate and fast methods and compare the
328 (let ((true (let (($fast_bfloat_conversion nil
))
329 (make-number (copy-list numlist
))))
330 (fast (let (($fast_bfloat_conversion t
))
331 (make-number (copy-list numlist
)))))
332 (format t
"Test ~3A: " k
)
333 (map nil
#'(lambda (x)
338 (unless (equalp true fast
)
340 (format t
"NUM: ~A~% TRUE: ~S~% FAST: ~S~%"
341 (reverse numlist
) true fast
))))))
342 (format t
"~D failures in ~D tests (~F%)~%"
343 failures n
(* 100 failures
(/ (float n
))))))
346 ;; WARNING: MAKE-NUMBER destructively modifies it argument! Should we
348 (defun make-number (data)
349 (setq data
(nreverse data
))
350 ;; Maxima really wants to read in any number as a flonum
351 ;; (except when we have a bigfloat, of course!). So convert exponent
352 ;; markers to the flonum-exponent-marker.
353 (let ((marker (car (nth 3 data
))))
354 (unless (eql marker flonum-exponent-marker
)
355 (when (member marker
'(#\E
#\F
#\S
#\D
#\L
#+cmu
#\W
))
356 (setf (nth 3 data
) (list flonum-exponent-marker
)))))
357 (if (not (equal (nth 3 data
) '(#\B
)))
358 (readlist (apply #'append data
))
361 (int-part (readlist (or (first data
) '(#\
0))))
362 (frac-part (readlist (or (third data
) '(#\
0))))
363 (frac-len (length (third data
)))
364 (exp-sign (first (fifth data
)))
365 (exp (readlist (sixth data
))))
366 (if (and $fast_bfloat_conversion
367 (> (abs exp
) $fast_bfloat_threshold
))
368 ;; Exponent is large enough that we don't want to do exact
369 ;; rational arithmetic. Instead we do bfloat arithmetic.
370 ;; For example, 1.234b1000 is converted by computing
371 ;; bfloat(1234)*10b0^(1000-3). Extra precision is used
372 ;; during the bfloat computations.
373 (let* ((extra-prec (+ *fast-bfloat-extra-bits
* (ceiling (log exp
2e0
))))
374 (fpprec (+ fpprec extra-prec
))
375 (mant (+ (* int-part
(expt 10 frac-len
)) frac-part
))
376 (bf-mant (bcons (intofp mant
)))
377 (p (power (bcons (intofp 10))
378 (- (if (char= exp-sign
#\-
)
382 ;; Compute the product using extra precision. This
383 ;; helps to get the last bit correct (but not
384 ;; always). If we didn't do this, then bf-mant and
385 ;; p would be rounded to the target precision and
386 ;; then the product is rounded again. Doing it
387 ;; this way, we still have 3 roundings, but bf-mant
388 ;; and p aren't rounded too soon.
389 (result (mul bf-mant p
)))
390 (let ((fpprec (- fpprec extra-prec
)))
391 ;; Now round the product back to the desired precision.
393 ;; For bigfloats, turn them into rational numbers then
394 ;; convert to bigfloat. Fix for the 0.25b0 # 2.5b-1 bug.
395 ;; Richard J. Fateman posted this fix to the Maxima list
396 ;; on 10 October 2005. Without this fix, some tests in
397 ;; rtestrationalize will fail. Used with permission.
398 (let ((ratio (* (+ int-part
(* frac-part
(expt 10 (- frac-len
))))
399 (expt 10 (if (char= exp-sign
#\-
)
402 ($bfloat
(cl-rat-to-maxima ratio
)))))))
404 ;; Richard J. Fateman wrote the big float to rational code and the function
407 (defun cl-rat-to-maxima (x)
410 (list '(rat simp
) (numerator x
) (denominator x
))))
412 (defun scan-digits (data continuation? continuation
&optional exponent-p
)
413 (do ((c (parse-tyipeek) (parse-tyipeek))
415 ((not (and (characterp c
) (digit-char-p c
(max 10.
*read-base
*))))
416 (cond ((member c continuation?
)
417 (funcall continuation
(list* (ncons (char-upcase
421 ((and (null l
) exponent-p
)
422 ;; We're trying to parse the exponent part of a number,
423 ;; and we didn't get a value after the exponent marker.
425 (mread-synerr "parser: incomplete number; missing exponent?"))
427 (make-number (cons (nreverse l
) data
)))))
430 (defun scan-number-after-dot (data)
431 (scan-digits data
'(#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
) #'scan-number-exponent
))
433 (defun scan-number-exponent (data)
434 (push (ncons (if (or (char= (parse-tyipeek) #\
+)
435 (char= (parse-tyipeek) #\-
))
439 (scan-digits data
() () t
))
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
443 ;;;;; The Expression Parser ;;;;;
445 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449 ;;; Based on a theory of parsing presented in: ;;;
451 ;;; Pratt, Vaughan R., ``Top Down Operator Precedence,'' ;;;
452 ;;; ACM Symposium on Principles of Programming Languages ;;;
453 ;;; Boston, MA; October, 1973. ;;;
455 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457 ;;; Implementation Notes ....
459 ;;; JPG Chars like ^A, ^B, ... get left around after interrupts and
460 ;;; should be thrown away by the scanner if not used as editting
463 ;;; KMP There is RBP stuff in DISPLA, too. Probably this sort of
464 ;;; data should all be in one place somewhere.
466 ;;; KMP Maybe the parser and/or scanner could use their own GC scheme
467 ;;; to recycle conses used in scan/parse from line to line which
468 ;;; really ought not be getting dynamically discarded and reconsed.
469 ;;; Alternatively, we could call RECLAIM explicitly on certain
470 ;;; pieces of structure which get used over and over. A
471 ;;; local-reclaim abstraction may want to be developed since this
472 ;;; stuff will always be needed, really. On small-address-space
473 ;;; machines, this could be overridden when the last DYNAMALLOC
474 ;;; GC barrier were passed (indicating that space was at a premium
475 ;;; -- in such case, real RECLAIM would be more economical -- or
476 ;;; would the code to control that be larger than the area locked
479 ;;; KMP GJC has a MAKE-EVALUATOR type package which could probably
480 ;;; replace the CALL-IF-POSSIBLE stuff used here.
481 ;;; [So it was written, so it was done. -gjc]
483 ;;; KMP DEFINE-SYMBOL and KILL-OPERATOR need to be redefined.
484 ;;; Probably these shouldn't be defined in this file anyway.
486 ;;; KMP The relationship of thisfile to SYNEX needs to be thought
487 ;;; out more carefully.
489 ;;; GJC Need macros for declaring INFIX, PREFIX, etc ops
491 ;;; GJC You know, PARSE-NARY isn't really needed it seems, since
492 ;;; the SIMPLIFIER makes the conversion of
493 ;;; ((MTIMES) ((MTIMES) A B) C) => ((MTIMES) A B C)
494 ;;; I bet you could get make "*" infix and nobody would
497 ;;; The following terms may be useful in deciphering this code:
499 ;;; NUD -- NUll left Denotation (op has nothing to its left (prefix))
500 ;;; LED -- LEft Denotation (op has something to left (postfix or infix))
502 ;;; LBP -- Left Binding Power (the stickiness to the left)
503 ;;; RBP -- Right Binding Power (the stickiness to the right)
508 (defvar scan-buffered-token
(list nil
)
509 "put-back buffer for scanner, a state-variable of the reader")
511 (defun peek-one-token ()
512 (peek-one-token-g nil nil
))
514 (defun peek-one-token-g (eof-ok? eof-obj
)
516 ((car scan-buffered-token
)
517 (cdr scan-buffered-token
))
518 (t (rplacd scan-buffered-token
(scan-one-token-g eof-ok? eof-obj
))
519 (cdr (rplaca scan-buffered-token t
)))))
521 (defun scan-one-token ()
522 (scan-one-token-g nil nil
))
524 (defun scan-one-token-g (eof-ok? eof-obj
)
525 (declare (special macsyma-operators
))
526 (cond ((car scan-buffered-token
)
527 (rplaca scan-buffered-token
())
528 (cdr scan-buffered-token
))
529 ((read-command-token macsyma-operators
))
531 (let ((test (parse-tyipeek)))
532 (cond ((eql test
*parse-stream-eof
*)
535 (maxima-error (intl:gettext
"parser: end of file while scanning expression."))))
538 (cond ((char= (parse-tyipeek) #\
*)
541 (scan-one-token-g eof-ok? eof-obj
))
543 ((eql test
#\.
) (parse-tyi) ; Read the dot
544 (if (digit-char-p (parse-tyipeek) 10.
)
545 (scan-number-after-dot (list (ncons #\.
) nil
))
549 (scan-macsyma-string))
552 (cond ((char= (parse-tyipeek) #\")
555 ((char= (parse-tyipeek) #\
:)
556 (scan-keyword-token))
560 (if (digit-char-p test
10.
)
561 (scan-number-before-dot ())
562 (scan-macsyma-token))))))))
564 ;; nested comments are permitted.
565 (defun gobble-comment ()
569 (setq c
(parse-tyipeek))
571 (cond ((= depth
0) (return t
)))
572 (cond ((eql c
*parse-stream-eof
*)
573 (error (intl:gettext
"parser: end of file in comment.")))
575 (cond ((char= (parse-tyipeek) #\
/)
578 (cond ((= depth
0) (return t
)))
581 (cond ((char= (parse-tyipeek) #\
*)
582 (incf depth
) (parse-tyi)
587 (defun scan-number-rest (data)
588 (let ((c (caar data
)))
591 (scan-number-after-dot data
))
592 ((member c
'(#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
))
593 ;; Dot missing but found exponent marker. Fake it.
594 (setf data
(push (ncons #\.
) (rest data
)))
595 (push (ncons #\
0) data
)
596 (push (ncons c
) data
)
597 (scan-number-exponent data
)))))
599 (defun scan-number-before-dot (data)
600 (scan-digits data
'(#\.
#\E
#\e
#\F
#\f #\B
#\b #\D
#\d
#\S
#\s
#\L
#\l
#+cmu
#\W
#+cmu
#\w
)
604 ;; "First character" and "Pop character"
606 (defmacro first-c
() '(peek-one-token))
607 (defmacro pop-c
() '(scan-one-token))
609 (defun mstringp (x) (stringp x
)) ;; OBSOLETE. PRESERVE FOR SAKE OF POSSIBLE CALLS FROM NON-MAXIMA CODE !!
611 (defun inherit-propl (op-to op-from getl
)
612 (let ((propl (getl op-from getl
)))
614 (progn (remprop op-to
(car propl
))
615 (putprop op-to
(cadr propl
) (car propl
)))
617 (maxima-error "has no ~a properties. ~a ~a" getl op-from
'wrng-type-arg
)
622 ;;; (LED <op> <left>)
624 ;;; <op> is the name of the operator which was just popped.
625 ;;; <left> is the stuff to the left of the operator in the LED case.
629 #+gcl
(eval compile load
)
630 #-gcl
(:execute
:compile-toplevel
:load-toplevel
)
631 (defmacro def-nud-equiv
(op equiv
)
632 (list 'putprop
(list 'quote op
) (list 'function equiv
)
635 (defmacro nud-propl
() ''(nud))
637 (defmacro def-nud-fun
(op-name op-l . body
)
638 (list* 'defun-prop
(list* op-name
'nud
'nil
) op-l body
))
640 (defmacro def-led-equiv
(op equiv
)
641 (list 'putprop
(list 'quote op
) (list 'function equiv
)
644 (defmacro led-propl
() ''(led))
646 (defmacro def-led-fun
(op-name op-l . body
)
647 (list* 'defun-prop
(list* op-name
'led
'nil
) op-l body
)))
650 (let ((tem (and (symbolp op
) (getl op
'(nud)))) res
)
654 (mread-synerr "~A is not a prefix operator" (mopstrip op
))
656 (funcall (cadr tem
) op
)))
659 (defun led-call (op l
)
660 (let ((tem (and (symbolp op
) (getl op
'(led)))) res
)
663 (mread-synerr "~A is not an infix operator" (mopstrip op
))
664 (funcall (cadr tem
) op l
)))
667 ;;; (DEF-NUD (op lbp rbp) bvl . body)
669 ;;; Defines a procedure for parsing OP as a prefix operator.
671 ;;; OP should be the name of the symbol as a string or symbol.
672 ;;; LBP is an optional left binding power for the operator.
673 ;;; RBP is an optional right binding power for the operator.
674 ;;; BVL must contain exactly one variable, which the compiler will not
675 ;;; complain about if unused, since it will rarely be of use anyway.
676 ;;; It will get bound to the operator being parsed.
677 ;;; lispm:Optional args not allowed in release 5 allowed, necessary afterwards..
679 (defmacro def-nud
((op . lbp-rbp
) bvl . body
)
680 (let (( lbp
(nth 0 lbp-rbp
))
681 ( rbp
(nth 1 lbp-rbp
)))
682 `(progn ,(make-parser-fun-def op
'nud bvl body
)
683 (set-lbp-and-rbp ',op
',lbp
',rbp
))))
685 (defun set-lbp-and-rbp (op lbp rbp
)
686 (cond ((not (consp op
))
687 (let ((existing-lbp (get op
'lbp
))
688 (existing-rbp (get op
'rbp
)))
689 (cond ((not lbp
) ;; ignore omitted arg
692 (putprop op lbp
'lbp
))
693 ((not (equal existing-lbp lbp
))
694 (maxima-error "Incompatible LBP's defined for this operator ~a" op
)))
695 (cond ((not rbp
) ;; ignore omitted arg
698 (putprop op rbp
'rbp
))
699 ((not (equal existing-rbp rbp
))
700 (maxima-error "Incompatible RBP's defined for this operator ~a" op
)))))
702 (mapcar #'(lambda (x) (set-lbp-and-rbp x lbp rbp
))
705 ;;; (DEF-LED (op lbp rbp) bvl . body)
707 ;;; Defines a procedure for parsing OP as an infix or postfix operator.
709 ;;; OP should be the name of the symbol as a string or symbol.
710 ;;; LBP is an optional left binding power for the operator.
711 ;;; RBP is an optional right binding power for the operator.
712 ;;; BVL must contain exactly two variables, the first of which the compiler
713 ;;; will not complain about if unused, since it will rarely be of use
714 ;;; anyway. Arg1 will get bound to the operator being parsed. Arg2 will
715 ;;; get bound to the parsed structure which was to the left of Arg1.
718 (defmacro def-led
((op . lbp-rbp
) bvl . body
)
719 (let (( lbp
(nth 0 lbp-rbp
))
720 ( rbp
(nth 1 lbp-rbp
)))
721 `(progn ,(make-parser-fun-def op
'led bvl body
)
722 (set-lbp-and-rbp ',op
',lbp
',rbp
))))
724 (defmacro def-collisions
(op &rest alist
)
725 (let ((keys (do ((i 1 (ash i
1))
726 (lis alist
(cdr lis
))
727 (nl () (cons (cons (caar lis
) i
) nl
)))
730 (defprop ,op
,(let nil
731 (copy-tree keys
)) keys
)
732 ,@(mapcar #'(lambda (data)
733 `(defprop ,(car data
)
734 ,(do ((i 0 (logior i
(cdr (assoc (car lis
) keys
:test
#'eq
))))
735 (lis (cdr data
) (cdr lis
)))
741 (defun collision-lookup (op active-bitmask key-bitmask
)
742 (let ((result (logand active-bitmask key-bitmask
)))
743 (if (not (zerop result
))
744 (do ((l (get op
'keys
) (cdr l
)))
745 ((null l
) (parse-bug-err 'collision-check
))
746 (if (not (zerop (logand result
(cdar l
))))
747 (return (caar l
)))))))
749 (defun collision-check (op active-bitmask key
)
750 (let ((key-bitmask (get key op
)))
751 (if (not key-bitmask
)
752 (mread-synerr "~A is an unknown keyword in a ~A statement."
753 (mopstrip key
) (mopstrip op
)))
754 (let ((collision (collision-lookup op active-bitmask key-bitmask
)))
756 (if (eq collision key
)
757 (mread-synerr "This ~A's ~A slot is already filled."
760 (mread-synerr "A ~A cannot have a ~A with a ~A field."
763 (mopstrip collision
))))
764 (logior (cdr (assoc key
(get op
'keys
) :test
#'eq
)) active-bitmask
))))
766 ;;;; Data abstraction
768 ;;; LBP = Left Binding Power
770 ;;; (LBP <op>) - reads an operator's Left Binding Power
771 ;;; (DEF-LBP <op> <val>) - defines an operator's Left Binding Power
773 (defun lbp (lex) (cond ((safe-get lex
'lbp
)) (t 200.
)))
775 (defmacro def-lbp
(sym val
) `(defprop ,sym
,val lbp
))
777 ;;; RBP = Right Binding Power
779 ;;; (RBP <op>) - reads an operator's Right Binding Power
780 ;;; (DEF-RBP <op> <val>) - defines an operator's Right Binding Power
782 (defun rbp (lex) (cond ((safe-get lex
'rbp
)) (t 200.
)))
784 (defmacro def-rbp
(sym val
) `(defprop ,sym
,val rbp
))
786 (defmacro def-match
(x m
) `(defprop ,x
,m match
))
788 ;;; POS = Part of Speech!
795 (defun lpos (op) (cond ((safe-get op
'lpos
)) (t '$any
)))
796 (defun rpos (op) (cond ((safe-get op
'rpos
)) (t '$any
)))
797 (defun pos (op) (cond ((safe-get op
'pos
)) (t '$any
)))
799 (defmacro def-pos
(op pos
) `(defprop ,op
,pos pos
))
800 (defmacro def-rpos
(op pos
) `(defprop ,op
,pos rpos
))
801 (defmacro def-lpos
(op pos
) `(defprop ,op
,pos lpos
))
805 (defun mheader (op) (add-lineinfo (or (safe-get op
'mheader
) (ncons op
))))
807 (defmacro def-mheader
(op header
) `(defprop ,op
,header mheader
))
810 (defmvar $parsewindow
10.
811 "The maximum number of 'lexical tokens' that are printed out on
812 each side of the error-point when a syntax (parsing) MAXIMA-ERROR occurs. This
813 option is especially useful on slow terminals. Setting it to -1 causes the
814 entire input string to be printed out when an MAXIMA-ERROR occurs."
818 ;;;; Misplaced definitions
820 (defmacro def-operatorp
()
821 `(defun operatorp (lex)
822 (and (symbolp lex
) (getl lex
'(,@(nud-propl) ,@(led-propl))))))
826 (defmacro def-operatorp1
()
827 ;Defmfun -- used by SYNEX if not others.
828 `(defun operatorp1 (lex)
829 ;; Referenced outside of package: OP-SETUP, DECLARE1
830 ;; Use for truth value only, not for return-value.
831 (and (symbolp lex
) (getl lex
'(lbp rbp
,@(nud-propl) ,@(led-propl))))))
835 ;;;; The Macsyma Parser
837 ;;; (MREAD) with arguments compatible with losing maclisp READ style.
839 ;;; Returns a parsed form of tokens read from stream.
841 ;;; If you want rubout processing, be sure to call some stream which knows
842 ;;; about such things. Also, I'm figuring that the PROMPT will be
843 ;;; an attribute of the stream which somebody can hack before calling
844 ;;; MREAD if he wants to.
847 ;;Important for lispm rubout handler
848 (defun mread (&rest read-args
)
851 (and *parse-window
* (setf (car *parse-window
*) nil
852 *parse-window
* (cdr *parse-window
*)))
853 (princ *mread-prompt
*)
855 (apply 'mread-raw read-args
)))
857 (defun mread-prompter (stream char
)
858 (declare (special *mread-prompt-internal
*)
861 (princ *mread-prompt-internal
* stream
))
863 ;; input can look like:
866 (defun mread-raw (*parse-stream
* &optional
*mread-eof-obj
*)
867 (let ((scan-buffered-token (list nil
))
869 (if (eq scan-buffered-token
;; a handly unique object for the EQ test.
870 (peek-one-token-g t scan-buffered-token
))
873 (input (parse '$any
0.
) (parse '$any
0.
)))
877 ;force a separate line info structure
878 (setf *current-line-info
* nil
)
879 (return (list (mheader (pop-c))
880 (if labels
(cons (mheader '|$
[|
) (nreverse labels
)))
886 (mread-synerr "Invalid && tag. Tag must be a symbol")))
888 (parse-bug-err 'mread-raw
)))))))
890 ;;; (PARSE <mode> <rbp>)
892 ;;; This will parse an expression containing operators which have a higher
893 ;;; left binding power than <rbp>, returning as soon as an operator of
894 ;;; lesser or equal binding power is seen. The result will be in the given
895 ;;; mode (which allows some control over the class of result expected).
896 ;;; Modes used are as follows:
897 ;;; $ANY = Match any type of expression
898 ;;; $CLAUSE = Match only boolean expressions (or $ANY)
899 ;;; $EXPR = Match only mathematical expressions (or $ANY)
900 ;;; If a mismatched mode occurs, a syntax error will be flagged. Eg,
901 ;;; this is why "X^A*B" parses but "X^A and B" does not. X^A is a $EXPR
902 ;;; and not coercible to a $CLAUSE. See CONVERT.
904 ;;; <mode> is the required mode of the result.
905 ;;; <rbp> is the right binding power to use for the parse. When an
906 ;;; LED-type operator is seen with a lower left binding power
907 ;;; than <rbp>, this parse returns what it's seen so far rather
908 ;;; than calling that operator.
911 (defun parse (mode rbp
)
912 (do ((left (nud-call (pop-c)) ; Envoke the null left denotation
913 (led-call (pop-c) left
))) ; and keep calling LED ops as needed
914 ((>= rbp
(lbp (first-c))) ; Until next op lbp too low
915 (convert left mode
)))) ; in which case, return stuff seen
917 ;;; (PARSE-PREFIX <op>)
919 ;;; Parses prefix forms -- eg, -X or NOT FOO.
921 ;;; This should be the NUD property on an operator. It fires after <op>
922 ;;; has been seen. It parses forward looking for one more expression
923 ;;; according to its right binding power, returning
924 ;;; ( <mode> . ((<op>) <arg1>) )
926 (defun parse-prefix (op)
927 (list (pos op
) ; Operator mode
928 (mheader op
) ; Standard Macsyma expression header
929 (parse (rpos op
) (rbp op
)))) ; Convert single argument for use
931 ;;; (PARSE-POSTFIX <op> <left>)
933 ;;; Parses postfix forms. eg, X!.
935 ;;; This should be the LED property of an operator. It fires after <left>
936 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
937 ;;; ( <mode> . ((<op>) <arg1>) )
939 (defun parse-postfix (op l
)
940 (list (pos op
) ; Operator's mode
941 (mheader op
) ; Standard Macsyma expression header
942 (convert l
(lpos op
)))) ; Convert single argument for use
944 ;;; (PARSE-INFIX <op> <left>)
946 ;;; Parses infix (non-nary) forms. eg, 5 mod 3.
948 ;;; This should be the led property of an operator. It fires after <left>
949 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
950 ;;; ( <mode> . ((<op>) <arg1> <arg2>) )
952 (defun parse-infix (op l
)
953 (list (pos op
) ; Operator's mode
954 (mheader op
) ; Standard Macsyma expression header
955 (convert l
(lpos op
)) ; Convert arg1 for immediate use
956 (parse (rpos op
) (rbp op
)))) ; Look for an arg2
958 ;;; (PARSE-NARY <op> <left>)
960 ;;; Parses nary forms. Eg, form1*form2*... or form1+form2+...
961 ;;; This should be the LED property on an operator. It fires after <op>
962 ;;; has been seen, accumulating and returning
963 ;;; ( <mode> . ((<op>) <arg1> <arg2> ...) )
965 ;;; <op> is the being parsed.
966 ;;; <left> is the stuff that has been seen to the left of <op> which
967 ;;; rightly belongs to <op> on the basis of parse precedence rules.
969 (defun parse-nary (op l
)
970 (list* (pos op
) ; Operator's mode
971 (mheader op
) ; Normal Macsyma operator header
972 (convert l
(lpos op
)) ; Check type-match of arg1
973 (prsnary op
(lpos op
) (lbp op
)))) ; Search for other args
975 ;;; (PARSE-MATCHFIX <lop>)
977 ;;; Parses matchfix forms. eg, [form1,form2,...] or (form1,form2,...)
979 ;;; This should be the NUD property on an operator. It fires after <op>
980 ;;; has been seen. It parses <lop><form1>,<form2>,...<rop> returning
981 ;;; ( <mode> . ((<lop>) <form1> <form2> ...) ).
983 (defun parse-matchfix (op)
984 (list* (pos op
) ; Operator's mode
985 (mheader op
) ; Normal Macsyma operator header
986 (prsmatch (safe-get op
'match
) (lpos op
)))) ; Search for matchfixed forms
988 ;;; (PARSE-NOFIX <op>)
990 ;;; Parses an operator of no args. eg, @+X where @ designates a function
991 ;;; call (eg, @() is implicitly stated by the lone symbol @.)
993 ;;; This should be a NUD property on an operator which takes no args.
994 ;;; It immediately returns ( <mode> . ((<op>)) ).
996 ;;; <op> is the name of the operator.
998 ;;; Note: This is not used by default and probably shouldn't be used by
999 ;;; someone who doesn't know what he's doing. Example lossage. If @ is
1000 ;;; a nofix op, then @(3,4) parses, but parses as "@"()(3,4) would -- ie,
1001 ;;; to ((MQAPPLY) (($@)) 3 4) which is perhaps not what the user will expect.
1003 (defun parse-nofix (op) (list (pos op
) (mheader op
)))
1005 ;;; (PRSNARY <op> <mode> <rbp>)
1007 ;;; Parses an nary operator tail Eg, ...form2+form3+... or ...form2*form3*...
1009 ;;; Expects to be entered after the leading form and the first call to an
1010 ;;; nary operator has been seen and popped. Returns a list of parsed forms
1011 ;;; which belong to that operator. Eg, for X+Y+Z; this should be called
1012 ;;; after the first + is popped. Returns (Y Z) and leaves the ; token
1013 ;;; in the parser scan buffer.
1015 ;;; <op> is the nary operator in question.
1016 ;;; <rbp> is (LBP <op>) and is provided for efficiency. It is for use in
1017 ;;; recursive parses as a binding power to parse for.
1018 ;;; <mode> is the name of the mode that each form must be.
1020 (defun prsnary (op mode rbp
)
1021 (do ((nl (list (parse mode rbp
)) ; Get at least one form
1022 (cons (parse mode rbp
) nl
))) ; and keep getting forms
1023 ((not (eq op
(first-c))) ; until a parse pops on a new op
1024 (nreverse nl
)) ; at which time return forms
1025 (pop-c))) ; otherwise pop op
1027 ;;; (PRSMATCH <match> <mode>)
1029 ;;; Parses a matchfix sequence. Eg, [form1,form2,...] or (form1,form2,...)
1030 ;;; Expects to be entered after the leading token is the popped (ie, at the
1031 ;;; point where the parse of form1 will begin). Returns (form1 form2 ...).
1033 ;;; <match> is the token to look for as a matchfix character.
1034 ;;; <mode> is the name of the mode that each form must be.
1036 (defun prsmatch (match mode
) ; Parse for matchfix char
1037 (cond ((eq match
(first-c)) (pop-c) nil
) ; If immediate match, ()
1039 (do ((nl (list (parse mode
10.
)) ; Get first element
1040 (cons (parse mode
10.
) nl
))) ; and Keep adding elements
1041 ((eq match
(first-c)) ; Until we hit the match.
1042 (pop-c) ; Throw away match.
1043 (nreverse nl
)) ; Put result back in order
1044 (if (eq '|$
,|
(first-c)) ; If not end, look for ","
1045 (pop-c) ; and pop it if it's there
1046 (mread-synerr "Missing ~A" ; or give an error message.
1047 (mopstrip match
)))))))
1049 ;;; (CONVERT <exp> <mode>)
1051 ;;; Parser coercion function.
1053 ;;; <exp> should have the form ( <expressionmode> . <expression> )
1054 ;;; <mode> is the target mode.
1056 ;;; If <expressionmode> and <mode> are compatible, returns <expression>.
1058 (defun convert (item mode
)
1059 (if (or (eq mode
(car item
)) ; If modes match exactly
1060 (eq '$any mode
) ; or target is $ANY
1061 (eq '$any
(car item
))) ; or input is $ANY
1062 (cdr item
) ; then return expression
1063 (mread-synerr "Found ~A expression where ~A expression expected"
1064 (get (car item
) 'english
)
1065 (get mode
'english
))))
1067 (defprop $any
"untyped" english
)
1068 (defprop $clause
"logical" english
)
1069 (defprop $expr
"algebraic" english
)
1071 ;;;; Parser Error Diagnostics
1073 ;; Call this for random user-generated parse errors
1075 (defun parse-err () (mread-synerr "Syntax error"))
1077 ;; Call this for random internal parser lossage (eg, code that shouldn't
1080 (defun parse-bug-err (op)
1082 "Parser bug in ~A. Please report this to the Maxima maintainers,~
1083 ~%including the characters you just typed which caused the error. Thanks."
1086 ;;; Random shared error messages
1088 (defun delim-err (op)
1089 (mread-synerr "Illegal use of delimiter ~A" (mopstrip op
)))
1091 (defun erb-err (op l
) l
;Ignored
1092 (mread-synerr "Too many ~A's" (mopstrip op
)))
1094 (defun premterm-err (op)
1095 (mread-synerr "Premature termination of input at ~A."
1098 ;;;; Operator Specific Data
1100 (def-nud-equiv |$
]| delim-err
)
1101 (def-led-equiv |$
]| erb-err
)
1104 (def-nud-equiv |$
[| parse-matchfix
)
1105 (def-match |$
[| |$
]|
)
1108 (def-mheader |$
[|
(mlist))
1110 (def-lpos |$
[| $any
)
1113 (def-led (|$
[|
200.
) (op left
)
1114 (setq left
(convert left
'$any
))
1115 (if (numberp left
) (parse-err)) ; number[...] invalid
1116 (let ((header (if (atom left
)
1117 (add-lineinfo (list (amperchk left
) 'array
))
1118 (add-lineinfo '(mqapply array
))))
1119 (right (prsmatch '|$
]|
'$any
))) ; get sublist in RIGHT
1120 (cond ((null right
) ; 1 subscript minimum
1121 (mread-synerr "No subscripts given"))
1122 ((atom left
) ; atom[...]
1123 (setq right
(cons header
1125 (cons '$any
(aliaslookup right
)))
1127 (cons '$any
(cons header
1128 (cons left right
)))))))
1131 (def-nud-equiv |$
)| delim-err
)
1132 (def-led-equiv |$
)| erb-err
)
1135 (def-mheader |$
(|
(mprogn))
1137 ;; KMP: This function optimizes out (exp) into just exp.
1138 ;; This is useful for mathy expressions, but obnoxious for non-mathy
1139 ;; expressions. I think DISPLA should be made smart about such things,
1140 ;; but probably the (...) should be carried around in the internal
1141 ;; representation. This would make things like BUILDQ much easier to
1143 ;; GJC: CGOL has the same behavior, so users tend to write extensions
1144 ;; to the parser rather than write Macros per se. The transformation
1145 ;; "(EXP)" ==> "EXP" is done by the evaluator anyway, the problem
1146 ;; comes inside quoted expressions. There are many other problems with
1147 ;; the "QUOTE" concept however.
1149 (def-nud (|$
(|
200.
) (op)
1150 (let ((right)(hdr (mheader '|$
(|
))) ; make mheader first for lineinfo
1151 (cond ((eq '|$
)|
(first-c)) (parse-err)) ; () is illegal
1152 ((or (null (setq right
(prsmatch '|$
)|
'$any
))) ; No args to MPROGN??
1153 (cdr right
)) ; More than one arg.
1154 (when (suspicious-mprogn-p right
)
1155 (mtell (intl:gettext
"warning: parser: I'll let it stand, but (...) doesn't recognize local variables.~%"))
1156 (mtell (intl:gettext
"warning: parser: did you mean to say: block(~M, ...) ?~%") (car right
)))
1157 (cons '$any
(cons hdr right
))) ; Return an MPROGN
1158 (t (cons '$any
(car right
)))))) ; Optimize out MPROGN
1160 (defun suspicious-mprogn-p (right)
1161 ;; Look for a Maxima list of symbols or assignments to symbols.
1162 (and ($listp
(car right
))
1163 (every #'(lambda (e) (or (symbolp e
)
1164 (and (consp e
) (eq (caar e
) 'msetq
) (symbolp (second e
)))))
1165 (rest (car right
)))))
1167 (def-led (|$
(|
200.
) (op left
)
1168 (setq left
(convert left
'$any
)) ;De-reference LEFT
1169 (if (numberp left
) (parse-err)) ;number(...) illegal
1170 (let ((hdr (and (atom left
)(mheader (amperchk left
))))
1171 (r (prsmatch '|$
)|
'$any
)) ;Get arglist in R
1173 (cons '$any
;Result is type $ANY
1174 (cond ((atom left
) ;If atom(...) =>
1175 (cons hdr r
)) ;(($atom) exp . args)
1176 (t ;Else exp(...) =>
1177 (cons '(mqapply) (cons left r
))))))) ;((MQAPPLY) op . args)
1179 (def-mheader |$
'|
(mquote))
1181 (def-nud (|$
'|
) (op)
1183 (cond ((eq '|$
(|
(first-c))
1184 (list '$any
(mheader '|$
'|
) (parse '$any
190.
)))
1185 ((or (atom (setq right
(parse '$any
190.
)))
1186 (member (caar right
) '(mquote mlist $set mprog mprogn lambda
) :test
#'eq
))
1187 (list '$any
(mheader '|$
'|
) right
))
1188 ((eq 'mqapply
(caar right
))
1189 (cond ((eq (caaadr right
) 'lambda
)
1190 (list '$any
(mheader '|$
'|
) right
))
1191 (t (rplaca (cdr right
)
1192 (cons (cons ($nounify
(caaadr right
))
1195 (cons '$any right
))))
1196 (t (cons '$any
(cons (cons ($nounify
(caar right
)) (cdar right
))
1199 (def-nud (|$
''|
) (op)
1202 (cond ((eq '|$
(|
(first-c)) (meval (parse '$any
190.
)))
1203 ((atom (setq right
(parse '$any
190.
))) (meval1 right
))
1204 ((eq 'mqapply
(caar right
))
1206 (cons (cons ($verbify
(caaadr right
)) (cdaadr right
))
1209 (t (cons (cons ($verbify
(caar right
)) (cdar right
))
1212 (def-led-equiv |$
:| parse-infix
)
1216 (def-rpos |$
:| $any
)
1217 (def-lpos |$
:| $any
)
1218 (def-mheader |$
:|
(msetq))
1220 (def-led-equiv |$
::| parse-infix
)
1221 (def-lbp |$
::|
180.
)
1223 (def-pos |$
::| $any
)
1224 (def-rpos |$
::| $any
)
1225 (def-lpos |$
::| $any
)
1226 (def-mheader |$
::|
(mset))
1228 (def-led-equiv |$
:=| parse-infix
)
1229 (def-lbp |$
:=|
180.
)
1231 (def-pos |$
:=| $any
)
1232 (def-rpos |$
:=| $any
)
1233 (def-lpos |$
:=| $any
)
1234 (def-mheader |$
:=|
(mdefine))
1236 (def-led-equiv |$
::=| parse-infix
)
1237 (def-lbp |$
::=|
180.
)
1238 (def-rbp |$
::=|
20.
)
1239 (def-pos |$
::=| $any
)
1240 (def-rpos |$
::=| $any
)
1241 (def-lpos |$
::=| $any
)
1242 (def-mheader |$
::=|
(mdefmacro))
1244 (def-led-equiv |$
!| parse-postfix
)
1247 (def-pos |$
!| $expr
)
1248 (def-lpos |$
!| $expr
)
1250 (def-mheader |$
!|
(mfactorial))
1252 (def-mheader |$
!!|
($genfact
))
1254 (def-led (|$
!!|
160.
) (op left
)
1257 (convert left
'$expr
)
1258 (list (mheader '$
/) (convert left
'$expr
) 2)
1263 (def-pos |$^| $expr
)
1264 (def-lpos |$^| $expr
)
1265 (def-rpos |$^| $expr
)
1266 (def-mheader |$^|
(mexpt))
1268 (def-led ((|$^| |$^^|
)) (op left
)
1270 (aliaslookup (list (mheader op
)
1271 (convert left
(lpos op
))
1272 (parse (rpos op
) (rbp op
))))))
1274 (mapc #'(lambda (prop) ; Make $** like $^
1275 (let ((propval (get '$^ prop
)))
1276 (if propval
(putprop '$
** propval prop
))))
1277 '(lbp rbp pos rpos lpos mheader
))
1279 (inherit-propl '$
** '$^
(led-propl))
1281 (def-lbp |$^^|
140.
)
1282 (def-rbp |$^^|
139.
)
1283 (def-pos |$^^| $expr
)
1284 (def-lpos |$^^| $expr
)
1285 (def-rpos |$^^| $expr
)
1286 (def-mheader |$^^|
(mncexpt))
1288 ;; note y^^4.z gives an error because it scans the number 4 together with
1289 ;; the trailing '.' as a decimal place. I think the error is correct.
1290 (def-led-equiv |$.| parse-infix
)
1293 (def-pos |$.| $expr
)
1294 (def-lpos |$.| $expr
)
1295 (def-rpos |$.| $expr
)
1296 (def-mheader |$.|
(mnctimes))
1298 ;; Copy properties to noun operator.
1299 (setf (get '%mnctimes
'op
) (get 'mnctimes
'op
))
1301 (def-led-equiv |$
*| parse-nary
)
1304 (def-pos |$
*| $expr
)
1306 (def-lpos |$
*| $expr
)
1307 (def-mheader |$
*|
(mtimes))
1309 (def-led-equiv $
/ parse-infix
)
1315 (def-mheader $
/ (mquotient))
1317 (def-nud-equiv |$
+| parse-prefix
)
1319 (def-rbp |$
+|
134.
) ; Value increased from 100 to 134 (DK 02/2010).
1320 (def-pos |$
+| $expr
)
1321 (def-rpos |$
+| $expr
)
1323 (def-mheader |$
+|
(mplus))
1325 (def-led ((|$
+| |$-|
) 100.
) (op left
)
1326 (setq left
(convert left
'$expr
))
1327 (do ((nl (list (if (eq op
'$-
)
1328 (list (mheader '$-
) (parse '$expr
100.
))
1329 (parse '$expr
100.
))
1331 (cons (parse '$expr
100.
) nl
)))
1332 ((not (member (first-c) '($
+ $-
) :test
#'eq
))
1333 (list* '$expr
(mheader '$
+) (nreverse nl
)))
1334 (if (eq (first-c) '$
+) (pop-c))))
1336 (def-nud-equiv |$-| parse-prefix
)
1339 (def-pos |$-| $expr
)
1340 (def-rpos |$-| $expr
)
1342 (def-mheader |$-|
(mminus))
1344 (def-led-equiv |$
=| parse-infix
)
1347 (def-pos |$
=| $clause
)
1348 (def-rpos |$
=| $expr
)
1349 (def-lpos |$
=| $expr
)
1350 (def-mheader |$
=|
(mequal))
1352 (def-led-equiv |$
#| parse-infix
)
1355 (def-pos |$
#| $clause
)
1356 (def-rpos |$
#| $expr
)
1357 (def-lpos |$
#| $expr
)
1358 (def-mheader |$
#|
(mnotequal))
1360 (def-led-equiv |$
>| parse-infix
)
1363 (def-pos |$
>| $clause
)
1364 (def-rpos |$
>| $expr
)
1365 (def-lpos |$
>| $expr
)
1366 (def-mheader |$
>|
(mgreaterp))
1368 (def-led-equiv |$
>=| parse-infix
)
1371 (def-pos |$
>=| $clause
)
1372 (def-rpos |$
>=| $expr
)
1373 (def-lpos |$
>=| $expr
)
1374 (def-mheader |$
>=|
(mgeqp))
1376 (def-led-equiv |$
<| parse-infix
)
1379 (def-pos |$
<| $clause
)
1380 (def-rpos |$
<| $expr
)
1381 (def-lpos |$
<| $expr
)
1382 (def-mheader |$
<|
(mlessp))
1384 (def-led-equiv |$
<=| parse-infix
)
1387 (def-pos |$
<=| $clause
)
1388 (def-rpos |$
<=| $expr
)
1389 (def-lpos |$
<=| $expr
)
1390 (def-mheader |$
<=|
(mleqp))
1392 (def-nud-equiv $not parse-prefix
)
1395 (def-pos $not $clause
)
1396 (def-rpos $not $clause
)
1397 (def-lpos $not $clause
)
1398 (def-mheader $not
(mnot))
1400 (def-led-equiv $and parse-nary
)
1403 (def-pos $and $clause
)
1405 (def-lpos $and $clause
)
1406 (def-mheader $and
(mand))
1408 (def-led-equiv $or parse-nary
)
1411 (def-pos $or $clause
)
1413 (def-lpos $or $clause
)
1414 (def-mheader $or
(mor))
1416 (def-led-equiv |$
,| parse-nary
)
1421 (def-lpos |$
,| $any
)
1422 (def-mheader |$
,|
($ev
))
1424 (def-nud-equiv $then delim-err
)
1428 (def-nud-equiv $else delim-err
)
1432 (def-nud-equiv $elseif delim-err
)
1433 (def-lbp $elseif
5.
)
1434 (def-rbp $elseif
45.
)
1435 (def-pos $elseif $any
)
1436 (def-rpos $elseif $clause
)
1438 ;No LBP - Default as high as possible
1441 (def-rpos $if $clause
)
1443 (def-mheader $if
(mcond))
1448 (parse-condition op
)))
1450 (defun parse-condition (op)
1451 (list* (parse (rpos op
) (rbp op
))
1452 (if (eq (first-c) '$then
)
1453 (parse '$any
(rbp (pop-c)))
1454 (mread-synerr "Missing `then'"))
1456 (($else
) (list t
(parse '$any
(rbp (pop-c)))))
1457 (($elseif
) (parse-condition (pop-c)))
1458 (t (list t
'$false
)))))
1460 (def-mheader $do
(mdo))
1462 (defun parse-$do
(lex &aux
(left (make-mdo)))
1463 (setf (car left
) (mheader 'mdo
))
1464 (do ((op lex
(pop-c)) (active-bitmask 0))
1466 (if (eq op
'|$
:|
) (setq op
'$from
))
1467 (setq active-bitmask
(collision-check '$do active-bitmask op
))
1468 (let ((data (parse (rpos op
) (rbp op
))))
1470 ($do
(setf (mdo-body left
) data
) (return (cons '$any left
)))
1471 ($for
(setf (mdo-for left
) data
))
1472 ($from
(setf (mdo-from left
) data
))
1473 ($in
(setf (mdo-op left
) 'mdoin
)
1474 (setf (mdo-from left
) data
))
1475 ($step
(setf (mdo-step left
) data
))
1476 ($next
(setf (mdo-next left
) data
))
1477 ($thru
(setf (mdo-thru left
) data
))
1480 (setq data
(list (mheader '$not
) data
)))
1481 (setf (mdo-unless left
)
1482 (if (null (mdo-unless left
))
1484 (list (mheader '$or
) data
(mdo-unless left
)))))
1485 (t (parse-bug-err '$do
))))))
1492 (def-lbp $unless
25.
)
1493 (def-lbp $while
25.
)
1496 (def-nud-equiv $for parse-$do
)
1497 (def-nud-equiv $from parse-$do
)
1498 (def-nud-equiv $step parse-$do
)
1499 (def-nud-equiv $next parse-$do
)
1500 (def-nud-equiv $thru parse-$do
)
1501 (def-nud-equiv $unless parse-$do
)
1502 (def-nud-equiv $while parse-$do
)
1503 (def-nud-equiv $do parse-$do
)
1512 (def-rbp $unless
45.
)
1513 (def-rbp $while
45.
)
1516 (def-rpos $for $any
)
1517 (def-rpos $from $any
)
1518 (def-rpos $step $expr
)
1519 (def-rpos $next $any
)
1520 (def-rpos $thru $expr
)
1521 (def-rpos $unless $clause
)
1522 (def-rpos $while $clause
)
1528 ($from .
($in $from
))
1529 ($in .
($in $from $step $next
))
1530 ($step .
($in $step $next
))
1531 ($next .
($in $step $next
))
1532 ($thru .
($in $thru
)) ;$IN didn't used to get checked for
1536 (def-mheader |$$|
(nodisplayinput))
1537 (def-nud-equiv |$$| premterm-err
)
1539 ;No RBP, POS, RPOS, RBP, or MHEADER
1541 (def-mheader |$
;| (displayinput))
1542 (def-nud-equiv |$
;| premterm-err)
1544 ;No RBP, POS, RPOS, RBP, or MHEADER
1546 (def-nud-equiv |$
&&| delim-err
)
1550 ;; kludge interface function to allow the use of lisp PRINC in places.
1551 (cond ((null x
) 'false
)
1552 ((or (eq x t
) (eq x
't
)) 'true
)
1555 (or (get x
'reversealias
)
1556 (let ((name (symbol-name x
)))
1557 (if (member (char name
0) '(#\$
#\%
) :test
#'char
=)
1562 (define-initial-symbols
1563 ;; * Note: /. is looked for explicitly rather than
1564 ;; existing in this chart. The reason is that
1565 ;; it serves a dual role (as a decimal point) and
1566 ;; must be special-cased.
1568 ;; Same for // because of the /* ... */ handling
1571 |
+| |-| |
*| |^| |
<| |
=| |
>| |
(| |
)| |
[| |
]| |
,|
1572 |
:| |
!| |
#| |
'| |;| |$| |
&|
1574 |
**| |^^| |
:=| |
::| |
!!| |
<=| |
>=| |
''| |
&&|
1579 ;; !! FOLLOWING MOVED HERE FROM MLISP.LISP (DEFSTRUCT STUFF)
1580 ;; !! SEE NOTE THERE
1583 ;;; User extensibility:
1584 (defmfun $prefix
(operator &optional
(rbp 180.
)
1587 (def-operator operator pos
() () rbp rpos
() t
1588 '(nud . parse-prefix
) 'msize-prefix
'dimension-prefix
() )
1591 (defmfun $postfix
(operator &optional
(lbp 180.
)
1594 (def-operator operator pos lbp lpos
() () t
()
1595 '(led . parse-postfix
) 'msize-postfix
'dimension-postfix
() )
1598 (defmfun $infix
(operator &optional
(lbp 180.
)
1603 (def-operator operator pos lbp lpos rbp rpos t t
1604 '(led . parse-infix
) 'msize-infix
'dimension-infix
() )
1607 (defmfun $nary
(operator &optional
(bp 180.
)
1610 (def-operator operator pos bp argpos bp
() t t
1611 '(led . parse-nary
) 'msize-nary
'dimension-nary
() )
1614 (defmfun $matchfix
(operator
1615 match
&optional
(argpos '$any
)
1617 ;shouldn't MATCH be optional?
1618 (def-operator operator pos
() argpos
() () () ()
1619 '(nud . parse-matchfix
) 'msize-matchfix
'dimension-match match
)
1622 (defmfun $nofix
(operator &optional
(pos '$any
))
1623 (def-operator operator pos
() () () () () ()
1624 '(nud . parse-nofix
) 'msize-nofix
'dimension-nofix
() )
1627 ;;; (DEF-OPERATOR op pos lbp lpos rbp rpos sp1 sp2
1628 ;;; parse-data grind-fn dim-fn match)
1629 ;;; OP is the operator name.
1630 ;;; POS is its ``part of speech.''
1631 ;;; LBP is its ``left binding power.''
1632 ;;; LPOS is the part of speech of the arguments to its left, or of all.
1633 ;;; arguments for NARY and MATCHFIX.
1634 ;;; RBP is its ``right binding power.''
1635 ;;; RPOS is the part of speech of the argument to its right.
1636 ;;; SP1 says if the DISSYM property needs a space on the right.
1637 ;;; SP2 says if the DISSYM property needs a space on the left.
1638 ;;; PARSE-DATA is (prop . fn) -- parser prop name dotted with function name
1639 ;;; GRIND-FN is the grinder function for the operator.
1640 ;;; DIM-FN is the dimension function for the operator.
1641 ;;; PARSEPROP is the property name to use for parsing. One of LED or NUD.
1642 ;;; MATCH if non-(), ignores SP1 and SP2. Should be the match symbol.
1643 ;;; sets OP up as matchfix with MATCH.
1645 ;;; For more complete descriptions of these naming conventions, see
1646 ;;; the comments in GRAM package, which describe them in reasonable detail.
1648 (defun def-operator (op pos lbp lpos rbp rpos sp1 sp2
1649 parse-data grind-fn dim-fn match
)
1651 (if (or (and rbp
(not (integerp (setq x rbp
))))
1652 (and lbp
(not (integerp (setq x lbp
)))))
1653 (merror (intl:gettext
"syntax extension: binding powers must be integers; found: ~M") x
))
1654 (if (stringp op
) (setq op
(define-symbol op
)))
1656 (let ((noun ($nounify op
))
1657 (dissym (cdr (exploden op
))))
1660 (setq dissym
(append (if sp1
'(#\space
)) dissym
(if sp2
'(#\space
)))))
1661 (t (if (stringp match
) (setq match
(define-symbol match
)))
1663 (putprop op match
'match
)
1664 (putprop match
5.
'lbp
)
1665 (setq dissym
(cons dissym
(cdr (exploden match
))))))
1666 (putprop op pos
'pos
)
1667 (putprop op
(cdr parse-data
) (car parse-data
))
1668 (putprop op grind-fn
'grind
)
1669 (putprop op dim-fn
'dimension
)
1670 (putprop noun dim-fn
'dimension
)
1671 (putprop op dissym
'dissym
)
1672 (putprop noun dissym
'dissym
)
1674 (putprop op rbp
'rbp
)
1675 (putprop noun rbp
'rbp
))
1677 (putprop op lbp
'lbp
)
1678 (putprop noun lbp
'lbp
))
1679 (when lpos
(putprop op lpos
'lpos
))
1680 (when rpos
(putprop op rpos
'rpos
))
1683 (defun op-setup (op)
1684 (declare (special *mopl
*))
1685 (let ((dummy (or (get op
'op
)
1686 (coerce (string* op
) 'string
))))
1687 (putprop op dummy
'op
)
1689 (if (and (operatorp1 op
) (not (member dummy
(cdr $props
) :test
#'eq
)))
1690 (push dummy
*mopl
*))
1691 (add2lnc dummy $props
)))
1693 (defun kill-operator (op)
1696 (noun-form ($nounify op
)))
1697 ;; Refuse to kill an operator which appears on *BUILTIN-$PROPS*.
1698 (unless (member opr
*builtin-$props
* :test
#'equal
)
1699 (undefine-symbol opr
)
1702 (mapc #'(lambda (x) (remprop op x
))
1703 '(nud nud-expr nud-subr
; NUD info
1704 led led-expr led-subr
; LED info
1705 lbp rbp
; Binding power info
1706 lpos rpos pos
; Part-Of-Speech info
1707 grind dimension dissym
; Display info
1708 op
)) ; Operator info
1709 (mapc #'(lambda (x) (remprop noun-form x
))
1710 '(dimension dissym lbp rbp
)))))
1714 ;; the functions get-instream etc.. are all defined in
1715 ;; gcl lsp/debug.lsp
1716 ;; they are all generic common lisp and could be used by
1717 ;; any Common lisp implementation.
1722 (line 0 :type fixnum
)
1726 (defvar *stream-alist
* nil
)
1729 (defun stream-name (path)
1731 (car (errset (namestring (pathname path
))))))
1734 (defun instream-name (instr)
1735 (or (instream-stream-name instr
)
1736 (stream-name (instream-stream instr
))))
1738 ;; (closedp stream) checks if a stream is closed.
1739 ;; how to do this in common lisp!!
1743 #+never-clean-up-dont-know-how-to-close
1744 (dolist (v *stream-alist
*)
1745 (if (closedp (instream-stream v
))
1746 (setq *stream-alist
* (delete v
*stream-alist
*)))))
1749 (defun get-instream (str)
1750 (or (dolist (v *stream-alist
*)
1751 (cond ((eq str
(instream-stream v
))
1754 (errset (setq name
(namestring str
)))
1755 (car (setq *stream-alist
*
1756 (cons (make-instream :stream str
:stream-name name
)
1757 *stream-alist
*))))))
1759 (defun newline (str)
1760 (incf (instream-line (get-instream str
)))
1763 (defun find-stream (stream)
1764 (dolist (v *stream-alist
*)
1765 (cond ((eq stream
(instream-stream v
))
1769 (defun add-lineinfo (lis)
1771 (eq *parse-stream
* *parse-string-input-stream
*) ;; avoid consing *parse-string-input-stream*
1772 ;; via get-instream to *stream-alist*
1773 (and (eq *parse-window
* *standard-input
*)
1774 (not (find-stream *parse-stream
*)) ))
1776 (let* ((st (get-instream *parse-stream
*))
1777 (n (instream-line st
))
1778 (nam (instream-name st
)))
1779 (or nam
(return-from add-lineinfo lis
))
1780 (setq *current-line-info
*
1781 (cond ((eq (cadr *current-line-info
*) nam
)
1782 (cond ((eql (car *current-line-info
*) n
)
1783 *current-line-info
*)
1784 (t (cons n
(cdr *current-line-info
*)))))
1785 (t (list n nam
'src
))))
1786 (cond ((null (cdr lis
))
1787 (list (car lis
) *current-line-info
*))
1788 (t (append lis
(list *current-line-info
*)))))))
1790 ;; Remove debugging stuff.
1791 ;; STRIP-LINEINFO does not modify EXPR.
1793 (defun strip-lineinfo (expr)
1794 (if (or (atom expr
) (specrepp expr
))
1796 (cons (strip-lineinfo-op (car expr
)) (mapcar #'strip-lineinfo
(cdr expr
)))))
1798 ;; If something in the operator looks like debugging stuff, remove it.
1799 ;; It is assumed here that debugging stuff is a list comprising an integer and a string
1800 ;; (and maybe other stuff, which is ignored).
1802 (defun strip-lineinfo-op (maxima-op)
1803 (remove-if #'(lambda (x) (and (consp x
) (integerp (first x
)) (stringp (second x
)))) maxima-op
))