Skip to content

Instantly share code, notes, and snippets.

@death
Created July 3, 2020 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save death/9f8cd25f4e946948a64a1f485e70f605 to your computer and use it in GitHub Desktop.
Save death/9f8cd25f4e946948a64a1f485e70f605 to your computer and use it in GitHub Desktop.
inheritance graph
(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