-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdebug-internal.ss
More file actions
257 lines (230 loc) · 8.31 KB
/
debug-internal.ss
File metadata and controls
257 lines (230 loc) · 8.31 KB
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
#lang scheme/base
(require (for-syntax scheme/base
scheme/require-transform
scheme/provide-transform
(only-in srfi/13 string-index-right)
"base.ss"
"syntax.ss")
scheme/require-syntax
scheme/path
scheme/provide-syntax
(only-in srfi/1 take-right)
srfi/13
"base.ss"
"contract.ss")
; (parameter boolean)
(define debug-enabled?
(make-parameter #t))
; string any -> void
(define (default-debug-printer message value)
; string
(define value-string
(with-pretty-indent " "
(pretty-format value)))
(printf "~a:~n~a~n" message value-string))
; (parameter (string any -> void))
(define current-debug-printer
(make-parameter default-debug-printer))
; string any -> any
;
; Prints the value of the specified expression and returns it transparently.
(define (debug message value)
(when (debug-enabled?)
((current-debug-printer) message value))
value)
; string syntax -> any
(define (debug-syntax message value)
(debug message (syntax->datum value))
value)
; (_ string any ...) -> any
;
; Prints the value of the specified expression and returns it transparently.
(define-syntax debug*
(syntax-rules ()
[(_ message proc arg ...)
(let ([value (proc arg ...)])
(when (debug-enabled?)
((current-debug-printer) message value))
value)]))
; (_) -> void
; (_ any) -> any
(define-syntax (debug-location stx)
(syntax-case stx ()
[(_)
#`(when (debug-enabled?)
(display "Reached ")
(display #,(syntax-location-string stx))
(newline))]
[(_ expr)
#`(debug (format "Reached ~a" #,(syntax-location-string stx)) expr)]))
; (_ id value)
(define-syntax (define/debug stx)
(define (args->exprs arg-stx)
(syntax-case arg-stx ()
[() null]
[(id rest ...)
(identifier? #'id)
(cons #'(list 'id id)
(args->exprs #'(rest ...)))]
[([id expr] rest ...)
(identifier? #'id)
(cons #'(list 'id id)
(args->exprs #'(rest ...)))]
[(kw rest ...)
(keyword? (syntax->datum #'kw))
(args->exprs #'(rest ...))]
[_ (raise-syntax-error #f "bad function argument" arg-stx stx)]))
(syntax-case stx ()
[(_ (id arg ...)
expr ...)
(identifier? #'id)
#`(define (id arg ...)
(debug (format ">>> ~a" 'id)
(list #,@(args->exprs #'(arg ...))))
(debug (format "<<< ~a" 'id)
((lambda () expr ...))))]
[(_ id expr)
(identifier? #'id)
#`(define id (debug (symbol->string 'id) expr))]))
; (_ (id ...) value)
(define-syntax (define-values/debug stx)
(syntax-case stx ()
[(_ (id ...) val)
#`(define-values (id ...)
(apply values (debug (format "~a" '(id ...))
(call-with-values (lambda () val) list))))]))
; (_ ([id value] ...) expr ...)
(define-syntax (let/debug stx)
(syntax-case stx ()
[(_ loop ([var val] ...) exp ...)
(identifier? #'loop)
#'(let loop ([var val] ...)
(debug (symbol->string 'var) var) ...
exp ...)]
[(_ ([var val] ...) exp ...)
#'(let ([var (debug (symbol->string 'var) val)] ...)
exp ...)]))
; (_ ([id value] ...) expr ...)
(define-syntax (let*/debug stx)
(syntax-case stx ()
[(_ ([var val] ...) exp ...)
#'(let* ([var (debug (symbol->string 'var) val)] ...)
exp ...)]))
; (_ ([id value] ...) expr ...)
(define-syntax (letrec/debug stx)
(syntax-case stx ()
[(_ ([var val] ...) exp ...)
#'(letrec ([var (debug (symbol->string 'var) val)] ...)
exp ...)]))
; (_ ([(id ...) value] ...) expr ...)
(define-syntax (let-values/debug stx)
(syntax-case stx ()
[(_ ([(var ...) val] ...) exp ...)
#`(let-values ([(var ...)
(apply values (debug (format "~a" '(var ...))
(call-with-values (lambda () val) list)))]
...)
exp ...)]))
; (_ ([(id ...) value] ...) expr ...)
(define-syntax (let*-values/debug stx)
(syntax-case stx ()
[(_ ([(var ...) val] ...) exp ...)
#`(let*-values ([(var ...)
(apply values (debug (format "~a" '(var ...))
(call-with-values (lambda () val) list)))]
...)
exp ...)]))
; (_ ([(id ...) value] ...) expr ...)
(define-syntax (letrec-values/debug stx)
(syntax-case stx ()
[(_ ([(var ...) val] ...) exp ...)
#`(letrec-values ([(var ...)
(apply values (debug (format "~a" '(var ...))
(call-with-values (lambda () val) list)))]
...)
exp ...)]))
; string (-> any) -> any
(define-syntax with-pretty-indent
(syntax-rules ()
[(_ indent expr ...)
(let* ([indent-string indent]
[indent-amount (string-length indent-string)])
(parameterize ([pretty-print-print-line
(lambda (line out offset width)
(cond [(eq? line 0) (display indent-string out)
indent-amount]
[(number? line) (if (number? width)
(begin
(newline out)
(display indent-string out)
indent-amount)
(begin
0))]
[else (when (number? width)
(newline out))
0]))])
expr ...))]))
; exn -> (listof symbol)
(define (exn-context exn)
(map (match-lambda
[(list-rest name srcloc)
(string->symbol
(if srcloc
(format "~a:~a:~a:~a"
(path->short-string (srcloc-source srcloc))
(srcloc-line srcloc)
(srcloc-column srcloc)
(or name '??))
(format "??:??:??:~a"
(or name '??))))])
(continuation-mark-set->context (exn-continuation-marks exn))))
; Deprecated aliases -----------------------------
(define-syntax-rule (define-debug . any) (define/debug . any))
(define-syntax-rule (define-values-debug . any) (define-values/debug . any))
(define-syntax-rule (let-debug . any) (let/debug . any))
(define-syntax-rule (let*-debug . any) (let*/debug . any))
(define-syntax-rule (letrec-debug . any) (letrec/debug . any))
(define-syntax-rule (let-values-debug . any) (let-values/debug . any))
(define-syntax-rule (let*-values-debug . any) (let*-values/debug . any))
(define-syntax-rule (letrec-values-debug . any) (letrec-values/debug . any))
; Helpers ----------------------------------------
; path -> string
(define (path->short-string path)
; (listof (U string 'up 'same))
(define path-elements
(foldr (lambda (element accum)
(cond [(path? element) (cons (path->string element) accum)]
[(eq? element 'same) accum]
[(eq? element 'up) (cons ".." accum)]))
null
(explode-path (simplify-path path))))
; natural
(define num-elements
(length path-elements))
(string-join (take-right path-elements (min 3 num-elements)) "/"))
; Provide statements -----------------------------
(provide debug*
debug-location
define/debug
define-values/debug
let/debug
let*/debug
letrec/debug
let-values/debug
let*-values/debug
letrec-values/debug
define-debug
define-values-debug
let-debug
let*-debug
letrec-debug
let-values-debug
let*-values-debug
letrec-values-debug
with-pretty-indent)
(provide/contract
[debug-enabled? (parameter/c boolean?)]
[current-debug-printer (parameter/c (-> string? any/c void?))]
[debug (-> string? any/c any)]
[debug-syntax (-> string? syntax? syntax?)]
[exn-context (-> exn? (listof symbol?))])