寻找一个可以帮助我为每个函数统计信息生成 lisp 程序代码行的程序



我正在寻找可以为我生成Lisp程序中每个函数的代码行数统计信息的程序。在 Lisp 中,这意味着每个函数或宏都要计算在顶级函数中递归包含多少函数。

任何指针将不胜感激。

对于每个函数或宏,以计算顶级函数中递归包含的函数数

我不确定这意味着什么。

如果要计算代码中的函数调用次数,则需要一个完整的代码步行者。

但是,对于文件中顶级表单数量的简单含义,该问题是相当容易处理的。我不知道现有的程序可以做到这一点,但听起来并不难:

(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可能不是必需的。看

  • with-open-file
  • make-string
  • file-length
  • read-sequence

.

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

请参阅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)))

  • with-input-from-string
  • *read-suppress*read工作所必需的在包含此映像中当前不存在的包的文件中。

.

(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

最新更新