factor/library/tools/describe.factor

112 lines
2.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
IN: inspector
USING: arrays generic hashtables io kernel kernel-internals
lists math prettyprint sequences strings vectors words ;
2005-10-03 20:54:05 -04:00
GENERIC: summary ( object -- string )
: sign-string ( n -- string )
0 > "a positive " "a negative " ? ;
M: integer summary
dup sign-string over 2 mod 0 = "even " "odd " ?
rot class word-name append3 ;
M: real summary
dup sign-string swap class word-name append ;
M: complex summary
"a complex number in the "
2005-10-03 21:04:07 -04:00
swap quadrant { "first" "second" "fourth" "third" } nth
2005-10-03 20:54:05 -04:00
" quadrant" append3 ;
GENERIC: sheet ( obj -- sheet )
2005-10-03 20:54:05 -04:00
M: object summary
"an instance of the " swap class word-name " class" append3 ;
M: object sheet ( obj -- sheet )
dup class "slots" word-prop
dup [ second ] map -rot
[ first slot ] map-with
2array ;
2005-10-03 20:54:05 -04:00
M: sequence summary
dup length 1 = [
drop "a sequence of 1 element"
] [
"a sequence of " swap length number>string
" elements" append3
] if ;
M: list sheet 1array ;
M: vector sheet 1array ;
M: array sheet 1array ;
2005-10-03 20:54:05 -04:00
M: hashtable summary
"a hashtable storing " swap hash-size number>string
" keys" append3 ;
M: hashtable sheet dup hash-keys swap hash-values 2array ;
2005-10-03 20:54:05 -04:00
M: word summary ( word -- )
dup word-vocabulary [
dup interned?
"a word in the " "a word orphaned from the " ?
swap word-vocabulary " vocabulary" append3
] [
drop "a uniquely generated symbol"
] if ;
2005-09-27 14:35:30 -04:00
: format-column ( list ? -- list )
2005-10-04 03:16:50 -04:00
>r [ unparse-short ] map r> [
2005-09-27 14:35:30 -04:00
[ 0 [ length max ] reduce ] keep
[ swap CHAR: \s pad-right ] map-with
] unless ;
: format-sheet ( sheet -- list )
2005-09-27 14:35:30 -04:00
#! We use an idiom to notify format-column if it is
#! formatting the last column.
dup length reverse-slice [ 0 = format-column ] 2map
flip [ " " join ] map ;
2005-09-25 21:59:22 -04:00
DEFER: describe
: sheet. ( sheet -- )
dup format-sheet swap peek
[ dup [ describe ] curry write-outliner ] 2each ;
2005-10-03 20:54:05 -04:00
: describe ( object -- ) dup summary print sheet sheet. ;
2005-09-25 21:59:22 -04:00
: word. ( word -- )
dup word-name swap dup [ see ] curry write-outliner ;
2005-10-03 19:53:32 -04:00
: simple-outliner ( seq quot -- | quot: obj -- )
2005-09-25 22:20:29 -04:00
swap [
2005-09-25 22:25:54 -04:00
[ unparse-short ] keep rot dupd curry write-outliner
2005-09-25 22:20:29 -04:00
] each-with ;
2005-09-25 22:20:29 -04:00
: words. ( vocab -- )
2005-10-03 19:53:32 -04:00
words word-sort [ see ] simple-outliner ;
2005-09-25 22:20:29 -04:00
: vocabs. ( -- )
#! Outlining word browser.
2005-09-25 22:20:29 -04:00
vocabs [ f over [ words. ] curry write-outliner ] each ;
2005-09-25 22:20:29 -04:00
: usage. ( word -- )
#! Outlining usages browser.
2005-09-27 15:14:25 -04:00
usage [ usage. ] simple-outliner ;
2005-09-25 22:20:29 -04:00
: uses. ( word -- )
#! Outlining call hierarchy browser.
2005-09-27 15:14:25 -04:00
uses [ uses. ] simple-outliner ;
2005-09-25 22:20:29 -04:00
2005-09-27 00:24:42 -04:00
: stack. ( seq -- seq )
reverse-slice >array describe ;
: .s datastack stack. ;
: .r callstack stack. ;