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

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

* empty log message *

File size: 5.0 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
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
[681]63(defun infix-print-to-stream (expr &optional (stream t) (op nil) (print-level 0))
[642]64 "Print an expression EXPR in infix notation to stream STREAM.
65If OP is not nil, the expression is parenthesized if its operator
[683]66has lower precedence than OP. Returns (VALUES)."
[642]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
[4006]83 ((and op
84 (operator-lessp (car expr) op))
[642]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 ;; Handle array references
126 ((eq (car expr) 'aref)
127 ;; Special syntax for subscripted variables
128 ;; consistent with the infix package.
129 (format stream "~a[" (cadr expr))
130 (infix-print-separated-list (cddr expr) '\, stream '\, (1+ print-level))
131 (format stream "]"))
132
133 ;; Handle lists
134 ((and (symbolp (car expr))
135 (string= (symbol-name (car expr)) "["))
136 (format stream "[")
137 (infix-print-separated-list (cdr expr) '\, stream '\, (1+ print-level))
138 (format stream "]"))
139
140
141 ;; Handle generic function call syntax
142 (t
143
144 (format stream "~a(" (car expr))
145 (infix-print-arg-list (cdr expr) stream (1+ print-level))
146 (format stream ")")))))))
147 (values))
[678]148
[680]149(defun infix-print-to-string (expr &optional (op nil) (print-level 0)
150 &aux (fstr (make-array '(0) :element-type 'base-char
151 :fill-pointer 0 :adjustable t)))
[692]152 "Print an expression EXPR in infix notation to a string. If OP is
153not nil, the expression is parenthesized if its operator has lower
154precedence than OP. Returns the string containing the printed
155expression."
[678]156 (with-output-to-string (s fstr)
[682]157 (infix-print-to-stream expr s op print-level))
[678]158 fstr)
[681]159
160(defun infix-print (expr &optional (stream t) (op nil) (print-level 0))
[688]161 "Print an expression EXPR in infix notation to stream STREAM or to
162string if (NULL STREAM). If OP is not nil, the expression is
163parenthesized if its operator has lower precedence than OP. Returns
[690]164the string containing the printed expression, or (VALUES) if (NULL
165STREAM) is true."
[682]166 (cond
167 ((null stream)
168 (infix-print-to-string expr op print-level))
[686]169 (t (infix-print-to-stream expr stream op print-level))))
Note: See TracBrowser for help on using the repository browser.