1 | ;;;;;; Dissection state |
---|
2 | |
---|
3 | ;;; Copyright (C) 2004, Taylor Campbell |
---|
4 | ;;; All rights reserved. |
---|
5 | ;;; See the LICENCE file for details. |
---|
6 | |
---|
7 | (define-record-type rtd/dissection |
---|
8 | (make-dissection focus menu history quit-cont inport outport) |
---|
9 | dissection? |
---|
10 | (focus dissection-focus set-dissection-focus!) |
---|
11 | (menu dissection-menu set-dissection-menu!) |
---|
12 | (history dissection-history set-dissection-history!) |
---|
13 | (quit-cont dissection-quit-cont) |
---|
14 | (inport dissection-inport) |
---|
15 | (outport dissection-outport)) |
---|
16 | |
---|
17 | (define (dissection-push! dissection object) |
---|
18 | (set-dissection-history! dissection |
---|
19 | (cons (dissection-focus dissection) |
---|
20 | (dissection-history dissection))) |
---|
21 | (set-dissection-focus! dissection object) |
---|
22 | (set-dissection-menu! dissection (prepare-dissection-menu object))) |
---|
23 | |
---|
24 | (define (dissection-pop! dissection) |
---|
25 | (let ((history (dissection-history dissection))) |
---|
26 | (if (null? history) |
---|
27 | (error "No more objects in dissection's history" dissection) |
---|
28 | (let ((focus (car history))) |
---|
29 | (set-dissection-history! dissection (cdr history)) |
---|
30 | (set-dissection-focus! dissection focus) |
---|
31 | (set-dissection-menu! dissection |
---|
32 | (prepare-dissection-menu focus)))))) |
---|
33 | |
---|
34 | (define (dissection-pop-multiple! dissection count) |
---|
35 | (let ((history (dissection-history dissection))) |
---|
36 | (cond ((maybe-drop history (- count 1)) |
---|
37 | => (lambda (history*) |
---|
38 | (let ((focus (car history*))) |
---|
39 | (set-dissection-history! dissection (cdr history*)) |
---|
40 | (set-dissection-focus! dissection focus) |
---|
41 | (set-dissection-menu! |
---|
42 | dissection (prepare-dissection-menu focus))))) |
---|
43 | (else |
---|
44 | (error "Too many history elements popped" count))))) |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | ;;; -------------------- |
---|
49 | ;;; Evaluating Scheme code, manipulating the focus object |
---|
50 | |
---|
51 | (define (dissection-apply dissection expression) |
---|
52 | (call-with-values (lambda () |
---|
53 | (eval expression (interaction-environment))) |
---|
54 | (case-lambda |
---|
55 | ((function) |
---|
56 | (receive results |
---|
57 | (function (dissection-focus dissection)) |
---|
58 | (let ((port (dissection-outport dissection))) |
---|
59 | (cond ((null? results) |
---|
60 | (display "; No values retrned" port)) |
---|
61 | ((null? (cdr results)) |
---|
62 | (write (car results) port)) |
---|
63 | (else |
---|
64 | (display "; " port) |
---|
65 | (write (length results) port) |
---|
66 | (display " values returned" port) |
---|
67 | (for-each (lambda (result) |
---|
68 | (newline port) |
---|
69 | (write result port)) |
---|
70 | results))) |
---|
71 | (newline port) |
---|
72 | (flush-output port)))) |
---|
73 | (results |
---|
74 | (error "Wrong number of values returned" |
---|
75 | results))))) |
---|
76 | |
---|
77 | (define (dissection-apply/dissect dissection expression) |
---|
78 | (call-with-values (lambda () |
---|
79 | (eval expression (interaction-environment))) |
---|
80 | (case-lambda |
---|
81 | ((function) |
---|
82 | (receive results |
---|
83 | (function (dissection-focus dissection)) |
---|
84 | (cond ((null? results) |
---|
85 | (error "Can't dissect zero values")) |
---|
86 | ((null? (cdr results)) |
---|
87 | (dissection-push! dissection (car results)) |
---|
88 | (display-dissection dissection)) |
---|
89 | (else |
---|
90 | (let ((port (dissection-outport dissection))) |
---|
91 | (display "; Dissecting " port) |
---|
92 | (write (length results) port) |
---|
93 | (display " values" port) |
---|
94 | (newline port) |
---|
95 | (dissection-push! dissection results) |
---|
96 | (display-dissection dissection)))))) |
---|
97 | (results |
---|
98 | (error "Wrong number of values returned" |
---|
99 | results))))) |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | ;;; -------------------- |
---|
104 | ;;; Printing the dissection state |
---|
105 | |
---|
106 | (define (display-dissection dissection) |
---|
107 | (let ((port (dissection-outport dissection))) |
---|
108 | ((dissection-overview-printer) (dissection-focus dissection) port) |
---|
109 | (if (not (undissectable? (dissection-focus dissection))) |
---|
110 | (display-dissection-menu (dissection-menu dissection) port)))) |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | ;;; -------------------- |
---|
115 | ;;; Main dissection loop |
---|
116 | |
---|
117 | (define (dissection-loop dissection) |
---|
118 | (let ((inport (dissection-inport dissection)) |
---|
119 | (outport (dissection-outport dissection))) |
---|
120 | (let loop () |
---|
121 | (display (dissection-prompt) outport) |
---|
122 | (flush-output outport) |
---|
123 | (read-dissection-command inport |
---|
124 | (lambda stuff ; EOF case: throw back to where |
---|
125 | ((dissection-quit-cont ; the original dissection |
---|
126 | dissection) ; occurred. |
---|
127 | (lambda () (apply values stuff)))) |
---|
128 | |
---|
129 | (lambda (line) ; Invalid line case: retry |
---|
130 | ;; Debugging changes/additions are marked with ;++ |
---|
131 | (display "Invalid dissection command line" outport) |
---|
132 | (write-char |
---|
133 | ;++ #\: |
---|
134 | #\. |
---|
135 | outport) |
---|
136 | (newline outport) |
---|
137 | ;++ (display " " outport) |
---|
138 | ;++ (display line outport) |
---|
139 | ;++ (newline outport) |
---|
140 | (flush-output outport) |
---|
141 | (loop)) |
---|
142 | loop ; Blank line case |
---|
143 | (lambda (command-name args) ; Success case |
---|
144 | (carefully-handle-dissection-command command-name dissection |
---|
145 | args) |
---|
146 | (loop)))))) |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | ;;; -------------------- |
---|
151 | ;;; Dissector commands |
---|
152 | |
---|
153 | ;;; General dissection commands |
---|
154 | |
---|
155 | (define-dissection-command 'print '(p) |
---|
156 | "Prints out the entirety of the current dissection." |
---|
157 | #f #f |
---|
158 | 0 |
---|
159 | display-dissection) |
---|
160 | |
---|
161 | (define-dissection-command 'overview '(o) |
---|
162 | "Prints the focus value, without a menu." |
---|
163 | #f #f |
---|
164 | 0 |
---|
165 | (lambda (dissection) |
---|
166 | ((dissection-overview-printer) dissection |
---|
167 | (dissection-outport dissection)))) |
---|
168 | |
---|
169 | (define-dissection-command 'up '(u) |
---|
170 | "Moves back up the history of dissected objects." |
---|
171 | "[<count>]" |
---|
172 | '("Moves COUNT elements back up the history of dissected objects." |
---|
173 | "If COUNT is absent, it defaults to 1.") |
---|
174 | '(0 1) |
---|
175 | (case-lambda |
---|
176 | ((dissection) |
---|
177 | (dissection-pop! dissection) |
---|
178 | (display-dissection dissection)) |
---|
179 | ((dissection count) |
---|
180 | (cond ((not (and (integer? count) |
---|
181 | (exact? count) |
---|
182 | (<= 0 count))) |
---|
183 | (error "Invalid count" |
---|
184 | '(expected exact nonnegative integer))) |
---|
185 | ((= count 1) |
---|
186 | (dissection-pop! dissection)) |
---|
187 | ((not (zero? count)) |
---|
188 | (dissection-pop-multiple! dissection count))) |
---|
189 | (display-dissection dissection)))) |
---|
190 | |
---|
191 | (define-dissection-command 'history '(h) |
---|
192 | "Prints the dissection history." |
---|
193 | #f #f |
---|
194 | 0 |
---|
195 | (lambda (dissection) |
---|
196 | (let ((port (dissection-outport dissection))) |
---|
197 | (call-with-limited-output-port port (dissector-right-margin) |
---|
198 | (lambda (port) |
---|
199 | (write (dissection-history dissection) port))) |
---|
200 | (newline port) |
---|
201 | (flush-output port)))) |
---|
202 | |
---|
203 | (define-dissection-command 'quit '(q exit) |
---|
204 | "Quits the dissection." |
---|
205 | #f #f |
---|
206 | 0 |
---|
207 | (lambda (dissection) |
---|
208 | ((dissection-quit-cont dissection) unspecific))) |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | ;;; Evaluating Scheme code, manipulating the focus object |
---|
213 | |
---|
214 | (define-dissection-command 'eval '(e scheme) |
---|
215 | "Evaluates an expression." |
---|
216 | "<expression>" |
---|
217 | '("Evalutes EXPRESSION in the interaction environment and prints the" |
---|
218 | "results. This does not modify the current dissection.") |
---|
219 | 1 |
---|
220 | (lambda (dissection expression) |
---|
221 | (dissection-apply dissection |
---|
222 | `(lambda (,(gensym)) ,expression)))) |
---|
223 | |
---|
224 | (define-dissection-command 'apply '(a) |
---|
225 | "Apply a function to the focus object." |
---|
226 | "<expression>" |
---|
227 | '("Evaluates EXPRESSION in the interaction environment, which should" |
---|
228 | "produce a unary function, and applies it to the current focus" |
---|
229 | "object. This does not modify the current dissection.") |
---|
230 | 1 |
---|
231 | dissection-apply) |
---|
232 | |
---|
233 | (define-dissection-command 'dissect '(d) |
---|
234 | "Dissect a completely new object." |
---|
235 | "<expression>" |
---|
236 | '("Evaluates EXPRESSION in the interaction environment and dissects" |
---|
237 | "the value that is produced. EXPRESSION may evaluate to one or" |
---|
238 | "more values. With one value, that value is dissected; with more," |
---|
239 | "a list of the values is dissected.") |
---|
240 | 1 |
---|
241 | (lambda (dissection expression) |
---|
242 | (dissection-apply/dissect dissection |
---|
243 | `(lambda (,(gensym)) ,expression)))) |
---|
244 | |
---|
245 | (define-dissection-command 'apply/dissect '(ad) |
---|
246 | "Apply a function to the focus object & dissect its result." |
---|
247 | "<expression>" |
---|
248 | '("Evaluates EXPRESSION in the interaction environment, which should" |
---|
249 | "produce a unary function, and applies it to the current focus" |
---|
250 | "object. It must return at least one value. If it returns one" |
---|
251 | "value, that value is dissected; if it returns more than one, a" |
---|
252 | "list containing the values is dissected.") |
---|
253 | 1 |
---|
254 | dissection-apply/dissect) |
---|