-
Notifications
You must be signed in to change notification settings - Fork 8
/
io.lisp
337 lines (300 loc) · 14.9 KB
/
io.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
;;; -*- Mode: Common-Lisp; Syntax: Common-Lisp; Package: LINJ; Base: 10 -*-
;;; Copyright (C) Antonio Menezes Leitao Created on Thu Apr 5 12:03:16 2001
;;; Copyright (C) eValuator, Lda
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package "LINJ")
;;This file requires the Linj readtable
(eval-when (:compile-toplevel :load-toplevel)
(setq *readtable* *linj-readtable*))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;A special statement for debugging:
(def-macro println (arg &optional (stream '*standard-output*))
`(send ,stream println ,arg))
(def-macro print (arg &optional (stream '*standard-output*))
`(send ,stream print (concat #\newline ,arg " ")))
(def-macro pprint (arg &optional (stream '*standard-output*))
`(send ,stream print (concat #\newline ,arg)))
(def-macro princ (arg &optional (stream '*standard-output*))
`(send ,stream print ,arg))
(def-macro prin1 (arg &optional (stream '*standard-output*))
`(princ ,arg ,stream))
(def-macro warn (&rest args)
`(format *trace-output* ,@args))
(def-macro terpri (&optional (stream '*standard-output*))
`(send ,stream println))
(def-macro fresh-line (&optional (stream '*standard-output*))
`(terpri ,stream))
(def-macro write-char (char &optional (stream '*standard-output*))
`(princ (real-the char ,char) ,stream))
(defun subseq-string-form (string start end)
(cond ((or (/= start 0) end)
`(send ,(subseq-string-form string 0 nil) substring ,start ,@(and end (list end))))
((stringp string)
string)
(t
`(real-the java.lang.string ,string))))
(def-macro write-string (string &optional (stream '*standard-output*)
&key (start '0) end)
`(send ,stream print ,(subseq-string-form string start end)))
(def-macro write-line (string &optional (stream '*standard-output*)
&key (start '0) end)
`(send ,stream println ,(subseq-string-form string start end)))
(def-macro write-byte (byte stream)
`(send ,stream write (real-the byte ,byte)))
;; (def-macro read-line (&optional (stream '*standard-input*))
;; (if (eq stream '*standard-input*)
;; `(send ,stream read-line)
;; `(send (new 'java.io.buffered-reader (new 'java.io.input-stream-reader ,stream)) read-line)))
(def-macro read-line (&optional (stream '*standard-input*) eof-error-p eof-value recursive-p)
(assert (null eof-error-p)) (assert (null eof-value)) (assert (not recursive-p))
(if (eq stream '*standard-input*)
`(send (new 'java.io.buffered-reader (new 'java.io.input-stream-reader *standard-input*)) read-line)
`(send ,stream read-line)))
;;Format
;; (defun old-emit-format-code (type fmt stream full-args)
;; (let ((length (length fmt)))
;; (labels ((iterate (i args)
;; (if (= i length)
;; (list)
;; (case (char fmt i)
;; ((#\~) ;;'dispatch' char
;; (incf i)
;; (let ((numeric-arg nil)
;; (colonp nil)
;; (atp nil))
;; (when (char<= #\0 (char fmt i) #\9)
;; (multiple-value-bind (num pos)
;; (parse-integer fmt :start i :junk-allowed t)
;; (setf numeric-arg num
;; i pos)))
;; (case (char fmt i)
;; ((#\:) ;;optional args
;; (setf colonp t)
;; (incf i)))
;; (case (char fmt i)
;; ((#\@) ;;optional args
;; (setf atp t)
;; (incf i)))
;; (case (char fmt i)
;; ((#\c #\C) ;;Character
;; (cond ((endp args)
;; (error "Not enough arguments for format string ~S ~S" fmt full-args))
;; (numeric-arg
;; (error "~~C does not accept numeric prefixes (in ~S)" fmt))
;; (t
;; (cons `((the char ,(first args)))
;; (iterate (+ i 1) args)))))
;; ((#\% ;;Newline
;; #\&) ;;Fresh-line
;; (nconc (make-list (or numeric-arg 1) :initial-element `newline)
;; (iterate (+ i 1) args)))
;; ((#\|) ;;Page
;; (nconc (make-list (or numeric-arg 1) :initial-element `(#\Page))
;; (iterate (+ i 1) args)))
;; ((#\~) ;;Tilde
;; (nconc (make-list numeric-arg :initial-element `(#\~))
;; (iterate (+ i 1) args)))
;; ((#\d #\D ;;Decimal
;; #\S #\s
;; #\A #\a) ;for the moment, they are all equal
;; (if (endp args)
;; (error "Not enough arguments for format string ~S ~S" fmt full-args)
;; (cons `(,(first args))
;; (iterate (+ i 1) args))))
;; ((#\*) ;;Go-to
;; (iterate (+ i 1)
;; (cond (atp
;; (nthcdr (or numeric-arg 0) full-args))
;; (colonp
;; (nthcdr (- (length (ldiff full-args args)) (or numeric-arg 1)) args))
;; (t
;; (nthcdr (or numeric-arg 1) args)))))
;; (otherwise
;; (error "Unknown format directive ~~~C" (char fmt i))))))
;; (otherwise
;; (do ((j i (+ j 1)))
;; ((or (= j length)
;; (char= (char fmt j) #\~))
;; (if (= j i)
;; (list)
;; (cons `(,(subseq fmt i j))
;; (iterate j args)))))))))
;; (emit-statements (exprs)
;; (cond ((null exprs)
;; (list))
;; ((null (rest exprs))
;; (if (eq (first exprs) 'newline)
;; (list `(send ,stream println))
;; (list `(send ,stream print ,@(first exprs)))))
;; ((and (not (eq (first exprs) 'newline))
;; (eq (second exprs) 'newline))
;; (cons `(send ,stream println ,@(first exprs))
;; (emit-statements (nthcdr 2 exprs))))
;; ((eq (first exprs) 'newline)
;; (cons `(send ,stream println)
;; (emit-statements (rest exprs))))
;; (t
;; (cons `(send ,stream print ,@(first exprs))
;; (emit-statements (rest exprs))))))
;; (emit-args (exprs result)
;; (cond ((null exprs)
;; result)
;; ((null (rest exprs))
;; (if (eq (first exprs) 'newline)
;; `(,@result #\newline)
;; `(,@result ,@(first exprs))))
;; ((and (not (eq (first exprs) 'newline))
;; (eq (second exprs) 'newline))
;; (emit-args (nthcdr 2 exprs)
;; `(,@result ,@(first exprs) #\newline)))
;; (t
;; (emit-args (rest exprs)
;; `(,@result ,@(first exprs))))))
;; (emit-expression (exprs)
;; `(concat ,@(emit-args (rest exprs) (first exprs)))))
;; (let ((elems (iterate 0 full-args)))
;; (if (eq type :statement)
;; (emit-statements elems)
;; (emit-expression elems))))))
;;The following macro shows that the expansion depends on the parser
;;expectations. In this particular case, we either want to deal with
;;statements or with expressions.
;;This also makes sense from the macro expansion point of view, as it knows
;;what it's trying to parse. So the solution is to, somehow, include a
;;kind of 'environment' arg `a la Common Lisp. Another option is to define
;;syntax specific versions of a macro. Not only is the right thing as it
;;also allows for a much more efficient version of the macro expansions.
;; (def-macro-transform statement (format nil ?fmt . ?args)
;; (assert (stringp ?fmt))
;; `(let ((buf (new 'string-buffer)))
;; ,(emit-format-code :string-buffer
;; ?fmt
;; 'buf
;; ?args)
;; (to-string buf)))
(defun make-format-method-call (stream fmt args)
(with-parent ((ast-node-parent stream))
(make-instance 'method-call-expression
:name (parse 'format 'linj-name)
:receiver stream
:arguments (make-instance 'argument-list :elements (cons fmt (argument-list-elements args)))
:original-form '???)))
(defun exists-format-method-p (stream fmt args)
(and (not (primitive-type-reference-p (get-type stream)))
(find-declaration (make-format-method-call stream fmt args))))
;;Statements are tried first
(def-linj-macro statement (format ?stream-expression/expression ?fmt/expression . ?args/argument-list)
(if (exists-format-method-p ?stream-expression ?fmt ?args)
(fail)
(let ((args (argument-list-elements ?args)))
(assert (and (literal-p ?fmt) (stringp (literal-value ?fmt))))
(let ((fmt (literal-value ?fmt)))
(cond ((false-literal-p ?stream-expression)
`(let ((buf (new 'string-buffer)))
,(emit-format-code :string-buffer
fmt
'buf
args)
(to-string buf)))
((string-buffer-type-p (get-type ?stream-expression))
(emit-format-code :string-buffer fmt ?stream-expression args))
((or (literal-p ?stream-expression)
(reference-p ?stream-expression)
(slot-declaration-p (find-declaration ?stream-expression))
(endp (rest (emit-format-code :statement fmt ?stream-expression args))))
(emit-format-code :statement
fmt
(if (true-literal-p ?stream-expression) '*standard-output* ?stream-expression)
args))
(t
(let ((stream 'format-internal-stream))
`(let ((,stream ,?stream-expression))
,(emit-format-code :statement
fmt
stream
args)))))))))
;;Expressions second
(def-linj-macro expression (format ?stream-expression/expression ?fmt/expression . ?args/argument-list)
(if (exists-format-method-p ?stream-expression ?fmt ?args)
(fail)
(let ((args (argument-list-elements ?args)))
(assert (and (literal-p ?fmt) (stringp (literal-value ?fmt))))
(let ((fmt (literal-value ?fmt)))
(if (false-literal-p ?stream-expression)
(emit-format-code :expression fmt 'stream args)
`(send ,(if (true-literal-p ?stream-expression) '*standard-output* ?stream-expression)
print
,(emit-format-code :expression fmt 'stream args)))))))
;;The following macros hide too much when errors happen...
(def-transform statement (with-open-thing (?var ?stream) . ?body)
(let ((?var ?stream))
(unwind-protect
(progn . ?body)
(unless (eq ?var null)
(close ?var)))))
(def-transform statement (with-open-stream . ?args)
(with-open-thing . ?args))
(def-macro with-open-file ((stream
filespec
&key
(direction :input)
(stream-class nil stream-class-p)
(decorator-classes nil decorator-classes-p))
&body body)
(unless stream-class-p
(setq stream-class
(ecase direction
((:output) 'java.io.file-writer)
((:input nil) 'java.io.file-reader))))
(unless decorator-classes-p
(setq decorator-classes
(ecase direction
((:output) '(java.io.print-writer))
((:input nil) '(java.io.buffered-reader)))))
`(with-open-stream (,stream ,(reduce #'(lambda (decorator rest)
`(new ',decorator ,rest))
decorator-classes
:from-end t
:initial-value `(new ',stream-class ,filespec)))
,@body))
(def-transform method-call-expression (read-from-string ?string)
(read (new 'linj-reader (new 'string-reader ?string))))
(def-linj-macro expression (to-base ?expr/expression ?base)
(let ((type (get-type ?expr)))
(let ((expr
(if (object-type-p type)
(ecase *numerical-object-is*
((:int)
`(maybe-convert-to-type ,?expr ,(int-type)))
((:long)
`(maybe-convert-to-type ,?expr ,(long-type)))
((:bignum)
`(maybe-convert-to-type ,?expr ,(bignum-type))))
?expr)))
(let ((method-call
(case ?base
((10) expr)
((2) `(to-binary-string ,expr))
((8) `(to-octal-string ,expr))
((16) `(to-upper-case (to-hex-string ,expr)))
(t `(to-upper-case (to-string ,expr ,?base))))))
(cond ((int-type-p type)
`(in (the java.lang.Integer) ,method-call))
((or (long-type-p type) (double-type-p type) (float-type-p type))
`(in (the java.lang.Long) ,method-call))
((or (big-integer-type-p type) (bignum-type-p type) (object-type-p type))
(if (and (numberp ?base) (<= ?base 10))
`(to-string ,expr ,?base)
`(to-upper-case (to-string ,expr ,?base))))
(t
(error "Can't print object ~A in base ~A" expr ?base)))))))