File: _guide.scm

package info (click to toggle)
gambc 4.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 54,404 kB
  • ctags: 10,660
  • sloc: ansic: 661,388; lisp: 143,554; sh: 3,531; makefile: 3,320; cpp: 2,143; perl: 1,730; sed: 498; java: 265
file content (395 lines) | stat: -rw-r--r-- 13,851 bytes parent folder | download | duplicates (7)
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
;==============================================================================

; File: "_guide.scm", Time-stamp: <2007-04-04 11:33:22 feeley>

; Copyright (c) 1994-2007 by Marc Feeley, All Rights Reserved.

;==============================================================================

(##include "../../lib/header.scm")
(##include "_guide#.scm")

;==============================================================================

(c-declare #<<end-of-c-declare

#include "_guide.h"
#include "guide.h"

end-of-c-declare
)

(c-define-type QString "QString" "QString_to_SCMOBJ" "SCMOBJ_to_QString" #f)
(c-define-type QApplication "QApplication")
(c-define-type QApplication* (pointer QApplication))
(c-define-type GuideUiMainWindow "GuideUiMainWindow")
(c-define-type GuideUiMainWindow* (pointer GuideUiMainWindow))
(c-define-type GuideUiScheme "GuideUiScheme")
(c-define-type GuideUiScheme* (pointer GuideUiScheme))

(define ##QApplication-new
  (c-lambda (nonnull-char-string-list) QApplication* "QApplication_new"))

(define ##QApplication-processEvents
  (c-lambda (QApplication*) void "QApplication_processEvents"))

(define ##GuideUiMainWindow-new
  (c-lambda () GuideUiMainWindow* "GuideUiMainWindow_new"))

(define ##GuideUiScheme-new
  (c-lambda (GuideUiMainWindow* QString scheme-object) GuideUiScheme* "GuideUiScheme_new"))

(define ##GuideUiScheme-print-text
  (c-lambda (GuideUiScheme* QString) void "GuideUiScheme_print_text"))

(c-define (##GuideUiScheme-typed-text channel text)
  (scheme-object QString) void "GuideUiScheme_typed_text" ""
  (let ((far (macro-repl-channel-guide-far-port channel)))
    (##write-string text far)))

(c-define (##GuideUiScheme-typed-eof channel)
  (scheme-object) void "GuideUiScheme_typed_eof" ""
  (let ((far (macro-repl-channel-guide-far-port channel)))
    (##close-output-port far)))

(define ##GuideUiScheme-continuation-set-highlight
  (c-lambda (GuideUiScheme* int) void "GuideUiScheme_continuation_set_highlight"))

(define ##GuideUiScheme-continuation-set-cell
  (c-lambda (GuideUiScheme* int int QString) void "GuideUiScheme_continuation_set_cell"))

(define ##GuideUiScheme-continuation-set-length
  (c-lambda (GuideUiScheme* int) void "GuideUiScheme_continuation_set_length"))

(define ##GuideUiScheme-environment-set-cell
  (c-lambda (GuideUiScheme* int int QString) void "GuideUiScheme_environment_set_cell"))

(define ##GuideUiScheme-environment-set-length
  (c-lambda (GuideUiScheme* int) void "GuideUiScheme_environment_set_length"))

(define ##GuideUiScheme-highlight-expr-in-console
  (c-lambda (GuideUiScheme* int int) void "GuideUiScheme_highlight_expr_in_console"))

(define ##GuideUiScheme-highlight-expr-in-file
  (c-lambda (GuideUiScheme* int int QString) void "GuideUiScheme_highlight_expr_in_file"))

;==============================================================================

(implement-type-repl-channel-guide)

(define-prim (##make-repl-channel-guide)
  (let ((ct (macro-current-thread)))
    (receive (near far)
             (##open-string-pipe '(buffering: #f permanent-close: #f))

      (macro-port-name-set! near (lambda (port) ct))

      (let ((channel
             (##still-copy
              (macro-make-repl-channel-guide

               (##make-mutex #f)
               (macro-current-thread)
               near
               near

               (lambda (channel level depth) ; read-command

                 (define prompt "> ")

                 (let ((output-port (macro-repl-channel-output-port channel)))
                   (if (##fixnum.< 0 level)
                     (##write level output-port))
                   (if (##fixnum.< 0 depth)
                     (begin
                       (##write-string "\\" output-port)
                       (##write depth output-port)))
                   (##write-string prompt output-port)
                   (##force-output output-port))

                 (let ((input-port (macro-repl-channel-input-port channel))
                       (output-port (macro-repl-channel-output-port channel)))
                   (let ((result (##read-expr-from-port input-port)))
                     (##output-port-column-set! output-port 1)
                     result)))

               (lambda (channel results) ; write-results
                 (let ((output-port (macro-repl-channel-output-port channel)))
                   (##for-each
                    (lambda (obj)
                      (if (##not (##eq? obj (##void)))
                        (##pretty-print obj output-port)))
                    results)))

               (lambda (channel writer) ; display-monoline-message
                 (let ((output-port (macro-repl-channel-output-port channel)))
                   (writer output-port)
                   (##newline output-port)))

               (lambda (channel writer) ; display-multiline-message
                 (let ((output-port (macro-repl-channel-output-port channel)))
                   (writer output-port)))

               ##repl-channel-guide-display-continuation ; display-continuation

               ##repl-channel-guide-pinpoint-continuation ; pinpoint-continuation

               (lambda (channel) ; really-quit?
                 (let ((input-port (macro-repl-channel-input-port channel))
                       (output-port (macro-repl-channel-output-port channel)))
                   (##write-string "*** EOF again to exit" output-port)
                   (##newline output-port)
                   (##not (##char? (##peek-char input-port)))))

               (lambda (channel) ; newline
                 (let ((output-port (macro-repl-channel-output-port channel)))
                   (##newline output-port)))

               far

               #f))))

        (macro-repl-channel-guide-GuideUiScheme-set!
         channel
         (##GuideUiScheme-new
          (##guide-main-window)
          (##object->string ct)
          channel))

        (##thread-start!
         (make-thread
          (lambda ()
            (let ((far (macro-repl-channel-guide-far-port channel)))

              (define (input-timeout-set! timeout)
                (##input-port-timeout-set! far timeout (lambda () #f)))

              (let loop ()
                (##peek-char far)
                (input-timeout-set! -inf.0)
                (let ((text (##read-line far #f #t #f)))
                  (input-timeout-set! #f)
                  (##GuideUiScheme-print-text
                   (macro-repl-channel-guide-GuideUiScheme channel)
                   text)
                  (loop)))))))

        channel))))

(define-prim (##repl-channel-guide-pinpoint-continuation channel cont)
  (and cont
       (let ((locat (##continuation-locat cont)))
         (and locat
              (let* ((filepos (##position->filepos (##locat-position locat)))
                     (line (##fixnum.+ (##filepos-line filepos) 1))
                     (col (##fixnum.+ (##filepos-col filepos) 1))
                     (container (##locat-container locat))
                     (file (##container->file container)))
                (if file
                  (begin
                    (##GuideUiScheme-highlight-expr-in-file
                     (macro-repl-channel-guide-GuideUiScheme channel)
                     line
                     col
                     file)
                    #t)
                  (if (##thread? container)
                    (begin
                      (##GuideUiScheme-highlight-expr-in-console
                       (##thread-repl-channel-get! container)
                       line
                       col)
                      #t)
                    #f)))))))

(define-prim (##repl-channel-guide-display-continuation channel cont depth)

  (define (display-frame cont i)

    (##GuideUiScheme-continuation-set-cell
     (macro-repl-channel-guide-GuideUiScheme channel)
     i
     0
     (##object->string i))

    (##GuideUiScheme-continuation-set-cell
     (macro-repl-channel-guide-GuideUiScheme channel)
     i
     1
     (##object->string
      (let ((creator (##continuation-creator cont)))
        (if creator
          (##procedure-name creator)
          '(interaction)))))

    (##GuideUiScheme-continuation-set-cell
     (macro-repl-channel-guide-GuideUiScheme channel)
     i
     2
     (let ((p (##open-output-string)))
       (let ((locat (##continuation-locat cont)))
         (##display-locat locat #t p)
         (##get-output-string p))))

    (##GuideUiScheme-continuation-set-cell
     (macro-repl-channel-guide-GuideUiScheme channel)
     i
     3
     (let ((call
            (if (##interp-continuation? cont)
              (let* (($code (##interp-continuation-code cont))
                     (cprc (macro-code-cprc $code)))
                (if (##eq? cprc ##interp-procedure-wrapper)
                  #f
                  (##decomp $code)))
              (let* ((ret (##continuation-ret cont))
                     (call (##decompile ret)))
                (if (##eq? call ret)
                  #f
                  call)))))
       (if call
         (##object->string call 100)
         ""))))

  (define (display-cont cont depth)
    (let loop ((i 0)
               (cont cont))
      (if cont
        (begin
          (display-frame cont i)
          (if (##fixnum.= i depth)
            (display-env cont))
          (loop (##fixnum.+ i 1)
                (##continuation-next-interesting cont)))
        (begin
          (##GuideUiScheme-continuation-set-length
           (macro-repl-channel-guide-GuideUiScheme channel)
           i)
          (##GuideUiScheme-continuation-set-highlight
           (macro-repl-channel-guide-GuideUiScheme channel)
           depth)))))

  (define (display-env cont)

    (define (display-var-val var val cte row)
      (let ((var-str
             (##object->string var))
            (val-str
             (##object->string
              (if (##cte-top? cte)
                (##inverse-eval-in-env val cte)
                (##inverse-eval-in-env val (##cte-parent-cte cte)))
              100)))
        (##GuideUiScheme-environment-set-cell
         (macro-repl-channel-guide-GuideUiScheme channel)
         row
         0
         var-str)
        (##GuideUiScheme-environment-set-cell
         (macro-repl-channel-guide-GuideUiScheme channel)
         row
         1
         val-str)
        (##fixnum.+ row 1)))

    (define (display-rte cte rte row)
      (let loop1 ((c cte)
                  (r rte)
                  (row row))
        (cond ((##cte-top? c)
               row)
              ((##cte-frame? c)
               (let loop2 ((vars (##cte-frame-vars c))
                           (vals (##cdr (##vector->list r)))
                           (row row))
                 (if (##pair? vars)
                   (let ((var (##car vars)))
                     (loop2 (##cdr vars)
                            (##cdr vals)
                            (if (##not (##hidden-local-var? var))
                              (display-var-val var (##car vals) c row)
                              row)))
                   (loop1 (##cte-parent-cte c)
                          (macro-rte-up r)
                          row))))
              (else
               (loop1 (##cte-parent-cte c)
                      r
                      row)))))

    (define (display-vars lst cte row)
      (let loop ((lst lst) (row row))
        (if (##pair? lst)
          (let* ((var-val (##car lst))
                 (var (##car var-val))
                 (val (##cdr var-val)))
            (display-var-val var val cte row)
            (loop (##cdr lst) (##fixnum.+ row 1)))
          row)))

    (define (display-locals lst cte row)
      (if lst
        (display-vars lst cte row)
        row))

    (define (display-parameters lst cte row)
      (let loop ((lst lst) (row row))
        (if (##pair? lst)
          (let* ((param-val (##car lst))
                 (param (##car param-val))
                 (val (##cdr param-val)))
            (loop (##cdr lst)
                  (if (##hidden-parameter? param)
                    row
                    (let ((x (##inverse-eval-in-env param cte)))
                      (display-var-val (##list x) val cte row)))))
          row)))

    (let* ((nb-lex/cte
            (if (##interp-continuation? cont)
              (let (($code (##interp-continuation-code cont))
                    (rte (##interp-continuation-rte cont)))
                (##cons (display-rte (macro-code-cte $code) rte 0)
                        (macro-code-cte $code)))
              (##cons (display-locals (##continuation-locals cont)
                                      ##interaction-cte
                                      0)
                      ##interaction-cte)))
           (nb-rows
            (if cont
              (display-parameters
               (##dynamic-env->list (macro-continuation-denv cont))
               (##cdr nb-lex/cte)
               (##car nb-lex/cte))
              (##car nb-lex/cte))))
      (##GuideUiScheme-environment-set-length
       (macro-repl-channel-guide-GuideUiScheme channel)
       nb-rows)))

  (display-cont cont depth))

;==============================================================================

(set! ##thread-make-repl-channel
  (lambda ()
    (##make-repl-channel-guide)))

(define-prim (##guide-main-window)
  (or ##main-window
      (let* ((app (##QApplication-new (##list (##car (##command-line)))))
             (w (##GuideUiMainWindow-new)))

        (##thread-start!
         (make-thread
          (lambda ()
            (let loop ()
              (##thread-sleep! 0.02)
              (##QApplication-processEvents app)
              (loop)))))

        (set! ##main-window w)
        w)))

(define ##main-window #f)

;==============================================================================