File: cafe.ss

package info (click to toggle)
chezscheme 9.5.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 61,640 kB
  • sloc: ansic: 17,508; sh: 759; makefile: 509; csh: 423
file content (217 lines) | stat: -rw-r--r-- 8,282 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
;;; cafe.ss
;;; Copyright 1984-2017 Cisco Systems, Inc.
;;; 
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;; 
;;; http://www.apache.org/licenses/LICENSE-2.0
;;; 
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(begin
(define default-prompt-and-read
  (lambda (n)
    (unless (and (integer? n) (>= n 0))
       ($oops 'default-prompt-and-read
              "~s is not a nonnegative integer"
              n))
    (let ([prompt (waiter-prompt-string)])
      (unless (string=? prompt "")
        (do ([n n (- n 1)])
            ((= n 0)
             (write-char #\space (console-output-port))
             (flush-output-port (console-output-port)))
            (display prompt (console-output-port))))
      (let ([x (read (console-input-port))])
         (when (and (eof-object? x) (not (string=? prompt "")))
            (newline (console-output-port))
            (flush-output-port (console-output-port)))
         x))))

(define waiter-prompt-and-read
   ($make-thread-parameter
      default-prompt-and-read
      (lambda (p)
         (unless (procedure? p)
            ($oops 'waiter-prompt-and-read "~s is not a procedure" p))
         p)))

(define waiter-write
   ($make-thread-parameter
      (lambda (x)
         (unless (eq? x (void))
            (pretty-print x (console-output-port)))
         (flush-output-port (console-output-port)))
      (lambda (p)
         (unless (procedure? p)
            ($oops 'waiter-write "~s is not a procedure" p))
         p)))

(define waiter-prompt-string
   ($make-thread-parameter
      ">"
      (lambda (s)
         (unless (string? s)
            ($oops 'waiter-prompt-string "~s is not a string" s))
         s)))

(define new-cafe)

(let ()
(define-threaded waiter-expr)
(define-threaded waiter-stat1)
(define-threaded waiter-stat2)
(define-threaded waiter-total-stats)

(define sstats-sum
  (lambda (a b)
    (define sstats-time-add
      (lambda (f a b)
        (add-duration (f a) (f b))))
    (make-sstats
      (sstats-time-add sstats-cpu a b)
      (sstats-time-add sstats-real a b)
      (+ (sstats-bytes a) (sstats-bytes b))
      (+ (sstats-gc-count a) (sstats-gc-count b))
      (sstats-time-add sstats-gc-cpu a b)
      (sstats-time-add sstats-gc-real a b)
      (+ (sstats-gc-bytes a) (sstats-gc-bytes b)))))

(define waiter
   (lambda (cafe eval)
      (let ([x ((waiter-prompt-and-read) cafe)])
         (when (eof-object? x) (exit))
         (fluid-let ([waiter-total-stats (make-sstats
                                           (make-time 'time-duration 0 0)
                                           (make-time 'time-duration 0 0)
                                           0
                                           0
                                           (make-time 'time-duration 0 0)
                                           (make-time 'time-duration 0 0)
                                           0)]
                     [waiter-expr x]
                     [waiter-stat1 (void)]
                     [waiter-stat2 (void)])
            (dynamic-wind #t
               (lambda ()
                  (set! waiter-stat1 (statistics))
                  (set! waiter-stat2 (statistics)))
               (lambda ()
                  (parameterize ([$interrupt waiter-interrupt])
                     (top-level eval x)))
               (lambda ()
                  (let ([s (statistics)])
                     (set! waiter-total-stats
                           (sstats-sum (sstats-difference
                                          (sstats-difference s waiter-stat2)
                                          (sstats-difference waiter-stat2
                                                             waiter-stat1))
                                       waiter-total-stats)))))))
      (waiter cafe eval)))

; This marks the "top-level" continuation for the debugger
(define top-level
  (lambda (eval x)
    (call/cc ; grab continuation & start a new stack segment
      (rec new-cafe
        (lambda (k)
          ($current-stack-link $null-continuation) ; toss what's below
          (call-with-values
            (lambda () (eval x))
            (lambda args (for-each (waiter-write) args)))
          (k))))))

(define waiter-interrupt
   (lambda ()
      (call/cc
         (lambda (k)
            (parameterize ([$interrupt void])
               (let ([s (statistics)])
                  (set! waiter-total-stats
                        (sstats-sum (sstats-difference
                                       (sstats-difference s waiter-stat2)
                                       (sstats-difference waiter-stat2
                                                          waiter-stat1))
                                    waiter-total-stats)))
               (clear-input-port (console-input-port))
               (let ([waiter (call/cc
                                (lambda (k)
                                   (rec f (lambda () (k f)))))])
                  (fprintf (console-output-port) "break> ")
                  (flush-output-port (console-output-port))
                  (case (let ([x (parameterize ([$interrupt waiter]
                                                [reset-handler waiter])
                                    (read (console-input-port)))])
                           (if (eof-object? x) 
                               (begin (newline (console-output-port))
                                      (flush-output-port (console-output-port))
                                      'exit)
                               x))
                     [(exit e)
                      (void)]
                     [(statistics s)
                      (parameterize ([print-level 2] [print-length 2])
                         (fprintf (console-output-port)
                                  "(time ~s)~%"
                                  waiter-expr))
                      (sstats-print waiter-total-stats (console-output-port))
                      (flush-output-port (console-output-port))
                      (waiter)]
                     [(reset r quit q)
                      (reset)]
                     [(abort a)
                      (abort)]
                     [(new-cafe n)
                      (new-cafe)
                      (waiter)]
                     [(inspect i)
                      (inspect k)
                      (waiter)]
                     [(?)
                      (fprintf (console-output-port) "
Type e to exit interrupt handler and continue
     r or q to reset scheme
     a to abort scheme
     n to enter new cafe
     i to inspect current continuation
     s to display statistics

")
                      (flush-output-port (console-output-port))
                      (waiter)]
                     [else
                      (fprintf (console-output-port)
                               "Invalid command.  Type ? for options.~%")
                      (flush-output-port (console-output-port))
                      (waiter)]))
               (set! waiter-stat1 (statistics))
               (set! waiter-stat2 (statistics)))))))

(set! $cafe ($make-thread-parameter 0))

(set! new-cafe
  (let ()
    (rec new-cafe
      (case-lambda
        [() (new-cafe eval)]
        [(eval)
         (unless (procedure? eval)
           ($oops 'new-cafe "~s is not a procedure" eval))
         (call/cc
           (lambda (k1)
             (parameterize ([exit-handler k1] [reset-handler (reset-handler)])
               (let ((k2 k1))
                 (reset-handler (lambda () (k2)))
                 (call/cc (lambda (k) (set! k2 k)))
                 (parameterize ([$cafe (+ ($cafe) 1)] [$interrupt reset])
                   (with-exception-handler
                     (lambda (c) ((base-exception-handler) c))
                     (lambda ()
                       (waiter ($cafe) eval))))))))]))))
)
)