-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverbose-bindings.rkt
193 lines (175 loc) · 5.72 KB
/
verbose-bindings.rkt
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
#lang racket
;;; File:
;;; verbose-bindings.rkt
;;; Author:
;;; Samuel A. Rebelsky
;;; Summary:
;;; A set of macros that make it easier to observe what's
;;; happening when we bind names to values.
;;; Contents:
;;; (verbose-define name value)
;;; Assign value to name, describing the binding.
;;; (verbose-let (name-value-pairs) exp1 ...)
;;; Assign each value to the corresponding name, but
;;; also describes the bindings.
;;; (verbose-let* (name-value-pairs) exp1 ...)
;;; Assign each value to the corresponding name, in sequence.
;;; Citations:
;;; Based closely on code written by Samuel A. Rebelsky in or about
;;; 2006.
(provide verbose-define)
(provide verbose-let)
(provide verbose-let*)
; +-------------+----------------------------------------------------
; | Indentation |
; +-------------+
;;; indentation : string?
;;; The current amount of indentation.
(define indentation "")
;;; (indent!) -> void?
;;; Add some indentation.
(define indent!
(lambda ()
(display indentation)))
;;; (increase-indentation!) -> void?
;;; Increase the indentation.
(define increase-indentation!
(lambda ()
(set! indentation (string-append indentation " "))))
;;; (decrease-indentation!) -> void?
;;; Decrease the indentation.
(define decrease-indentation!
(lambda ()
(when (>= (string-length indentation) 2)
(set! indentation (substring indentation 2)))))
;;; (report str) -> void?
;;; str : string?
;;; Report something.
(define report
(lambda (str)
(indent!)
(displayln str)))
; +--------+---------------------------------------------------------------
; | Macros |
; +--------+
;;; Procedure (Macro):
;;; verbose-define
;;; Parameters:
;;; name, a symbol [unverified]
;;; exp, a valid expression.
;;; Purpose:
;;; Globally binds val to name while reporting the binding (for
;;; observation).
;;; Preconditions:
;;; val must be a valid expression.
;;; Postconditions:
;;; name can be now used in future expressions, and gives the value
;;; of exp.
;;; Philosophy:
;;; It may be helpful to students learning about the techniques for
;;; binding values to see the bindings happen. This procedure does
;;; little more than report the binding and then call define.
(define-syntax verbose-define
(syntax-rules ()
[(verbose-define name exp)
(begin
(report "Beginning define")
(increase-indentation!)
(report-binding name exp "define")
(define name exp)
(decrease-indentation!)
(report "Ending define"))]))
(define-syntax verbose-define/alt
(syntax-rules ()
[(verbose-define name exp)
(begin
(report (format "Starting definition of ~a" 'name))
(increase-indentation!)
(report (format "Evaluating ~a" 'exp))
(define name null)
(let ([_tmp_ exp])
(set! name _tmp_)
(decrease-indentation!)
(report (format "Binding ~a to ~a with define" 'a _tmp_))))]))
;;; Procedure (Macro):
;;; verbose-let
;;; Parameters:
;;; list-of-bindings, a list of name/expression lists
;;; exp0 ..., a list of expressions
;;; Purpose:
;;; Does the same thing as (let list-of-bindings exp0 ...), except that
;;; it also provides a report on what happens.
;;; Preconditions:
;;; The corresponding let operation must be valid.
;;; Postconditions:
;;; See those for let.
;;; Philosophy:
;;; It may be helpful to students learning about the techniques for
;;; binding values to see the bindings happen. This procedure does
;;; little more than report the binding and then call let.
(define-syntax verbose-let
(syntax-rules ()
[(verbose-let definitions body ...)
(begin
(report "Beginning let")
(increase-indentation!)
(display-let-bindings definitions)
(let ([result (let definitions body ...)])
(decrease-indentation!)
(report "Ending let")
result))]))
;;; Procedure (Macro):
;;; verbose-let*
;;; Parameters:
;;; list-of-bindings, a list of name/expression lists
;;; exp0 ..., a list of expressions
;;; Purpose:
;;; Does the same thing as (let* list-of-bindings exp0 ...), except that
;;; it also provides a report on what happens.
;;; Preconditions:
;;; The corresponding let* operation must be valid.
;;; Postconditions:
;;; See those for let*.
;;; Philosophy:
;;; It may be helpful to students learning about the techniques for
;;; binding values to see the bindings happen. This procedure shows them
;;; as they happen.
;;; Props:
;;; This code is based on a definition of let* taken from Chapter 8 of
;;; the 3rd edition of R Kent Dybvig's _The Scheme Programming Language_,
;;; found on the Web at <http://www.scheme.com/tspl3/syntax.html>.
(define-syntax verbose-let*
(syntax-rules ()
[(verbose-let* definitions body ...)
(begin
(report "Beginning let*")
(increase-indentation!)
(let ((result (verbose-let*-helper definitions body ...)))
(decrease-indentation!)
(report "Ending let*")
result))]))
(define-syntax verbose-let*-helper
(syntax-rules ()
[(verbose-let*-helper () e0 ...)
(let () e0 ...)]
[(verbose-let*-helper ((n0 v0) (n1 v1) ...) e0 ...)
(begin
(report-binding n0 v0 "let*")
(let ((n0 v0))
(verbose-let*-helper ((n1 v1) ...) e0 ...)))]))
; +---------+--------------------------------------------------------------
; | Helpers |
; +---------+
(define-syntax report-binding
(syntax-rules ()
((_ name exp how)
(report (format "Binding ~a to ~a with ~a"
'name 'exp how)))))
(define-syntax display-let-bindings
(syntax-rules ()
((_ ())
(display ""))
((_ ((n1 v1) (n2 v2) ...))
(begin
(report-binding n1 v1 "let")
(display-let-bindings ((n2 v2) ...))))))