@@ -41,12 +41,17 @@ struct Empty end
4141
4242(rw:: Empty )(x) = nothing
4343
44+ instrument (x, f) = f (x)
45+ instrument (x:: Empty , f) = x
46+
4447struct IfElse{F, A, B}
4548 cond:: F
4649 yes:: A
4750 no:: B
4851end
4952
53+ instrument (x:: IfElse , f) = IfElse (x. cond, instrument (x. yes, f), instrument (x. no, f))
54+
5055function (rw:: IfElse )(x)
5156 rw. cond (x) ? rw. yes (x) : rw. no (x)
5257end
@@ -67,11 +72,14 @@ function (rw::Chain)(x)
6772 return x
6873end
6974
75+ instrument (c:: Chain , f) = Chain (map (x-> instrument (x,f), c. rws))
7076
7177struct RestartedChain{Cs}
7278 rws:: Cs
7379end
7480
81+ instrument (c:: RestartedChain , f) = RestartedChain (map (x-> instrument (x,f), c. rws))
82+
7583function (rw:: RestartedChain )(x)
7684 for f in rw. rws
7785 y = @timer cached_repr (f) f (x)
@@ -99,6 +107,8 @@ struct Fixpoint{C}
99107 rw:: C
100108end
101109
110+ instrument (x:: Fixpoint , f) = Fixpoint (instrument (x. rw, f))
111+
102112function (rw:: Fixpoint )(x)
103113 f = rw. rw
104114 y = @timer cached_repr (f) f (x)
@@ -116,6 +126,13 @@ struct Walk{ord, C, F, threaded}
116126 similarterm:: F
117127end
118128
129+ function instrument (x:: Walk{ord, C,F,threaded} , f) where {ord,C,F,threaded}
130+ irw = instrument (x. rw, f)
131+ Walk {ord, typeof(irw), typeof(x.similarterm), threaded} (irw,
132+ x. thread_cutoff,
133+ x. similarterm)
134+ end
135+
119136using . Threads
120137
121138function Postwalk (rw; threaded:: Bool = false , thread_cutoff= 100 , similarterm= similarterm)
129146struct PassThrough{C}
130147 rw:: C
131148end
149+ instrument (x:: PassThrough , f) = PassThrough (instrument (x. rw, f))
150+
132151(p:: PassThrough )(x) = (y= p. rw (x); y === nothing ? x : y)
133152
134153passthrough (x, default) = x === nothing ? default : x
@@ -170,5 +189,19 @@ function (p::Walk{ord, C, F, true})(x) where {ord, C, F}
170189 end
171190end
172191
192+ function instrument_io (x)
193+ function io_instrumenter (r)
194+ function (args... )
195+ println (" Rule: " , r)
196+ println (" Input: " , args)
197+ res = r (args... )
198+ println (" Output: " , res)
199+ res
200+ end
201+ end
202+
203+ instrument (x, io_instrumenter)
204+ end
205+
173206end # end module
174207
0 commit comments