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@ 4415

Last change on this file since 4415 was 4415, checked in by Marek Rychlik, 8 years ago

* empty log message *

File size: 5.9 KB
RevLine 
[4005]1;;; -*- Mode: Lisp -*-
[642]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 )
[685]16 (:export infix-print infix-print-to-string infix-print-to-stream))
[642]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
[4413]23 &optional
24 (alt-op nil)
25 (alt-sep alt-op)
[642]26 &aux
[4412]27 (beg t)
28 (count 0)
29 true-op
30 true-sep)
[4414]31 "Print a list LST using SEP as separator, to stream STREAM. Every
32argument is printed usin OP as main operator. PRINT-LEVEL is used to
33control printing nested expressions as expected: subexpressions at
34level exceeding PRINT-LEVEL are printed as ellipsis. The argument BEG
35indicates whether this is the start of a sequence of arguments with
36the main operator OP. The argument ALT-OP, if not NIL, replaces
37operator OP for list elements beyond the first one. Similarly, ALT-SEP
38replaces SEP for list elements beyond the first one."
[4403]39
[642]40 (cond
41 ((endp lst) nil)
42
43 ;; Handle *print-level*
44 ((and (numberp *print-level*)
45 (> print-level *print-level*))
46 (format stream "#"))
47
48 (t
49 (dolist (arg lst)
[4401]50 ;;(format t "Processing arg: ~S~%" arg)
[4412]51 (setf true-op op
52 true-sep sep)
53
[642]54 (incf count)
[4412]55
[4409]56 ;; This is to handle unary minus and unary division.
57 ;; Thus:
58 ;; (+ x (- y z)) --> X + Y - Z
59 ;; (+ x (- y)) --> X - Y
[642]60 (when (and alt-op
61 (> count 1)
62 (consp arg)
[4408]63 (endp (cddr arg))
[642]64 (eq alt-op (car arg)))
[4414]65 (setf arg (cadr arg)
66 true-sep alt-sep)
[4403]67 )
[4402]68
[4408]69
[4415]70 (cond
71 (beg
72 (setf beg nil))
73 (t
74 (format stream "~a" true-sep)))
[642]75
76 ;; If *print-length* exceeded, print ellipsis
77 (when (and (numberp *print-length*) (> count *print-length*))
78 (format stream "...")
79 (return-from infix-print-separated-list (values)))
80
[4412]81 (infix-print-to-stream arg stream true-op print-level))))
[642]82 (values))
83
84(defun infix-print-arg-list (lst stream print-level)
85 "Print a comma-separated list."
86 (infix-print-separated-list lst '\, stream '\, print-level))
87
[681]88(defun infix-print-to-stream (expr &optional (stream t) (op nil) (print-level 0))
[642]89 "Print an expression EXPR in infix notation to stream STREAM.
90If OP is not nil, the expression is parenthesized if its operator
[683]91has lower precedence than OP. Returns (VALUES)."
[642]92 (cond
93 ;; Handle *print-level*
[4405]94 ((and (numberp *print-level*)
95 (> print-level *print-level*))
96 (format stream "#"))
[642]97
[4405]98 ;; Null expression is an error
99 ((null expr)
100 (error "Null expression."))
[642]101
[4405]102 ;; Atoms are printed using ~A format directive
103 ((atom expr)
104 (format stream "~a" expr))
[642]105
[4405]106 ;; Check if the operator of this expression has lower precedence
107 ;; than the surrounding operator, and parenthesize if necessary
108 ((and op
109 (operator-lessp (car expr) op))
110 (format stream "(")
111 (infix-print expr stream nil (1+ print-level))
112 (format stream ")"))
[642]113
[4405]114 ;; Unary minus needs special handling
115 ((and (eq (car expr) '-) (endp (cddr expr)))
116 (format stream "-")
[4415]117
[4405]118 ;; Print the second element in product context
119 (infix-print (cadr expr) stream '* (1+ print-level)))
[642]120
[4405]121 ;; All other operators
122 (t
123 (case (car expr)
[642]124
[4405]125 ;; Arithmetic operators
126 ((+ - * /)
127 (infix-print-separated-list
128 (cdr expr)
129 (car expr)
130 stream
131 (car expr)
132 (1+ print-level)
133 (cond ((eq (car expr) '+) '-)
134 ((eq (car expr) '-) '-)
[4409]135 ((eq (car expr) '*) '/)
136 ((eq (car expr) '/) '/)
[4405]137 (t nil))))
[642]138
[4405]139 ;; Exponentials
140 (expt
141 (unless (= (length (cdr expr)) 2)
142 (error "expt must take 2 arguments."))
143 (infix-print-separated-list
144 (cdr expr)
145 '^
146 stream
147 '^
148 (1+ print-level)))
[642]149
[4405]150 ;; Assuming function call
151 (otherwise
152 (cond
[4400]153
[4405]154 ;; Handle array references
155 ((eq (car expr) 'aref)
156 ;; Special syntax for subscripted variables
157 ;; consistent with the infix package.
158 (format stream "~a[" (cadr expr))
159 (infix-print-separated-list (cddr expr) '\, stream '\, (1+ print-level))
160 (format stream "]"))
[4400]161
[4405]162 ;; Handle lists
163 ((and (symbolp (car expr))
164 (string= (symbol-name (car expr)) "["))
165 (format stream "[")
166 (infix-print-separated-list (cdr expr) '\, stream '\, (1+ print-level))
167 (format stream "]"))
[4400]168
[4405]169 ;; Handle generic function call syntax
170 (t
171 (format stream "~a(" (car expr))
172 (infix-print-arg-list (cdr expr) stream (1+ print-level))
173 (format stream ")")))))))
[642]174 (values))
[678]175
[680]176(defun infix-print-to-string (expr &optional (op nil) (print-level 0)
177 &aux (fstr (make-array '(0) :element-type 'base-char
178 :fill-pointer 0 :adjustable t)))
[692]179 "Print an expression EXPR in infix notation to a string. If OP is
180not nil, the expression is parenthesized if its operator has lower
181precedence than OP. Returns the string containing the printed
182expression."
[678]183 (with-output-to-string (s fstr)
[682]184 (infix-print-to-stream expr s op print-level))
[678]185 fstr)
[681]186
187(defun infix-print (expr &optional (stream t) (op nil) (print-level 0))
[688]188 "Print an expression EXPR in infix notation to stream STREAM or to
189string if (NULL STREAM). If OP is not nil, the expression is
190parenthesized if its operator has lower precedence than OP. Returns
[690]191the string containing the printed expression, or (VALUES) if (NULL
192STREAM) is true."
[682]193 (cond
194 ((null stream)
195 (infix-print-to-string expr op print-level))
[686]196 (t (infix-print-to-stream expr stream op print-level))))
Note: See TracBrowser for help on using the repository browser.