Debian Codegraph @

scheme/ikarus.cafe.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 #|procedure:new-cafe
17 synopsis:
18 (new-cafe [eval])
19 description:
20 The procedure new-cafe starts a new read-eval-print loop inside
21 the current cafe (if one exists). It prompts the user for an
22 expression, evaluates it, prints the result back, and repeats the
23 process. If new-cafe is called with an argument, eval, then that
24 argument must be a procedure that takes a single argument. The
25 eval procedure will be used to evaluate the expressions.
26
27 Every time a new cafe is started, the prompt is changed to reflect
28 the depth of the current cafe (i.e. how many eof objects is takes
29 to exit the outermost cafe).
30
31 Input and output performed by the cafe can be changed by the
32 console-input-port and console-output-port parameters.
33
34 If an die occurs during reading, evaluating, or printing an
35 expression, then the die is printed to the error-port and the
36 operations of the cafe resume as normal.|#
37 #|FIXME:new-cafe
38 Be specific about what the error-port is |#
39
40 (library (ikarus cafe)
41 (export new-cafe waiter-prompt-string)
42 (import
43 (only (rnrs) with-exception-handler)
44 (except (ikarus) new-cafe waiter-prompt-string))
45
46 (define eval-depth 0)
47
48 (define waiter-prompt-string
49 (make-parameter ">"
50 (lambda (x)
51 (if (string? x)
52 x
53 (die 'waiter-prompt-string "not a string" x)))))
54
55 (define display-prompt
56 (lambda (i)
57 (if (fx= i eval-depth)
58 (display " " (console-output-port))
59 (begin
60 (display (waiter-prompt-string) (console-output-port))
61 (display-prompt (fx+ i 1))))))
62
63 (define (print-ex ex)
64 (flush-output-port (console-output-port))
65 (display "Unhandled exception\n" (console-error-port))
66 (print-condition ex (console-error-port)))
67
68 (define (reset k)
69 (reset-input-port! (console-input-port))
70 (k))
71
72 (define (sigint-interrupt? ex)
73 (and (interrupted-condition? ex)
74 (eq? 'SIGINT (interrupted-signal ex))))
75
76 (define wait1
77 (lambda (eval-proc k escape-k)
78 (display-prompt 0)
79 (let ([x (with-exception-handler
80 (lambda (ex)
81 (cond
82 [(lexical-violation? ex)
83 (print-ex ex)
84 (reset k)]
85 [(sigint-interrupt? ex)
86 (flush-output-port (console-output-port))
87 (newline (console-output-port))
88 (reset k)]
89 [else (raise-continuable ex)]))
90 (lambda ()
91 (read (console-input-port))))])
92 (cond
93 [(eof-object? x)
94 (newline (console-output-port))
95 (escape-k (void))]
96 [else
97 (call-with-values
98 (lambda ()
99 (with-exception-handler
100 (lambda (ex)
101 (if (non-continuable-violation? ex)
102 (reset k)
103 (raise-continuable ex)))
104 (lambda ()
105 (with-exception-handler
106 (lambda (ex)
107 (print-ex ex)
108 (when (serious-condition? ex)
109 (reset k)))
110 (lambda ()
111 (eval-proc x))))))
112 (lambda v*
113 (unless (andmap (lambda (v) (eq? v (void))) v*)
114 (with-exception-handler
115 (lambda (ex)
116 (cond
117 [(sigint-interrupt? ex)
118 (flush-output-port (console-output-port))
119 (newline (console-output-port))
120 (reset k)]
121 [else (raise-continuable ex)]))
122 (lambda ()
123 (for-each
124 (lambda (v)
125 (pretty-print v (console-output-port)))
126 v*))))))]))))
127
128 (define do-new-cafe
129 (lambda (eval-proc)
130 (dynamic-wind
131 (lambda () (set! eval-depth (fxadd1 eval-depth)))
132 (lambda ()
133 (call/cc
134 (lambda (k)
135 (let loop ()
136 (call/cc
137 (lambda (k1)
138 (with-exception-handler
139 (lambda (ex)
140 (if (interrupted-condition? ex)
141 (raise-continuable ex)
142 (with-exception-handler k1
143 (lambda ()
144 (flush-output-port (console-output-port))
145 (newline (console-output-port))
146 (reset k1)))))
147 (lambda () (wait1 eval-proc k1 k)))))
148 (loop)))))
149 (lambda () (set! eval-depth (fxsub1 eval-depth))))))
150
151 (define default-cafe-eval
152 (lambda (x)
153 (eval x (interaction-environment))))
154
155 (define new-cafe
156 (case-lambda
157 [() (do-new-cafe default-cafe-eval)]
158 [(p)
159 (unless (procedure? p)
160 (die 'new-cafe "not a procedure" p))
161 (do-new-cafe p)]))
162 )
163