Debian Codegraph @

lab/read-exprs.ss

raw
1 #!/usr/bin/env scheme-script
2 (import (ikarus) (ikarus system interrupts))
3
4 (define (prompt-and-read-exprs p)
5 (define (read-lines ls)
6 (let ([p (open-input-string
7 (apply string-append (reverse ls)))])
8 (let f ([ls '()])
9 (let ([x (read p)])
10 (cond
11 [(eof-object? x)
12 (if (null? ls)
13 (eof-object)
14 (reverse ls))]
15 [else (f (cons x ls))])))))
16 (define (read-lines-maybe ls)
17 (call/cc
18 (lambda (k)
19 (with-exception-handler
20 (lambda (cn)
21 (cond
22 [(interrupted-condition? cn)
23 (raise-continuable cn)]
24 [else (k #f)]))
25 (lambda ()
26 (read-lines ls))))))
27 (display "> " (console-output-port))
28 (let f ([lns '()])
29 (let ([x (get-line p)])
30 (cond
31 [(eof-object? x)
32 (read-lines lns)]
33 [else
34 (let ([lns (cons x lns)])
35 (or (read-lines-maybe lns)
36 (f lns)))]))))
37
38 (printf "Experimental prompt\n")
39 (printf "This just echos the output pretty-printed\n\n")
40 (let f ()
41 (define (try k f)
42 (with-exception-handler
43 (lambda (cn)
44 (flush-output-port (current-error-port))
45 (flush-output-port (current-output-port))
46 (reset-input-port! (current-input-port))
47 (newline (console-output-port))
48 (unless (interrupted-condition? cn)
49 (fprintf (console-output-port)
50 "Error while reading expression\n")
51 (print-condition cn (console-output-port)))
52 (k))
53 f))
54 (call/cc
55 (lambda (k)
56 (let ([x
57 (try k
58 (lambda ()
59 (prompt-and-read-exprs
60 (current-input-port))))])
61 (cond
62 [(eof-object? x)
63 (newline (console-output-port))
64 (exit)]
65 [else
66 (for-each
67 (lambda (x)
68 (pretty-print x (console-output-port)))
69 x)]))))
70 (f))
71