-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand.lisp
140 lines (125 loc) · 4.7 KB
/
expand.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
(in-package #:lispy-format)
;;;; ~FORMAT-related.
(defvar *~formats* (make-hash-table :test 'eq))
(defclass %format ()
((%name :initarg :name
:reader %format-name
:type symbol)
(%opargs :initarg :opargs
:reader %format-opargs
:type list)
(%args :initarg :args
:reader %format-args
:type list)
(%expander :initarg :expander
:reader %format-expander)))
(defmethod print-object ((format %format) stream)
(print-unreadable-object (format stream :type t)
(format stream "~A ~S ~S"
(%format-name format)
(%format-opargs format)
(%format-args format))))
(defun find-~format (name &key (errorp t))
(check-type name symbol)
(or (gethash name *~formats*)
(when errorp
(error "There is no ~S operator named ~S."
'~format name))))
(defun (setf find-~format) (new name &key (errorp t))
(declare (ignore errorp))
(check-type name symbol)
(setf (gethash name *~formats*) new))
(defun formatexpand-finalize (form ~
&aux (stream `(,~ stream)) optimized)
(with-open-stream (concatenated-constants (make-string-output-stream))
(labels ((cut ()
(let ((concatenated (get-output-stream-string
concatenated-constants)))
(unless (zerop (length concatenated))
(push `(write-string ,concatenated ,stream)
optimized))))
(optimize (form)
(etypecase form
((cons (eql progn))
(mapc #'optimize (rest form)))
(string
(write-string form concatenated-constants))
(t (cut) (push form optimized)))))
(optimize form)
(cut))
(setf optimized (nreverse optimized))
(if (rest optimized)
`(progn ,@optimized)
(first optimized))))
(defun formatexpand (form ~)
(formatexpand-finalize (formatexpand-partially form ~) ~))
(defun formatexpand-forms (forms ~)
(let ((result (formatexpand `(progn ,@forms) ~)))
(if (typep result '(cons (eql progn)))
(rest result)
(list result))))
(defun formatexpand-partially (form ~)
(flet ((expand (operator opargs args)
(let ((format (find-~format operator :errorp nil)))
(if format
(funcall (%format-expander format)
operator ~ opargs args)
form))))
(typecase form
((cons symbol)
(expand (first form) nil (rest form)))
((cons (cons symbol))
(destructuring-bind ((operator &rest opargs) &rest args) form
(expand operator opargs args)))
(string form)
((or character number) (write-to-string form :escape nil))
(t form))))
(defun formatexpand-forms-partially (forms ~)
(mapcar (lambda (form)
(formatexpand-partially form ~))
forms))
(defmacro %with-~ ((~ stream) &body body)
`(macrolet ((,~ (&rest args)
(if (and (= (length args) 1)
(eq (first args) 'stream))
',stream
(error "Unrecognized: ~S."
(cons ',~ args)))))
,@body))
(defmacro with-~format-stream ((var stream &key (maybe-output-to-string t))
&body body)
(if maybe-output-to-string
(let ((shared (gensym (string '#:shared))))
`(flet ((,shared (,var)
,@body))
(let ((,var ,stream))
(if ,var
(,shared (if (eq ,var 't)
*standard-output*
,var))
(with-output-to-string (,var)
(,shared ,var))))))
`(let ((,var ,stream))
,@body)))
(defmacro ~format ((~ &optional stream) &body body)
(let* ((stream-var (gensym (string '#:stream))))
`(with-~format-stream (,stream-var ,stream)
(%with-~ (,~ ,stream-var)
,@(formatexpand-forms body ~)))))
(defmacro define-~format (name
(~ &rest opargs) (&rest args)
&body body)
(let ((e-operator (gensym (string '#:operator)))
(e-opargs (gensym (string '#:opargs)))
(e-args (gensym (string '#:args))))
`(setf (find-~format ',name)
(make-instance
'%format
:name ',name
:opargs ',opargs
:args ',args
:expander (lambda (,e-operator ,~ ,e-opargs ,e-args)
(declare (ignorable ,e-operator))
(destructuring-bind ,opargs ,e-opargs
(destructuring-bind ,args ,e-args
,@body)))))))