Merge branch 'master' into gcl_cleanup
[maxima/cygwin.git] / src / nparse.lisp
blob5f7d5e52e2c94f01f91ca5ba0e008b7053c76054
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 (defvar *ascii-space-chars-for-maxima* '(#\tab #\space #\linefeed #\return #\page #\newline))
19 (defvar *unicode-space-chars-for-maxima*
20 #-(or unicode sb-unicode openmcl-unicode-strings abcl (and allegro ics)) nil
21 #+(or unicode sb-unicode openmcl-unicode-strings abcl (and allegro ics))
22 ;; Adapted from the list given by: https://jkorpela.fi/chars/spaces.html
23 ;; omitting SPACE, OGHAM SPACE MARK, MONGOLIAN VOWEL SEPARATOR, IDEOGRAPHIC SPACE,
24 ;; and ZERO WIDTH NO-BREAK SPACE.
26 #.(code-char #x00A0) ;; NO-BREAK SPACE
27 #.(code-char #x2000) ;; EN QUAD
28 #.(code-char #x2001) ;; EM QUAD
29 #.(code-char #x2002) ;; EN SPACE
30 #.(code-char #x2003) ;; EM SPACE
31 #.(code-char #x2004) ;; THREE-PER-EM SPACE
32 #.(code-char #x2005) ;; FOUR-PER-EM SPACE
33 #.(code-char #x2006) ;; SIX-PER-EM SPACE
34 #.(code-char #x2007) ;; FIGURE SPACE
35 #.(code-char #x2008) ;; PUNCTUATION SPACE
36 #.(code-char #x2009) ;; THIN SPACE
37 #.(code-char #x200A) ;; HAIR SPACE
38 #.(code-char #x200B) ;; ZERO WIDTH SPACE
39 #.(code-char #x202F) ;; NARROW NO-BREAK SPACE
40 #.(code-char #x205F) ;; MEDIUM MATHEMATICAL SPACE
43 (defmvar *whitespace-chars* (append *ascii-space-chars-for-maxima* *unicode-space-chars-for-maxima*))
45 (defun alphabetp (n)
46 (and (characterp n)
47 (or (alpha-char-p n) #+gcl(>= (char-code n) 128)
48 (member n *alphabet*))))
50 (defun ascii-numberp (num)
51 (and (characterp num) (char<= num #\9) (char>= num #\0)))
53 (defvar *parse-window* nil)
54 (defvar *parse-stream* () "input stream for Maxima parser")
55 (defvar *parse-stream-eof* -1 "EOF value for Maxima parser")
56 (defvar *parse-tyi* nil)
58 (defvar *mread-prompt* nil "prompt used by `mread'")
59 (defvar *mread-eof-obj* () "Bound by `mread' for use by `mread-raw'")
60 (defvar *current-line-info* nil)
62 (defvar *parse-string-input-stream* ;; reference to the input stream
63 (let ((stream (make-string-input-stream ""))) ;; used by parse-string
64 (close stream) ;; in share/stringroc/eval_string.lisp
65 stream )) ;; (see also add-lineinfo below)
67 (defmvar $report_synerr_line t "If T, report line number where syntax error occurs; otherwise, report FILE-POSITION of error.")
68 (defmvar $report_synerr_info t "If T, report the syntax error details from all sources; otherwise, only report details from standard-input.")
70 (defun mread-synerr (format-string &rest l)
71 (let ((fp (and (not (eq *parse-stream* *standard-input*))
72 (file-position *parse-stream*)))
73 (file (and (not (eq *parse-stream* *standard-input*))
74 (cadr *current-line-info*))))
75 (flet ((line-number ()
76 ;; Fix me: Neither batch nor load track the line number
77 ;; correctly. batch, via dbm-read, does not track the
78 ;; line number at all (a bug?).
80 ;; Find the line number by jumping to the start of file
81 ;; and reading line-by-line til we reach the current
82 ;; position
83 (cond ((and fp (file-position *parse-stream* 0))
84 (do ((l (read-line *parse-stream* nil nil) (read-line *parse-stream* nil nil))
85 (o 1 (1+ p))
86 (p (file-position *parse-stream*) (file-position *parse-stream*))
87 (n 1 (1+ n)))
88 ((or (null p) (>= p fp))
89 (cons n (- fp o)))))
90 (t '())))
91 (column ()
92 (let ((n (get '*parse-window* 'length))
93 ch some)
94 (loop for i from (1- n) downto (- n 20)
95 while (setq ch (nth i *parse-window*))
97 (cond ((or (eql ch *parse-stream-eof*)
98 (char= ch #\newline))
99 (return-from column some))
100 (t (push ch some))))
101 some))
102 (printer (x)
103 (cond ((symbolp x)
104 (print-invert-case (stripdollar x)))
105 ((stringp x)
106 (maybe-invert-string-case x))
107 (t x)))
109 (case (and file $report_synerr_line)
110 ((t)
111 ;; print the file, line and column information
112 (let ((line+column (line-number)))
113 (format t "~&~a:~a:~a:" file (car line+column) (cdr line+column))))
114 (otherwise
115 ;; if file=nil, then print a fresh line only; otherwise print
116 ;; file and character location
117 (format t "~&~:[~;~:*~a:~a:~]" file fp)))
118 (format t (intl:gettext "incorrect syntax: "))
119 (apply 'format t format-string (mapcar #'printer l))
120 (cond ((or $report_synerr_info (eql *parse-stream* *standard-input*))
121 (let ((some (column)))
122 (format t "~%~{~c~}~%~vt^" some (max 0 (- (length some) 2)))
123 (read-line *parse-stream* nil nil))))
124 (terpri)
125 (finish-output)
126 (throw-macsyma-top))))
128 (defun tyi-parse-int (stream eof)
129 (or *parse-window*
130 (progn (setq *parse-window* (make-list 25))
131 (setf (get '*parse-window* 'length) (length *parse-window*))
132 (nconc *parse-window* *parse-window*)))
133 (let ((tem (tyi stream eof)))
134 (setf (car *parse-window*) tem *parse-window*
135 (cdr *parse-window*))
136 (if (eql tem #\newline)
137 (newline stream))
138 tem))
140 (defun *mread-prompt* (out-stream char)
141 (declare (ignore char))
142 (format out-stream "~&~A" *mread-prompt*))
144 (defun aliaslookup (op)
145 (if (symbolp op)
146 (or (get op 'alias) op)
147 op))
149 ;;;; Tokenizing
151 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152 ;;;;; ;;;;;
153 ;;;;; The Input Scanner ;;;;;
154 ;;;;; ;;;;;
155 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
157 (defun gobble-whitespace ()
158 (do ((ch (parse-tyipeek) (parse-tyipeek)))
159 ((not (member ch *whitespace-chars*)))
160 (parse-tyi)))
162 (defun read-command-token (obj)
163 (gobble-whitespace)
164 (read-command-token-aux obj))
166 (defun safe-assoc (item lis)
167 "maclisp would not complain about (car 3), it gives nil"
168 (loop for v in lis
169 when (and (consp v)
170 (equal (car v) item))
172 (return v)))
174 ;; list contains an atom, only check
175 ;; (parser-assoc 1 '(2 1 3)) ==>(1 3)
176 ;; (parser-assoc 1 '(2 (1 4) 3)) ==>(1 4)
178 (defun parser-assoc (c lis )
179 (loop for v on lis
181 (cond ((consp (car v))
182 (if (eq (caar v) c)
183 (return (car v))))
184 ((eql (car v) c)
185 (return v)))))
187 ;; we need to be able to unparse-tyi an arbitrary number of
188 ;; characters, since if you do
189 ;; PREFIX("ABCDEFGH");
190 ;; then ABCDEFGA should read as a symbol.
191 ;; 99% of the time we don't have to unparse-tyi, and so there will
192 ;; be no consing...
194 (defun parse-tyi ()
195 (let ((tem *parse-tyi*))
196 (cond ((null tem)
197 (tyi-parse-int *parse-stream* *parse-stream-eof*))
198 ((atom tem)
199 (setq *parse-tyi* nil)
200 tem)
201 (t ;;consp
202 (setq *parse-tyi* (cdr tem))
203 (car tem)))))
205 ;; read one character but leave it there. so next parse-tyi gets it
206 (defun parse-tyipeek ()
207 (let ((tem *parse-tyi*))
208 (cond ((null tem)
209 (setq *parse-tyi* (tyi-parse-int *parse-stream* *parse-stream-eof*)))
210 ((atom tem) tem)
211 (t (car tem)))))
213 ;; push characters back on the stream
214 (defun unparse-tyi (c)
215 (let ((tem *parse-tyi*))
216 (if (null tem)
217 (setq *parse-tyi* c)
218 (setq *parse-tyi* (cons c tem)))))
220 ;;I know that the tradition says there should be no comments
221 ;;in tricky code in maxima. However the operator parsing
222 ;;gave me a bit of trouble. It was incorrect because
223 ;;it could not handle things produced by the extensions
224 ;;the following was broken for prefixes
226 (defun read-command-token-aux (obj)
227 (let* (result
228 (ch (parse-tyipeek))
229 (lis (if (eql ch *parse-stream-eof*)
231 (parser-assoc ch obj))))
232 (cond ((null lis)
233 nil)
235 (parse-tyi)
236 (cond ((atom (cadr lis))
237 ;; INFIX("ABC"); puts into macsyma-operators
238 ;;something like: (#\A #\B #\C (ANS $abc))
239 ;; ordinary things are like:
240 ;; (#\< (ANS $<) (#\= (ANS $<=)))
241 ;; where if you fail at the #\< #\X
242 ;; stage, then the previous step was permitted.
243 (setq result (read-command-token-aux (list (cdr lis)))))
244 ((null (cddr lis))
245 ;; lis something like (#\= (ANS $<=))
246 ;; and this says there are no longer operators
247 ;; starting with this.
248 (setq result
249 (and (eql (car (cadr lis)) 'ans)
250 ;; When we have an operator, which starts with a
251 ;; literal, we check, if the operator is
252 ;; followed with a whitespace. With this code
253 ;; Maxima parses an expression grad x or grad(x)
254 ;; as (($grad) x) and gradef(x) as (($gradef) x),
255 ;; when grad is defined as a prefix operator.
256 ;; See bug report ID: 2970792.
257 (or (not (alphabetp (cadr (exploden (cadr (cadr lis))))))
258 (member (parse-tyipeek) *whitespace-chars*))
259 (cadr (cadr lis)))))
261 (let ((res (and (eql (car (cadr lis)) 'ans)
262 (cadr (cadr lis))))
263 (com-token (read-command-token-aux (cddr lis) )))
264 (setq result (or com-token res
265 (read-command-token-aux (list (cadr lis))))))))
266 (or result (unparse-tyi ch))
267 result))))
269 (defun scan-macsyma-token ()
270 ;; note that only $-ed tokens are GETALIASed.
271 (getalias (implode (cons '#\$ (scan-token t)))))
273 (defun scan-lisp-token ()
274 (let ((charlist (scan-token nil)))
275 (if charlist
276 (implode charlist)
277 (mread-synerr "Lisp symbol expected."))))
279 ;; Example: ?mismatch(x+y,x*z,?:from\-end,true); => 3
280 (defun scan-keyword-token ()
281 (let ((charlist (cdr (scan-token nil))))
282 (if charlist
283 (let ((*package* (find-package :keyword)))
284 (implode charlist))
285 (mread-synerr "Lisp keyword expected."))))
287 (defun scan-token (flag)
288 (do ((c (parse-tyipeek) (parse-tyipeek))
289 (l () (cons c l)))
290 ((or (eql c *parse-stream-eof*)
291 (and flag
292 (not (or (digit-char-p c (max 10 *read-base*))
293 (alphabetp c)
294 (char= c #\\ )))))
295 (nreverse (or l (list (parse-tyi))))) ; Read at least one char ...
296 (when (char= (parse-tyi) #\\ )
297 (setq c (parse-tyi)))
298 (setq flag t)))
300 (defun scan-lisp-string () (scan-string))
301 (defun scan-macsyma-string () (scan-string))
303 (defun scan-string (&optional init)
304 (let ((buf (make-array 50 :element-type ' #.(array-element-type "a")
305 :fill-pointer 0 :adjustable t)))
306 (when init
307 (vector-push-extend init buf))
308 (do ((c (parse-tyipeek) (parse-tyipeek)))
309 ((cond ((eql c *parse-stream-eof*))
310 ((char= c #\")
311 (parse-tyi) t))
312 (copy-seq buf))
313 (if (char= (parse-tyi) #\\ )
314 (setq c (parse-tyi)))
315 (vector-push-extend c buf))))
317 (defun readlist (lis)
318 (read-from-string (coerce lis 'string)))
320 ;; These variables control how we convert bfloat inputs to the
321 ;; internal bfloat representation. These variables should probably go
322 ;; away after some testing.
323 (defmvar $fast_bfloat_conversion t
324 "Use fast, but possibly inaccurate conversion")
325 (defmvar $fast_bfloat_threshold 100000.
326 "Exponents larger than this (in absolute value) will use the fast
327 conversion instead of the accurate conversion")
328 (defvar *fast-bfloat-extra-bits* 0)
330 ;; Here is a test routine to test the fast bfloat conversion
331 #+nil
332 (defun test-make-number (&optional (n 1000))
333 (let ((failures 0))
334 (dotimes (k n)
335 (flet ((digit-list (n)
336 (coerce (format nil "~D" n) 'list)))
337 (let ((numlist nil))
338 ;; Generate a random number with 30 fraction digits and an
339 ;; large exponent.
340 (push (digit-list (random 10)) numlist)
341 (push '(#\.) numlist)
342 (push (digit-list (random (expt 10 30))) numlist)
343 (push '(#\B) numlist)
344 (push (if (zerop (random 2)) '(#\+) '(#\-)) numlist)
345 (push (digit-list (+ $fast_bfloat_threshold
346 (random $fast_bfloat_threshold)))
347 numlist)
348 ;; Convert using accurate and fast methods and compare the
349 ;; results.
350 (let ((true (let (($fast_bfloat_conversion nil))
351 (make-number (copy-list numlist))))
352 (fast (let (($fast_bfloat_conversion t))
353 (make-number (copy-list numlist)))))
354 (format t "Test ~3A: " k)
355 (map nil #'(lambda (x)
356 (map nil #'princ x))
357 (reverse numlist))
358 (terpri)
359 (finish-output)
360 (unless (equalp true fast)
361 (incf failures)
362 (format t "NUM: ~A~% TRUE: ~S~% FAST: ~S~%"
363 (reverse numlist) true fast))))))
364 (format t "~D failures in ~D tests (~F%)~%"
365 failures n (* 100 failures (/ (float n))))))
368 ;; WARNING: MAKE-NUMBER destructively modifies it argument! Should we
369 ;; change that?
370 (defun make-number (data)
371 (setq data (nreverse data))
372 ;; Maxima really wants to read in any number as a flonum
373 ;; (except when we have a bigfloat, of course!). So convert exponent
374 ;; markers to the flonum-exponent-marker.
375 (let ((marker (car (nth 3 data))))
376 (unless (eql marker flonum-exponent-marker)
377 (when (member marker '(#\E #\F #\S #\D #\L #+cmu #\W))
378 (setf (nth 3 data) (list flonum-exponent-marker)))))
379 (if (not (equal (nth 3 data) '(#\B)))
380 (readlist (apply #'append data))
381 (let*
382 ((*read-base* 10.)
383 (int-part (readlist (or (first data) '(#\0))))
384 (frac-part (readlist (or (third data) '(#\0))))
385 (frac-len (length (third data)))
386 (exp-sign (first (fifth data)))
387 (exp (readlist (sixth data))))
388 (if (and $fast_bfloat_conversion
389 (> (abs exp) $fast_bfloat_threshold))
390 ;; Exponent is large enough that we don't want to do exact
391 ;; rational arithmetic. Instead we do bfloat arithmetic.
392 ;; For example, 1.234b1000 is converted by computing
393 ;; bfloat(1234)*10b0^(1000-3). Extra precision is used
394 ;; during the bfloat computations.
395 (let* ((extra-prec (+ *fast-bfloat-extra-bits* (ceiling (log exp 2e0))))
396 (fpprec (+ fpprec extra-prec))
397 (mant (+ (* int-part (expt 10 frac-len)) frac-part))
398 (bf-mant (bcons (intofp mant)))
399 (p (power (bcons (intofp 10))
400 (- (if (char= exp-sign #\-)
401 (- exp)
402 exp)
403 frac-len)))
404 ;; Compute the product using extra precision. This
405 ;; helps to get the last bit correct (but not
406 ;; always). If we didn't do this, then bf-mant and
407 ;; p would be rounded to the target precision and
408 ;; then the product is rounded again. Doing it
409 ;; this way, we still have 3 roundings, but bf-mant
410 ;; and p aren't rounded too soon.
411 (result (mul bf-mant p)))
412 (let ((fpprec (- fpprec extra-prec)))
413 ;; Now round the product back to the desired precision.
414 (bigfloatp result)))
415 ;; For bigfloats, turn them into rational numbers then
416 ;; convert to bigfloat. Fix for the 0.25b0 # 2.5b-1 bug.
417 ;; Richard J. Fateman posted this fix to the Maxima list
418 ;; on 10 October 2005. Without this fix, some tests in
419 ;; rtestrationalize will fail. Used with permission.
420 (let ((ratio (* (+ int-part (* frac-part (expt 10 (- frac-len))))
421 (expt 10 (if (char= exp-sign #\-)
422 (- exp)
423 exp)))))
424 ($bfloat (cl-rat-to-maxima ratio)))))))
426 ;; Richard J. Fateman wrote the big float to rational code and the function
427 ;; cl-rat-to-maxmia.
429 (defun cl-rat-to-maxima (x)
430 (if (integerp x)
432 (list '(rat simp) (numerator x) (denominator x))))
434 (defun scan-digits (data continuation? continuation &optional exponent-p)
435 (do ((c (parse-tyipeek) (parse-tyipeek))
436 (l () (cons c l)))
437 ((not (and (characterp c) (digit-char-p c (max 10. *read-base*))))
438 (cond ((member c continuation?)
439 (funcall continuation (list* (ncons (char-upcase
440 (parse-tyi)))
441 (nreverse l)
442 data)))
443 ((and (null l) exponent-p)
444 ;; We're trying to parse the exponent part of a number,
445 ;; and we didn't get a value after the exponent marker.
446 ;; That's an error.
447 (mread-synerr "parser: incomplete number; missing exponent?"))
449 (make-number (cons (nreverse l) data)))))
450 (parse-tyi)))
452 (defun scan-number-after-dot (data)
453 (scan-digits data '(#\E #\e #\F #\f #\B #\b #\D #\d #\S #\s #\L #\l #+cmu #\W #+cmu #\w) #'scan-number-exponent))
455 (defun scan-number-exponent (data)
456 (push (ncons (if (or (char= (parse-tyipeek) #\+)
457 (char= (parse-tyipeek) #\-))
458 (parse-tyi)
459 #\+))
460 data)
461 (scan-digits data () () t))
463 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
464 ;;;;; ;;;;;
465 ;;;;; The Expression Parser ;;;;;
466 ;;;;; ;;;;;
467 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
469 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
470 ;;; ;;;
471 ;;; Based on a theory of parsing presented in: ;;;
472 ;;; ;;;
473 ;;; Pratt, Vaughan R., ``Top Down Operator Precedence,'' ;;;
474 ;;; ACM Symposium on Principles of Programming Languages ;;;
475 ;;; Boston, MA; October, 1973. ;;;
476 ;;; ;;;
477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 ;;; Implementation Notes ....
481 ;;; JPG Chars like ^A, ^B, ... get left around after interrupts and
482 ;;; should be thrown away by the scanner if not used as editing
483 ;;; commands.
485 ;;; KMP There is RBP stuff in DISPLA, too. Probably this sort of
486 ;;; data should all be in one place somewhere.
488 ;;; KMP Maybe the parser and/or scanner could use their own GC scheme
489 ;;; to recycle conses used in scan/parse from line to line which
490 ;;; really ought not be getting dynamically discarded and reconsed.
491 ;;; Alternatively, we could call RECLAIM explicitly on certain
492 ;;; pieces of structure which get used over and over. A
493 ;;; local-reclaim abstraction may want to be developed since this
494 ;;; stuff will always be needed, really. On small-address-space
495 ;;; machines, this could be overridden when the last DYNAMALLOC
496 ;;; GC barrier were passed (indicating that space was at a premium
497 ;;; -- in such case, real RECLAIM would be more economical -- or
498 ;;; would the code to control that be larger than the area locked
499 ;;; down ...?)
501 ;;; KMP GJC has a MAKE-EVALUATOR type package which could probably
502 ;;; replace the CALL-IF-POSSIBLE stuff used here.
503 ;;; [So it was written, so it was done. -gjc]
505 ;;; KMP DEFINE-SYMBOL and KILL-OPERATOR need to be redefined.
506 ;;; Probably these shouldn't be defined in this file anyway.
508 ;;; KMP The relationship of thisfile to SYNEX needs to be thought
509 ;;; out more carefully.
511 ;;; GJC Need macros for declaring INFIX, PREFIX, etc ops
513 ;;; GJC You know, PARSE-NARY isn't really needed it seems, since
514 ;;; the SIMPLIFIER makes the conversion of
515 ;;; ((MTIMES) ((MTIMES) A B) C) => ((MTIMES) A B C)
516 ;;; I bet you could get make "*" infix and nobody would
517 ;;; ever notice.
519 ;;; The following terms may be useful in deciphering this code:
521 ;;; NUD -- NUll left Denotation (op has nothing to its left (prefix))
522 ;;; LED -- LEft Denotation (op has something to left (postfix or infix))
524 ;;; LBP -- Left Binding Power (the stickiness to the left)
525 ;;; RBP -- Right Binding Power (the stickiness to the right)
528 ;;;; Macro Support
530 (defvar scan-buffered-token (list nil)
531 "put-back buffer for scanner, a state-variable of the reader")
533 (defun peek-one-token ()
534 (peek-one-token-g nil nil))
536 (defun peek-one-token-g (eof-ok? eof-obj)
537 (cond
538 ((car scan-buffered-token)
539 (cdr scan-buffered-token))
540 (t (rplacd scan-buffered-token (scan-one-token-g eof-ok? eof-obj))
541 (cdr (rplaca scan-buffered-token t)))))
543 (defun scan-one-token ()
544 (scan-one-token-g nil nil))
546 (defun scan-one-token-g (eof-ok? eof-obj)
547 (declare (special macsyma-operators))
548 (cond ((car scan-buffered-token)
549 (rplaca scan-buffered-token ())
550 (cdr scan-buffered-token))
551 ((read-command-token macsyma-operators))
553 (let ((test (parse-tyipeek)))
554 (cond ((eql test *parse-stream-eof*)
555 (parse-tyi)
556 (if eof-ok? eof-obj
557 (mread-synerr (intl:gettext "end of file while scanning expression."))))
558 ((eql test #\/)
559 (parse-tyi)
560 (cond ((char= (parse-tyipeek) #\*)
561 (parse-tyi)
562 (gobble-comment)
563 (scan-one-token-g eof-ok? eof-obj))
564 (t '$/)))
565 ((eql test #\.) (parse-tyi) ; Read the dot
566 (if (digit-char-p (parse-tyipeek) 10.)
567 (scan-number-after-dot (list (ncons #\.) nil))
568 '|$.|))
569 ((eql test #\")
570 (parse-tyi)
571 (scan-macsyma-string))
572 ((eql test #\?)
573 (parse-tyi)
574 (cond ((char= (parse-tyipeek) #\")
575 (parse-tyi)
576 (scan-lisp-string))
577 ((char= (parse-tyipeek) #\:)
578 (scan-keyword-token))
580 (scan-lisp-token))))
582 (if (digit-char-p test 10.)
583 (scan-number-before-dot ())
584 (scan-macsyma-token))))))))
586 ;; nested comments are permitted.
587 (defun gobble-comment ()
588 (prog (c depth)
589 (setq depth 1)
590 read
591 (setq c (parse-tyipeek))
592 (parse-tyi)
593 (cond ((= depth 0) (return t)))
594 (cond ((eql c *parse-stream-eof*)
595 (mread-synerr (intl:gettext "end of file in comment.")))
596 ((char= c #\*)
597 (cond ((char= (parse-tyipeek) #\/)
598 (decf depth)
599 (parse-tyi)
600 (cond ((= depth 0) (return t)))
601 (go read))))
602 ((char= c #\/)
603 (cond ((char= (parse-tyipeek) #\*)
604 (incf depth) (parse-tyi)
605 (go read)))))
606 (go read))
609 (defun scan-number-rest (data)
610 (let ((c (caar data)))
611 (cond ((char= c #\.)
612 ;; We found a dot
613 (scan-number-after-dot data))
614 ((member c '(#\E #\e #\F #\f #\B #\b #\D #\d #\S #\s #\L #\l #+cmu #\W #+cmu #\w))
615 ;; Dot missing but found exponent marker. Fake it.
616 (setf data (push (ncons #\.) (rest data)))
617 (push (ncons #\0) data)
618 (push (ncons c) data)
619 (scan-number-exponent data)))))
621 (defun scan-number-before-dot (data)
622 (scan-digits data '(#\. #\E #\e #\F #\f #\B #\b #\D #\d #\S #\s #\L #\l #+cmu #\W #+cmu #\w)
623 #'scan-number-rest))
626 ;; "First character" and "Pop character"
628 (defmacro first-c () '(peek-one-token))
629 (defmacro pop-c () '(scan-one-token))
631 (defun mstringp (x) (stringp x)) ;; OBSOLETE. PRESERVE FOR SAKE OF POSSIBLE CALLS FROM NON-MAXIMA CODE !!
633 (defun inherit-propl (op-to op-from getl)
634 (let ((propl (getl op-from getl)))
635 (if propl
636 (progn (remprop op-to (car propl))
637 (putprop op-to (cadr propl) (car propl)))
638 (inherit-propl op-to
639 (maxima-error "has no ~a properties. ~a ~a" getl op-from 'wrng-type-arg)
640 getl))))
643 ;;; (NUD <op>)
644 ;;; (LED <op> <left>)
646 ;;; <op> is the name of the operator which was just popped.
647 ;;; <left> is the stuff to the left of the operator in the LED case.
650 (eval-when
651 (:execute :compile-toplevel :load-toplevel)
652 (defmacro def-nud-equiv (op equiv)
653 (list 'putprop (list 'quote op) (list 'function equiv)
654 (list 'quote 'nud)))
656 (defmacro nud-propl () ''(nud))
658 (defmacro def-nud-fun (op-name op-l . body)
659 (list* 'defun-prop (list* op-name 'nud 'nil) op-l body))
661 (defmacro def-led-equiv (op equiv)
662 (list 'putprop (list 'quote op) (list 'function equiv)
663 (list 'quote 'led)))
665 (defmacro led-propl () ''(led))
667 (defmacro def-led-fun (op-name op-l . body)
668 (list* 'defun-prop (list* op-name 'led 'nil) op-l body)))
670 (defun nud-call (op)
671 (let ((tem (and (symbolp op) (getl op '(nud)))) res)
672 (setq res
673 (if (null tem)
674 (if (operatorp op)
675 (mread-synerr "~A is not a prefix operator" (mopstrip op))
676 (cons '$any op))
677 (funcall (cadr tem) op)))
678 res))
680 (defun led-call (op l)
681 (let ((tem (and (symbolp op) (getl op '(led)))) res)
682 (setq res
683 (if (null tem)
684 (mread-synerr "~A is not an infix operator" (mopstrip op))
685 (funcall (cadr tem) op l)))
686 res))
688 ;;; (DEF-NUD (op lbp rbp) bvl . body)
690 ;;; Defines a procedure for parsing OP as a prefix operator.
692 ;;; OP should be the name of the symbol as a string or symbol.
693 ;;; LBP is an optional left binding power for the operator.
694 ;;; RBP is an optional right binding power for the operator.
695 ;;; BVL must contain exactly one variable, which the compiler will not
696 ;;; complain about if unused, since it will rarely be of use anyway.
697 ;;; It will get bound to the operator being parsed.
698 ;;; lispm:Optional args not allowed in release 5 allowed, necessary afterwards..
700 (defmacro def-nud ((op . lbp-rbp) bvl . body)
701 (let (( lbp (nth 0 lbp-rbp))
702 ( rbp (nth 1 lbp-rbp)))
703 `(progn ,(make-parser-fun-def op 'nud bvl body)
704 (set-lbp-and-rbp ',op ',lbp ',rbp))))
706 (defun set-lbp-and-rbp (op lbp rbp)
707 (cond ((not (consp op))
708 (let ((existing-lbp (get op 'lbp))
709 (existing-rbp (get op 'rbp)))
710 (cond ((not lbp) ;; ignore omitted arg
712 ((not existing-lbp)
713 (putprop op lbp 'lbp))
714 ((not (equal existing-lbp lbp))
715 (maxima-error "Incompatible LBP's defined for this operator ~a" op)))
716 (cond ((not rbp) ;; ignore omitted arg
718 ((not existing-rbp)
719 (putprop op rbp 'rbp))
720 ((not (equal existing-rbp rbp))
721 (maxima-error "Incompatible RBP's defined for this operator ~a" op)))))
723 (mapcar #'(lambda (x) (set-lbp-and-rbp x lbp rbp))
724 op))))
726 ;;; (DEF-LED (op lbp rbp) bvl . body)
728 ;;; Defines a procedure for parsing OP as an infix or postfix operator.
730 ;;; OP should be the name of the symbol as a string or symbol.
731 ;;; LBP is an optional left binding power for the operator.
732 ;;; RBP is an optional right binding power for the operator.
733 ;;; BVL must contain exactly two variables, the first of which the compiler
734 ;;; will not complain about if unused, since it will rarely be of use
735 ;;; anyway. Arg1 will get bound to the operator being parsed. Arg2 will
736 ;;; get bound to the parsed structure which was to the left of Arg1.
739 (defmacro def-led((op . lbp-rbp) bvl . body)
740 (let (( lbp (nth 0 lbp-rbp))
741 ( rbp (nth 1 lbp-rbp)))
742 `(progn ,(make-parser-fun-def op 'led bvl body)
743 (set-lbp-and-rbp ',op ',lbp ',rbp))))
745 (defmacro def-collisions (op &rest alist)
746 (let ((keys (do ((i 1 (ash i 1))
747 (lis alist (cdr lis))
748 (nl () (cons (cons (caar lis) i) nl)))
749 ((null lis) nl))))
750 `(progn
751 (defprop ,op ,(let nil
752 (copy-tree keys )) keys)
753 ,@(mapcar #'(lambda (data)
754 `(defprop ,(car data)
755 ,(do ((i 0 (logior i (cdr (assoc (car lis) keys :test #'eq))))
756 (lis (cdr data) (cdr lis)))
757 ((null lis) i))
758 ,op))
759 alist))))
762 (defun collision-lookup (op active-bitmask key-bitmask)
763 (let ((result (logand active-bitmask key-bitmask)))
764 (if (not (zerop result))
765 (do ((l (get op 'keys) (cdr l)))
766 ((null l) (parse-bug-err 'collision-check))
767 (if (not (zerop (logand result (cdar l))))
768 (return (caar l)))))))
770 (defun collision-check (op active-bitmask key)
771 (let ((key-bitmask (get key op)))
772 (if (not key-bitmask)
773 (mread-synerr "~A is an unknown keyword in a ~A statement."
774 (mopstrip key) (mopstrip op)))
775 (let ((collision (collision-lookup op active-bitmask key-bitmask)))
776 (if collision
777 (if (eq collision key)
778 (mread-synerr "This ~A's ~A slot is already filled."
779 (mopstrip op)
780 (mopstrip key))
781 (mread-synerr "A ~A cannot have a ~A with a ~A field."
782 (mopstrip op)
783 (mopstrip key)
784 (mopstrip collision))))
785 (logior (cdr (assoc key (get op 'keys) :test #'eq)) active-bitmask))))
787 ;;;; Data abstraction
789 ;;; LBP = Left Binding Power
791 ;;; (LBP <op>) - reads an operator's Left Binding Power
792 ;;; (DEF-LBP <op> <val>) - defines an operator's Left Binding Power
794 (defun lbp (lex) (cond ((safe-get lex 'lbp)) (t 200.)))
796 (defmacro def-lbp (sym val) `(defprop ,sym ,val lbp))
798 ;;; RBP = Right Binding Power
800 ;;; (RBP <op>) - reads an operator's Right Binding Power
801 ;;; (DEF-RBP <op> <val>) - defines an operator's Right Binding Power
803 (defun rbp (lex) (cond ((safe-get lex 'rbp)) (t 200.)))
805 (defmacro def-rbp (sym val) `(defprop ,sym ,val rbp))
807 (defmacro def-match (x m) `(defprop ,x ,m match))
809 ;;; POS = Part of Speech!
811 ;;; (LPOS <op>)
812 ;;; (RPOS <op>)
813 ;;; (POS <op>)
816 (defun lpos (op) (cond ((safe-get op 'lpos)) (t '$any)))
817 (defun rpos (op) (cond ((safe-get op 'rpos)) (t '$any)))
818 (defun pos (op) (cond ((safe-get op 'pos)) (t '$any)))
820 (defmacro def-pos (op pos) `(defprop ,op ,pos pos))
821 (defmacro def-rpos (op pos) `(defprop ,op ,pos rpos))
822 (defmacro def-lpos (op pos) `(defprop ,op ,pos lpos))
824 ;;; MHEADER
826 (defun mheader (op) (add-lineinfo (or (safe-get op 'mheader) (ncons op))))
828 (defmacro def-mheader (op header) `(defprop ,op ,header mheader))
830 ;;;; Misplaced definitions
832 (defmacro def-operatorp ()
833 `(defun operatorp (lex)
834 (and (symbolp lex) (getl lex '(,@(nud-propl) ,@(led-propl))))))
836 (def-operatorp)
838 (defmacro def-operatorp1 ()
839 ;Defmfun -- used by SYNEX if not others.
840 `(defun operatorp1 (lex)
841 ;; Referenced outside of package: OP-SETUP, DECLARE1
842 ;; Use for truth value only, not for return-value.
843 (and (symbolp lex) (getl lex '(lbp rbp ,@(nud-propl) ,@(led-propl))))))
845 (def-operatorp1)
847 ;;;; The Macsyma Parser
849 ;;; (MREAD) with arguments compatible with losing maclisp READ style.
851 ;;; Returns a parsed form of tokens read from stream.
853 ;;; If you want rubout processing, be sure to call some stream which knows
854 ;;; about such things. Also, I'm figuring that the PROMPT will be
855 ;;; an attribute of the stream which somebody can hack before calling
856 ;;; MREAD if he wants to.
859 ;;Important for lispm rubout handler
860 (defun mread (&rest read-args)
861 (progn
862 (when *mread-prompt*
863 (and *parse-window* (setf (car *parse-window*) nil
864 *parse-window* (cdr *parse-window*)))
865 (princ *mread-prompt*)
866 (finish-output))
867 (apply 'mread-raw read-args)))
869 (defun mread-prompter (stream char)
870 (declare (special *mread-prompt-internal*)
871 (ignore char))
872 (fresh-line stream)
873 (princ *mread-prompt-internal* stream))
875 ;; input can look like:
876 ;;aa && bb && jim:3;
878 (defun mread-raw (*parse-stream* &optional *mread-eof-obj*)
879 (let ((scan-buffered-token (list nil))
880 *parse-tyi*)
881 (if (eq scan-buffered-token ;; a handly unique object for the EQ test.
882 (peek-one-token-g t scan-buffered-token))
883 *mread-eof-obj*
884 (do ((labels ())
885 (input (parse '$any 0.) (parse '$any 0.)))
886 (nil)
887 (case (first-c)
888 ((|$;| |$$|)
889 ;force a separate line info structure
890 (setf *current-line-info* nil)
891 (return (list (mheader (pop-c))
892 (if labels (cons (mheader '|$[|) (nreverse labels)))
893 input)))
894 ((|$&&|)
895 (pop-c)
896 (if (symbolp input)
897 (push input labels)
898 (mread-synerr "Invalid && tag. Tag must be a symbol")))
900 (parse-bug-err 'mread-raw)))))))
902 ;;; (PARSE <mode> <rbp>)
904 ;;; This will parse an expression containing operators which have a higher
905 ;;; left binding power than <rbp>, returning as soon as an operator of
906 ;;; lesser or equal binding power is seen. The result will be in the given
907 ;;; mode (which allows some control over the class of result expected).
908 ;;; Modes used are as follows:
909 ;;; $ANY = Match any type of expression
910 ;;; $CLAUSE = Match only boolean expressions (or $ANY)
911 ;;; $EXPR = Match only mathematical expressions (or $ANY)
912 ;;; If a mismatched mode occurs, a syntax error will be flagged. Eg,
913 ;;; this is why "X^A*B" parses but "X^A and B" does not. X^A is a $EXPR
914 ;;; and not coercible to a $CLAUSE. See CONVERT.
916 ;;; <mode> is the required mode of the result.
917 ;;; <rbp> is the right binding power to use for the parse. When an
918 ;;; LED-type operator is seen with a lower left binding power
919 ;;; than <rbp>, this parse returns what it's seen so far rather
920 ;;; than calling that operator.
923 (defun parse (mode rbp)
924 (do ((left (nud-call (pop-c)) ; Envoke the null left denotation
925 (led-call (pop-c) left))) ; and keep calling LED ops as needed
926 ((>= rbp (lbp (first-c))) ; Until next op lbp too low
927 (convert left mode)))) ; in which case, return stuff seen
929 ;;; (PARSE-PREFIX <op>)
931 ;;; Parses prefix forms -- eg, -X or NOT FOO.
933 ;;; This should be the NUD property on an operator. It fires after <op>
934 ;;; has been seen. It parses forward looking for one more expression
935 ;;; according to its right binding power, returning
936 ;;; ( <mode> . ((<op>) <arg1>) )
938 (defun parse-prefix (op)
939 (list (pos op) ; Operator mode
940 (mheader op) ; Standard Macsyma expression header
941 (parse (rpos op) (rbp op)))) ; Convert single argument for use
943 ;;; (PARSE-POSTFIX <op> <left>)
945 ;;; Parses postfix forms. eg, X!.
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>) )
951 (defun parse-postfix (op l)
952 (list (pos op) ; Operator's mode
953 (mheader op) ; Standard Macsyma expression header
954 (convert l (lpos op)))) ; Convert single argument for use
956 ;;; (PARSE-INFIX <op> <left>)
958 ;;; Parses infix (non-nary) forms. eg, 5 mod 3.
960 ;;; This should be the led property of an operator. It fires after <left>
961 ;;; has been accumulated and <op> has been seen and gobbled up. It returns
962 ;;; ( <mode> . ((<op>) <arg1> <arg2>) )
964 (defun parse-infix (op l)
965 (list (pos op) ; Operator's mode
966 (mheader op) ; Standard Macsyma expression header
967 (convert l (lpos op)) ; Convert arg1 for immediate use
968 (parse (rpos op) (rbp op)))) ; Look for an arg2
970 ;;; (PARSE-NARY <op> <left>)
972 ;;; Parses nary forms. Eg, form1*form2*... or form1+form2+...
973 ;;; This should be the LED property on an operator. It fires after <op>
974 ;;; has been seen, accumulating and returning
975 ;;; ( <mode> . ((<op>) <arg1> <arg2> ...) )
977 ;;; <op> is the being parsed.
978 ;;; <left> is the stuff that has been seen to the left of <op> which
979 ;;; rightly belongs to <op> on the basis of parse precedence rules.
981 (defun parse-nary (op l)
982 (list* (pos op) ; Operator's mode
983 (mheader op) ; Normal Macsyma operator header
984 (convert l (lpos op)) ; Check type-match of arg1
985 (prsnary op (lpos op) (lbp op)))) ; Search for other args
987 ;;; (PARSE-MATCHFIX <lop>)
989 ;;; Parses matchfix forms. eg, [form1,form2,...] or (form1,form2,...)
991 ;;; This should be the NUD property on an operator. It fires after <op>
992 ;;; has been seen. It parses <lop><form1>,<form2>,...<rop> returning
993 ;;; ( <mode> . ((<lop>) <form1> <form2> ...) ).
995 (defun parse-matchfix (op)
996 (list* (pos op) ; Operator's mode
997 (mheader op) ; Normal Macsyma operator header
998 (prsmatch (safe-get op 'match) (lpos op)))) ; Search for matchfixed forms
1000 ;;; (PARSE-NOFIX <op>)
1002 ;;; Parses an operator of no args. eg, @+X where @ designates a function
1003 ;;; call (eg, @() is implicitly stated by the lone symbol @.)
1005 ;;; This should be a NUD property on an operator which takes no args.
1006 ;;; It immediately returns ( <mode> . ((<op>)) ).
1008 ;;; <op> is the name of the operator.
1010 ;;; Note: This is not used by default and probably shouldn't be used by
1011 ;;; someone who doesn't know what he's doing. Example lossage. If @ is
1012 ;;; a nofix op, then @(3,4) parses, but parses as "@"()(3,4) would -- ie,
1013 ;;; to ((MQAPPLY) (($@)) 3 4) which is perhaps not what the user will expect.
1015 (defun parse-nofix (op) (list (pos op) (mheader op)))
1017 ;;; (PRSNARY <op> <mode> <rbp>)
1019 ;;; Parses an nary operator tail Eg, ...form2+form3+... or ...form2*form3*...
1021 ;;; Expects to be entered after the leading form and the first call to an
1022 ;;; nary operator has been seen and popped. Returns a list of parsed forms
1023 ;;; which belong to that operator. Eg, for X+Y+Z; this should be called
1024 ;;; after the first + is popped. Returns (Y Z) and leaves the ; token
1025 ;;; in the parser scan buffer.
1027 ;;; <op> is the nary operator in question.
1028 ;;; <rbp> is (LBP <op>) and is provided for efficiency. It is for use in
1029 ;;; recursive parses as a binding power to parse for.
1030 ;;; <mode> is the name of the mode that each form must be.
1032 (defun prsnary (op mode rbp)
1033 (do ((nl (list (parse mode rbp)) ; Get at least one form
1034 (cons (parse mode rbp) nl))) ; and keep getting forms
1035 ((not (eq op (first-c))) ; until a parse pops on a new op
1036 (nreverse nl)) ; at which time return forms
1037 (pop-c))) ; otherwise pop op
1039 ;;; (PRSMATCH <match> <mode>)
1041 ;;; Parses a matchfix sequence. Eg, [form1,form2,...] or (form1,form2,...)
1042 ;;; Expects to be entered after the leading token is the popped (ie, at the
1043 ;;; point where the parse of form1 will begin). Returns (form1 form2 ...).
1045 ;;; <match> is the token to look for as a matchfix character.
1046 ;;; <mode> is the name of the mode that each form must be.
1048 (defun prsmatch (match mode) ; Parse for matchfix char
1049 (cond ((eq match (first-c)) (pop-c) nil) ; If immediate match, ()
1050 (t ; Else, ...
1051 (do ((nl (list (parse mode 10.)) ; Get first element
1052 (cons (parse mode 10.) nl))) ; and Keep adding elements
1053 ((eq match (first-c)) ; Until we hit the match.
1054 (pop-c) ; Throw away match.
1055 (nreverse nl)) ; Put result back in order
1056 (if (eq '|$,| (first-c)) ; If not end, look for ","
1057 (pop-c) ; and pop it if it's there
1058 (mread-synerr "Missing ~A" ; or give an error message.
1059 (mopstrip match)))))))
1061 ;;; (CONVERT <exp> <mode>)
1063 ;;; Parser coercion function.
1065 ;;; <exp> should have the form ( <expressionmode> . <expression> )
1066 ;;; <mode> is the target mode.
1068 ;;; If <expressionmode> and <mode> are compatible, returns <expression>.
1070 (defun convert (item mode)
1071 (if (or (eq mode (car item)) ; If modes match exactly
1072 (eq '$any mode) ; or target is $ANY
1073 (eq '$any (car item))) ; or input is $ANY
1074 (cdr item) ; then return expression
1075 (mread-synerr "Found ~A expression where ~A expression expected"
1076 (get (car item) 'english)
1077 (get mode 'english))))
1079 (defprop $any "untyped" english)
1080 (defprop $clause "logical" english)
1081 (defprop $expr "algebraic" english)
1083 ;;;; Parser Error Diagnostics
1085 ;; Call this for random user-generated parse errors
1087 (defun parse-err () (mread-synerr "Syntax error"))
1089 ;; Call this for random internal parser lossage (eg, code that shouldn't
1090 ;; be reachable.)
1092 (defun parse-bug-err (op)
1093 (mread-synerr
1094 "Parser bug in ~A. Please report this to the Maxima maintainers,~
1095 ~%including the characters you just typed which caused the error. Thanks."
1096 (mopstrip op)))
1098 ;;; Random shared error messages
1100 (defun delim-err (op)
1101 (mread-synerr "Illegal use of delimiter ~A" (mopstrip op)))
1103 (defun erb-err (op l) l ;Ignored
1104 (mread-synerr "Too many ~A's" (mopstrip op)))
1106 (defun premterm-err (op)
1107 (mread-synerr "Premature termination of input at ~A."
1108 (mopstrip op)))
1110 ;;;; Operator Specific Data
1112 (def-nud-equiv |$]| delim-err)
1113 (def-led-equiv |$]| erb-err)
1114 (def-lbp |$]| 5.)
1116 (def-nud-equiv |$[| parse-matchfix)
1117 (def-match |$[| |$]|)
1118 (def-lbp |$[| 200.)
1119 ;No RBP
1120 (def-mheader |$[| (mlist))
1121 (def-pos |$[| $any)
1122 (def-lpos |$[| $any)
1123 ;No RPOS
1125 (def-led (|$[| 200.) (op left)
1126 (setq left (convert left '$any))
1127 (if (numberp left) (parse-err)) ; number[...] invalid
1128 (let ((header (if (atom left)
1129 (add-lineinfo (list (amperchk left) 'array))
1130 (add-lineinfo '(mqapply array))))
1131 (right (prsmatch '|$]| '$any))) ; get sublist in RIGHT
1132 (cond ((null right) ; 1 subscript minimum
1133 (mread-synerr "No subscripts given"))
1134 ((atom left) ; atom[...]
1135 (setq right (cons header
1136 right))
1137 (cons '$any (aliaslookup right)))
1138 (t ; exp[...]
1139 (cons '$any (cons header
1140 (cons left right)))))))
1143 (def-nud-equiv |$)| delim-err)
1144 (def-led-equiv |$)| erb-err)
1145 (def-lbp |$)| 5.)
1147 (def-mheader |$(| (mprogn))
1149 ;; KMP: This function optimizes out (exp) into just exp.
1150 ;; This is useful for mathy expressions, but obnoxious for non-mathy
1151 ;; expressions. I think DISPLA should be made smart about such things,
1152 ;; but probably the (...) should be carried around in the internal
1153 ;; representation. This would make things like BUILDQ much easier to
1154 ;; work with.
1155 ;; GJC: CGOL has the same behavior, so users tend to write extensions
1156 ;; to the parser rather than write Macros per se. The transformation
1157 ;; "(EXP)" ==> "EXP" is done by the evaluator anyway, the problem
1158 ;; comes inside quoted expressions. There are many other problems with
1159 ;; the "QUOTE" concept however.
1161 (def-nud (|$(| 200.) (op)
1162 (let ((right)(hdr (mheader '|$(|))) ; make mheader first for lineinfo
1163 (cond ((eq '|$)| (first-c)) (parse-err)) ; () is illegal
1164 ((or (null (setq right (prsmatch '|$)| '$any))) ; No args to MPROGN??
1165 (cdr right)) ; More than one arg.
1166 (when (suspicious-mprogn-p right)
1167 (mtell (intl:gettext "warning: parser: I'll let it stand, but (...) doesn't recognize local variables.~%"))
1168 (mtell (intl:gettext "warning: parser: did you mean to say: block(~M, ...) ?~%") (car right)))
1169 (cons '$any (cons hdr right))) ; Return an MPROGN
1170 (t (cons '$any (car right)))))) ; Optimize out MPROGN
1172 (defun suspicious-mprogn-p (right)
1173 ;; Look for a Maxima list of symbols or assignments to symbols.
1174 (and ($listp (car right))
1175 (every #'(lambda (e) (or (symbolp e)
1176 (and (consp e) (eq (caar e) 'msetq) (symbolp (second e)))))
1177 (rest (car right)))))
1179 (def-led (|$(| 200.) (op left)
1180 (setq left (convert left '$any)) ;De-reference LEFT
1181 (if (numberp left) (parse-err)) ;number(...) illegal
1182 (let ((hdr (and (atom left)(mheader (amperchk left))))
1183 (r (prsmatch '|$)| '$any)) ;Get arglist in R
1185 (cons '$any ;Result is type $ANY
1186 (cond ((atom left) ;If atom(...) =>
1187 (cons hdr r)) ;(($atom) exp . args)
1188 (t ;Else exp(...) =>
1189 (cons '(mqapply) (cons left r))))))) ;((MQAPPLY) op . args)
1191 (def-mheader |$'| (mquote))
1193 (def-nud (|$'|) (op)
1194 (let (right)
1195 (cond ((eq '|$(| (first-c))
1196 (list '$any (mheader '|$'|) (parse '$any 190.)))
1197 ((or (atom (setq right (parse '$any 190.)))
1198 (member (caar right) '(mquote mlist $set mprog mprogn lambda) :test #'eq))
1199 (list '$any (mheader '|$'|) right))
1200 ((eq 'mqapply (caar right))
1201 (cond ((eq (caaadr right) 'lambda)
1202 (list '$any (mheader '|$'|) right))
1203 (t (rplaca (cdr right)
1204 (cons (cons ($nounify (caaadr right))
1205 (cdaadr right))
1206 (cdadr right)))
1207 (cons '$any right))))
1208 (t (cons '$any (cons (cons ($nounify (caar right)) (cdar right))
1209 (cdr right)))))))
1211 (def-nud (|$''|) (op)
1212 (let (right)
1213 (cons '$any
1214 (cond ((eq '|$(| (first-c)) (meval (parse '$any 190.)))
1215 ((atom (setq right (parse '$any 190.))) (meval1 right))
1216 ((eq 'mqapply (caar right))
1217 (rplaca (cdr right)
1218 (cons (cons ($verbify (caaadr right)) (cdaadr right))
1219 (cdadr right)))
1220 right)
1221 (t (cons (cons ($verbify (caar right)) (cdar right))
1222 (cdr right)))))))
1224 (def-led-equiv |$:| parse-infix)
1225 (def-lbp |$:| 180.)
1226 (def-rbp |$:| 20.)
1227 (def-pos |$:| $any)
1228 (def-rpos |$:| $any)
1229 (def-lpos |$:| $any)
1230 (def-mheader |$:| (msetq))
1232 (def-led-equiv |$::| parse-infix)
1233 (def-lbp |$::| 180.)
1234 (def-rbp |$::| 20.)
1235 (def-pos |$::| $any)
1236 (def-rpos |$::| $any)
1237 (def-lpos |$::| $any)
1238 (def-mheader |$::| (mset))
1240 (def-led-equiv |$:=| parse-infix)
1241 (def-lbp |$:=| 180.)
1242 (def-rbp |$:=| 20.)
1243 (def-pos |$:=| $any)
1244 (def-rpos |$:=| $any)
1245 (def-lpos |$:=| $any)
1246 (def-mheader |$:=| (mdefine))
1248 (def-led-equiv |$::=| parse-infix)
1249 (def-lbp |$::=| 180.)
1250 (def-rbp |$::=| 20.)
1251 (def-pos |$::=| $any)
1252 (def-rpos |$::=| $any)
1253 (def-lpos |$::=| $any)
1254 (def-mheader |$::=| (mdefmacro))
1256 (def-led-equiv |$!| parse-postfix)
1257 (def-lbp |$!| 160.)
1258 ;No RBP
1259 (def-pos |$!| $expr)
1260 (def-lpos |$!| $expr)
1261 ;No RPOS
1262 (def-mheader |$!| (mfactorial))
1264 (def-mheader |$!!| ($genfact))
1266 (def-led (|$!!| 160.) (op left)
1267 (list '$expr
1268 (mheader '$!!)
1269 (convert left '$expr)
1270 (list (mheader '$/) (convert left '$expr) 2)
1273 (def-lbp |$^| 140.)
1274 (def-rbp |$^| 139.)
1275 (def-pos |$^| $expr)
1276 (def-lpos |$^| $expr)
1277 (def-rpos |$^| $expr)
1278 (def-mheader |$^| (mexpt))
1280 (def-led ((|$^| |$^^|)) (op left)
1281 (cons '$expr
1282 (aliaslookup (list (mheader op)
1283 (convert left (lpos op))
1284 (parse (rpos op) (rbp op))))))
1286 (mapc #'(lambda (prop) ; Make $** like $^
1287 (let ((propval (get '$^ prop)))
1288 (if propval (putprop '$** propval prop))))
1289 '(lbp rbp pos rpos lpos mheader))
1291 (inherit-propl '$** '$^ (led-propl))
1293 (def-lbp |$^^| 140.)
1294 (def-rbp |$^^| 139.)
1295 (def-pos |$^^| $expr)
1296 (def-lpos |$^^| $expr)
1297 (def-rpos |$^^| $expr)
1298 (def-mheader |$^^| (mncexpt))
1300 ;; note y^^4.z gives an error because it scans the number 4 together with
1301 ;; the trailing '.' as a decimal place. I think the error is correct.
1302 (def-led-equiv |$.| parse-infix)
1303 (def-lbp |$.| 130.)
1304 (def-rbp |$.| 129.)
1305 (def-pos |$.| $expr)
1306 (def-lpos |$.| $expr)
1307 (def-rpos |$.| $expr)
1308 (def-mheader |$.| (mnctimes))
1310 ;; Copy properties to noun operator.
1311 (setf (get '%mnctimes 'op) (get 'mnctimes 'op))
1313 (def-led-equiv |$*| parse-nary)
1314 (def-lbp |$*| 120.)
1315 ;RBP not needed
1316 (def-pos |$*| $expr)
1317 ;RPOS not needed
1318 (def-lpos |$*| $expr)
1319 (def-mheader |$*| (mtimes))
1321 (def-led-equiv $/ parse-infix)
1322 (def-lbp $/ 120.)
1323 (def-rbp $/ 120.)
1324 (def-pos $/ $expr)
1325 (def-rpos $/ $expr)
1326 (def-lpos $/ $expr)
1327 (def-mheader $/ (mquotient))
1329 (def-nud-equiv |$+| parse-prefix)
1330 (def-lbp |$+| 100.)
1331 (def-rbp |$+| 134.) ; Value increased from 100 to 134 (DK 02/2010).
1332 (def-pos |$+| $expr)
1333 (def-rpos |$+| $expr)
1334 ;LPOS not needed
1335 (def-mheader |$+| (mplus))
1337 (def-led ((|$+| |$-|) 100.) (op left)
1338 (setq left (convert left '$expr))
1339 (do ((nl (list (if (eq op '$-)
1340 (list (mheader '$-) (parse '$expr 100.))
1341 (parse '$expr 100.))
1342 left)
1343 (cons (parse '$expr 100.) nl)))
1344 ((not (member (first-c) '($+ $-) :test #'eq))
1345 (list* '$expr (mheader '$+) (nreverse nl)))
1346 (if (eq (first-c) '$+) (pop-c))))
1348 (def-nud-equiv |$-| parse-prefix)
1349 (def-lbp |$-| 100.)
1350 (def-rbp |$-| 134.)
1351 (def-pos |$-| $expr)
1352 (def-rpos |$-| $expr)
1353 ;LPOS not needed
1354 (def-mheader |$-| (mminus))
1356 (def-led-equiv |$=| parse-infix)
1357 (def-lbp |$=| 80.)
1358 (def-rbp |$=| 80.)
1359 (def-pos |$=| $clause)
1360 (def-rpos |$=| $expr)
1361 (def-lpos |$=| $expr)
1362 (def-mheader |$=| (mequal))
1364 (def-led-equiv |$#| parse-infix)
1365 (def-lbp |$#| 80.)
1366 (def-rbp |$#| 80.)
1367 (def-pos |$#| $clause)
1368 (def-rpos |$#| $expr)
1369 (def-lpos |$#| $expr)
1370 (def-mheader |$#| (mnotequal))
1372 (def-led-equiv |$>| parse-infix)
1373 (def-lbp |$>| 80.)
1374 (def-rbp |$>| 80.)
1375 (def-pos |$>| $clause)
1376 (def-rpos |$>| $expr)
1377 (def-lpos |$>| $expr)
1378 (def-mheader |$>| (mgreaterp))
1380 (def-led-equiv |$>=| parse-infix)
1381 (def-lbp |$>=| 80.)
1382 (def-rbp |$>=| 80.)
1383 (def-pos |$>=| $clause)
1384 (def-rpos |$>=| $expr)
1385 (def-lpos |$>=| $expr)
1386 (def-mheader |$>=| (mgeqp))
1388 (def-led-equiv |$<| parse-infix)
1389 (def-lbp |$<| 80.)
1390 (def-rbp |$<| 80.)
1391 (def-pos |$<| $clause)
1392 (def-rpos |$<| $expr)
1393 (def-lpos |$<| $expr)
1394 (def-mheader |$<| (mlessp))
1396 (def-led-equiv |$<=| parse-infix)
1397 (def-lbp |$<=| 80.)
1398 (def-rbp |$<=| 80.)
1399 (def-pos |$<=| $clause)
1400 (def-rpos |$<=| $expr)
1401 (def-lpos |$<=| $expr)
1402 (def-mheader |$<=| (mleqp))
1404 (def-nud-equiv $not parse-prefix)
1405 ;LBP not needed
1406 (def-rbp $not 70.)
1407 (def-pos $not $clause)
1408 (def-rpos $not $clause)
1409 (def-lpos $not $clause)
1410 (def-mheader $not (mnot))
1412 (def-led-equiv $and parse-nary)
1413 (def-lbp $and 65.)
1414 ;RBP not needed
1415 (def-pos $and $clause)
1416 ;RPOS not needed
1417 (def-lpos $and $clause)
1418 (def-mheader $and (mand))
1420 (def-led-equiv $or parse-nary)
1421 (def-lbp $or 60.)
1422 ;RBP not needed
1423 (def-pos $or $clause)
1424 ;RPOS not needed
1425 (def-lpos $or $clause)
1426 (def-mheader $or (mor))
1428 (def-led-equiv |$,| parse-nary)
1429 (def-lbp |$,| 10.)
1430 ;RBP not needed
1431 (def-pos |$,| $any)
1432 ;RPOS not needed
1433 (def-lpos |$,| $any)
1434 (def-mheader |$,| ($ev))
1436 (def-nud-equiv $then delim-err)
1437 (def-lbp $then 5.)
1438 (def-rbp $then 25.)
1440 (def-nud-equiv $else delim-err)
1441 (def-lbp $else 5.)
1442 (def-rbp $else 25.)
1444 (def-nud-equiv $elseif delim-err)
1445 (def-lbp $elseif 5.)
1446 (def-rbp $elseif 45.)
1447 (def-pos $elseif $any)
1448 (def-rpos $elseif $clause)
1450 ;No LBP - Default as high as possible
1451 (def-rbp $if 45.)
1452 (def-pos $if $any)
1453 (def-rpos $if $clause)
1454 ;No LPOS
1455 (def-mheader $if (mcond))
1457 (def-nud ($if) (op)
1458 (list* (pos op)
1459 (mheader op)
1460 (parse-condition op)))
1462 (defun parse-condition (op)
1463 (list* (parse (rpos op) (rbp op))
1464 (if (eq (first-c) '$then)
1465 (parse '$any (rbp (pop-c)))
1466 (mread-synerr "Missing `then'"))
1467 (case (first-c)
1468 (($else) (list t (parse '$any (rbp (pop-c)))))
1469 (($elseif) (parse-condition (pop-c)))
1470 (t (list t '$false)))))
1472 (def-mheader $do (mdo))
1474 (defun parse-$do (lex &aux (left (make-mdo)))
1475 (setf (car left) (mheader 'mdo))
1476 (do ((op lex (pop-c)) (active-bitmask 0))
1477 (nil)
1478 (if (eq op '|$:|) (setq op '$from))
1479 (setq active-bitmask (collision-check '$do active-bitmask op))
1480 (let ((data (parse (rpos op) (rbp op))))
1481 (case op
1482 ($do (setf (mdo-body left) data) (return (cons '$any left)))
1483 ($for (setf (mdo-for left) data))
1484 ($from (setf (mdo-from left) data))
1485 ($in (setf (mdo-op left) 'mdoin)
1486 (setf (mdo-from left) data))
1487 ($step (setf (mdo-step left) data))
1488 ($next (setf (mdo-next left) data))
1489 ($thru (setf (mdo-thru left) data))
1490 (($unless $while)
1491 (if (eq op '$while)
1492 (setq data (list (mheader '$not) data)))
1493 (setf (mdo-unless left)
1494 (if (null (mdo-unless left))
1495 data
1496 (list (mheader '$or) data (mdo-unless left)))))
1497 (t (parse-bug-err '$do))))))
1499 (def-lbp $for 25.)
1500 (def-lbp $from 25.)
1501 (def-lbp $step 25.)
1502 (def-lbp $next 25.)
1503 (def-lbp $thru 25.)
1504 (def-lbp $unless 25.)
1505 (def-lbp $while 25.)
1506 (def-lbp $do 25.)
1508 (def-nud-equiv $for parse-$do)
1509 (def-nud-equiv $from parse-$do)
1510 (def-nud-equiv $step parse-$do)
1511 (def-nud-equiv $next parse-$do)
1512 (def-nud-equiv $thru parse-$do)
1513 (def-nud-equiv $unless parse-$do)
1514 (def-nud-equiv $while parse-$do)
1515 (def-nud-equiv $do parse-$do)
1517 (def-rbp $do 25.)
1518 (def-rbp $for 200.)
1519 (def-rbp $from 95.)
1520 (def-rbp $in 95.)
1521 (def-rbp $step 95.)
1522 (def-rbp $next 45.)
1523 (def-rbp $thru 95.)
1524 (def-rbp $unless 45.)
1525 (def-rbp $while 45.)
1527 (def-rpos $do $any)
1528 (def-rpos $for $any)
1529 (def-rpos $from $any)
1530 (def-rpos $step $expr)
1531 (def-rpos $next $any)
1532 (def-rpos $thru $expr)
1533 (def-rpos $unless $clause)
1534 (def-rpos $while $clause)
1537 (def-collisions $do
1538 ($do . ())
1539 ($for . ($for))
1540 ($from . ($in $from))
1541 ($in . ($in $from $step $next))
1542 ($step . ($in $step $next))
1543 ($next . ($in $step $next))
1544 ($thru . ($in $thru)) ;$IN didn't used to get checked for
1545 ($unless . ())
1546 ($while . ()))
1548 (def-mheader |$$| (nodisplayinput))
1549 (def-nud-equiv |$$| premterm-err)
1550 (def-lbp |$$| -1)
1551 ;No RBP, POS, RPOS, RBP, or MHEADER
1553 (def-mheader |$;| (displayinput))
1554 (def-nud-equiv |$;| premterm-err)
1555 (def-lbp |$;| -1)
1556 ;No RBP, POS, RPOS, RBP, or MHEADER
1558 (def-nud-equiv |$&&| delim-err)
1559 (def-lbp |$&&| -1)
1561 (defun mopstrip (x)
1562 ;; kludge interface function to allow the use of lisp PRINC in places.
1563 (cond ((null x) 'false)
1564 ((or (eq x t) (eq x 't)) 'true)
1565 ((numberp x) x)
1566 ((symbolp x)
1567 (or (get x 'reversealias)
1568 (let ((name (symbol-name x)))
1569 (if (member (char name 0) '(#\$ #\%) :test #'char=)
1570 (subseq name 1)
1571 name))))
1572 (t x)))
1574 (define-initial-symbols
1575 ;; * Note: /. is looked for explicitly rather than
1576 ;; existing in this chart. The reason is that
1577 ;; it serves a dual role (as a decimal point) and
1578 ;; must be special-cased.
1580 ;; Same for // because of the /* ... */ handling
1581 ;; by the tokenizer
1582 ;; Single character
1583 |+| |-| |*| |^| |<| |=| |>| |(| |)| |[| |]| |,|
1584 |:| |!| |#| |'| |;| |$| |&|
1585 ;;Two character
1586 |**| |^^| |:=| |::| |!!| |<=| |>=| |''| |&&|
1587 ;; Three character
1588 |::=|
1591 ;; !! FOLLOWING MOVED HERE FROM MLISP.LISP (DEFSTRUCT STUFF)
1592 ;; !! SEE NOTE THERE
1593 (define-symbol "@")
1595 ;;; User extensibility:
1596 (defmfun $prefix (operator &optional (rbp 180.)
1597 (rpos '$any)
1598 (pos '$any))
1599 (def-operator operator pos () () rbp rpos () t
1600 '(nud . parse-prefix) 'msize-prefix 'dimension-prefix () )
1601 operator)
1603 (defmfun $postfix (operator &optional (lbp 180.)
1604 (lpos '$any)
1605 (pos '$any))
1606 (def-operator operator pos lbp lpos () () t ()
1607 '(led . parse-postfix) 'msize-postfix 'dimension-postfix () )
1608 operator)
1610 (defmfun $infix (operator &optional (lbp 180.)
1611 (rbp 180.)
1612 (lpos '$any)
1613 (rpos '$any)
1614 (pos '$any))
1615 (def-operator operator pos lbp lpos rbp rpos t t
1616 '(led . parse-infix) 'msize-infix 'dimension-infix () )
1617 operator)
1619 (defmfun $nary (operator &optional (bp 180.)
1620 (argpos '$any)
1621 (pos '$any))
1622 (def-operator operator pos bp argpos bp () t t
1623 '(led . parse-nary) 'msize-nary 'dimension-nary () )
1624 operator)
1626 (defmfun $matchfix (operator
1627 match &optional (argpos '$any)
1628 (pos '$any))
1629 ;shouldn't MATCH be optional?
1630 (def-operator operator pos () argpos () () () ()
1631 '(nud . parse-matchfix) 'msize-matchfix 'dimension-match match)
1632 operator)
1634 (defmfun $nofix (operator &optional (pos '$any))
1635 (def-operator operator pos () () () () () ()
1636 '(nud . parse-nofix) 'msize-nofix 'dimension-nofix () )
1637 operator)
1639 ;;; (DEF-OPERATOR op pos lbp lpos rbp rpos sp1 sp2
1640 ;;; parse-data grind-fn dim-fn match)
1641 ;;; OP is the operator name.
1642 ;;; POS is its ``part of speech.''
1643 ;;; LBP is its ``left binding power.''
1644 ;;; LPOS is the part of speech of the arguments to its left, or of all.
1645 ;;; arguments for NARY and MATCHFIX.
1646 ;;; RBP is its ``right binding power.''
1647 ;;; RPOS is the part of speech of the argument to its right.
1648 ;;; SP1 says if the DISSYM property needs a space on the right.
1649 ;;; SP2 says if the DISSYM property needs a space on the left.
1650 ;;; PARSE-DATA is (prop . fn) -- parser prop name dotted with function name
1651 ;;; GRIND-FN is the grinder function for the operator.
1652 ;;; DIM-FN is the dimension function for the operator.
1653 ;;; PARSEPROP is the property name to use for parsing. One of LED or NUD.
1654 ;;; MATCH if non-(), ignores SP1 and SP2. Should be the match symbol.
1655 ;;; sets OP up as matchfix with MATCH.
1657 ;;; For more complete descriptions of these naming conventions, see
1658 ;;; the comments in GRAM package, which describe them in reasonable detail.
1660 (defun def-operator (op pos lbp lpos rbp rpos sp1 sp2
1661 parse-data grind-fn dim-fn match)
1662 (let ((x))
1663 (if (or (and rbp (not (integerp (setq x rbp))))
1664 (and lbp (not (integerp (setq x lbp)))))
1665 (merror (intl:gettext "syntax extension: binding powers must be integers; found: ~M") x))
1666 (if (stringp op) (setq op (define-symbol op)))
1667 (op-setup op)
1668 (let ((noun ($nounify op))
1669 (dissym (cdr (exploden op))))
1670 (cond
1671 ((not match)
1672 (setq dissym (append (if sp1 '(#\space)) dissym (if sp2 '(#\space)))))
1673 (t (if (stringp match) (setq match (define-symbol match)))
1674 (op-setup match)
1675 (putprop op match 'match)
1676 (putprop match 5. 'lbp)
1677 (setq dissym (cons dissym (cdr (exploden match))))))
1678 (putprop op pos 'pos)
1679 (putprop op (cdr parse-data) (car parse-data))
1680 (putprop op grind-fn 'grind)
1681 (putprop op dim-fn 'dimension)
1682 (putprop noun dim-fn 'dimension)
1683 (putprop op dissym 'dissym)
1684 (putprop noun dissym 'dissym)
1685 (when rbp
1686 (putprop op rbp 'rbp)
1687 (putprop noun rbp 'rbp))
1688 (when lbp
1689 (putprop op lbp 'lbp)
1690 (putprop noun lbp 'lbp))
1691 (when lpos (putprop op lpos 'lpos))
1692 (when rpos (putprop op rpos 'rpos))
1693 (getopr op))))
1695 (defun op-setup (op)
1696 (let ((dummy (or (get op 'op)
1697 (coerce (string* op) 'string))))
1698 (putprop op dummy 'op )
1699 (putopr dummy op)
1700 (if (and (operatorp1 op) (not (member dummy (cdr $props) :test #'eq)))
1701 (push dummy *mopl*))
1702 (add2lnc dummy $props)))
1704 (defun kill-operator (op)
1705 (let
1706 ((opr (get op 'op))
1707 (noun-form ($nounify op)))
1708 ;; Refuse to kill an operator which appears on *BUILTIN-$PROPS*.
1709 (unless (member opr *builtin-$props* :test #'equal)
1710 (undefine-symbol opr)
1711 (remopr opr)
1712 (rempropchk opr)
1713 (mapc #'(lambda (x) (remprop op x))
1714 '(nud nud-expr nud-subr ; NUD info
1715 led led-expr led-subr ; LED info
1716 lbp rbp ; Binding power info
1717 lpos rpos pos ; Part-Of-Speech info
1718 grind dimension dissym ; Display info
1719 op)) ; Operator info
1720 (mapc #'(lambda (x) (remprop noun-form x))
1721 '(dimension dissym lbp rbp)))))
1725 ;; the functions get-instream etc.. are all defined in
1726 ;; gcl lsp/debug.lsp
1727 ;; they are all generic common lisp and could be used by
1728 ;; any Common lisp implementation.
1730 #-gcl
1731 (defstruct instream
1732 stream
1733 (line 0 :type fixnum)
1734 stream-name)
1736 #-gcl
1737 (defvar *stream-alist* nil)
1739 #-gcl
1740 (defun stream-name (path)
1741 (let ((errset nil))
1742 (car (errset (namestring (pathname path))))))
1744 #-gcl
1745 (defun instream-name (instr)
1746 (or (instream-stream-name instr)
1747 (stream-name (instream-stream instr))))
1749 ;; (closedp stream) checks if a stream is closed.
1750 ;; how to do this in common lisp!!
1752 #-gcl
1753 (defun cleanup ()
1754 #+never-clean-up-dont-know-how-to-close
1755 (dolist (v *stream-alist*)
1756 (if (closedp (instream-stream v))
1757 (setq *stream-alist* (delete v *stream-alist*)))))
1759 #-gcl
1760 (defun get-instream (str)
1761 (or (dolist (v *stream-alist*)
1762 (cond ((eq str (instream-stream v))
1763 (return v))))
1764 (let (name errset)
1765 (errset (setq name (namestring str)))
1766 (car (setq *stream-alist*
1767 (cons (make-instream :stream str :stream-name name)
1768 *stream-alist*))))))
1770 (defun newline (str)
1771 (incf (instream-line (get-instream str)))
1772 (values))
1774 (defun find-stream (stream)
1775 (dolist (v *stream-alist*)
1776 (cond ((eq stream (instream-stream v))
1777 (return v)))))
1780 (defun add-lineinfo (lis)
1781 (if (or (atom lis)
1782 (eq *parse-stream* *parse-string-input-stream*) ;; avoid consing *parse-string-input-stream*
1783 ;; via get-instream to *stream-alist*
1784 (and (eq *parse-window* *standard-input*)
1785 (not (find-stream *parse-stream*)) ))
1787 (let* ((st (get-instream *parse-stream*))
1788 (n (instream-line st))
1789 (nam (instream-name st)))
1790 (or nam (return-from add-lineinfo lis))
1791 (setq *current-line-info*
1792 (cond ((eq (cadr *current-line-info*) nam)
1793 (cond ((eql (car *current-line-info*) n)
1794 *current-line-info*)
1795 (t (cons n (cdr *current-line-info*)))))
1796 (t (list n nam 'src))))
1797 (cond ((null (cdr lis))
1798 (list (car lis) *current-line-info*))
1799 (t (append lis (list *current-line-info*)))))))
1801 ;; Remove debugging stuff.
1802 ;; STRIP-LINEINFO does not modify EXPR.
1804 (defun strip-lineinfo (expr)
1805 (if (or (atom expr) (specrepp expr))
1806 expr
1807 (cons (strip-lineinfo-op (car expr)) (mapcar #'strip-lineinfo (cdr expr)))))
1809 ;; If something in the operator looks like debugging stuff, remove it.
1810 ;; It is assumed here that debugging stuff is a list comprising an integer and a string
1811 ;; (and maybe other stuff, which is ignored).
1813 (defun strip-lineinfo-op (maxima-op)
1814 (remove-if #'(lambda (x) (and (consp x) (integerp (first x)) (stringp (second x)))) maxima-op))