forked from biwascheme/biwascheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini-framework.scm
194 lines (155 loc) · 5.92 KB
/
mini-framework.scm
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
;;;; shift-reset
(define-macro (reset . prog)
`(*reset (lambda () ,@prog)))
(define-macro (shift arg . prog)
`(*shift (lambda (,arg) ,@prog)))
(define (*meta-continuation* v)
(error "You forgot the top-level reset..."))
(define (*abort thunk)
(let ((v (thunk)))
(*meta-continuation* v)))
(define (*reset thunk)
(let ((mc *meta-continuation*))
(call-with-current-continuation
(lambda (k)
(set! *meta-continuation*
(lambda (v)
(set! *meta-continuation* mc)
(k v)))
(*abort thunk)))))
(define (*shift f)
(call-with-current-continuation
(lambda (k)
(*abort (lambda ()
(f (lambda (v)
(reset (k v)))))))))
;;;; utils
;;;; (Some things I'm used to from Common Lisp)
(define first car)
(define second cadr)
(define third caddr)
(define rest cdr)
(define-macro (push! item place)
`(set! ,place (cons ,item ,place)))
(define-macro (delete! item place)
`(set! ,place (remove ,item ,place)))
(define (remove item list)
(cond
((equal? item (car list))
(cdr list))
(else
(cons (car list)
(remove item (cdr list))))))
(define-macro (when cond . body)
`(if ,cond (begin ,@body)))
(define-macro (unless cond . body)
`(if (not ,cond) (begin ,@body)))
;;;; Some other utils
(define-macro (while test expr)
`(let loop ()
(when ,test
,expr
(loop))))
(define (displayln text)
(display text)
(newline))
(define-macro (wind-protect setup teardown . body)
(let ((result-g (gensym "RESULT")))
`(begin
,setup
(let ((,result-g (begin ,@body)))
,teardown
,result-g))))
;; This is from the unit tests (https://github.com/biwascheme/biwascheme/blob/master/test/unit.js):
;;(let1 ls '() (dotimes (x 3 ls) (set! ls (cons x ls))))
;; This almost works.
;;(dotimes '(print 1 5))
;; Define a simple iterator (which actually uses tail-call rescursion):
;; Adaptation of the factorial at https://wiki.c2.com/?SchemeIdioms .
;; See also _ANSI Common Lisp_, p. 164 and _Teach Yourself Scheme_, Chapter 8.
(define-macro (repeat n . body)
(let ((i (gensym)))
`(let loop ((,i 1))
,@body
(if (< ,i ,n)
(loop (+ 1 ,i))))))
;;;; JS stuff
(define-macro (js-call% func . args)
`(js-call (js-eval ,func) ,@args))
(define-macro (js-lambda args . body)
`(js-closure (lambda ,args ,@body)))
;;;; with-handlers
(define (input-listener-cont . args)
(display "Handler called with no listener continuation up"))
(define (call-with-handlers handlers fn)
(wind-protect
(setup-handlers handlers)
(remove-handlers handlers)
(fn)))
(define-macro (with-handlers handlers . body)
`(call-with-handlers ',handlers (lambda () ,@body)))
(define (setup-handlers handlers)
(process-handlers handlers second))
(define (remove-handlers handlers)
(process-handlers handlers third))
(define (process-handlers handlers part)
(map (lambda (handler)
(let ((handler-func (part (assq (first handler) handlers-impl)))
(handler-args (rest handler)))
(apply handler-func handler-args)))
handlers))
(define (make-event-handler sym)
(lambda (this event)
(input-listener-cont (list sym this event))))
(define-macro (define-event-handler handler-name event-sym)
(let* ((event-name (symbol->string event-sym))
(handlers-container (string->symbol (string-append event-name "-handlers%")))
(add-handler `(lambda (selector)
(let ((handler (add-handler! selector ,event-name
(make-event-handler ',event-sym))))
(hashtable-set! ,handlers-container selector handler))))
(remove-handler `(lambda (selector)
(let ((handler (hashtable-ref ,handlers-container selector #f)))
(remove-handler! selector ,event-name handler)
(hashtable-delete! ,handlers-container selector)))))
`(begin
(define ,handlers-container (make-eqv-hashtable))
(push! (list ',handler-name
,add-handler
,remove-handler)
handlers-impl))))
(define ajax-handlers-container (make-eqv-hashtable))
(define timeout-handlers-container (make-eqv-hashtable))
(define handlers-impl
(list
(list 'timeout-handler
(lambda (key timeout)
(hashtable-set! timeout-handlers-container key
(js-call% "setTimeout"
(js-lambda ()
(input-listener-cont (list 'timeout key)))
timeout)))
(lambda (key timeout)
(js-call% "clearTimeout"
(hashtable-ref timeout-handlers-container key #f))
(hashtable-delete! timeout-handlers-container key)))
(list 'ajax-handler
(lambda (key url data)
(hashtable-set! ajax-handlers-container key #t)
(let ((ajax-func (js-eval "$.ajax")))
(ajax-func (js-obj "url" url
"data" (apply js-obj data)
"success" (js-lambda (data)
(if (hashtable-ref ajax-handlers-container key #f)
(input-listener-cont (list key 'success data))))
"error" (js-lambda (jqxhr status error)
(if (hashtable-ref ajax-handlers-container key #f)
(input-listener-cont (list key 'error status error))))))))
(lambda (key url data)
(hashtable-delete! ajax-handlers-container key)))))
(define-event-handler click-handler click)
(define-event-handler keydown-handler keydown)
(define-event-handler keyup-handler keyup)
;;;; get-input
(define-macro (get-input)
`(shift c (set! input-listener-cont c)))