-
Notifications
You must be signed in to change notification settings - Fork 5
/
shampoo-compile.el
273 lines (249 loc) · 9.29 KB
/
shampoo-compile.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
;;; shampoo-compile.el --- Shampoo parse utilities
;;
;; Copyright (C) 2010 - 2012 Dmitry Matveev <[email protected]>
;;
;; This software is released under terms of the MIT license,
;; please refer to the LICENSE file for details.
(eval-when-compile (require 'cl))
(require 'shampoo-dict)
(require 'shampoo-state)
(require 'shampoo-state-format)
(require 'shampoo-regexp)
(require 'shampoo-utils)
(require 'shampoo-requests)
(require 'shampoo-response)
(require 'shampoo-dialect)
(require 'shampoo-list-mode)
(defconst *class-pattern*
'(:Wa :sp "subclass:" :sp "#" :Wd
:sp "instanceVariableNames:" :sp "'" :Ws "'"
:sp "classVariableNames:" :sp "'" :Ws "'"
:sp "poolDictionaries:" :sp "'" :Ws "'"
:sp "category:" :sp "'" :Wc "'"))
(defconst *class-side-pattern*
'(:Wd :sp "class" :sp "instanceVariableNames:" :sp "'" :Ws "'"))
(defconst *shampoo-class-template*
'(("instanceVariableNames:" . instvar)
("classVariableNames:" . classvar)
("poolDictionaries:" . poolvar)))
(defconst *shampoo-class-side-template*
'(("instanceVariableNames:" . instvar)))
(defun* shampoo-parse-message (&key code pattern bindings to-split)
(let* ((data (make-shampoo-dict))
(binder (shampoo-dict-binder-for-regexp data)))
(if (shampoo-regexp-parse-and-bind code pattern bindings binder)
(shampoo-dict-apply-many to-split 'shampoo-split-string data)
(progn (message "Shampoo: syntax error")
nil))))
(defun shampoo-parse-subclassing-message (code)
(shampoo-parse-message
:code code
:pattern *class-pattern*
:bindings '(:super :name :instvars :classvars :pooldicts :category)
:to-split '(:instvars :classvars :pooldicts)))
(defun shampoo-compile-class-instance (class-data)
(when class-data
(multiple-value-bind (superspace superclass)
(shampoo-infer-superspace class-data)
(let ((request-id (shampoo-give-id)))
(shampoo-subscribe
request-id
(shampoo-make-class-reloader
(shampoo-dict-get :name class-data)))
(shampoo-dict-put
:key :super
:value superclass
:into class-data)
(shampoo-send-message
(shampoo-make-compile-instance-rq
:id request-id
:ss superspace
:side (shampoo-side)
:ns (shampoo-get-current-namespace)
:cat (shampoo-dict-get :category class-data)
:desc class-data))))))
(defun shampoo-infer-superspace (class-data)
(let ((info
(with-~shampoo~
(shampoo-dialect-extract-parent
(shampoo-current-smalltalk ~shampoo~)
(shampoo-dict-get :super class-data)))))
(multiple-value-bind (superspace superclass) info
(values (if (null superspace)
(shampoo-get-current-namespace)
superspace)
superclass))))
(defun shampoo-parse-class-side-message (code)
(shampoo-parse-message
:code code
:pattern *class-side-pattern*
:bindings '(:name :instvars)
:to-split '(:instvars)))
(defun shampoo-compile-class-class (class-data)
(when class-data
(let ((request-id (shampoo-give-id)))
(shampoo-subscribe
request-id
(shampoo-make-class-reloader
(shampoo-dict-get :name class-data)))
(shampoo-send-message
(shampoo-make-compile-class-rq
:id request-id
:ss "Smalltalk"
:side (shampoo-side)
:ns (shampoo-get-current-namespace)
:desc class-data)))))
(defun shampoo-compile-class ()
(interactive)
(let ((code (shampoo-buffer-contents "*shampoo-code*")))
(if (shampoo-side-is :instance)
(shampoo-compile-class-instance
(shampoo-parse-subclassing-message code))
(shampoo-compile-class-class
(shampoo-parse-class-side-message code)))))
(defun shampoo-compile-method ()
(interactive)
(let ((request-id (shampoo-give-id))
(current-cat (shampoo-get-current-category)))
(shampoo-subscribe
request-id
(lexical-let ((cat current-cat))
(lambda (resp)
(when (shampoo-response-is-success resp)
(shampoo-reload-categories-list :open-then cat
:need-open t)))))
(shampoo-send-message
(shampoo-make-compile-method-rq
:id request-id
:ns (shampoo-get-current-namespace)
:class (shampoo-get-current-class)
:side (shampoo-side)
:category current-cat
:code (shampoo-buffer-contents "*shampoo-code*")))))
(defun shampoo-print-class-message-from-response (template resp)
(dolist (each template)
(let* ((items (shampoo-response-items-named (cdr each) resp))
(strs (mapcar 'shampoo-response-aggr-item items))
(text (if strs (shampoo-join-strings " " strs) "")))
(insert (format " %s '%s'" (car each) text))
(newline))))
(defun shampoo-print-class-instance-from-response (resp)
(insert
(format "%s subclass: #%s"
(shampoo-response-attr 'superclass resp)
(shampoo-response-attr 'class resp)))
(newline)
(shampoo-print-class-message-from-response
*shampoo-class-template*
resp)
(insert (format
" category: '%s'"
(shampoo-response-attr 'category resp)))
(newline))
(defun shampoo-print-class-class-from-response (resp)
(insert (format "%s class" (shampoo-response-attr 'class resp)))
(newline)
(shampoo-print-class-message-from-response
*shampoo-class-side-template*
resp))
(defun shampoo-handle-class-response (resp)
(with-current-buffer "*shampoo-code*"
(save-excursion
(erase-buffer)
(setq header-line-format (shampoo-make-header))
(if (shampoo-side-is :instance)
(with-~shampoo~
(shampoo-print-class-instance-from-response resp)
(setf (shampoo-current-class-category ~shampoo~)
(shampoo-response-attr 'category resp)))
(shampoo-print-class-class-from-response resp)))))
(defun shampoo-remove-class (class-name)
(when (yes-or-no-p
(format "Are you sure you want to remove class %s? "
class-name))
(let ((request-id (shampoo-give-id)))
(shampoo-subscribe
request-id
(lambda (resp)
(when (shampoo-response-is-success resp)
(shampoo-reload-class-list))))
(shampoo-send-message
(shampoo-make-remove-class-rq
:id request-id
:ns (shampoo-get-current-namespace)
:class class-name)))))
(defun shampoo-remove-method (method-selector)
(when (yes-or-no-p
(format "Are you sure you want to remove method #%s? "
method-selector))
(let ((request-id (shampoo-give-id)))
(shampoo-subscribe
request-id
(lambda (resp)
(when (shampoo-response-is-success resp)
(shampoo-reload-categories-list :open-then nil
:need-open t))))
(shampoo-send-message
(shampoo-make-remove-method-rq
:id request-id
:ns (shampoo-get-current-namespace)
:class (shampoo-get-current-class)
:side (shampoo-side)
:method method-selector)))))
(defun shampoo-remove-category (category)
(when (yes-or-no-p
(format "Are you sure you want to remove category \"%s\"? "
category))
(let ((request-id (shampoo-give-id)))
(shampoo-subscribe
request-id
(lambda (resp)
(when (shampoo-response-is-success resp)
(shampoo-reload-categories-list :open-then nil
:need-open t))))
(shampoo-send-message
(shampoo-make-remove-category-rq
:id request-id
:ns (shampoo-get-current-namespace)
:class (shampoo-get-current-class)
:side (shampoo-side)
:category category)))))
(defun shampoo-change-method-category (method)
(let ((request-id (shampoo-give-id))
(target-cat (read-string "Change method's category to: ")))
(shampoo-subscribe
request-id
(lexical-let ((cat target-cat))
(lambda (resp)
(when (shampoo-response-is-success resp)
(shampoo-reload-categories-list :open-then cat
:need-open t)))))
(shampoo-send-message
(shampoo-make-change-category-rq
:id request-id
:ns (shampoo-get-current-namespace)
:class (shampoo-get-current-class)
:side (shampoo-side)
:method method
:category target-cat))))
(defun shampoo-rename-category (category)
(let* ((request-id (shampoo-give-id))
(prompt (format "Rename category \"%s\" to: " category))
(to-cat (read-string prompt)))
(shampoo-subscribe
request-id
(lexical-let ((cat to-cat))
(lambda (resp)
(when (shampoo-response-is-success resp)
(shampoo-reload-categories-list :open-then cat
:need-open t)))))
(shampoo-send-message
(shampoo-make-rename-category-rq
:id request-id
:ns (shampoo-get-current-namespace)
:class (shampoo-get-current-class)
:side (shampoo-side)
:from category
:to to-cat))))
(provide 'shampoo-compile)
;;; shampoo-compile.el ends here.