-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfud.scm
375 lines (335 loc) · 8.96 KB
/
fud.scm
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
;;; fud.scm --- Scheme-side section of Fud -*- mode: Gimp; -*-
;; Copyright (C) 2008-2009, 2012 Niels Giesen.
;; Author: Niels Giesen <[email protected]>
;; Keywords: processes, languages, multimedia, tools
;; Author: Niels Giesen <nielsforkgiesen@gmailspooncom, but please
;; replace the kitchen utensils with a dot before hitting "Send">
;; Keywords: processes, multimedia, extensions, tools, gimp, scheme
;; Homepage: http://niels.kicks-ass.org/gimpmode
;; Keywords: lisp, tools, scheme, debugging
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; FUD stands for the FU Debugger. It is developed for use
;; with gimp-mode to debug code written in the TinyScheme
;; implementation shipped with the GIMP, but its design is such that
;; it could in principle work with any scheme.
;;
;; It's basis is setting and handling break-points FUD.scm integrates
;; with fud.el, which in turn is integrated in gimp-mode.
(define (fud-prompt)
(newline)
(fud-write-string "FUD> "))
(define fud-result #f)
(define fud-recursion 0)
(define (fud-recursion++)
(set! fud-recursion
(+ 1 fud-recursion)))
(define (fud-recursion--)
(set! fud-recursion
(- fud-recursion 1)))
(define (fud-reset)
(set! fud-recursion 0))
(define (fud-write-string . strngs)
(display
(string-append
(make-string fud-recursion)
(unbreakupstr strngs ""))))
(define fud-tracing #t)
(define (fud-untrace)
(set! fud-tracing #f))
(define (fud-trace)
(set! fud-tracing #t))
(define (fud-inside-steppable? form)
(list? form))
(define-macro (fud-log . form)
"Very simple logging."
`(let ((evalled (eval (car ',form))))
(newline)
(display "FUD log on")
(display evalled)
(newline)
evalled))
(define-macro (unfud f)
;; "Remove instruction by fudify on function F"
(let
((name f))
`(begin
(if
(not
(assq ',name fudlist))
(error
(string-append
(symbol->string ',f)
" is not fudified")))
(define ,f
(eval
(cadr
(assq ',name fudlist))))
(set! fudlist
(fud-delete
(assq ',name fudlist)
fudlist))
',name)))
(define (fud-breakify sxp)
(list 'fud-break "0.0 file:nil" sxp))
(define-macro (fud-break breakpoint . form)
`(begin
(when fud-tracing
(newline))
(fud-recursion++)
(when fud-tracing
(fud-write-string
(string-append "Break I-> " ,breakpoint))
(newline)
(fud-write-string "I: ")
(write (car ',form)))
(newline)
(fud-write-string
"ENTER: step over, "
(if (fud-inside-steppable? (car ',form))
" I: step inside" "")
" Q: quit,"
" G: go"
" P: poke at environment,"
" V: use value")
(fud-prompt)
(prog1
(call/cc
(lambda (return)
(let (($ #f))
;; INPUT
(case (char-downcase (read-char))
;; Go...
((#\g)
(display "Continued... (fud-trace) to trace again")
(newline)
(return (eval (car ',form))))
;; Inside...
((#\i)
(read-char)
(if (fud-inside-steppable? (car ',form))
(set! $ (eval (fud-instruct-1 (car ',form))))))
;; Quit...
((#\q)
(fud-reset)
(*error-hook* "Quit tracing"))
;; Inspect...
((#\p)
(letrec ((handler (lambda (err)
(display err) "")))
(display "Expression (q to quit inspection): ")
(fud-prompt)
(let loop ((expr (read)))
(cond ((eqv? 'q expr))
((push-handler handler)
(fud-write-string "")
(write (eval expr))
(if (and (pair? *handlers*)
(eq? handler (car *handlers*)))
(pop-handler))
(newline)
(fud-write-string "Type expression: ")
(fud-prompt)
(loop (read)))))))
;; Use value...
((#\v)
(read-char)
(fud-write-string "Enter value: ")
(fud-prompt)
(set! $ (eval (read)))
(read-char)))
;; OUTPUT
(let (($ (or $ (eval (car ',form)))))
(when fud-tracing
(fud-write-string
(string-append "Break O<- " ,breakpoint))
(display " on ")
(write (car ',form))
(newline)
(fud-write-string "O: ")
(write $)
(newline)
(fud-write-string
"ENTER: next breakpoint, "
" Q: quit,"
" G: go (skipping breakpoints),"
" P: poke at environment,"
" V: use value")
(fud-prompt)
(case (char-downcase (read-char))
;; Go...
((#\g)
(display "Continued... (fud-trace) to trace again")
(newline)
(fud-untrace))
;; Quit...
((#\q)
(fud-reset)
(*error-hook* (make-environment)))
;; Inspect...
((#\p)
(letrec ((handler (lambda (err)
(display err) "")))
(display "Expression (q to quit inspection) current value is bound to `$': ")
(fud-prompt)
(let loop ((expr (read)))
(cond ((eqv? 'q expr))
((push-handler handler)
(fud-write-string "")
(write (eval expr))
(if (and (pair? *handlers*)
(eq? handler (car *handlers*)))
(pop-handler))
(newline)
(fud-write-string "Type expression: ")
(fud-prompt)
(loop (read)))))))
;; Use value...
((#\v)
(fud-write-string "Enter value: ")
(fud-prompt)
(return (eval (read))))
(else (return $)))
(return $))))))
(fud-recursion--)
(if (= fud-recursion 0)
(fud-trace)))))
(define (fud-instruct-1 thunk)
;; "Instruct evaluatable members of THUNK with `fud-breakify'.
;; Special forms and macros supported are if, cond, let, let*, letrec, do
;; and lambda. Forms BEGINNING with a symbol in `blacklist' are returned
;; as is.
;; For instruction of functions and macros --blacklisted here--, see
;; `fudify' and `unfud'."
(let ((in-let? #f)
(in-lambda? #f)
(in-do? #f)
(in-cond? #f)
(blacklist '(define define-macro))
(blacklisted? #f)
(num 0))
(if (eq? (car thunk) 'quote)
(cdr thunk))
(mapcar (lambda (th)
(set! num (+ 1 num))
(cond
;; IF
((and (= num 1)
(memq th '(if)))
th)
;; COND
((and (= num 1)
(memq th '(cond)))
(set! in-cond? #t)
'cond)
(in-cond?
(mapcar (lambda (clause)
(fud-breakify clause)) th))
;; LET, LET* and LETREC
((and (= num 1)
(memq th '(let let* letrec)))
(set! in-let? #t)
th)
(in-let?
(if (symbol? th) th
(begin
(set! in-let? #f)
(mapcar (lambda (th)
(list (car th)
(fud-breakify (cadr th)))) th))))
;; DO
((and (= num 1)
(memq th '(do)))
(set! in-do? 'bindings)
th)
((eq? in-do? 'bindings)
(if (symbol? th) th
(begin
(set! in-do? 'test)
(mapcar (lambda (th)
(cond ((= (length th) 2)
(list (car th)
(fud-breakify (cadr th))))
((= (length th) 3)
(list (car th)
(fud-breakify (cadr th))
(fud-breakify (caddr th))))))
th))))
((eq? in-do? 'test)
(set! in-do? #f)
(list (car th)
(fud-breakify (cadr th))))
;; LAMBDA
((and (= num 1)
(memq th '(lambda)))
(set! in-lambda? #t)
'lambda)
(in-lambda?
(set! in-lambda? #f)
th)
;; BLACKLISTED FORMS
((or blacklisted?
(and (= num 1)
(memq th blacklist)))
(set! blacklisted? #t)
th)
(else
(fud-breakify th))))
thunk)))
;; Instruction of functions:
(define (fud-delete item lst)
(let
((out
'()))
(map
(lambda
(item-in-list)
(if
(not
(equal? item item-in-list))
(set! out
(cons item-in-list out))))
lst)
(if
(pair? out)
(reverse out))))
(define fudlist
'())
(define-macro (fudify f)
;; "Instruct a function for debugging;
;; Any subsequent call to function F will cause fud-breaks to occur at
;; any immediate sublevel of F. Remove the instruction with (unfud
;; FUNTION).
;; Any redefinition will uninstruct the function. This will give you the
;; error `Function is already fudified' if after that you want to fudify
;; it again. Client agents can to some intercepting to make this
;; automatic."
(let
((name f))
`(begin
(if
(assq ',name fudlist)
(error
(string-append
(symbol->string ',f)
" is already fudified")))
(set! fudlist
(cons
(list ',name
(get-closure-code ,f))
fudlist))
(define ,f
(eval
(fud-instruct-1
(get-closure-code ,f)))))))