1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math sorting words parser io summary
4 quotations sequences prettyprint continuations effects
5 definitions compiler.units namespaces assocs tools.walker
6 tools.time generic inspector fry tools.continuations
7 locals generalizations macros ;
12 GENERIC: (reset) ( word -- )
15 subwords [ (reset) ] each ;
18 dup "unannotated-def" word-prop [
19 dup dup "unannotated-def" word-prop define
20 f "unannotated-def" set-word-prop
26 [ (reset) ] with-compilation-unit ;
28 ERROR: cannot-annotate-twice word ;
30 M: cannot-annotate-twice summary drop "Cannot annotate a word twice" ;
34 : check-annotate-twice ( word -- word )
35 dup "unannotated-def" word-prop [
39 GENERIC# (annotate) 1 ( word quot -- )
42 [ "methods" word-prop values ] dip '[ _ (annotate) ] each ;
45 [ check-annotate-twice ] dip
46 [ dup def>> 2dup "unannotated-def" set-word-prop ] dip
47 call( old -- new ) define ;
51 : annotate ( word quot -- )
52 [ (annotate) ] with-compilation-unit ;
56 :: trace-quot ( word effect quot str -- quot' )
57 effect quot call :> values
60 "--- " write str write bl word .
61 n ndup n narray values swap zip simple-table.
65 MACRO: entering ( word -- quot )
66 dup stack-effect [ in>> ] "Entering" trace-quot ;
68 MACRO: leaving ( word -- quot )
69 dup stack-effect [ out>> ] "Leaving" trace-quot ;
71 : (watch) ( word def -- def )
72 over '[ _ entering @ _ leaving ] ;
77 dup '[ [ _ ] dip (watch) ] annotate ;
81 : (watch-vars) ( word vars quot -- newquot )
83 "--- Entering: " write _ .
84 "--- Variable values:" print _ [ dup get ] H{ } map>assoc describe
90 : watch-vars ( word vars -- )
91 dupd '[ [ _ _ ] dip (watch-vars) ] annotate ;
93 : breakpoint ( word -- )
94 [ add-breakpoint ] annotate ;
96 : breakpoint-if ( word quot -- )
97 '[ [ _ [ [ break ] when ] ] dip 3append ] annotate ;
101 word-timing [ H{ } clone ] initialize
103 : reset-word-timing ( -- )
104 word-timing get clear-assoc ;
108 : (add-timing) ( def word -- def' )
109 '[ _ benchmark _ word-timing get at+ ] ;
113 : add-timing ( word -- )
114 dup '[ _ (add-timing) ] annotate ;
116 : word-timing. ( -- )
118 >alist [ 1000000 /f ] assoc-map sort-values