gpt4 book ai didi

c - 我在哪里可以找到 C 语言的 Lisp 阅读器?

转载 作者:太空狗 更新时间:2023-10-29 16:41:16 26 4
gpt4 key购买 nike

我有一个用 Java 编写的 Lisp 阅读器,我正在考虑将其翻译成 C。(或者可能是 C++。)这是一个相当完整和有用的 hack,所以主要问题是用一种语言进行动态存储分配而无需垃圾收集。如果有人已经考虑过这一点,我宁愿借用他们的代码也不愿自己弄清楚。 (C 不是我最喜欢的语言。)

当然,拥有一个 Lisp 阅读器是没有意义的,除非你打算用你读到的东西做点什么,所以也许我应该用措辞表达这个问题,我在哪里可以找到用 C 编写的简单 Lisp 核心?,但是根据我的经验,编写 Lisp 最难避免的部分(有点令人惊讶)是读者。另外,我不想有垃圾收集器;我期待一个应用程序,其中列表结构将或多或少地手动释放。

最佳答案

加里诺特的 Interpreting Lisp是非常好的。您也可以尝试其他人,例如 Jim Mayfield's口齿不清。那里可能有很多小 Lisp...

您提到您不喜欢 C。也许您会喜欢 Haskell——在这种情况下您可以尝试 "Write yourself a Scheme in 48 hours" ,一个有趣的教程(你可以用 Haskell 编写一个 Scheme 解释器)。

更新:我知道 Lisper 不太会使用 Haskell,但是嘿,它比 C 更舒服(至少对我而言)!除此之外,HAskell 具有良好的 FFI,因此将 Haskell 制作的 Lisp 阅读器用作 C 兼容库应该很容易。

更新 2:如果您想要使用 XLisp,正如其他用户所建议的,您可能需要 src/xlread.c(863 行)和 include/xlisp.h(1379 行)- - 但我可能是错的...

更新 3:如果您使用 Gary Knott 的 Lisp(一个 942 行的 C 文件),函数签名是 int32 sread(void)。如果我不需要任何花哨的东西(比如读取宏)或高度优化的东西(有一个 PDF 文件描述了代码是如何实现的,所以你不必在迷宫中找到自己的方式),这将是我的选择。该函数的文档是:

This procedure scans an input string g using a lexical token scanning
routine, e(), where e() returns
1 if the token is '('
2 if the token is '''
3 if the token is '.'
4 if the token is ')' or a typed pointer d to an
atom or number stored in row ptrv(d) in the atom or number tables.
Due to the typecode (8 or 9) of d, d is a negative 32-bit integer. The
token found by e() is stripped from the front of g.

SREAD constructs an S-expression and returns a typed pointer to it as
its result.

看到 Gary 的 Lisp 很旧,您需要更改它才能编译。不包含 linuxenv.h,而是包含:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <setjmp.h>

此外,它在 64 位机器上不起作用(sread 的文档应该会告诉您原因...)

更新 4:还有 Scheme implementations by Nils Holm(有描述内部结构的书籍)

关于c - 我在哪里可以找到 C 语言的 Lisp 阅读器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1511621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com