2004-07-22 19:48:50 -04:00
|
|
|
! :folding=indent:collapseFolds=1:
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
! $Id$
|
|
|
|
!
|
|
|
|
! Copyright (C) 2003, 2004 Slava Pestov.
|
|
|
|
!
|
|
|
|
! Redistribution and use in source and binary forms, with or without
|
|
|
|
! modification, are permitted provided that the following conditions are met:
|
|
|
|
!
|
|
|
|
! 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer.
|
|
|
|
!
|
|
|
|
! 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
! this list of conditions and the following disclaimer in the documentation
|
|
|
|
! and/or other materials provided with the distribution.
|
|
|
|
!
|
|
|
|
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
IN: prettyprint
|
|
|
|
USE: combinators
|
2004-07-19 16:10:18 -04:00
|
|
|
USE: errors
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: format
|
|
|
|
USE: kernel
|
|
|
|
USE: logic
|
|
|
|
USE: lists
|
2004-08-26 22:21:17 -04:00
|
|
|
USE: math
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: namespaces
|
|
|
|
USE: stack
|
|
|
|
USE: stdio
|
|
|
|
USE: strings
|
2004-10-30 23:18:55 -04:00
|
|
|
USE: presentation
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: unparser
|
2004-07-19 16:10:18 -04:00
|
|
|
USE: vectors
|
2004-07-16 02:26:21 -04:00
|
|
|
USE: words
|
2004-11-25 20:37:05 -05:00
|
|
|
USE: hashtables
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: tab-size
|
|
|
|
#! Change this to suit your tastes.
|
|
|
|
4 ;
|
|
|
|
|
2004-07-23 01:21:47 -04:00
|
|
|
: prettyprint-limit ( -- limit )
|
|
|
|
#! Avoid infinite loops -- maximum indent, 10 levels.
|
|
|
|
"prettyprint-limit" get [ 40 ] unless* ;
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: prettyprint-indent ( indent -- )
|
|
|
|
#! Print the given number of spaces.
|
|
|
|
" " fill write ;
|
|
|
|
|
|
|
|
: prettyprint-newline ( indent -- )
|
|
|
|
"\n" write prettyprint-indent ;
|
|
|
|
|
|
|
|
: prettyprint-space ( -- )
|
|
|
|
" " write ;
|
|
|
|
|
|
|
|
! Real definition follows
|
|
|
|
DEFER: prettyprint*
|
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: prettyprint-element ( indent obj -- indent )
|
2004-11-23 22:20:23 -05:00
|
|
|
prettyprint* prettyprint-space ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
|
|
|
: <prettyprint ( indent -- indent )
|
|
|
|
tab-size +
|
|
|
|
"prettyprint-single-line" get [
|
|
|
|
prettyprint-space
|
|
|
|
] [
|
|
|
|
dup prettyprint-newline
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: prettyprint> ( indent -- indent )
|
|
|
|
tab-size -
|
|
|
|
"prettyprint-single-line" get [
|
|
|
|
dup prettyprint-newline
|
|
|
|
] unless ;
|
|
|
|
|
|
|
|
: prettyprint-[ ( indent -- indent )
|
2004-11-25 20:37:05 -05:00
|
|
|
\ [ prettyprint-word <prettyprint ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
|
|
|
: prettyprint-] ( indent -- indent )
|
2004-11-25 20:37:05 -05:00
|
|
|
prettyprint> \ ] prettyprint-word ;
|
2004-07-21 19:26:41 -04:00
|
|
|
|
2004-11-11 16:45:55 -05:00
|
|
|
: prettyprint-list ( indent list -- indent )
|
|
|
|
#! Pretty-print a list, without [ and ].
|
2004-07-27 22:52:35 -04:00
|
|
|
[
|
|
|
|
uncons >r prettyprint-element r>
|
|
|
|
dup cons? [
|
2004-11-11 16:45:55 -05:00
|
|
|
prettyprint-list
|
2004-07-27 22:52:35 -04:00
|
|
|
] [
|
|
|
|
[
|
|
|
|
"|" write prettyprint-space prettyprint-element
|
|
|
|
] when*
|
|
|
|
] ifte
|
|
|
|
] when* ;
|
2004-07-23 01:21:47 -04:00
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: prettyprint-[] ( indent list -- indent )
|
|
|
|
swap prettyprint-[ swap prettyprint-list prettyprint-] ;
|
|
|
|
|
2004-07-19 16:10:18 -04:00
|
|
|
: prettyprint-{ ( indent -- indent )
|
2004-11-25 20:37:05 -05:00
|
|
|
\ { prettyprint-word <prettyprint ;
|
2004-07-19 16:10:18 -04:00
|
|
|
|
|
|
|
: prettyprint-} ( indent -- indent )
|
2004-11-25 20:37:05 -05:00
|
|
|
prettyprint> \ } prettyprint-word ;
|
2004-07-19 16:10:18 -04:00
|
|
|
|
|
|
|
: prettyprint-vector ( indent list -- indent )
|
|
|
|
#! Pretty-print a vector, without { and }.
|
2004-11-11 16:45:55 -05:00
|
|
|
[ prettyprint-element ] vector-each ;
|
2004-07-19 16:10:18 -04:00
|
|
|
|
2004-08-22 23:02:29 -04:00
|
|
|
: prettyprint-{} ( indent vector -- indent )
|
|
|
|
dup vector-length 0 = [
|
2004-11-25 20:37:05 -05:00
|
|
|
drop prettyprint-{ prettyprint-}
|
2004-08-22 23:02:29 -04:00
|
|
|
] [
|
|
|
|
swap prettyprint-{ swap prettyprint-vector prettyprint-}
|
|
|
|
] ifte ;
|
2004-07-19 16:10:18 -04:00
|
|
|
|
2004-11-25 20:37:05 -05:00
|
|
|
: prettyprint-{{ ( indent -- indent )
|
|
|
|
\ {{ prettyprint-word <prettyprint ;
|
|
|
|
|
|
|
|
: prettyprint-}} ( indent -- indent )
|
|
|
|
prettyprint> \ }} prettyprint-word ;
|
|
|
|
|
|
|
|
: prettyprint-{{}} ( indent hashtable -- indent )
|
|
|
|
hash>alist dup length 0 = [
|
|
|
|
drop prettyprint-{{ prettyprint-}}
|
|
|
|
] [
|
|
|
|
swap prettyprint-{{ swap prettyprint-list prettyprint-}}
|
|
|
|
] ifte ;
|
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: trim-newline ( str -- str )
|
|
|
|
dup ends-with-newline? dup [ nip ] [ drop ] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: prettyprint-comment ( comment -- )
|
2004-10-30 23:18:55 -04:00
|
|
|
trim-newline "comments" style write-attr ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: word-link ( word -- link )
|
2004-11-11 15:15:43 -05:00
|
|
|
[
|
|
|
|
"vocabularies'" ,
|
|
|
|
dup word-vocabulary ,
|
|
|
|
"'" ,
|
|
|
|
word-name ,
|
|
|
|
] make-string ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-10-30 23:18:55 -04:00
|
|
|
: word-actions ( -- list )
|
|
|
|
[
|
|
|
|
[ "Describe" | "describe-path" ]
|
|
|
|
[ "Push" | "lookup" ]
|
|
|
|
[ "Execute" | "lookup execute" ]
|
|
|
|
[ "jEdit" | "lookup jedit" ]
|
|
|
|
[ "Usages" | "lookup usages." ]
|
|
|
|
] ;
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: word-attrs ( word -- attrs )
|
2004-11-09 22:19:43 -05:00
|
|
|
#! Words without a vocabulary do not get a link or an action
|
|
|
|
#! popup.
|
|
|
|
dup word-vocabulary [
|
|
|
|
word-link [ "object-link" swons ] keep
|
2004-10-30 23:18:55 -04:00
|
|
|
word-actions <actions> "actions" swons
|
|
|
|
t "underline" swons
|
|
|
|
3list
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
2004-11-09 22:19:43 -05:00
|
|
|
drop [ ]
|
|
|
|
] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: prettyprint-word ( word -- )
|
2004-11-09 22:19:43 -05:00
|
|
|
dup word-name
|
|
|
|
swap dup word-attrs swap word-style append
|
|
|
|
write-attr ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: prettyprint-object ( indent obj -- indent )
|
2004-07-21 19:26:41 -04:00
|
|
|
unparse write ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: prettyprint* ( indent obj -- indent )
|
2004-11-11 16:45:55 -05:00
|
|
|
over prettyprint-limit >= [
|
|
|
|
unparse write
|
|
|
|
] [
|
|
|
|
[
|
2004-11-25 20:37:05 -05:00
|
|
|
[ f = ] [ prettyprint-object ]
|
|
|
|
[ cons? ] [ prettyprint-[] ]
|
|
|
|
[ hashtable? ] [ prettyprint-{{}} ]
|
|
|
|
[ vector? ] [ prettyprint-{} ]
|
|
|
|
[ word? ] [ prettyprint-word ]
|
|
|
|
[ drop t ] [ prettyprint-object ]
|
2004-11-11 16:45:55 -05:00
|
|
|
] cond
|
|
|
|
] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-07-21 19:26:41 -04:00
|
|
|
: prettyprint ( obj -- )
|
|
|
|
0 swap prettyprint* drop terpri ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-17 23:09:16 -04:00
|
|
|
: vocab-link ( vocab -- link )
|
2004-11-11 15:15:43 -05:00
|
|
|
"vocabularies'" swap cat2 ;
|
2004-08-17 23:09:16 -04:00
|
|
|
|
|
|
|
: vocab-attrs ( word -- attrs )
|
2004-08-30 20:24:19 -04:00
|
|
|
vocab-link "object-link" default-style acons ;
|
2004-08-17 23:09:16 -04:00
|
|
|
|
|
|
|
: prettyprint-vocab ( vocab -- )
|
2004-08-21 02:55:37 -04:00
|
|
|
dup vocab-attrs write-attr ;
|
2004-08-17 23:09:16 -04:00
|
|
|
|
2004-08-29 01:50:34 -04:00
|
|
|
: prettyprint-IN: ( indent word -- )
|
2004-11-11 16:45:55 -05:00
|
|
|
\ IN: prettyprint-word prettyprint-space
|
2004-08-29 01:50:34 -04:00
|
|
|
word-vocabulary prettyprint-vocab prettyprint-newline ;
|
2004-08-17 23:09:16 -04:00
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: prettyprint-: ( indent -- indent )
|
2004-11-11 16:45:55 -05:00
|
|
|
\ : prettyprint-word prettyprint-space
|
2004-07-16 02:26:21 -04:00
|
|
|
tab-size + ;
|
|
|
|
|
|
|
|
: prettyprint-; ( indent -- indent )
|
2004-11-11 16:45:55 -05:00
|
|
|
\ ; prettyprint-word
|
2004-07-16 02:26:21 -04:00
|
|
|
tab-size - ;
|
|
|
|
|
2004-08-17 23:09:16 -04:00
|
|
|
: prettyprint-plist ( word -- )
|
2004-09-28 00:24:36 -04:00
|
|
|
dup "parsing" word-property [ " parsing" write ] when
|
|
|
|
"inline" word-property [ " inline" write ] when ;
|
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
|
|
|
[
|
2004-07-23 01:21:47 -04:00
|
|
|
"prettyprint-single-line" on
|
2004-07-23 01:27:54 -04:00
|
|
|
tab-size 4 * "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
|
|
|
|
|
|
|
: [.] ( 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.
|
2004-11-20 16:57:01 -05:00
|
|
|
stack>list [ . ] each ;
|
2004-10-27 21:21:31 -04:00
|
|
|
|
2004-11-20 16:57:01 -05:00
|
|
|
: .n namestack [.] ;
|
2004-10-27 21:21:31 -04:00
|
|
|
: .s datastack {.} ;
|
|
|
|
: .r callstack {.} ;
|
|
|
|
: .c catchstack {.} ;
|
2004-09-14 23:23:05 -04:00
|
|
|
|
|
|
|
! For integers only
|
|
|
|
: .b >bin print ;
|
|
|
|
: .o >oct print ;
|
|
|
|
: .h >hex print ;
|
2004-11-23 22:20:23 -05:00
|
|
|
|
|
|
|
: stack-effect. ( word -- )
|
|
|
|
stack-effect [
|
|
|
|
" " write
|
|
|
|
[ CHAR: ( , , CHAR: ) , ] make-string prettyprint-comment
|
|
|
|
] when* ;
|
|
|
|
|
|
|
|
: documentation. ( indent word -- indent )
|
|
|
|
documentation [
|
|
|
|
"\n" split [
|
|
|
|
"#!" swap cat2 prettyprint-comment
|
|
|
|
dup prettyprint-newline
|
|
|
|
] each
|
|
|
|
] when* ;
|
|
|
|
|
|
|
|
: prettyprint-docs ( indent word -- indent )
|
|
|
|
[
|
|
|
|
stack-effect. dup prettyprint-newline
|
|
|
|
] keep documentation. ;
|
|
|
|
|
|
|
|
: see-compound ( word -- )
|
|
|
|
0 swap
|
|
|
|
[ dupd prettyprint-IN: prettyprint-: ] keep
|
|
|
|
[ prettyprint-word ] keep
|
|
|
|
[ prettyprint-docs ] keep
|
|
|
|
[ word-parameter prettyprint-list prettyprint-; ] keep
|
|
|
|
prettyprint-plist prettyprint-newline ;
|
|
|
|
|
|
|
|
: see-primitive ( word -- )
|
|
|
|
"PRIMITIVE: " write dup unparse write stack-effect. terpri ;
|
|
|
|
|
|
|
|
: see-symbol ( word -- )
|
|
|
|
"SYMBOL: " write . ;
|
|
|
|
|
|
|
|
: see-undefined ( word -- )
|
|
|
|
drop "Not defined" print ;
|
|
|
|
|
|
|
|
: see ( name -- )
|
|
|
|
#! Show a word definition.
|
|
|
|
[
|
|
|
|
[ compound? ] [ see-compound ]
|
|
|
|
[ symbol? ] [ see-symbol ]
|
|
|
|
[ primitive? ] [ see-primitive ]
|
|
|
|
[ word? ] [ see-undefined ]
|
|
|
|
[ drop t ] [ "Not a word: " write . ]
|
|
|
|
] cond ;
|