Remove some code duplication in TRANSLATE-PREDICATE
[maxima.git] / src / nparse.lisp
bloba077b25dc258268f3290a0046d90d4b16d07b3a0
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
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))
23 (defun alphabetp (n)
24 (and (characterp n)
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
60 ;; position
61 (cond ((and fp (file-position *parse-stream* 0))
62 (do ((l (read-line *parse-stream* nil nil) (read-line *parse-stream* nil nil))
63 (o 1 (1+ p))
64 (p (file-position *parse-stream*) (file-position *parse-stream*))
65 (n 1 (1+ n)))
66 ((or (null p) (>= p fp))
67 (cons n (- fp o)))))
68 (t '())))
69 (column ()
70 (let ((n (get '*parse-window* 'length))
71 ch some)
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))
77 (t (push ch some))))
78 some))
79 (printer (x)
80 (cond ((symbolp x)
81 (print-invert-case (stripdollar x)))
82 ((stringp x)
83 (maybe-invert-string-case x))
84 (t x)))
86 (case (and file $report_synerr_line)
87 ((t)
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))))
91 (otherwise
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))))
101 (terpri)
102 (finish-output)
103 (throw-macsyma-top))))
105 (defun tyi-parse-int (stream eof)
106 (or *parse-window*
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)
114 (newline stream))
115 tem))
117 (defun *mread-prompt* (out-stream char)
118 (declare (ignore char))
119 (format out-stream "~&~A" *mread-prompt*))
121 (defun aliaslookup (op)
122 (if (symbolp op)
123 (or (get op 'alias) op)
124 op))
126 ;;;; Tokenizing
128 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
129 ;;;;; ;;;;;
130 ;;;;; The Input Scanner ;;;;;
131 ;;;;; ;;;;;
132 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
134 (defun gobble-whitespace ()
135 (do ((ch (parse-tyipeek) (parse-tyipeek)))
136 ((not (member ch *whitespace-chars*)))
137 (parse-tyi)))
139 (defun read-command-token (obj)
140 (gobble-whitespace)
141 (read-command-token-aux obj))
143 (defun safe-assoc (item lis)
144 "maclisp would not complain about (car 3), it gives nil"
145 (loop for v in lis
146 when (and (consp v)
147 (equal (car v) item))
149 (return v)))
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 )
156 (loop for v on lis
158 (cond ((consp (car v))
159 (if (eq (caar v) c)
160 (return (car v))))
161 ((eql (car v) c)
162 (return 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
169 ;; be no consing...
171 (defun parse-tyi ()
172 (let ((tem *parse-tyi*))
173 (cond ((null tem)
174 (tyi-parse-int *parse-stream* *parse-stream-eof*))
175 ((atom tem)
176 (setq *parse-tyi* nil)
177 tem)
178 (t ;;consp
179 (setq *parse-tyi* (cdr tem))
180 (car tem)))))
182 ;; read one character but leave it there. so next parse-tyi gets it
183 (defun parse-tyipeek ()
184 (let ((tem *parse-tyi*))
185 (cond ((null tem)
186 (setq *parse-tyi* (tyi-parse-int *parse-stream* *parse-stream-eof*)))
187 ((atom tem) tem)
188 (t (car tem)))))
190 ;; push characters back on the stream
191 (defun unparse-tyi (c)
192 (let ((tem *parse-tyi*))
193 (if (null tem)
194 (setq *parse-tyi* c)
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)
204 (let* (result
205 (ch (parse-tyipeek))
206 (lis (if (eql ch *parse-stream-eof*)
208 (parser-assoc ch obj))))
209 (cond ((null lis)
210 nil)
212 (parse-tyi)
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)))))
221 ((null (cddr lis))
222 ;; lis something like (#\= (ANS $<=))
223 ;; and this says there are no longer operators
224 ;; starting with this.
225 (setq result
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*))
236 (cadr (cadr lis)))))
238 (let ((res (and (eql (car (cadr lis)) 'ans)
239 (cadr (cadr lis))))
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))
244 result))))
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)))
252 (if charlist
253 (implode charlist)
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))))
259 (if charlist
260 (let ((*package* (find-package :keyword)))
261 (implode charlist))
262 (mread-synerr "Lisp keyword expected."))))
264 (defun scan-token (flag)
265 (do ((c (parse-tyipeek) (parse-tyipeek))
266 (l () (cons c l)))
267 ((or (eql c *parse-stream-eof*)
268 (and flag
269 (not (or (digit-char-p c (max 10 *read-base*))
270 (alphabetp c)
271 (char= c #\\ )))))
272 (nreverse (or l (list (parse-tyi))))) ; Read at least one char ...
273 (when (char= (parse-tyi) #\\ )
274 (setq c (parse-tyi)))
275 (setq flag t)))
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)))
283 (when init
284 (vector-push-extend init buf))
285 (do ((c (parse-tyipeek) (parse-tyipeek)))
286 ((cond ((eql c *parse-stream-eof*))
287 ((char= c #\")
288 (parse-tyi) t))
289 (copy-seq buf))
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
308 #+nil
309 (defun test-make-number (&optional (n 1000))
310 (let ((failures 0))
311 (dotimes (k n)
312 (flet ((digit-list (n)
313 (coerce (format nil "~D" n) 'list)))
314 (let ((numlist nil))
315 ;; Generate a random number with 30 fraction digits and an
316 ;; large exponent.
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)))
324 numlist)
325 ;; Convert using accurate and fast methods and compare the
326 ;; results.
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)
333 (map nil #'princ x))
334 (reverse numlist))
335 (terpri)
336 (finish-output)
337 (unless (equalp true fast)
338 (incf failures)
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
346 ;; change that?
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))
358 (let*
359 ((*read-base* 10.)
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 #\-)
378 (- exp)
379 exp)
380 frac-len)))
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.
391 (bigfloatp result)))
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 #\-)
399 (- exp)
400 exp)))))
401 ($bfloat (cl-rat-to-maxima ratio)))))))
403 ;; Richard J. Fateman wrote the big float to rational code and the function
404 ;; cl-rat-to-maxmia.
406 (defun cl-rat-to-maxima (x)
407 (if (integerp 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))
413 (l () (cons c l)))
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
417 (parse-tyi)))
418 (nreverse l)
419 data)))
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.
423 ;; That's an error.
424 (mread-synerr "parser: incomplete number; missing exponent?"))
426 (make-number (cons (nreverse l) data)))))
427 (parse-tyi)))
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) #\-))
435 (parse-tyi)
436 #\+))
437 data)
438 (scan-digits data () () t))
440 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
441 ;;;;; ;;;;;
442 ;;;;; The Expression Parser ;;;;;
443 ;;;;; ;;;;;
444 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447 ;;; ;;;
448 ;;; Based on a theory of parsing presented in: ;;;
449 ;;; ;;;
450 ;;; Pratt, Vaughan R., ``Top Down Operator Precedence,'' ;;;
451 ;;; ACM Symposium on Principles of Programming Languages ;;;
452 ;;; Boston, MA; October, 1973. ;;;
453 ;;; ;;;
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
460 ;;; commands.
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
476 ;;; down ...?)
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
494 ;;; ever notice.
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)
505 ;;;; Macro Support
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)
514 (cond
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*)
532 (parse-tyi)
533 (if eof-ok? eof-obj
534 (maxima-error (intl:gettext "parser: end of file while scanning expression."))))
535 ((eql test #\/)
536 (parse-tyi)
537 (cond ((char= (parse-tyipeek) #\*)
538 (parse-tyi)
539 (gobble-comment)
540 (scan-one-token-g eof-ok? eof-obj))
541 (t '$/)))
542 ((eql test #\.) (parse-tyi) ; Read the dot
543 (if (digit-char-p (parse-tyipeek) 10.)
544 (scan-number-after-dot (list (ncons #\.) nil))
545 '|$.|))
546 ((eql test #\")
547 (parse-tyi)
548 (scan-macsyma-string))
549 ((eql test #\?)
550 (parse-tyi)
551 (cond ((char= (parse-tyipeek) #\")
552 (parse-tyi)
553 (scan-lisp-string))
554 ((char= (parse-tyipeek) #\:)
555 (scan-keyword-token))
557 (scan-lisp-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 ()
565 (prog (c depth)
566 (setq depth 1)
567 read
568 (setq c (parse-tyipeek))
569 (parse-tyi)
570 (cond ((= depth 0) (return t)))
571 (cond ((eql c *parse-stream-eof*)
572 (error (intl:gettext "parser: end of file in comment.")))
573 ((char= c #\*)
574 (cond ((char= (parse-tyipeek) #\/)
575 (decf depth)
576 (parse-tyi)
577 (cond ((= depth 0) (return t)))
578 (go read))))
579 ((char= c #\/)
580 (cond ((char= (parse-tyipeek) #\*)
581 (incf depth) (parse-tyi)
582 (go read)))))
583 (go read))
586 (defun scan-number-rest (data)
587 (let ((c (caar data)))
588 (cond ((char= c #\.)
589 ;; We found a dot
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)
600 #'scan-number-rest))
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)))
612 (if propl
613 (progn (remprop op-to (car propl))
614 (putprop op-to (cadr propl) (car propl)))
615 (inherit-propl op-to
616 (maxima-error "has no ~a properties. ~a ~a" getl op-from 'wrng-type-arg)
617 getl))))
620 ;;; (NUD <op>)
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.
627 (eval-when
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)
632 (list 'quote 'nud)))
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)
641 (list 'quote 'led)))
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)))
648 (defun nud-call (op)
649 (let ((tem (and (symbolp op) (getl op '(nud)))) res)
650 (setq res
651 (if (null tem)
652 (if (operatorp op)
653 (mread-synerr "~A is not a prefix operator" (mopstrip op))
654 (cons '$any op))
655 (funcall (cadr tem) op)))
656 res))
658 (defun led-call (op l)
659 (let ((tem (and (symbolp op) (getl op '(led)))) res)
660 (setq res
661 (if (null tem)
662 (mread-synerr "~A is not an infix operator" (mopstrip op))
663 (funcall (cadr tem) op l)))
664 res))
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
690 ((not existing-lbp)
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
696 ((not existing-rbp)
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))
702 op))))
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)))
727 ((null lis) nl))))
728 `(progn
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)))
735 ((null lis) i))
736 ,op))
737 alist))))
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)))
754 (if collision
755 (if (eq collision key)
756 (mread-synerr "This ~A's ~A slot is already filled."
757 (mopstrip op)
758 (mopstrip key))
759 (mread-synerr "A ~A cannot have a ~A with a ~A field."
760 (mopstrip op)
761 (mopstrip key)
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!
789 ;;; (LPOS <op>)
790 ;;; (RPOS <op>)
791 ;;; (POS <op>)
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))
802 ;;; MHEADER
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."
814 fixnum)
817 ;;;; Misplaced definitions
819 (defmacro def-operatorp ()
820 `(defun operatorp (lex)
821 (and (symbolp lex) (getl lex '(,@(nud-propl) ,@(led-propl))))))
823 (def-operatorp)
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))))))
832 (def-operatorp1)
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)
848 (progn
849 (when *mread-prompt*
850 (and *parse-window* (setf (car *parse-window*) nil
851 *parse-window* (cdr *parse-window*)))
852 (princ *mread-prompt*)
853 (finish-output))
854 (apply 'mread-raw read-args)))
856 (defun mread-prompter (stream char)
857 (declare (special *mread-prompt-internal*)
858 (ignore char))
859 (fresh-line stream)
860 (princ *mread-prompt-internal* stream))
862 ;; input can look like:
863 ;;aa && bb && jim:3;
865 (defun mread-raw (*parse-stream* &optional *mread-eof-obj*)
866 (let ((scan-buffered-token (list nil))
867 *parse-tyi*)
868 (if (eq scan-buffered-token ;; a handly unique object for the EQ test.
869 (peek-one-token-g t scan-buffered-token))
870 *mread-eof-obj*
871 (do ((labels ())
872 (input (parse '$any 0.) (parse '$any 0.)))
873 (nil)
874 (case (first-c)
875 ((|$;| |$$|)
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)))
880 input)))
881 ((|$&&|)
882 (pop-c)
883 (if (symbolp input)
884 (push input 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, ()
1037 (t ; Else, ...
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
1077 ;; be reachable.)
1079 (defun parse-bug-err (op)
1080 (mread-synerr
1081 "Parser bug in ~A. Please report this to the Maxima maintainers,~
1082 ~%including the characters you just typed which caused the error. Thanks."
1083 (mopstrip op)))
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."
1095 (mopstrip op)))
1097 ;;;; Operator Specific Data
1099 (def-nud-equiv |$]| delim-err)
1100 (def-led-equiv |$]| erb-err)
1101 (def-lbp |$]| 5.)
1103 (def-nud-equiv |$[| parse-matchfix)
1104 (def-match |$[| |$]|)
1105 (def-lbp |$[| 200.)
1106 ;No RBP
1107 (def-mheader |$[| (mlist))
1108 (def-pos |$[| $any)
1109 (def-lpos |$[| $any)
1110 ;No RPOS
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
1123 right))
1124 (cons '$any (aliaslookup right)))
1125 (t ; exp[...]
1126 (cons '$any (cons header
1127 (cons left right)))))))
1130 (def-nud-equiv |$)| delim-err)
1131 (def-led-equiv |$)| erb-err)
1132 (def-lbp |$)| 5.)
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
1141 ;; work with.
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)
1181 (let (right)
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))
1192 (cdaadr right))
1193 (cdadr right)))
1194 (cons '$any right))))
1195 (t (cons '$any (cons (cons ($nounify (caar right)) (cdar right))
1196 (cdr right)))))))
1198 (def-nud (|$''|) (op)
1199 (let (right)
1200 (cons '$any
1201 (cond ((eq '|$(| (first-c)) (meval (parse '$any 190.)))
1202 ((atom (setq right (parse '$any 190.))) (meval1 right))
1203 ((eq 'mqapply (caar right))
1204 (rplaca (cdr right)
1205 (cons (cons ($verbify (caaadr right)) (cdaadr right))
1206 (cdadr right)))
1207 right)
1208 (t (cons (cons ($verbify (caar right)) (cdar right))
1209 (cdr right)))))))
1211 (def-led-equiv |$:| parse-infix)
1212 (def-lbp |$:| 180.)
1213 (def-rbp |$:| 20.)
1214 (def-pos |$:| $any)
1215 (def-rpos |$:| $any)
1216 (def-lpos |$:| $any)
1217 (def-mheader |$:| (msetq))
1219 (def-led-equiv |$::| parse-infix)
1220 (def-lbp |$::| 180.)
1221 (def-rbp |$::| 20.)
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.)
1229 (def-rbp |$:=| 20.)
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)
1244 (def-lbp |$!| 160.)
1245 ;No RBP
1246 (def-pos |$!| $expr)
1247 (def-lpos |$!| $expr)
1248 ;No RPOS
1249 (def-mheader |$!| (mfactorial))
1251 (def-mheader |$!!| ($genfact))
1253 (def-led (|$!!| 160.) (op left)
1254 (list '$expr
1255 (mheader '$!!)
1256 (convert left '$expr)
1257 (list (mheader '$/) (convert left '$expr) 2)
1260 (def-lbp |$^| 140.)
1261 (def-rbp |$^| 139.)
1262 (def-pos |$^| $expr)
1263 (def-lpos |$^| $expr)
1264 (def-rpos |$^| $expr)
1265 (def-mheader |$^| (mexpt))
1267 (def-led ((|$^| |$^^|)) (op left)
1268 (cons '$expr
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)
1290 (def-lbp |$.| 130.)
1291 (def-rbp |$.| 129.)
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)
1301 (def-lbp |$*| 120.)
1302 ;RBP not needed
1303 (def-pos |$*| $expr)
1304 ;RPOS not needed
1305 (def-lpos |$*| $expr)
1306 (def-mheader |$*| (mtimes))
1308 (def-led-equiv $/ parse-infix)
1309 (def-lbp $/ 120.)
1310 (def-rbp $/ 120.)
1311 (def-pos $/ $expr)
1312 (def-rpos $/ $expr)
1313 (def-lpos $/ $expr)
1314 (def-mheader $/ (mquotient))
1316 (def-nud-equiv |$+| parse-prefix)
1317 (def-lbp |$+| 100.)
1318 (def-rbp |$+| 134.) ; Value increased from 100 to 134 (DK 02/2010).
1319 (def-pos |$+| $expr)
1320 (def-rpos |$+| $expr)
1321 ;LPOS not needed
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.))
1329 left)
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)
1336 (def-lbp |$-| 100.)
1337 (def-rbp |$-| 134.)
1338 (def-pos |$-| $expr)
1339 (def-rpos |$-| $expr)
1340 ;LPOS not needed
1341 (def-mheader |$-| (mminus))
1343 (def-led-equiv |$=| parse-infix)
1344 (def-lbp |$=| 80.)
1345 (def-rbp |$=| 80.)
1346 (def-pos |$=| $clause)
1347 (def-rpos |$=| $expr)
1348 (def-lpos |$=| $expr)
1349 (def-mheader |$=| (mequal))
1351 (def-led-equiv |$#| parse-infix)
1352 (def-lbp |$#| 80.)
1353 (def-rbp |$#| 80.)
1354 (def-pos |$#| $clause)
1355 (def-rpos |$#| $expr)
1356 (def-lpos |$#| $expr)
1357 (def-mheader |$#| (mnotequal))
1359 (def-led-equiv |$>| parse-infix)
1360 (def-lbp |$>| 80.)
1361 (def-rbp |$>| 80.)
1362 (def-pos |$>| $clause)
1363 (def-rpos |$>| $expr)
1364 (def-lpos |$>| $expr)
1365 (def-mheader |$>| (mgreaterp))
1367 (def-led-equiv |$>=| parse-infix)
1368 (def-lbp |$>=| 80.)
1369 (def-rbp |$>=| 80.)
1370 (def-pos |$>=| $clause)
1371 (def-rpos |$>=| $expr)
1372 (def-lpos |$>=| $expr)
1373 (def-mheader |$>=| (mgeqp))
1375 (def-led-equiv |$<| parse-infix)
1376 (def-lbp |$<| 80.)
1377 (def-rbp |$<| 80.)
1378 (def-pos |$<| $clause)
1379 (def-rpos |$<| $expr)
1380 (def-lpos |$<| $expr)
1381 (def-mheader |$<| (mlessp))
1383 (def-led-equiv |$<=| parse-infix)
1384 (def-lbp |$<=| 80.)
1385 (def-rbp |$<=| 80.)
1386 (def-pos |$<=| $clause)
1387 (def-rpos |$<=| $expr)
1388 (def-lpos |$<=| $expr)
1389 (def-mheader |$<=| (mleqp))
1391 (def-nud-equiv $not parse-prefix)
1392 ;LBP not needed
1393 (def-rbp $not 70.)
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)
1400 (def-lbp $and 65.)
1401 ;RBP not needed
1402 (def-pos $and $clause)
1403 ;RPOS not needed
1404 (def-lpos $and $clause)
1405 (def-mheader $and (mand))
1407 (def-led-equiv $or parse-nary)
1408 (def-lbp $or 60.)
1409 ;RBP not needed
1410 (def-pos $or $clause)
1411 ;RPOS not needed
1412 (def-lpos $or $clause)
1413 (def-mheader $or (mor))
1415 (def-led-equiv |$,| parse-nary)
1416 (def-lbp |$,| 10.)
1417 ;RBP not needed
1418 (def-pos |$,| $any)
1419 ;RPOS not needed
1420 (def-lpos |$,| $any)
1421 (def-mheader |$,| ($ev))
1423 (def-nud-equiv $then delim-err)
1424 (def-lbp $then 5.)
1425 (def-rbp $then 25.)
1427 (def-nud-equiv $else delim-err)
1428 (def-lbp $else 5.)
1429 (def-rbp $else 25.)
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
1438 (def-rbp $if 45.)
1439 (def-pos $if $any)
1440 (def-rpos $if $clause)
1441 ;No LPOS
1442 (def-mheader $if (mcond))
1444 (def-nud ($if) (op)
1445 (list* (pos op)
1446 (mheader op)
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'"))
1454 (case (first-c)
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))
1464 (nil)
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))))
1468 (case 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))
1477 (($unless $while)
1478 (if (eq op '$while)
1479 (setq data (list (mheader '$not) data)))
1480 (setf (mdo-unless left)
1481 (if (null (mdo-unless left))
1482 data
1483 (list (mheader '$or) data (mdo-unless left)))))
1484 (t (parse-bug-err '$do))))))
1486 (def-lbp $for 25.)
1487 (def-lbp $from 25.)
1488 (def-lbp $step 25.)
1489 (def-lbp $next 25.)
1490 (def-lbp $thru 25.)
1491 (def-lbp $unless 25.)
1492 (def-lbp $while 25.)
1493 (def-lbp $do 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)
1504 (def-rbp $do 25.)
1505 (def-rbp $for 200.)
1506 (def-rbp $from 95.)
1507 (def-rbp $in 95.)
1508 (def-rbp $step 95.)
1509 (def-rbp $next 45.)
1510 (def-rbp $thru 95.)
1511 (def-rbp $unless 45.)
1512 (def-rbp $while 45.)
1514 (def-rpos $do $any)
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)
1524 (def-collisions $do
1525 ($do . ())
1526 ($for . ($for))
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
1532 ($unless . ())
1533 ($while . ()))
1535 (def-mheader |$$| (nodisplayinput))
1536 (def-nud-equiv |$$| premterm-err)
1537 (def-lbp |$$| -1)
1538 ;No RBP, POS, RPOS, RBP, or MHEADER
1540 (def-mheader |$;| (displayinput))
1541 (def-nud-equiv |$;| premterm-err)
1542 (def-lbp |$;| -1)
1543 ;No RBP, POS, RPOS, RBP, or MHEADER
1545 (def-nud-equiv |$&&| delim-err)
1546 (def-lbp |$&&| -1)
1548 (defun mopstrip (x)
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)
1552 ((numberp x) x)
1553 ((symbolp x)
1554 (or (get x 'reversealias)
1555 (let ((name (symbol-name x)))
1556 (if (member (char name 0) '(#\$ #\%) :test #'char=)
1557 (subseq name 1)
1558 name))))
1559 (t x)))
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
1568 ;; by the tokenizer
1569 ;; Single character
1570 |+| |-| |*| |^| |<| |=| |>| |(| |)| |[| |]| |,|
1571 |:| |!| |#| |'| |;| |$| |&|
1572 ;;Two character
1573 |**| |^^| |:=| |::| |!!| |<=| |>=| |''| |&&|
1574 ;; Three character
1575 |::=|
1578 ;; !! FOLLOWING MOVED HERE FROM MLISP.LISP (DEFSTRUCT STUFF)
1579 ;; !! SEE NOTE THERE
1580 (define-symbol "@")
1582 ;;; User extensibility:
1583 (defmfun $prefix (operator &optional (rbp 180.)
1584 (rpos '$any)
1585 (pos '$any))
1586 (def-operator operator pos () () rbp rpos () t
1587 '(nud . parse-prefix) 'msize-prefix 'dimension-prefix () )
1588 operator)
1590 (defmfun $postfix (operator &optional (lbp 180.)
1591 (lpos '$any)
1592 (pos '$any))
1593 (def-operator operator pos lbp lpos () () t ()
1594 '(led . parse-postfix) 'msize-postfix 'dimension-postfix () )
1595 operator)
1597 (defmfun $infix (operator &optional (lbp 180.)
1598 (rbp 180.)
1599 (lpos '$any)
1600 (rpos '$any)
1601 (pos '$any))
1602 (def-operator operator pos lbp lpos rbp rpos t t
1603 '(led . parse-infix) 'msize-infix 'dimension-infix () )
1604 operator)
1606 (defmfun $nary (operator &optional (bp 180.)
1607 (argpos '$any)
1608 (pos '$any))
1609 (def-operator operator pos bp argpos bp () t t
1610 '(led . parse-nary) 'msize-nary 'dimension-nary () )
1611 operator)
1613 (defmfun $matchfix (operator
1614 match &optional (argpos '$any)
1615 (pos '$any))
1616 ;shouldn't MATCH be optional?
1617 (def-operator operator pos () argpos () () () ()
1618 '(nud . parse-matchfix) 'msize-matchfix 'dimension-match match)
1619 operator)
1621 (defmfun $nofix (operator &optional (pos '$any))
1622 (def-operator operator pos () () () () () ()
1623 '(nud . parse-nofix) 'msize-nofix 'dimension-nofix () )
1624 operator)
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)
1649 (let ((x))
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)))
1654 (op-setup op)
1655 (let ((noun ($nounify op))
1656 (dissym (cdr (exploden op))))
1657 (cond
1658 ((not match)
1659 (setq dissym (append (if sp1 '(#\space)) dissym (if sp2 '(#\space)))))
1660 (t (if (stringp match) (setq match (define-symbol match)))
1661 (op-setup 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)
1672 (when rbp
1673 (putprop op rbp 'rbp)
1674 (putprop noun rbp 'rbp))
1675 (when lbp
1676 (putprop op lbp 'lbp)
1677 (putprop noun lbp 'lbp))
1678 (when lpos (putprop op lpos 'lpos))
1679 (when rpos (putprop op rpos 'rpos))
1680 (getopr op))))
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 )
1687 (putopr 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)
1693 (let
1694 ((opr (get op '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)
1699 (remopr opr)
1700 (rempropchk 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.
1718 #-gcl
1719 (defstruct instream
1720 stream
1721 (line 0 :type fixnum)
1722 stream-name)
1724 #-gcl
1725 (defvar *stream-alist* nil)
1727 #-gcl
1728 (defun stream-name (path)
1729 (let ((errset nil))
1730 (car (errset (namestring (pathname path))))))
1732 #-gcl
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!!
1740 #-gcl
1741 (defun cleanup ()
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*)))))
1747 #-gcl
1748 (defun get-instream (str)
1749 (or (dolist (v *stream-alist*)
1750 (cond ((eq str (instream-stream v))
1751 (return v))))
1752 (let (name errset)
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)))
1760 (values))
1762 (defun find-stream (stream)
1763 (dolist (v *stream-alist*)
1764 (cond ((eq stream (instream-stream v))
1765 (return v)))))
1768 (defun add-lineinfo (lis)
1769 (if (or (atom 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))