-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjb-objc-gen.el
314 lines (230 loc) · 9.97 KB
/
pjb-objc-gen.el
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
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: pjb-objc-genel
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Generate some Objective-C code.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2012-11-12 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; AGPL3
;;;;
;;;; Copyright Pascal Bourguignon 2012 - 2012
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Affero General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU Affero General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Affero General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
(require 'pjb-cl)
(defun ensure-string (object)
(typecase object
(string object)
(symbol (symbol-name object))
(list (apply 'string object))
(t (prin1-to-string object))))
(defun make-set-selector (selector)
"Return a setSelector: selector."
(let ((name (ensure-string selector)))
(intern (if (and (< 2 (length name))
(string= "is" (substring name 0 2))
(upper-case-p (aref name 2)))
(format "set%s:" (subseq name 2))
(format "set%c%s:" (char-upcase (aref name 0)) (subseq name 1))))))
(defun split-selector (selector)
(let* ((parts (reverse (split-string (ensure-string selector) ":")))
(parts-with-args (mapcar (lambda (part) (format "%s:" part)) (rest parts))))
(mapcar (function intern)
(nreverse (if (string= "" (first parts))
parts-with-args
(cons (first parts)parts-with-args))))))
(defun test/split-selector ()
(assert (equal (split-selector 'isObscure) '(isObscure)))
(assert (equal (split-selector 'setObscure:) '(setObscure:)))
(assert (equal (split-selector 'initWithCoordinates::) '(initWithCoordinates: :)))
(assert (equal (split-selector 'initWithFrame:andObject:) '(initWithFrame: andObject:)))
:success)
(defmacro* with-parens (parens &body body)
`(progn
(insert ,(elt parens 0))
(prog1 (progn ,@body)
(insert ,(elt parens 1)))))
(defclass c-node ()
())
(defun generate (node)
(if (typep node 'c-node)
(generate-node node)
(insert (format "%S" node))))
(defmacro* defclass/c (class-name (&rest superclasses) (&rest slots) &rest options)
`(progn
(defclass ,class-name ,superclasses ,slots ,@options)
(defun* ,(intern (format "make-%s" class-name)) (&rest arguments &key &allow-other-keys)
(apply 'make-instance ',class-name arguments))
',class-name))
(defmacro* with-parens (parens &body body)
`(progn
(insert ,(elt parens 0))
(prog1 (progn ,@body)
(insert ,(elt parens 1)))))
(defmethod generate-node ((node c-node))
(with-parens ("/*" "*/\n")
(insert (format "ERROR: Missing generate-node method for %S" node))))
(defclass c-declaration (c-node)
())
(defclass c-definition (c-node)
())
(defclass c-statement (c-node)
())
(defclass c-expression (c-node)
())
(defclass/c c-dot (c-expression)
((object :initarg :object :accessor c-dot-object)
(slot :initarg :slot :accessor c-dot-slot)))
(defmethod generate-node ((node c-dot))
(insert (format "%s.%s" (c-dot-object node) (c-dot-slot node))))
(defclass/c c-assign (c-expression)
((lval :initarg :lval :accessor c-assign-lval)
(rval :initarg :rval :accessor c-assign-rval)))
(defmethod generate-node ((node c-assign))
(generate (c-assign-lval node))
(insert " = ")
(generate (c-assign-rval node)))
(defclass/c c-cond (c-statement)
((clauses :initarg :clauses :accessor c-cond-clauses
:documentation "A list, the first element is the condition expression, the rest is a list of statement body.")))
(defmethod generate-node ((node c-cond))
(loop
for insert-else = nil then t
for (condition . body) in (c-cond-clauses node)
do (progn
(when insert-else
(insert "else "))
(with-parens ("if(" ")\n")
(generate condition))
(generate (make-c-block :statements body)))))
(defclass/c c-return (c-statement)
((result :initarg :result :accessor c-return-result)))
(defmethod generate-node ((node c-return))
(with-parens ("return " "")
(generate (c-return-result node))))
(defclass/c c-block (c-statement)
((statements :initarg :statements :accessor c-block-statements)))
(defmethod generate-node ((node c-block))
(with-parens ("{\n" "}\n")
(dolist (statement (c-block-statements node))
(generate statement)
(insert ";\n"))))
(defclass objc-definition (c-definition)
())
(defclass objc-declaration (c-declaration)
())
(defclass objc-expression (c-expression)
())
(defmacro generate-constructor (&rest class-names)
`(progn
,@(mapcar (lambda (class-name)
`(defun* ,(intern (format "make-%s" class-name)) (&rest arguments &key &allow-other-keys)
(apply 'make-instance ',class-name arguments)))
class-names)
',class-names))
(defun generate-intermingled-selector-and-things (selector things)
"Inserts a method signature, or a message sending.
`selector': an Objective-C selector designator.
`things': a list of objc-parameter or objc-argument.
"
(loop
with selector-parts = (split-selector selector)
with arguments = things
for sep = "" then " "
while selector-parts
do (progn
(insert sep)
(generate (pop selector-parts))
(when arguments
(generate (pop arguments))))
finally (loop while arguments
do (insert "," (pop arguments)))))
(defclass/c objc-string (objc-expression)
((string :initarg :string :accessor objc-string-string)))
(defmethod generate-node ((node objc-string))
(let ((string (objc-string-string node)))
(insert (format "@%S" (typecase string
(string string)
(symbol (symbol-name string))
(t (prin1-to-string string)))))))
(defclass/c objc-method (objc-definition)
((instance-method :initform t :reader objc-method-instance-method-p)
(result-type :initform 'void :initarg :result-type :accessor objc-method-result-type)
(selector :initarg :selector :accessor objc-method-selector)
(parameters :initform '() :initarg :parameters :accessor objc-method-parameters)
(body :initform '() :initarg :body :accessor objc-method-body)))
(defclass/c objc-class-method (objc-method)
((instance-method :initform nil :reader objc-method-instance-method-p)))
(defmethod objc-method-sign ((node objc-method)) "-")
(defmethod objc-method-sign ((node objc-class-method)) "+")
(defmethod generate-node ((node objc-method))
(insert (format "\n\n%s " (objc-method-sign node)))
(with-parens ("(" ")")
(insert (ensure-string (objc-method-result-type node))))
(generate-intermingled-selector-and-things (objc-method-selector node) (objc-method-parameters node))
(insert "\n")
(generate (make-c-block :statements (objc-method-body node))))
(defclass/c objc-send (objc-expression)
((recipient :initarg :recipient :accessor objc-send-recipient)
(selector :initarg :selector :accessor objc-send-selector)
(arguments :initform '() :initarg :arguments :accessor objc-send-arguments)))
(defmethod generate-node ((node objc-send))
(with-parens ("[" "]")
(generate (objc-send-recipient node))
(insert " ")
(generate-intermingled-selector-and-things (objc-send-selector node) (objc-send-arguments node))))
(defclass/c objc-parameter (objc-declaration)
((type :initarg :type :accessor objc-parameter-type)
(name :initarg :name :accessor objc-parameter-name)))
(defmethod generate-node ((node objc-parameter))
(insert (format "(%s)%s " (objc-parameter-type node) (objc-parameter-name node))))
(defun generate-forward-methods (properties target)
"Inserts methods for the given properties that forward to the give target.
`properties': a list of property descriptor. Each one is a list
containing a type, and a property name or a list (getter setter).
`target': a target object to which the messages are forwarded.
"
(loop
for (type prop-name) in properties
do (let ((getter (if (atom prop-name)
prop-name
(first prop-name)))
(setter (if (atom prop-name)
(make-set-selector prop-name)
(second prop-name))))
(generate (make-objc-method :result-type type
:selector getter
:parameters '()
:body (list (make-c-return :result (make-objc-send :recipient target
:selector getter)))))
(generate (make-objc-method :result-type 'void
:selector setter
:parameters (list (make-objc-parameter :type type :name 'value))
:body (list (make-objc-send :recipient target
:selector setter
:arguments '(value))))))))
(defun test/pjb-objc-gen ()
(test/split-selector))
(test/pjb-objc-gen)
(provide 'pjb-objc-gen)
;;;; THE END ;;;;