factor/basis/tools/trace/trace.factor

82 lines
2.1 KiB
Factor
Raw Normal View History

2009-04-08 07:23:07 -04:00
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: concurrency.promises models tools.continuations kernel
2009-04-15 23:16:52 -04:00
sequences concurrency.messaging locals continuations threads
namespaces namespaces.private make assocs accessors io strings
prettyprint math math.parser words effects summary io.styles classes
generic.math combinators.short-circuit ;
2009-04-08 07:23:07 -04:00
IN: tools.trace
: callstack-depth ( callstack -- n )
2009-04-15 23:16:52 -04:00
callstack>array length 2/ ;
2009-04-08 07:23:07 -04:00
SYMBOL: end
SYMBOL: exclude-vocabs
SYMBOL: include-vocabs
2009-04-15 23:16:52 -04:00
exclude-vocabs { "math" "accessors" } swap set-global
2009-04-08 07:23:07 -04:00
: include? ( vocab -- ? )
include-vocabs get dup [ member? ] [ 2drop t ] if ;
: exclude? ( vocab -- ? )
exclude-vocabs get dup [ member? ] [ 2drop f ] if ;
: into? ( obj -- ? )
2009-04-15 23:16:52 -04:00
{
[ word? ]
[ predicate? not ]
[ math-generic? not ]
[
{
[ inline? ]
[
{
[ vocabulary>> include? ]
[ vocabulary>> exclude? not ]
} 1&&
]
} 1||
]
} 1&& ;
2009-04-08 07:23:07 -04:00
TUPLE: trace-step word inputs ;
M: trace-step summary
[
[ "Word: " % word>> name>> % ]
[ " -- inputs: " % inputs>> unparse-short % ] bi
] "" make ;
: <trace-step> ( continuation word -- trace-step )
[ nip ] [ [ data>> ] [ stack-effect in>> length ] bi* short tail* ] 2bi
\ trace-step boa ;
: print-step ( continuation -- )
dup continuation-current dup word? [
[ nip name>> ] [ <trace-step> ] 2bi write-object nl
] [
nip short.
] if ;
2009-04-15 23:16:52 -04:00
: print-depth ( continuation -- )
call>> callstack-depth
[ CHAR: \s <string> write ]
[ number>string write ": " write ] bi ;
2009-04-08 07:23:07 -04:00
: trace-step ( continuation -- continuation' )
dup continuation-current end eq? [
2009-04-15 23:16:52 -04:00
[ print-depth ]
2009-04-08 07:23:07 -04:00
[ print-step ]
[
dup continuation-current into?
[ continuation-step-into ] [ continuation-step ] if
2009-04-15 23:16:52 -04:00
] tri
2009-04-08 07:23:07 -04:00
] unless ;
: trace ( quot -- data )
[ [ trace-step ] break-hook ] dip
[ break ] [ end drop ] surround
with-variable ;