Commit | Line | Data |
---|---|---|
abdf50aa MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; Output driver for SOD translator | |
4 | ;;; | |
5 | ;;; (c) 2009 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
10 | ;;; This file is part of the Simple Object Definition system. | |
11 | ;;; | |
12 | ;;; SOD is free software; you can redistribute it and/or modify | |
13 | ;;; it under the terms of the GNU General Public License as published by | |
14 | ;;; the Free Software Foundation; either version 2 of the License, or | |
15 | ;;; (at your option) any later version. | |
16 | ;;; | |
17 | ;;; SOD is distributed in the hope that it will be useful, | |
18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;;; GNU General Public License for more details. | |
21 | ;;; | |
22 | ;;; You should have received a copy of the GNU General Public License | |
23 | ;;; along with SOD; if not, write to the Free Software Foundation, | |
24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | (cl:in-package #:sod) | |
27 | ||
28 | ;;;-------------------------------------------------------------------------- | |
1f1d88f5 MW |
29 | ;;; Sequencing machinery. |
30 | ||
31 | (defclass sequencer-item () | |
77027cca MW |
32 | ((name :initarg :name :reader sequencer-item-name) |
33 | (functions :initarg :functions :initform nil | |
34 | :type list :accessor sequencer-item-functions)) | |
1f1d88f5 MW |
35 | (:documentation |
36 | "Represents a distinct item to be sequenced by a SEQUENCER. | |
37 | ||
38 | A SEQUENCER-ITEM maintains a list of FUNCTIONS which are invoked when the | |
39 | sequencer is invoked. This class is not intended to be subclassed.")) | |
40 | ||
41 | (defmethod print-object ((item sequencer-item) stream) | |
42 | (print-unreadable-object (item stream :type t) | |
43 | (prin1 (sequencer-item-name item) stream))) | |
44 | ||
45 | (defclass sequencer () | |
77027cca MW |
46 | ((constraints :initarg :constraints :initform nil |
47 | :type list :accessor sequencer-constraints) | |
1f1d88f5 MW |
48 | (table :initform (make-hash-table :test #'equal) |
49 | :reader sequencer-table)) | |
50 | (:documentation | |
51 | "A sequencer tracks items and invokes them in the proper order. | |
52 | ||
53 | The job of a SEQUENCER object is threefold. Firstly, it collects | |
54 | sequencer items and stores them in its table indexed by name. Secondly, | |
55 | it gathers CONSTRAINTS, which impose an ordering on the items. Thirdly, | |
56 | it can be instructed to invoke the items in an order compatible with the | |
57 | established constraints. | |
58 | ||
59 | Sequencer item names may may any kind of object which can be compared with | |
60 | EQUAL. In particular, symbols, integers and strings are reasonable | |
61 | choices for atomic names, and lists work well for compound names -- so | |
62 | it's possible to construct a hierarchy.")) | |
63 | ||
64 | (defgeneric ensure-sequencer-item (sequencer name) | |
65 | (:documentation | |
66 | "Arrange that SEQUENCER has a sequencer-item called NAME. | |
67 | ||
68 | Returns the corresponding SEQUENCER-ITEM object.")) | |
69 | ||
70 | (defgeneric add-sequencer-constraint (sequencer constraint) | |
71 | (:documentation | |
72 | "Attach the given CONSTRAINT to an SEQUENCER. | |
73 | ||
74 | The CONSTRAINT should be a list of sequencer-item names; see | |
75 | ENSURE-SEQUENCER-ITEM for what they look like. Note that the names | |
76 | needn't have been declared in advance; indeed, they needn't be mentioned | |
77 | anywhere else at all.")) | |
78 | ||
79 | (defgeneric add-sequencer-item-function (sequencer name function) | |
80 | (:documentation | |
81 | "Arranges to call FUNCTION when the item called NAME is traversed. | |
82 | ||
83 | More than one function can be associated with a given sequencer item. | |
84 | They are called in the same order in which they were added. | |
85 | ||
86 | Note that an item must be mentioned in at least one constraint in order to | |
87 | be traversed by INVOKE-SEQUENCER-ITEMS. If there are no special ordering | |
88 | requirments for a particular item, then the trivial constraint (NAME) will | |
89 | suffice.")) | |
90 | ||
91 | (defgeneric invoke-sequencer-items (sequencer &rest arguments) | |
92 | (:documentation | |
93 | "Invoke functions attached to the SEQUENCER's items in the right order. | |
94 | ||
95 | Each function is invoked in turn with the list of ARGUMENTS. The return | |
96 | values of the functions are discarded.")) | |
97 | ||
98 | (defmethod ensure-sequencer-item ((sequencer sequencer) name) | |
99 | (with-slots (table) sequencer | |
100 | (or (gethash name table) | |
101 | (setf (gethash name table) | |
102 | (make-instance 'sequencer-item :name name))))) | |
103 | ||
104 | (defmethod add-sequencer-constraint ((sequencer sequencer) (constraint list)) | |
105 | (let ((converted-constraint (mapcar (lambda (name) | |
106 | (ensure-sequencer-item sequencer | |
107 | name)) | |
108 | constraint))) | |
109 | (with-slots (constraints) sequencer | |
110 | (pushnew converted-constraint constraints :test #'equal)))) | |
111 | ||
112 | (defmethod add-sequencer-item-function ((sequencer sequencer) name function) | |
113 | (let ((item (ensure-sequencer-item sequencer name))) | |
114 | (pushnew function (sequencer-item-functions item)))) | |
115 | ||
116 | (defmethod invoke-sequencer-items ((sequencer sequencer) &rest arguments) | |
117 | (dolist (item (merge-lists (reverse (sequencer-constraints sequencer)))) | |
118 | (dolist (function (reverse (sequencer-item-functions item))) | |
119 | (apply function arguments)))) | |
120 | ||
121 | ;;;-------------------------------------------------------------------------- | |
122 | ;;; Output preparation. | |
123 | ||
124 | (defgeneric add-output-hooks (object reason sequencer) | |
125 | (:documentation | |
126 | "Announces the intention to write SEQUENCER, with a particular REASON. | |
127 | ||
128 | The SEQUENCER is an SEQUENCER instance; the REASON will be a symbol which | |
129 | can be matched using an EQL-specializer. In response, OBJECT should add | |
130 | any constrains and item functions that it wishes, and pass the | |
131 | announcement to its sub-objects.") | |
132 | (:method-combination progn) | |
133 | (:method progn (object reason sequencer) | |
134 | nil)) | |
135 | ||
136 | (defvar *seen-announcement*) ;Keep me unbound! | |
137 | #+hmm | |
138 | (defmethod add-output-hooks :around (object reason sequencer &rest stuff) | |
139 | "Arrange not to invoke any object more than once during a particular | |
140 | announcement." | |
141 | (declare (ignore stuff)) | |
142 | (cond ((not (boundp '*seen-announcement*)) | |
143 | (let ((*seen-announcement* (make-hash-table))) | |
144 | (setf (gethash object *seen-announcement*) t) | |
145 | (call-next-method))) | |
146 | ((gethash object *seen-announcement*) | |
147 | nil) | |
148 | (t | |
149 | (setf (gethash object *seen-announcement*) t) | |
150 | (call-next-method)))) | |
151 | ||
152 | ;;;-------------------------------------------------------------------------- | |
abdf50aa MW |
153 | ;;; Utilities. |
154 | ||
155 | (defun banner (title output &key (blank-line-p t)) | |
156 | (format output "~&~%/*----- ~A ~A*/~%" | |
157 | title | |
158 | (make-string (- 77 2 5 1 (length title) 1 2) | |
159 | :initial-element #\-)) | |
160 | (when blank-line-p | |
161 | (terpri output))) | |
162 | ||
163 | ;;;-------------------------------------------------------------------------- | |
164 | ;;; Header output. | |
165 | ||
166 | (defun write-module-header (module) | |
167 | (let* ((file (merge-pathnames (make-pathname :type "H" :case :common) | |
168 | (module-name module))) | |
169 | (fakename (make-pathname :name (pathname-name file) | |
170 | :type (pathname-type file)))) | |
171 | (with-open-file (uoutput file | |
172 | :direction :output | |
173 | :if-exists :supersede | |
174 | :if-does-not-exist :create) | |
175 | (let ((output (make-instance 'position-aware-output-stream | |
176 | :stream uoutput | |
177 | :file fakename))) | |
178 | ||
179 | ;; Format the header and guards. | |
180 | (format output "~ | |
181 | /* -*-c-*- | |
182 | * | |
183 | * Header file generated by SOD for ~A | |
184 | */ | |
185 | ||
186 | #ifndef ~A | |
187 | #define ~:*~A | |
188 | ||
189 | #ifdef __cplusplus | |
190 | extern \"C\" { | |
191 | #endif~%" | |
192 | (namestring (module-name module)) | |
193 | (or (getf (module-plist module) 'include-guard) | |
194 | (with-output-to-string (guard) | |
195 | (let ((name (namestring file)) | |
196 | (uscore t)) | |
197 | (dotimes (i (length name)) | |
198 | (let ((ch (char name i))) | |
199 | (cond ((alphanumericp ch) | |
200 | (write-char (char-upcase ch) guard) | |
201 | (setf uscore nil)) | |
202 | ((not uscore) | |
203 | (write-char #\_ guard) | |
204 | (setf uscore t))))))))) | |
205 | ||
206 | ;; Forward declarations of all the structures and types. Nothing | |
207 | ;; interesting gets said here; this is just so that the user code | |
208 | ;; can talk meainingfully about the things we're meant to be | |
209 | ;; defining here. | |
210 | ;; | |
211 | ;; FIXME | |
212 | ||
213 | ;; The user fragments. | |
214 | (when (module-header-fragments module) | |
215 | (banner "User code" output) | |
216 | (dolist (frag (module-header-fragments module)) | |
1f1d88f5 | 217 | (princ frag output))) |
abdf50aa MW |
218 | |
219 | ;; The definitions of the necessary structures. | |
220 | ;; | |
221 | ;; FIXME | |
222 | ||
223 | ;; The definitions of the necessary direct-methods. | |
224 | ;; | |
225 | ;; FIXME | |
226 | ||
227 | ;; The trailer section. | |
228 | (banner "That's all, folks" output) | |
229 | (format output "~ | |
230 | #ifdef __cplusplus | |
231 | } | |
232 | #endif | |
233 | ||
234 | #endif~%"))))) | |
235 | ||
236 | ;;;-------------------------------------------------------------------------- | |
237 | ;;; Source output. | |
238 | ||
239 | (defun write-module-source (module) | |
240 | (let* ((file (merge-pathnames (make-pathname :type "C" :case :common) | |
241 | (module-name module))) | |
242 | (fakename (make-pathname :name (pathname-name file) | |
243 | :type (pathname-type file)))) | |
244 | (with-open-file (uoutput file | |
245 | :direction :output | |
246 | :if-exists :supersede | |
247 | :if-does-not-exist :create) | |
248 | (let ((output (make-instance 'position-aware-output-stream | |
249 | :stream uoutput | |
250 | :file fakename))) | |
251 | ||
252 | ;; Format the header. | |
253 | (format output "~ | |
254 | /* -*-c-*- | |
255 | * | |
256 | * Source file generated by SOD for ~A | |
257 | */~%" | |
258 | (namestring (module-name module))) | |
259 | ||
260 | ;; The user fragments. | |
261 | (when (module-source-fragments module) | |
262 | (banner "User code" output) | |
263 | (dolist (frag (module-source-fragments module)) | |
1f1d88f5 | 264 | (princ frag output))) |
abdf50aa MW |
265 | |
266 | ;; The definitions of the necessary tables. | |
267 | ;; | |
268 | ;; FIXME | |
269 | ||
270 | ;; The definitions of the necessary effective-methods. | |
271 | ;; | |
272 | ;; FIXME | |
273 | ||
274 | ;; The trailer section. | |
275 | (banner "That's all, folks" output :blank-line-p nil))))) | |
276 | ||
277 | ;;;----- That's all, folks -------------------------------------------------- |