Paste: A word to print a class hierarchy
Author: | Jean-Marc Lugrin |
Mode: | factor |
Date: | Mon, 6 Nov 2023 10:06:22 |
Plain Text |
USING: classes hashtables ui.gadgets assocs kernel sequences prettyprint vectors
math io formatting strings sorting accessors io.styles ;
IN: introspection.gadget-classes
<PRIVATE
: add-child ( c h -- )
over
superclass-of
swap at*
[ swap suffix! drop ]
[ 2drop ]
if
;
: print-class-name ( c -- )
dup name>> swap write-object
;
: print-class ( c i -- )
2 * CHAR: . <string> write
print-class-name
nl
;
: print-superclasses ( c -- )
superclass-of dup
[ " < " write
[ print-class-name ]
[ print-superclasses ] bi
]
[ drop ]
if
;
: print-root-class ( c -- )
[ print-class-name ]
[ print-superclasses ] bi
nl
;
:: print-children ( h c i -- )
i 0 = [ c print-root-class ] [ c i print-class ] if
c h at
[ h swap i 1 + print-children ] each
;
PRIVATE>
: print-hierarchy ( aclass -- )
classes [ drop V{ } clone ] zip-with >hashtable
classes [ over add-child ] each
[ sort ] assoc-map
swap 0 print-children
;
New Annotation