-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.scm
317 lines (243 loc) · 8.33 KB
/
console.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
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
(declare (standard-bindings)
(extended-bindings)
(not safe))
(define (console-log str)
(##inline-host-expression "console.log(gambit_scm2host(@1@))" str))
(define (console-log-js str)
(##inline-host-expression "console.log(@1@)" str))
;;----------------------------------------------------------------------------
;; Utility
;;----------------------------------------------------------------------------
(define (foldr fn acc lst)
(if (null? lst)
acc
(fn (car lst) (foldr fn acc (cdr lst)))))
(define (foldl fn acc lst)
(if (null? lst)
acc
(foldl fn (fn acc (car lst)) (cdr lst))))
(define (append-strings . strs)
(foldl string-append "" strs))
(define-macro (ml-str . str)
(define (append-strings lst acc)
(cond
((null? lst) acc)
((and (pair? lst) (null? (cdr lst)))
(string-append acc (car lst)))
(else
(append-strings
(cdr lst)
(string-append acc
(string-append (car lst) "\n"))))))
(append-strings str ""))
;;----------------------------------------------------------------------------
;; Javascript / SVG interface
;;----------------------------------------------------------------------------
(##inline-host-declaration
"var svg = document.documentElement;
var svgNS = svg.namespaceURI")
(define svg (##inline-host-expression "svg"))
(define (create-element el)
(##inline-host-expression
"document.createElementNS(svgNS, @1@)" el))
(define (create-text-node str)
(##inline-host-expression
"document.createTextNode(@1@)" str))
(define (set-attribute element attribute value)
(##inline-host-expression
"(@1@).setAttribute(@2@, @3@)" element attribute value))
(define (append-child parent child)
(##inline-host-expression
"(@1@).appendChild(@2@)" parent child))
(define (set-text-content el text)
(##inline-host-expression "(@1@).textContent = gambit_scm2host(@2@)" el text))
(define (set-onkeypress fn)
(##inline-host-expression "document.onkeypress = @1@" fn))
(define (set-onkeydown fn)
(##inline-host-expression "document.onkeydown = @1@" fn))
(define from-char-code
(##inline-host-expression "gambit_host2scm(String.fromCharCode)"))
;;----------------------------------------------------------------------------
;; TTY LOGIC
;;----------------------------------------------------------------------------
(define-type input val prefix update)
(define input-set! input-val-set!)
(define (input-get i) (string-append (input-prefix i) (input-val i)))
(define (input-add! i c)
(input-val-set! i (string-append (input-val i) c)))
(define (input-pop! i)
(let ((str (input-val i)))
(input-val-set! i (substring str 0 (- (string-length str) 1)))))
(define-type tty size history update)
(define (tty-add-line str tty)
(let ((history (tty-history tty)))
(tty-history-set! tty (cons str history))))
;;----------------------------------------------------------------------------
;; TTY INTERFACE
;;----------------------------------------------------------------------------
(define (make-tty-line y)
(define input (create-element "text"))
(define input-text (create-text-node ""))
(append-child input input-text)
(set-attribute input "font-size" 20)
(set-attribute input "font-family" "Courier")
(set-attribute input "x" 25)
(set-attribute input "y" y)
(append-child svg input)
input-text)
(define (make-tty-lines n)
(define tty (make-vector tty-size n))
(let loop ((m 0))
(if (< m n)
(begin
(vector-set! tty
m
(make-tty-line (* 25 (+ m 1))))
(loop (+ m 1)))
tty)))
;;----------------------------------------------------------------------------
;; TTY START
;;----------------------------------------------------------------------------
(define input (make-input "" ">>> " #f))
(define tty (make-tty 31 '() #f))
(define history-interface (make-tty-lines (tty-size tty)))
(define input-interface (make-tty-line (* 25 (+ (tty-size tty) 1))))
(input-update-set! input
(lambda ()
(set-text-content
input-interface
(string-append (input-get input)
"_"))))
(tty-update-set! tty
(lambda ()
(let loop ((i (- (tty-size tty) 1)) (content (tty-history tty)))
(if (< i 0)
#!void
(if (null? content)
(begin
(set-text-content (vector-ref history-interface i) "")
(loop (- i 1) content))
(begin
(set-text-content (vector-ref history-interface i)
(car content))
(loop (- i 1) (cdr content))))))))
((input-update input))
;;----------------------------------------------------------------------------
;; Reader and printer
;;----------------------------------------------------------------------------
(define (tty-println str tty)
(tty-add-line str tty))
;; note: use primitives for faster loop
(define (tty-print str tty)
(let ((len (string-length str)))
(let loop ((i 0) (last-substring 0) (last-space 0))
(cond
((##fx> (##fx- i last-substring) 79)
(if (eq? (string-ref str i) #\space)
(begin
(tty-println (substring str last-substring last-space) tty)
(loop (##fx+ i 1) (+ last-space 1) last-space))
(if (##fx> last-space last-substring)
(begin
(tty-println (substring str last-substring last-space) tty)
(loop i (+ last-space 1) last-space))
(begin
(tty-println (substring str last-substring i) tty)
(loop i i last-space)))))
((eq? (string-ref str i) #\newline)
(tty-println (substring str last-substring i) tty)
(loop (##fx+ i 1) i last-space))
((eq? (string-ref str i) #\space)
(loop (##fx+ i 1) last-substring i))
((##fx= len i)
(tty-println (substring str last-substring i) tty))
(else
(loop (##fx+ i 1) last-substring last-space))))))
;;---------------------------------
;; Event handling
;;---------------------------------
; Note: these functions return #f to prevent the browser from
; handling the default key binding.
(define (keypress-handle k)
(console-log k)
(cond
((= k 13)
(let ((str (input-val input)))
(tty-print (input-get input) tty)
((tty-update tty))
(input-val-set! input "")
((input-update input))
(console-log "before")
(let ((result (eval (string->object str))))
(console-log "after")
(if (not (eq? #!void result))
(tty-print (object->string result) tty)))
((tty-update tty))))
((= (string-length (from-char-code k)) 1)
(input-add! input (from-char-code k))
((input-update input))))
#f)
(define (keydown-handle k)
(case k
((40) ; DOWN key
(console-log "DOWN")
#f)
((38) ; UP key
(console-log "UP")
#f)
((8) ; BACKSPACE key
(if (> (string-length (input-val input)) 0)
(begin (input-pop! input)
((input-update input))))
#f)
(else #t)))
(set-onkeypress
(##inline-host-expression
"function(e) { return gambit_scmfn2host(@1@)((e || window.event).which); }"
keypress-handle))
(set-onkeydown
(##inline-host-expression
"function(e) { return gambit_scmfn2host(@1@)((e || window.event).which); }"
keydown-handle))
;;
;; My console function
;;
(define (println str)
(tty-print str tty)
#!void)
(define (clear)
(tty-history-set! tty '())
((tty-update tty))
#!void)
(define (help)
(tty-print
(ml-str
"This is a Gambit Scheme console based on the Gambit scheme universal"
"librairy (univ-lib)."
"Note: The console is far from feature complete. Errors are currently"
"handled silently. The univ-lib isn't complete which explains why many"
"functions don't work."
""
"Commands"
"========"
" * (clear)"
" Clears the console history. There is no limit on the history size so"
" a clear once in a while should prevent the console from using to much"
" memory."
""
" * (println <string>)"
" Prints a string to the console."
""
"To do"
"====="
" * Use the univ-lib repl implementation to provide functionalities"
" similar to the Gambit interpreter"
" * Remove global states so that multiple consoles could be embeded"
" in a web page"
" * Maybe use html instead of svg..."
" * Add command history (might come with repl functionalities)."
)
tty)
#!void)
(help)
((tty-update tty))