Debian Codegraph @

%3a166/base.scm

raw symbols SCIP
1
2 ;;> The minimal base formatting combinators and show interface.
3
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5
6 (define-syntax fn
7 (syntax-rules ()
8 ((fn . x)
9 (computation-fn . x))))
10
11 ;; The base formatting handles outputting raw strings and a simple,
12 ;; configurable handler for formatting objects.
13
14 ;; Utility - default value of string-width.
15 (define (substring-length str . o)
16 (let ((start (if (pair? o) (car o) 0))
17 (end (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-length str))))
18 (- end start)))
19
20 (define (call-with-output-string proc)
21 (let ((out (open-output-string)))
22 (proc out)
23 (let ((res (get-output-string out)))
24 (close-output-port out)
25 res)))
26
27 ;;> Raw output - displays str to the formatter output port and updates
28 ;;> row and col.
29 (define (output-default str)
30 (fn (port (r row) (c col) string-width)
31 (let ((nl-index (string-index-right str #\newline)))
32 (write-string str port)
33 (if (string-cursor>? nl-index (string-cursor-start str))
34 (with! (row (+ r (string-count str (lambda (ch) (eqv? ch #\newline)))))
35 (col (string-width str (string-cursor->index str nl-index))))
36 (with! (col (+ c (string-width str))))))))
37
38 (define-computation-type make-show-env show-run
39 (port (current-output-port))
40 (col 0)
41 (row 0)
42 (width 78)
43 (radix 10)
44 (pad-char #\space)
45 (output output-default)
46 (string-width substring-length)
47 (substring/width substring)
48 (substring/preserve #f)
49 (word-separator? char-whitespace?)
50 (ambiguous-is-wide? #f)
51 (ellipsis "")
52 (decimal-align #f)
53 (decimal-sep #f)
54 (comma-sep #f)
55 (comma-rule #f)
56 (sign-rule #f)
57 (precision #f)
58 (writer #f)
59 (pretty-environment (interaction-environment))
60 )
61
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63
64 ;;> \procedure{(show out [args ...])}
65 ;;>
66 ;;> Run the combinators \var{args}, accumulating the output to
67 ;;> \var{out}, which is either an output port or a boolean, with
68 ;;> \scheme{#t} indicating \scheme{current-output-port} and
69 ;;> \scheme{#f} to collect the output as a string.
70 (define (show out . args)
71 (let ((proc (each-in-list args)))
72 (cond
73 ((output-port? out)
74 (show-run (sequence (with! (port out)) proc)))
75 ((eq? #t out)
76 (show-run (sequence (with! (port (current-output-port))) proc)))
77 ((eq? #f out)
78 (call-with-output-string
79 (lambda (out)
80 (show-run (sequence (with! (port out)) proc)))))
81 (else
82 (error "unknown output to show" out)))))
83
84
85 ;;> Temporarily bind the parameters in the body \var{x}.
86
87 (define-syntax with
88 (syntax-rules ()
89 ((with params x ... y)
90 (computation-with params (each x ... y)))))
91
92 ;;> The noop formatter. Generates no output and leaves the state
93 ;;> unmodified.
94 (define nothing (fn () (with!)))
95
96 ;;> Formats a displayed version of x - if a string or char, outputs the
97 ;;> raw characters (as with `display'), if x is already a formatter
98 ;;> defers to that, otherwise outputs a written version of x.
99 (define (displayed x)
100 (cond
101 ((procedure? x) x)
102 ((string? x) (fn ((output1 output)) (output1 x)))
103 ((char? x) (displayed (string x)))
104 (else (written x))))
105
106 ;;> Formats a written version of x, as with `write'. The formatting
107 ;;> can be updated with the \scheme{'writer} field.
108 (define (written x)
109 (fn (writer) ((or writer written-default) x)))
110
111 ;;> Takes a single list of formatters, combined in sequence with
112 ;;> \scheme{each}.
113 (define (each-in-list args)
114 (if (pair? args)
115 (if (pair? (cdr args))
116 (sequence (displayed (car args)) (each-in-list (cdr args)))
117 (fn () (displayed (car args))))
118 nothing))
119
120 ;;> Combines each of the formatters in a sequence using
121 ;;> \scheme{displayed}, so that strings and chars will be output
122 ;;> directly and other objects will be \scheme{written}.
123 (define (each . args)
124 (each-in-list args))
125
126 ;;> Captures the output of \var{producer} and formats the result with
127 ;;> \var{consumer}.
128 (define (call-with-output producer consumer)
129 (let ((out (open-output-string)))
130 (forked (with ((port out) (output output-default)) producer)
131 (fn () (consumer (get-output-string out))))))