close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/infix-printer.lisp@ 4400

Last change on this file since 4400 was 4400, checked in by Marek Rychlik, 9 years ago

* empty log message *

File size: 5.0 KB
Line 
1;;; -*- Mode: Lisp -*-
2#|
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 "COMMON-LISP"
14 "INFIX" ;for operator-lessp
15 )
16 (:export infix-print infix-print-to-string infix-print-to-stream))
17
18(in-package "INFIX-PRINTER")
19
20(proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 3)))
21
22(defun infix-print-separated-list (lst sep stream op print-level
23 &optional (alt-op nil) (alt-sep alt-op)
24 &aux
25 (beg t)
26 (count 0)
27 true-sep)
28 (cond
29 ((endp lst) nil)
30
31 ;; Handle *print-level*
32 ((and (numberp *print-level*)
33 (> print-level *print-level*))
34 (format stream "#"))
35
36 (t
37 (dolist (arg lst)
38 (setf true-sep sep)
39 (incf count)
40
41 (when (and alt-op
42 (> count 1)
43 (consp arg)
44 (eq alt-op (car arg)))
45 (setf arg (cadr arg)
46 true-sep alt-sep))
47 (if beg
48 (setf beg nil)
49 (format stream "~a" true-sep))
50
51 ;; If *print-length* exceeded, print ellipsis
52 (when (and (numberp *print-length*) (> count *print-length*))
53 (format stream "...")
54 (return-from infix-print-separated-list (values)))
55
56 (infix-print arg stream op print-level))))
57 (values))
58
59(defun infix-print-arg-list (lst stream print-level)
60 "Print a comma-separated list."
61 (infix-print-separated-list lst '\, stream '\, print-level))
62
63(defun infix-print-to-stream (expr &optional (stream t) (op nil) (print-level 0))
64 "Print an expression EXPR in infix notation to stream STREAM.
65If OP is not nil, the expression is parenthesized if its operator
66has lower precedence than OP. Returns (VALUES)."
67 (cond
68 ;; Handle *print-level*
69 ((and (numberp *print-level*)
70 (> print-level *print-level*))
71 (format stream "#"))
72
73 ;; Null expression is an error
74 ((null expr)
75 (error "Null expression."))
76
77 ;; Atoms are printed using ~A format directive
78 ((atom expr)
79 (format stream "~a" expr))
80
81 ;; Check if the operator of this expression has lower precedence
82 ;; than the surrounding operator, and parenthesize if necessary
83 ((and op
84 (operator-lessp (car expr) op))
85 (format stream "(")
86 (infix-print expr stream nil (1+ print-level))
87 (format stream ")"))
88
89 ;; Unary minus needs special handling
90 ((and (eq (car expr) '-) (endp (cddr expr)))
91 (format stream "-")
92 ;; Print the second element in product context
93 (infix-print (cadr expr) stream '* (1+ print-level)))
94
95 ;; All other operators
96 (t
97 (case (car expr)
98
99 ;; Arithmetic operators
100 ((+ - * /)
101 (infix-print-separated-list
102 (cdr expr)
103 (car expr)
104 stream
105 (car expr)
106 (1+ print-level)
107 (cond ((eq (car expr) '+) '-)
108 ((eq (car expr) '-) '-)
109 (t nil))))
110
111 ;; Exponentials
112 (expt
113 (unless (= (length (cdr expr)) 2)
114 (error "expt must take 2 arguments."))
115 (infix-print-separated-list
116 (cdr expr)
117 '^
118 stream
119 '^
120 (1+ print-level)))
121
122 ;; Assuming function call
123 (otherwise
124 (cond
125
126 ;; Handle array references
127 ((eq (car expr) 'aref)
128 ;; Special syntax for subscripted variables
129 ;; consistent with the infix package.
130 (format stream "~a[" (cadr expr))
131 (infix-print-separated-list (cddr expr) '\, stream '\, (1+ print-level))
132 (format stream "]"))
133
134 ;; Handle lists
135 ((and (symbolp (car expr))
136 (string= (symbol-name (car expr)) "["))
137 (format stream "[")
138 (infix-print-separated-list (cdr expr) '\, stream '\, (1+ print-level))
139 (format stream "]"))
140
141 ;; Handle generic function call syntax
142 (t
143 (format stream "~a(" (car expr))
144 (infix-print-arg-list (cdr expr) stream (1+ print-level))
145 (format stream ")")))))))
146 (values))
147
148(defun infix-print-to-string (expr &optional (op nil) (print-level 0)
149 &aux (fstr (make-array '(0) :element-type 'base-char
150 :fill-pointer 0 :adjustable t)))
151 "Print an expression EXPR in infix notation to a string. If OP is
152not nil, the expression is parenthesized if its operator has lower
153precedence than OP. Returns the string containing the printed
154expression."
155 (with-output-to-string (s fstr)
156 (infix-print-to-stream expr s op print-level))
157 fstr)
158
159(defun infix-print (expr &optional (stream t) (op nil) (print-level 0))
160 "Print an expression EXPR in infix notation to stream STREAM or to
161string if (NULL STREAM). If OP is not nil, the expression is
162parenthesized if its operator has lower precedence than OP. Returns
163the string containing the printed expression, or (VALUES) if (NULL
164STREAM) is true."
165 (cond
166 ((null stream)
167 (infix-print-to-string expr op print-level))
168 (t (infix-print-to-stream expr stream op print-level))))
Note: See TracBrowser for help on using the repository browser.