forked from gigamonkey/monkeylib-foo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlispscript.lisp
467 lines (375 loc) · 15.7 KB
/
lispscript.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
;;
;; Copyright (c) 2005, Gigamonkeys Consulting All rights reserved.
;;
;;; BUGS:
;;; closures over "this" don't always work. For instance,
;;;
;;; (defmethod (Foo a-method) ()
;;; (set (@ someobject onclick) (lambda () this)))
;;;
;;; sets up an event handler which is a method on 'someobject' so it
;;; will return someobject rather than the Foo object which is 'this'
;;; in a-method. Blech.
(in-package :foo.lispscript)
(defclass lispscript (javascript)
()
(:default-initargs
:input-package (find-package :foo.lispscript)))
(defparameter *lispscript* (make-instance 'lispscript))
(defparameter *html* (make-instance 'foo.xml::html))
;;; Compilation environment
(defmethod top-level-environment ((language lispscript))
(new-env 'needs-value t (call-next-method)))
(defun needs-value (env)
(or (cdr (assoc 'needs-value env))
(eql (foo.javascript::statement-or-expression env) :expression)))
(defun find-open-block (name env &key (expected t))
(let ((cons (assoc `(open-block ,name) env :test #'equal)))
(cond
((and expected (not cons))
(error "No open block named ~a in environment ~s" name env))
#+(or)((and (not expected) cons)
(error "Already an open block named ~a in environment ~s" name env))
(t cons))))
(defun add-open-block (name env)
(find-open-block name env :expected nil)
(new-env `(open-block ,name) nil env))
(defun mark-block-return (name env)
(let ((cons (find-open-block name env :expected t)))
(setf (cdr cons) t)))
(defun block-returned-p (name env)
(let ((cons (find-open-block name env :expected t)))
(cdr cons)))
;;; Sub-primitive special operators. Not meant to be used directly in
;;; user code but handy for implementing primitive special operators
;;; and "built-in" macros.
(define-javascript-macro needs-value (&body body)
`(augment-environment ((needs-value . t)) ,@body))
(define-javascript-macro discard-value (&body body)
`(augment-environment ((needs-value . nil)) ,@body))
(define-javascript-macro return-last (&body body)
`(prog (discard-value (prog ,@(butlast body))) (needs-value (return ,@(last body)))))
(define-javascript-macro scope ((&rest bindings) &body body)
`(|.call|
(function (,@(mapcar #'first bindings)) ,@body)
|this| ,@(mapcar #'second bindings)))
;;; Language constructs. These are the "special-operators" of the
;;; Lispscript language.
(define-javascript-macro |let| ((&rest bindings) &body body &environment env)
`(scope (,@bindings)
,(if (needs-value env)
`(return-last ,@body)
`(discard-value (prog ,@body)))))
(define-javascript-macro |let*| ((&rest bindings) &body body)
(if bindings
`(|let| (,(first bindings))
,@(if (rest bindings)
`((|let*| (,@(rest bindings)) ,@body))
body))
`(|let| () ,@body)))
#|
Bah! To make closures work properly relative to `this', so you
can do things like:
(defmethod (Widget whatever)
(lambda () (something this)))
we need to jump through all kinds of hoops to return a closure
that gets passed the appropriate object and then when called
CALLs the actual closure function with that object as the
thisObj.
Here's a sketch of the needed Javascript code:
function Widget () {
return this;
}
Widget.prototype.aMethod = function () { return this; };
Widget.prototype.aClosure = function () {
return (function (gensym) {
return function () {
return (function () { return this; }).call(gensym);
};
})(this);
}
|#
(define-javascript-macro |lambda| ((&rest params) &body body)
`(function ,params (return-last ,@body)))
(define-javascript-macro |progn| (&body body &environment env)
(if (needs-value env)
`(|let| () ,@body)
`(prog ,@body)))
(define-javascript-macro |block| (name &body body &environment env)
(let ((e (javascript-gensym))
(return-var (block-name name)))
(if (not (returned-p return-var body env))
`(|progn| ,@body)
`(augment-environment
(((open-block ,return-var) . nil))
,(if (needs-value env)
`((function
(,return-var)
(try
(return (|progn| ,@body))
(catch ,e
(if (|eq| ,e ,return-var)
(return (@ ,return-var |value|))
(throw ,e)))))
(object |value| |undefined|))
`((function
(,return-var)
(try (prog ,@body)
(catch ,e
(if (|eq| ,e ,return-var)
(return (@ ,return-var |value|))
(throw ,e)))))
(object |value| |undefined|)))))))
(defun block-name (symbol)
(intern (format nil "block$~a" (symbol-name symbol)) :keyword))
(defun returned-p (block-name body env)
"Walk the forms in body to see if the given block is ever returned
from. Basically we set up a dummy environment in which the block is
open and then process each form, igoring the value returned by process
but checking whether the environment has been modified to mark the
block as returned from. If there's no return from the block then we
don't need to set up all the machinery for returning."
(loop with new-env = (add-open-block block-name env)
for form in body
do (process *lispscript* nil form new-env)
thereis (block-returned-p block-name new-env)))
(define-javascript-macro |return-from| (name value &environment env)
(mark-block-return (block-name name) env)
(if (needs-value env)
`(|progn|
(|set| (@ ,(block-name name) |value|) ,value)
((function () (throw ,(block-name name)))))
`(prog
(|set| (@ ,(block-name name) |value|) ,value)
(throw ,(block-name name)))))
(define-javascript-macro |return| (value)
`(|return-from| |nil| ,value))
;; Scheme style -- internal DEFUN's are local
#+(or)(define-javascript-macro |defun| (name (&rest params) &body body)
`(function ,name ,params (return (|block| ,name ,@body))))
;; Common Lisp style -- even internal DEFUN's are gobal
(define-javascript-macro |defun| (name (&rest params) &body body)
`(|set| (@ |window| ,name) (function ,params (return (|block| ,name ,@body)))))
;; Scheme style
#+(or)(define-javascript-macro |defvar| (name &optional value)
`(VAR ,name ,@(when value `(,value))))
;; Common Lisp style
(define-javascript-macro |defvar| (name &optional value)
`(|set| (@ |window| ,name) ,(when value `(needs-value ,value))))
(define-javascript-macro |if| (test then &optional else &environment env)
(if (needs-value env)
`(scope () (if ,test (block (return ,then)) (block (return ,(if else else '|undefined|)))))
`(if ,test (block ,then) ,@(if else `((block ,else))))))
(define-javascript-macro |cond| (&body clauses)
(destructuring-bind ((test &body body) &rest clauses) clauses
(if (eql test '|true|)
`(|progn| ,@body)
`(|if| (needs-value ,test)
(|progn| ,@body)
,@(when clauses
`((|cond| ,@clauses)))))))
(define-javascript-macro |when| (test &body body)
`(|if| ,test (|progn| ,@body)))
(define-javascript-macro |unless| (test &body body)
`(|if| (! ,test) (|progn| ,@body)))
(define-javascript-macro |and| (first &rest rest)
`(&& ,first ,@rest))
(define-javascript-macro |or| (first &rest rest)
`(\|\| ,first ,@rest))
(define-javascript-macro |not| (form)
`(! ,form))
(define-javascript-macro |do| ((&rest variables) (end-test-form &optional (result-form '|undefined|)) &body body)
(multiple-value-bind (bindings step-forms) (parse-do-variables variables)
`(|block| |nil|
(|let| (,@bindings)
(while (! ,end-test-form)
(block ,@body
,@step-forms))
,@(when result-form (list result-form))))))
(define-javascript-macro |do*| ((&rest variables) (end-test-form &optional result-form) &body body)
(multiple-value-bind (bindings step-forms) (parse-do-variables variables)
`(|block| |nil|
(|let*| (,@bindings)
(while (! ,end-test-form)
(block ,@body
,@step-forms))
,@(when result-form (list result-form))))))
(define-javascript-macro |dotimes| ((var iterations) &body body &environment env)
(let ((iterations-value (javascript-gensym)))
`(|block| |nil|
(var ,iterations-value ,iterations)
(|for| ((var ,var 0) (< ,var ,iterations-value) (++ ,var :post)) ,@body))))
(define-javascript-macro |doarray| ((var array) &body body)
(let* ((idxvar (javascript-gensym))
(array-value (javascript-gensym))
(iterations-value (javascript-gensym)))
`(|block| |nil|
(var ,array-value ,array)
(var ,iterations-value (@ ,array |length|))
(|for| ((var ,idxvar 0) (< ,idxvar ,iterations-value) (++ ,idxvar :post))
(block (discard-value (|let| ((,var (ref ,array-value ,idxvar))) ,@body)))))))
(define-javascript-macro |dolist| ((var sequence) &body body &environment env)
(let* ((idxvar (javascript-gensym))
(sequence-value (javascript-gensym)))
`(|block| |nil|
(var ,sequence-value ,sequence)
(for (,idxvar in ,sequence-value)
(block (discard-value (|let| ((,var (ref ,sequence-value ,idxvar))) ,@body))))
,@(if (needs-value env) '(|undefined|)))))
(define-javascript-macro |doslots| (((name value) sequence) &body body &environment env)
(let* ((sequence-value (javascript-gensym)))
`(|block| |nil|
(var ,sequence-value ,sequence)
(for (,name in ,sequence-value)
(block (discard-value (|let| ((,value (ref ,sequence-value ,name))) ,@body))))
,@(if (needs-value env) '(|undefined|)))))
(define-javascript-macro |while| (condition &body body &environment env)
`(|block| |nil| (while ,condition (block (discard-value ,@body))) |undefined|))
(define-javascript-macro |until| (condition &body body)
`(|while| (! ,condition) ,@body))
(defun parse-do-variables (variables)
(flet ((normalize (variable)
(let ((as-cons (if (symbolp variable) (list variable) variable)))
(loop repeat 3 collect (pop as-cons)))))
(loop for (var init step) in (mapcar #'normalize variables)
collect (list var (or init '|undefined|)) into bindings
when step collect `(|set| ,var ,step) into step-forms
finally (return (values bindings step-forms)))))
(define-javascript-macro |for| ((var test step) &body body &environment env)
(if (needs-value env)
`(|progn|
(for (,var ,test ,step) (block ,@body))
|undefined|)
`(for (,var ,test ,step) (block ,@body))))
(define-javascript-macro |new| (type &rest args)
`(NEW ,type ,@args))
(define-javascript-macro |ignore-errors| (&body body &environment env)
(let ((e (javascript-gensym)))
(if (needs-value env)
`((function () (try (return-last ,@body) (catch ,e (return |false|)))))
`(try ,@body (catch ,e)))))
(define-javascript-macro |defmethod| ((class name) (&rest parameters) &body body)
`(|set| (@ ,class |prototype| ,name) (function ,parameters (return (|block| ,name ,@body)))))
(define-javascript-macro |define-class-method| ((class name) (&rest parameters) &body body)
`(|set| (@ ,class ,name) (function ,parameters (return (|block| ,name ,@body)))))
(define-javascript-macro |ref| (var &rest slots)
`(ref ,var ,@slots))
(define-javascript-macro |array| (var &rest slots)
`(array ,var ,@slots))
(define-javascript-macro |case| (value &body clauses)
`(scope ()
(switch
,value
,@(loop for (val . body) in clauses collect
(if (eql val '|t|)
`(:default (return-last ,@body))
`(,val (return-last ,@body)))))))
(define-javascript-macro |1+| (form)
`(+ ,form 1))
(define-javascript-macro |1-| (form)
`(- ,form 1))
;; Equality operators
(define-javascript-macro |eq| (x y) `(=== ,x ,y))
(define-javascript-macro |equal| (x y) `(== ,x ,y))
(define-javascript-macro = (x y) `(== (- ,x 0) (- ,y 0)))
(define-javascript-macro |string=| (x y) `(== (+ "" ,x) (+ "" ,y)))
;; Assignment
(define-javascript-macro |set| (form value) `(foo.javascript:= ,form ,value))
(define-javascript-macro |debug| (&rest stuff) `(|alert| (+ ,@stuff)))
;;; DOM
(defparameter *xml-tag-case-conversion* nil)
(defun case-convert-tag (tag)
(case *xml-tag-case-conversion*
(:upcase (string-upcase tag))
(:downcase (string-downcase tag))
((nil) (string tag))))
(defun attribute-value (thing)
(typecase thing
(string thing)
(number (princ-to-string thing))
(symbol thing)))
(define-javascript-macro |xml| (body)
(multiple-value-bind (tag attributes body) (foo.xml::parse-cons-form body)
`(|let*| ((document (|.createDocument| (@ |document| |implementation|) "" ,(case-convert-tag tag) |null|)))
,@(loop for (name value) on attributes by #'cddr
collect `(|.setAttribute| (@ document |document-element|) ,(case-convert-tag name) ,(attribute-value value)))
,@(loop for thing in body collect
`(|.appendChild| (@ document |document-element|) (|xml-element| (document) ,thing)))
document)))
(define-javascript-macro |xml-element| ((document) body)
(etypecase body
(string `(|.create-text-node| ,document ,body))
(number `(|.create-text-node| |document| (+ "" ,body)))
(symbol `(|if| (|string=| (|typeof| ,body) "string")
(|.create-text-node| |document| ,body)
,body))
(cons
(cond
((sexp-form-p *html* body)
(multiple-value-bind (tag attributes body) (foo.xml::parse-cons-form body)
`(|let*| ((element (|.create-element| ,document ,(case-convert-tag tag))))
,@(loop for (name value) on attributes by #'cddr
collect `(|.setAttribute| element ,(case-convert-tag name) ,(attribute-value value)))
,@(loop for thing in body collect
`(|.appendChild| element (|xml-element| (,document) ,thing)))
element)))
(t body)))))
(define-javascript-macro |xml-element/cs| ((document) body)
(etypecase body
(string `(|.create-text-node| ,document ,body))
(number `(|.create-text-node| |document| (+ "" ,body)))
(symbol `(|if| (|string=| (|typeof| ,body) "string")
(|.create-text-node| |document| ,body)
,body))
(cons
(cond
((sexp-form-p *html* body)
(multiple-value-bind (tag attributes body) (foo.xml::parse-cons-form body)
`(|let*| ((element (|.create-element| ,document ,(string tag))))
,@(loop for (name value) on attributes by #'cddr
collect `(|.setAttribute| element ,(string name) ,(attribute-value value)))
,@(loop for thing in body collect
`(|.appendChild| element (|xml-element| (,document) ,thing)))
element)))
(t body)))))
(define-javascript-macro |xml-element/ns| ((document) body)
(etypecase body
(string `(|.create-text-node| ,document ,body))
(number `(|.create-text-node| |document| (+ "" ,body)))
(symbol `(|if| (|string=| (|typeof| ,body) "string")
(|.create-text-node| |document| ,body)
,body))
(cons
(cond
((sexp-form-p *html* body)
(multiple-value-bind (tag attributes body) (foo.xml::parse-cons-form body)
`(|let*| ((element (|.create-elementNS| ,document ,(string tag))))
,@(loop for (name value) on attributes by #'cddr
collect `(|.setAttribute| element ,(string name) ,(attribute-value value)))
,@(loop for thing in body collect
`(|.appendChild| element (|xml-element| (,document) ,thing)))
element)))
(t body)))))
(define-javascript-macro |html| (body)
(flet ((attribute-value (thing)
(typecase thing
(string thing)
(number (princ-to-string thing))
(symbol thing))))
(etypecase body
(string `(|.create-text-node| |document| ,body))
(number `(|.create-text-node| |document| (+ "" ,body)))
(symbol `(|if| (|string=| (|typeof| ,body) "string")
(|.create-text-node| |document| ,body)
,body))
(cons
(cond
((sexp-form-p *html* body)
(multiple-value-bind (tag attributes body) (foo.xml::parse-cons-form body)
`(|let*| ((element (|.create-element| |document| ,(case-convert-tag tag))))
,@(loop for (name value) on attributes by #'cddr
collect `(|.set-attribute| element ,(case-convert-tag name) ,(attribute-value value)))
,@(loop for thing in body collect
`(|.append-child| element (|html| ,thing)))
element)))
(t body))))))