Skip to content

Instantly share code, notes, and snippets.

@stassats
Created December 15, 2010 08:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stassats/741780 to your computer and use it in GitHub Desktop.
Save stassats/741780 to your computer and use it in GitHub Desktop.
lisp file analyzer
;;; Usage (percentage (analyze-systems '(a b c d))) => 42.23
(eval-when (:compile-toplevel :load-toplevel :execute)
(when (asdf:find-system :named-readtables nil)
(asdf:load-system :named-readtables)))
(defun analyze-form (form)
(typecase form
(symbol
(when (eql (symbol-package form) (find-package 'cl))
(list form)))
(atom nil)
(cons
(append (analyze-form (car form))
(when (cdr form)
(analyze-form (cdr form)))))))
(defun handle-package-and-readtable (form)
(when (consp form)
(case (car form)
(in-package
(let ((package (find-package (cadr form))))
(when package
(setf *package* package))) )
#+named-readtables
(named-readtables:in-readtable
(setf *readtable*
(named-readtables:ensure-readtable (cadr form)))))))
(defun analyze-file (file)
(when (equal (pathname-type file) "lisp")
(let ((*package* *package*)
(*readtable* *readtable*))
(with-open-file (stream file)
(remove-duplicates
(loop for form = (read stream nil)
while form
do (handle-package-and-readtable form)
append (remove-duplicates (analyze-form form))))))))
(defun asdf-module-files (module)
(mapcan (lambda (component)
(typecase component
(asdf:source-file
(list (asdf:component-pathname component)))
(asdf:module
(asdf-module-files component))))
(asdf:module-components module)))
(defun analyze-system (system)
(asdf:load-system system)
(remove-duplicates
(mapcan #'analyze-file (asdf-module-files (asdf:find-system system)))))
(defun analyze-systems (systems)
(remove-duplicates
(mapcan #'analyze-system systems)))
(defun percentage (symbols)
(/ (length symbols)
9.78))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment