Created
July 3, 2020 19:35
-
-
Save death/9f8cd25f4e946948a64a1f485e70f605 to your computer and use it in GitHub Desktop.
inheritance graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage #:snippets/inheritance-graph | |
(:use #:cl) | |
(:import-from #:sb-mop | |
#:class-direct-subclasses)) | |
(in-package #:snippets/inheritance-graph) | |
(defstruct digraph | |
(strict-p t) | |
(name "unnamed") | |
(contents '())) | |
(defun write-digraph (digraph &optional (stream *standard-output*)) | |
(format stream | |
"~<~;~:[~;strict ~]digraph ~A {~2I~{~:@_\"~A\"~:_ -> ~:_\"~A\"~}~%}~%~:>" | |
(list | |
(digraph-strict-p digraph) | |
(digraph-name digraph) | |
(digraph-contents digraph)))) | |
(defun edgify (superclass subclasses) | |
(let ((superclass-name (class-name superclass))) | |
(mapcan (lambda (subclass) | |
(list superclass-name (class-name subclass))) | |
subclasses))) | |
(defun traverse-hierarchy (root-class) | |
(let ((direct-subclasses (class-direct-subclasses root-class))) | |
(when direct-subclasses | |
(nconc (edgify root-class direct-subclasses) | |
(mapcan #'traverse-hierarchy direct-subclasses))))) | |
(defvar *digraph* | |
(make-digraph | |
:name "inheritance" | |
:contents (traverse-hierarchy (find-class 't)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment