用Python编写第一个解释器 - 选择OCaml语言

kir*_*off 2 python compiler-construction parsing interpreter ocaml

我正在阅读P. Norvig用Python编写的Scheme解释器代码,我想尝试用Python编写一个解释器.这恰好是帖子的主题:初级程序员可以用什么语言实现翻译呢?.但是,我非常有兴趣学习OCaml很长一段时间,如果我为OCaml做出决定,这可能是正确的时机.

我是否应该花费很多精力来实现OCaml语言的Python解释器?这只是一个个人项目,我的目标(除了学习OCaml),主要是练习我在编译器和解释器上阅读的内容.

如果OCaml不合适,你个人会建议什么?

你可以暗示OCaml的好参考(甚至:写一个OCampl解析器/解释器).

##编辑

Lua/Ruby/BASIC怎么样?(因为我也想学习那些)

感谢致敬

Jef*_*eld 7

你似乎在说你想用Python创建一个OCaml解释器(不是OCaml中的Python解释器,对吧?).在我看来,OCaml本身对于教育项目来说是一个太大的语言.我会选择一种更小的语言.这就是为什么Scheme是一个不错的选择 - 核心语言非常小.

使用OCaml,您还可以进行类型推断,这非常值得学习,但同样是一个相当大的主题.

对于小而有点ML-ish的东西,你可以从无类型的lambda演算开始.这是一个非常常见的测试用例.


gas*_*che 5

如果你想很好地介绍 OCaml 的语义基础(一些理论),你应该看看Didier Rémy所著的Using, Understanding, and Unraveling The OCaml Language一书。

Writing an implementation of the full OCaml language would be quite some work, but writing an implementation of a reasonable subset of it is doable, for example, as a semester project.

Given than ML languages are very good at symbolic manipulations (interpreters, compilers, analysers, proof checkers...), it is probably more fun to write a Python implementation in OCaml than an OCaml implementation in Python -- which may explain some confusion around your question.