0

I am looking for program that can generate for me a statistics of the lines of code per functions in a lisp program. In Lisp this means for each function or macro to count how many functions are recursively included in the top level function.

Any pointer would be much appreciated.

sds
  • 58,617
  • 29
  • 161
  • 278
user2193970
  • 345
  • 2
  • 10
  • There was a static analyzer called Xref. Maybe it is even included in SLIME... See: https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/lisp/code/tools/xref/0.html Don't know if there is a newer version of it. – Rainer Joswig Jan 16 '18 at 08:57

1 Answers1

6

for each function or macro to count how many functions are recursively included in the top level function

I am not sure what this means.

If you want to count the number of function calls in the code, you would need a full fledged code walker.

However, for the simple meaning of the number of top-level forms in the file, the problem is quite tractable. I am not aware of an existing program that does that, but it does not sound hard to roll it:

(defun read-file-as-string (file-name)
  (with-open-file (in file-name :external-format charset:iso-8859-1)
    (let ((ret (make-string (1- (file-length in)))))
      (read-sequence ret in)
      ret)))

:external-format may not be necessary. See

.

(defun count-lines (string)
  (count #\Newline string))

See count.

(defun count-forms (string)
  (with-input-from-string (in string)
    (loop with *read-suppress* = t
      until (eq in (read in nil in))
      count t)))

See

.

(defun file-code-stats (file-name)
  (let* ((file-text (read-file-as-string file-name))
         (lines (count-lines file-text))
         (forms (count-forms file-text)))
    (format t "File ~S contains ~:D characters, ~:D lines, ~:D forms (~,2F lines per form)"
            file-name (length file-text) lines forms (/ lines forms))
    (values (length file-text) lines forms)))



(file-code-stats "~/lisp/scratch.lisp")
File "~/lisp/scratch.lisp" contains 133,221 characters, 3,728 lines, 654 forms (5.70 lines per form)
133221 ;
3728 ;
654
sds
  • 58,617
  • 29
  • 161
  • 278