2005-01-14 12:01:48 -05:00
|
|
|
! Copyright (C) 2003, 2005 Slava Pestov.
|
2005-02-05 22:51:41 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: prettyprint
|
2005-07-06 03:29:42 -04:00
|
|
|
USING: alien errors generic hashtables io kernel lists math
|
|
|
|
matrices memory namespaces parser presentation sequences strings
|
|
|
|
styles unparser vectors words ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-12-29 03:35:46 -05:00
|
|
|
SYMBOL: prettyprint-limit
|
2005-02-06 00:21:26 -05:00
|
|
|
SYMBOL: one-line
|
|
|
|
SYMBOL: tab-size
|
2005-02-09 20:57:19 -05:00
|
|
|
SYMBOL: recursion-check
|
2004-12-29 03:35:46 -05:00
|
|
|
|
2004-12-12 23:49:44 -05:00
|
|
|
GENERIC: prettyprint* ( indent obj -- indent )
|
|
|
|
|
|
|
|
M: object prettyprint* ( indent obj -- indent )
|
2005-07-11 22:47:38 -04:00
|
|
|
dup unparse swap presented swons unit write-attr ;
|
2004-12-12 23:49:44 -05:00
|
|
|
|
2005-07-06 03:29:42 -04:00
|
|
|
: word-attrs ( word -- style )
|
2005-03-18 19:38:27 -05:00
|
|
|
#! Return the style values for the HTML word browser
|
2005-07-06 03:29:42 -04:00
|
|
|
[
|
|
|
|
presented over cons ,
|
|
|
|
dup word-vocabulary [
|
|
|
|
"word" over word-name cons ,
|
|
|
|
"vocab" swap word-vocabulary cons ,
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte
|
|
|
|
] make-list ;
|
2004-11-25 21:08:09 -05:00
|
|
|
|
2005-03-26 20:12:14 -05:00
|
|
|
: word. ( word -- ) dup word-name swap word-attrs write-attr ;
|
2004-11-25 21:08:09 -05:00
|
|
|
|
2005-02-09 20:57:19 -05:00
|
|
|
M: word prettyprint* ( indent word -- indent )
|
2005-05-02 00:18:34 -04:00
|
|
|
dup parsing? [ \ POSTPONE: word. bl ] when word. ;
|
2005-02-09 20:57:19 -05:00
|
|
|
|
|
|
|
: indent ( indent -- )
|
|
|
|
#! Print the given number of spaces.
|
2005-04-19 20:28:01 -04:00
|
|
|
CHAR: \s fill write ;
|
2005-02-09 20:57:19 -05:00
|
|
|
|
|
|
|
: prettyprint-newline ( indent -- )
|
|
|
|
"\n" write indent ;
|
|
|
|
|
2005-05-02 00:18:34 -04:00
|
|
|
: \? ( list -- ? )
|
|
|
|
#! Is the head of the list a [ foo ] car?
|
|
|
|
dup car dup cons? [
|
2005-05-18 16:26:22 -04:00
|
|
|
dup car word? [
|
2005-05-28 20:52:23 -04:00
|
|
|
cdr [ drop f ] [ second \ car = ] ifte
|
2005-05-18 16:26:22 -04:00
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte
|
2005-05-02 00:18:34 -04:00
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] ifte ;
|
|
|
|
|
2005-02-09 20:57:19 -05:00
|
|
|
: prettyprint-elements ( indent list -- indent )
|
2005-05-02 00:18:34 -04:00
|
|
|
[
|
|
|
|
dup \? [
|
|
|
|
\ \ word. bl
|
2005-05-18 16:26:22 -04:00
|
|
|
uncons >r car word. bl
|
2005-05-02 00:18:34 -04:00
|
|
|
r> cdr prettyprint-elements
|
|
|
|
] [
|
|
|
|
uncons >r prettyprint* bl
|
|
|
|
r> prettyprint-elements
|
|
|
|
] ifte
|
|
|
|
] when* ;
|
2005-02-09 20:57:19 -05:00
|
|
|
|
2005-04-13 20:44:06 -04:00
|
|
|
: ?prettyprint-newline ( indent -- )
|
|
|
|
one-line get [
|
2005-05-02 00:18:34 -04:00
|
|
|
bl drop
|
2005-02-09 20:57:19 -05:00
|
|
|
] [
|
2005-04-13 20:44:06 -04:00
|
|
|
prettyprint-newline
|
2005-02-09 20:57:19 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2005-04-13 20:44:06 -04:00
|
|
|
: <prettyprint ( indent -- indent )
|
|
|
|
tab-size get + dup ?prettyprint-newline ;
|
|
|
|
|
2005-02-09 20:57:19 -05:00
|
|
|
: prettyprint> ( indent -- indent )
|
|
|
|
tab-size get - one-line get
|
|
|
|
[ dup prettyprint-newline ] unless ;
|
|
|
|
|
|
|
|
: prettyprint-limit? ( indent -- ? )
|
|
|
|
prettyprint-limit get dup [ >= ] [ nip ] ifte ;
|
|
|
|
|
|
|
|
: check-recursion ( indent obj quot -- ? indent )
|
|
|
|
#! We detect circular structure.
|
|
|
|
pick prettyprint-limit? >r
|
|
|
|
over recursion-check get memq? r> or [
|
|
|
|
2drop "..." write
|
|
|
|
] [
|
|
|
|
over recursion-check [ cons ] change
|
|
|
|
call
|
|
|
|
recursion-check [ cdr ] change
|
|
|
|
] ifte ;
|
|
|
|
|
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 [
|
2005-03-26 20:12:14 -05:00
|
|
|
>r >r word. <prettyprint
|
2005-02-09 20:57:19 -05:00
|
|
|
r> prettyprint-elements
|
2005-03-26 20:12:14 -05:00
|
|
|
prettyprint> r> word.
|
2005-02-06 00:21:26 -05:00
|
|
|
] [
|
2005-05-02 00:18:34 -04:00
|
|
|
>r >r word. bl r> drop r> word.
|
2005-02-06 00:21:26 -05:00
|
|
|
] ifte ;
|
2005-01-13 19:49:47 -05:00
|
|
|
|
|
|
|
M: list prettyprint* ( indent list -- indent )
|
2005-02-10 23:58:28 -05:00
|
|
|
[
|
|
|
|
\ [ swap \ ] prettyprint-sequence
|
|
|
|
] check-recursion ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-13 19:49:47 -05: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.
|
2005-02-09 20:57:19 -05:00
|
|
|
[
|
|
|
|
\ [[ swap uncons 2list \ ]] prettyprint-sequence
|
|
|
|
] check-recursion ;
|
2004-07-19 16:10:18 -04:00
|
|
|
|
2004-12-12 23:49:44 -05:00
|
|
|
M: vector prettyprint* ( indent vector -- indent )
|
2005-02-09 20:57:19 -05:00
|
|
|
[
|
2005-04-02 02:39:33 -05:00
|
|
|
\ { swap >list \ } prettyprint-sequence
|
2005-02-09 20:57:19 -05:00
|
|
|
] check-recursion ;
|
2004-11-25 20:37:05 -05:00
|
|
|
|
2004-12-12 23:49:44 -05:00
|
|
|
M: hashtable prettyprint* ( indent hashtable -- indent )
|
2005-02-09 20:57:19 -05:00
|
|
|
[
|
|
|
|
\ {{ swap hash>alist \ }} prettyprint-sequence
|
|
|
|
] check-recursion ;
|
2004-11-25 20:37:05 -05:00
|
|
|
|
2005-02-05 22:51:41 -05:00
|
|
|
M: tuple prettyprint* ( indent tuple -- indent )
|
2005-02-09 20:57:19 -05:00
|
|
|
[
|
2005-04-12 13:35:27 -04:00
|
|
|
\ << swap tuple>list \ >> prettyprint-sequence
|
2005-02-09 20:57:19 -05:00
|
|
|
] check-recursion ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-04-10 18:58:30 -04:00
|
|
|
M: alien prettyprint* ( alien -- str )
|
2005-05-02 00:18:34 -04:00
|
|
|
\ ALIEN: word. bl alien-address unparse write ;
|
2005-04-10 18:58:30 -04:00
|
|
|
|
2005-05-03 04:40:13 -04:00
|
|
|
: matrix-rows. ( indent list -- indent )
|
|
|
|
uncons >r [ one-line on prettyprint* ] with-scope r>
|
|
|
|
[ over ?prettyprint-newline matrix-rows. ] when* ;
|
|
|
|
|
|
|
|
M: matrix prettyprint* ( indent obj -- indent )
|
2005-05-19 15:16:25 -04:00
|
|
|
\ M[ word. bl >r 3 + r>
|
2005-05-03 04:40:13 -04:00
|
|
|
row-list matrix-rows.
|
2005-05-19 15:16:25 -04:00
|
|
|
bl \ ]M word. 3 - ;
|
2005-05-03 04:40:13 -04:00
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: prettyprint ( obj -- )
|
2005-02-09 20:57:19 -05:00
|
|
|
[
|
|
|
|
recursion-check off
|
|
|
|
0 swap prettyprint* drop terpri
|
|
|
|
] with-scope ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-17 23:09:16 -04:00
|
|
|
: vocab-link ( vocab -- link )
|
2005-05-18 16:26:22 -04:00
|
|
|
"vocabularies'" swap append ;
|
2004-08-17 23:09:16 -04:00
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: . ( obj -- )
|
2004-08-08 21:24:01 -04:00
|
|
|
[
|
2005-02-06 00:21:26 -05:00
|
|
|
one-line on
|
2004-12-29 03:35:46 -05:00
|
|
|
16 prettyprint-limit set
|
2004-07-23 01:21:47 -04:00
|
|
|
prettyprint
|
2004-08-08 21:24:01 -04:00
|
|
|
] with-scope ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
2005-05-02 00:18:34 -04:00
|
|
|
: [.] ( sequence -- )
|
2004-10-27 21:21:31 -04:00
|
|
|
#! Unparse each element on its own line.
|
2005-05-14 17:18:45 -04:00
|
|
|
[ . ] each ;
|
2004-10-27 21:21:31 -04:00
|
|
|
|
2005-05-03 23:50:04 -04:00
|
|
|
: .s datastack reverse [.] flush ;
|
|
|
|
: .r callstack reverse [.] flush ;
|
|
|
|
: .n namestack [.] flush ;
|
|
|
|
: .c catchstack [.] flush ;
|
2004-09-14 23:23:05 -04:00
|
|
|
|
|
|
|
! For integers only
|
|
|
|
: .b >bin print ;
|
|
|
|
: .o >oct print ;
|
|
|
|
: .h >hex print ;
|
2004-12-29 03:35:46 -05:00
|
|
|
|
2005-02-09 20:57:19 -05:00
|
|
|
global [ 4 tab-size set ] bind
|