factor/library/syntax/prettyprint.factor

141 lines
3.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
IN: prettyprint
2005-02-06 00:21:26 -05:00
! This using kernel-internals is pretty bad. Remove the
! kernel-internals usage as soon as the tuple class is moved
! to the generic vocabulary.
USING: errors generic kernel kernel-internals lists math
namespaces stdio strings presentation unparser vectors words
hashtables ;
2004-07-16 02:26:21 -04:00
SYMBOL: prettyprint-limit
2005-02-06 00:21:26 -05:00
SYMBOL: one-line
SYMBOL: tab-size
GENERIC: prettyprint* ( indent obj -- indent )
M: object prettyprint* ( indent obj -- indent )
unparse write ;
2004-12-25 21:28:47 -05:00
: indent ( indent -- )
2004-07-16 02:26:21 -04:00
#! Print the given number of spaces.
" " fill write ;
: prettyprint-newline ( indent -- )
2004-12-25 21:28:47 -05:00
"\n" write indent ;
2004-07-16 02:26:21 -04:00
: prettyprint-element ( indent obj -- indent )
over prettyprint-limit get >= [
unparse write
] [
prettyprint*
2004-12-29 18:01:23 -05:00
] ifte " " write ;
: <prettyprint ( indent -- indent )
2005-02-06 00:21:26 -05:00
tab-size get + one-line get [
2004-12-29 18:01:23 -05:00
" " write
] [
dup prettyprint-newline
] ifte ;
: prettyprint> ( indent -- indent )
2005-02-06 00:21:26 -05:00
tab-size get - one-line get
[ dup prettyprint-newline ] unless ;
2004-11-25 21:08:09 -05:00
: word-link ( word -- link )
[
2004-12-20 15:29:55 -05:00
dup word-name unparse ,
" [ " ,
word-vocabulary unparse ,
" ] search" ,
2004-11-25 21:08:09 -05:00
] make-string ;
2004-12-20 15:29:55 -05:00
: word-actions ( search -- list )
2004-11-25 21:08:09 -05:00
[
[[ "See" "see" ]]
[[ "Push" "" ]]
[[ "Execute" "execute" ]]
[[ "jEdit" "jedit" ]]
[[ "Usages" "usages." ]]
2004-11-25 21:08:09 -05:00
] ;
: word-attrs ( word -- attrs )
#! Words without a vocabulary do not get a link or an action
#! popup.
dup word-vocabulary [
2004-12-20 15:29:55 -05:00
word-link word-actions <actions> "actions" swons unit
2004-11-25 21:08:09 -05:00
] [
drop [ ]
] ifte ;
M: word prettyprint* ( indent word -- indent )
2004-11-25 21:08:09 -05:00
dup word-name
swap dup word-attrs swap word-style append
write-attr ;
2005-02-06 00:21:26 -05:00
: prettyprint-sequence ( indent start list end -- indent )
#! Prettyprint a list, with start/end delimiters; eg, [ ],
#! or { }, or << >>. The body of the list is indented,
#! unless the list is empty.
over [
>r
>r prettyprint* <prettyprint
r> [ prettyprint-element ] each
prettyprint> r> prettyprint*
] [
>r >r prettyprint* " " write r> drop r> prettyprint*
] ifte ;
M: list prettyprint* ( indent list -- indent )
2005-02-06 00:21:26 -05:00
\ [ swap \ ] prettyprint-sequence ;
2004-07-16 02:26:21 -04:00
M: cons prettyprint* ( indent cons -- indent )
2005-02-06 00:21:26 -05:00
#! Here we turn the cons into a list of two elements.
\ [[ swap uncons 2list \ ]] prettyprint-sequence ;
2004-07-19 16:10:18 -04:00
M: vector prettyprint* ( indent vector -- indent )
2005-02-06 00:21:26 -05:00
\ { swap vector>list \ } prettyprint-sequence ;
2004-11-25 20:37:05 -05:00
M: hashtable prettyprint* ( indent hashtable -- indent )
2005-02-06 00:21:26 -05:00
\ {{ swap hash>alist \ }} prettyprint-sequence ;
2004-11-25 20:37:05 -05:00
M: tuple prettyprint* ( indent tuple -- indent )
2005-02-06 00:21:26 -05:00
\ << swap tuple>list \ >> prettyprint-sequence ;
: prettyprint-1 ( obj -- )
0 swap prettyprint* drop ;
2004-07-16 02:26:21 -04:00
: prettyprint ( obj -- )
prettyprint-1 terpri ;
2004-07-16 02:26:21 -04:00
2004-08-17 23:09:16 -04:00
: vocab-link ( vocab -- link )
"vocabularies'" swap cat2 ;
2004-08-17 23:09:16 -04:00
: . ( obj -- )
[
2005-02-06 00:21:26 -05:00
one-line on
16 prettyprint-limit set
prettyprint
] with-scope ;
: [.] ( list -- )
#! Unparse each element on its own line.
[ . ] each ;
2004-10-27 21:21:31 -04:00
: {.} ( vector -- )
#! Unparse each element on its own line.
2005-01-23 16:47:28 -05:00
vector>list reverse [ . ] each ;
2004-10-27 21:21:31 -04:00
: .s datastack {.} ;
: .r callstack {.} ;
2004-11-25 23:14:17 -05:00
: .n namestack [.] ;
: .c catchstack [.] ;
! For integers only
: .b >bin print ;
: .o >oct print ;
: .h >hex print ;
2005-02-06 00:21:26 -05:00
global [ 40 prettyprint-limit set 4 tab-size set ] bind