我想实现一个方案解释器来学习SICP
我正在读这本书结构与解释 计算机程序,我想逐步编写一个方案解释器。
您知道最容易阅读(且简短)的方案的实现吗? 我将用 C 语言编写 JavaScript。
I'm reading the book Structure and Interpretation
of Computer Programs, and I'd like to code a scheme interpreter gradually.
Do you knows the implementation of the scheme most easy to read (and short)?
I will make a JavaScript in C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
SICP 本身有几个部分详细介绍了如何构建元循环解释器,但我建议您看一下以下两本书,以获得有关方案解释器的更好资源: 编程语言:应用和解释和编程语言要点 。它们都很容易阅读,并逐步指导您构建解释器。
SICP itself has several sections detailing how to build a meta-circular interpreter, but I would suggest that you take a look at the following two books for better resources on Scheme interpreters: Programming Languages: Application and Interpretation and Essentials of Programming Languages. They're both easy to read and gradually guide you through building interpreters.
Christian Queinnec 的《Lisp In Small Pieces》一书非常棒。比 EoPL 更现代。涵盖了 Lisp 和 Scheme,并详细介绍了大多数书籍都忽略的低级内容。
Christian Queinnec's book Lisp In Small Pieces is superb. More modern that EoPL. Covers both Lisp and Scheme, and goes into detail about the gory low-level stuff that most books omit.
我会推荐博客系列从头开始的方案,它逐步构建了一个方案解释器C.
I would recommend the blog series Scheme from scratch which incrementally builds up a scheme interpreter in C.
我建议阅读 Kent Dybvig 的论文“方案的三种实现模型”。不是整个论文,但他讨论基于堆的模型的第一部分(直到第三章)非常适合Scheme的简单实现。
另一个很棒的资源(如果我理解正确并且你想用 C 实现它)是 Nils Holm 的“Scheme 9 from Empty Space” 。此链接指向尼尔斯的页面,底部有一个链接,指向该书旧的公共领域版本和较新的、更易于阅读的商业版本。两本都读过并且很喜欢。
I would recommend reading Kent Dybvig's dissertation "Three Implementation Models for Scheme". Not the whole dissertation, but the first part (up to chapter 3) where he discusses the Heap-Based Model is very suitable for a naive implementation of Scheme.
Another great resource (if I understood it correctly and you want to implement it in C) is Nils Holm's "Scheme 9 from Empty Space". This link is to Nils's page, and there's a link at the bottom to the old, public domain, edition of the book and to the newer, easier to read, commercially available edition. Read both and loved 'em.
我可以向您概述我的口译员是如何工作的,也许它可以让您了解一般情况。
虽然答案很晚,但我希望这可以帮助其他来到这个帖子并想要一个总体想法的人。
a) Define_Evaluator:用于定义语句
b) Funcall_Evaluator:用于处理其他用户定义的函数
c) Read_Evaluator:用于读取表达式并将其转换为方案对象
d) Print_Evaluator :根据对象的类型打印对象。
e) Eval_Evaluator:对表达式进行实际处理。
3.->首先,使用读取评估器读取每个表达式,该评估器将从表达式中创建一个方案对象。嵌套表达式会递归计算,直到表达式完成。
-> 接下来,Eval_Evaluator 被触发,它处理第一步中形成的方案表达式对象。
则会发生这种情况:
如果要计算的表达式是符号, a)。返回它的值。因此变量blk将返回该块的对象。
b) 如果要计算的表达式是一个列表。打印列表。
c) 如果要计算的表达式是函数。查找将使用 Funcall_Evaluator 返回评估的函数的定义。
->最后,打印评估器被触发以打印结果,该打印将取决于输出表达式的类型。
免责声明:
这就是我的口译员的工作方式,不一定是这样。
I can give you an overview on how my interpreter works, maybe it can give you an idea of the general thing.
Although the answer is pretty late, I hope this can help someone else, who has come to this thread and wants a general idea.
a) Define_Evaluator :for define statements
b) Funcall_Evaluator :for processing other user defined functions
c) Read_Evaluator :for reading an expression and converting it to a scheme object
d) Print_Evaluator :prints the object depending on the type of the object.
e) Eval_Evaluator :does the actual processing of the expression.
3.-> First each expression is read using the Read Evaluator which will create a scheme object out of the expression. Nested expressions are calculated recursively until the expression is complete.
->Next, the Eval_Evaluator is fired which processes the Scheme Expression Object formed in the first step.
this happens as so
a) if the expression to be evaluated is a symbol. Return its value. Therefore the variable blk will return the object for that block.
b) if the expression to be evaluated is a list. Print the list.
c) if the expression to be evaluated is a function. Look for the definition of the function which will return the evaluation using the Funcall_Evaluator.
->Lastly the print evaluator is fired to print the outcome , this print will depend on what type the output expression is.
Disclaimer:
This is how my interpreter works , doesnt have to be that way.
我一直在执行类似的任务,但几年后,建议:
http://howtowriteaprogram.blogspot .co.uk/2010/11/lisp-interpreter-in-90-lines-of-c.html
我仍在寻找有关创建 lisp/scheme VM 的优秀博客文章,它可以与 JIT 结合使用(对于任何竞争性 JS 实现都很重要:)。
I've been on a similar mission but several years later, recommendations:
http://howtowriteaprogram.blogspot.co.uk/2010/11/lisp-interpreter-in-90-lines-of-c.html
I'm still searching for good blog posts on creating a lisp/scheme VM, which could be coupled with JIT (important for any competitive JS implementation :).
除了 Queinnec 的书,这可能是方案中最全面的一本
到 C 的转换,您还可以阅读旧平台 library 中的文献.readscheme.org。
Apart from Queinnec's book, which probably is the most comprehensive one in scheme
to C conversion, you can read also literature from the old platform library.readscheme.org.