我正在寻找可以为我生成 lisp 程序中每个函数的代码行统计数据的程序。在 Lisp 中,这意味着每个函数或宏要计算有多少函数被递归地包含在顶级函数中。
任何指针将不胜感激。
最佳答案
for each function or macro to count how many functions are recursively included in the top level function
我不确定这是什么意思。
如果你想计算代码中的函数调用次数,你需要一个完整的 code walker .
不过,对于文件中顶层表单数量的简单含义,这个问题还是比较容易处理的。 我不知道有这样做的现有程序,但它听起来并不难:
(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
可能不是必需的。
见
.
(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
关于lisp - 寻找一个可以帮助我为 lisp 程序生成每个函数统计代码行的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48264593/