source: CGBLisp/trunk/src/infix-printer.lisp@ 36

Last change on this file since 36 was 36, checked in by Marek Rychlik, 15 years ago

Changed order of functions.

File size: 3.4 KB
Line 
1#|
2 $Id: infix-printer.lisp,v 1.18 2002/03/29 14:00:18 marek Exp $
3 *--------------------------------------------------------------------------*
4 | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@math.arizona.edu) |
5 | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
6 | |
7 | Everyone is permitted to copy, distribute and modify the code in this |
8 | directory, as long as this copyright note is preserved verbatim. |
9 *--------------------------------------------------------------------------*
10|#
11
12(defpackage "INFIX-PRINTER"
13 (:use "INFIX" "COMMON-LISP")
14 (:export infix-print))
15
16(in-package "INFIX-PRINTER")
17
18
19
20(defun infix-print-separated-list (lst sep stream op print-level
21 &optional (alt-op nil) (alt-sep alt-op)
22 &aux
23 (beg t)
24 (count 0)
25 true-sep)
26 (cond
27 ((endp lst) nil)
28 ((and (numberp *print-level*) (> print-level *print-level*))
29 (format stream "#"))
30 (t
31 (dolist (arg lst)
32 (setf true-sep sep)
33 (incf count)
34 (when (and alt-op (> count 1) (consp arg) (eq alt-op (car arg)))
35 (setf arg (cadr arg)
36 true-sep alt-sep))
37 (if beg
38 (setf beg nil)
39 (format stream "~a" true-sep))
40 (when (and (numberp *print-length*) (> count *print-length*))
41 (format stream "..." true-sep)
42 (return-from infix-print-separated-list))
43 (infix-print arg stream op print-level)))))
44
45
46(defun infix-print (expr &optional (stream t) (op nil) (print-level 0))
47 "Print an expression EXPR in infix notation to stream STREAM.
48If OP is not nil, the expression is parenthesized if its operator
49has lower precedence than OP."
50 (cond
51 ((and (numberp *print-level*) (> print-level *print-level*))
52 (format stream "#"))
53 ((null expr) (error "Null expression."))
54 ((atom expr) (format stream "~a" expr))
55 ((and op (operator-lessp (car expr) op))
56 (format stream "(")
57 (infix-print expr stream nil (1+ print-level))
58 (format stream ")"))
59 ((and (eq (car expr) '-)
60 (endp (cddr expr))) ;unary minus
61 (format stream "-")
62 (infix-print (cadr expr) stream '* (1+ print-level)))
63 (t
64 (case (car expr)
65 ((+ - * /)
66 (infix-print-separated-list (cdr expr) (car expr) stream (car expr) (1+ print-level)
67 (cond ((eq (car expr) '+) '-)
68 ((eq (car expr) '-) '-)
69 (t nil))))
70 (wedge
71 (infix-print-separated-list (cdr expr) '|/\\| stream '* (1+ print-level)))
72 (expt
73 (unless (= (length (cdr expr)) 2)
74 (error "expt must take 2 arguments."))
75 (infix-print-separated-list (cdr expr) '^ stream '^ (1+ print-level)))
76 (otherwise ;assumed function call
77 (cond
78 ((and (eq (car expr) 'd)
79 (consp (cdr expr))
80 (symbolp (cadr expr)))
81 ;; Special syntax for differential forms
82 (format stream "d~a" (cadr expr)))
83 ((eq (car expr) 'aref)
84 ;; Special syntax for subscripted variables
85 ;; consistent with the infix package.
86 (format stream "~a[" (cadr expr))
87 (infix-print-separated-list (cddr expr) '\, stream '\, (1+ print-level))
88 (format stream "]"))
89 ((and (symbolp (car expr))
90 (string= (symbol-name (car expr)) "["))
91 (format stream "[")
92 (infix-print-separated-list (cdr expr) '\, stream '\, (1+ print-level))
93 (format stream "]"))
94 (t
95 ;; Generic function call syntax
96 (format stream "~a(" (car expr))
97 (infix-print-separated-list (cdr expr) '\, stream '\, (1+ print-level))
98 (format stream ")"))))))))
Note: See TracBrowser for help on using the repository browser.