craftinginterpreters README

The project's README from github.com/munificent/craftinginterpreters. As one markdown file: README.md.

This is the repo used for the in-progress book "Crafting Interpreters". It contains the Markdown text of the book, full implementations of both interpreters, as well as the build system to weave the two together into the final site.

If you find an error or have a suggestion, please do file an issue here. Thank you!

Contributing

One of the absolute best things about writing a book online and putting it out there before it's done is that people like you have been kind enough to give me feedback, point out typos, and find other errors or unclear text.

If you'd like to do that, great! You can just file bugs here on the repo, or send a pull request if you're so inclined. If you want to send a pull request, but don't want to get the build system set up to regenerate the HTML too, don't worry about it. I'll do that when I pull it in.

Ports and implementations

Another way to get involved is by sharing your own implementation of Lox. Ports to other languages are particularly useful since not every reader likes Java and C. Feel free to add your Lox port or implementation to the wiki:

Building Stuff

I am a terribly forgetful, error-prone mammal, so I automated as much as I could.

Prerequisites

I develop on an OS X machine, but any POSIX system should work too. With a little extra effort, you should be able to get this working on Windows as well, though I can't help you out much.

Most of the work is orchestrated by make. The build scripts, test runner, and other utilities are all written in Dart. Instructions to install Dart are here. Once you have Dart installed and on your path, run:

$ make get

This downloads all of the packages used by the build and test scripts.

In order to compile the two interpreters, you also need a C compiler on your path as well as javac.

Building

Once you've got that setup, try:

$ make

If everything is working, that will generate the site for the book as well as compiling the two interpreters clox and jlox. You can run either interpreter right from the root of the repo:

$ ./clox
$ ./jlox

Hacking on the book

The Markdown and snippets of source code are woven together into the final HTML using a hand-written static site generator that started out as a single tiny Python script for my first book and somehow grew into something approximating a real program.

The generated HTML is committed in the repo under site/. It is built from a combination of Markdown for prose, which lives in book/, and snippets of code that are weaved in from the Java and C implementations in java/ and c/. (All of those funny looking comments in the source code are how it knows which snippet goes where.)

The script that does all the magic is tool/bin/build.dart. You can run that directly, or run:

$ make book

That generates the entire site in one batch. If you are incrementally working on it, you'll want to run the development server:

$ make serve

This runs a little HTTP server on localhost rooted at the site/ directory. Any time you request a page, it regenerates any files whose sources have been changed, including Markdown files, interpreter source files, templates, and assets. Just let that keep running, edit files locally, and refresh your browser to see the changes.

Building the interpreters

You can build each interpreter like so:

$ make clox
$ make jlox

This builds the final version of each interpreter as it appears at the end of its part in the book.

You can also see what the interpreters look like at the end of each chapter. (I use this to make sure they are working even in the middle of the book.) This is driven by a script, tool/bin/split_chapters.dart that uses the same comment markers for the code snippets to determine which chunks of code are present in each chapter. It takes only the snippets that have been seen by the end of each chapter and produces a new copy of the source in gen/, one directory for each chapter's code. (These are also an easier way to view the source code since they have all of the distracting marker comments stripped out.)

Then, each of those can be built separately. Run:

$ make c_chapters

And in the build/ directory, you'll get an executable for each chapter, like chap14_chunks, etc. Likewise:

$ make java_chapters

This compiles the Java code to classfiles in build/gen/ in a subdirectory for each chapter.

Testing

I have a full Lox test suite that I use to ensure the interpreters in the book do what they're supposed to do. The test cases live in test/. The Dart program tool/bin/test.dart is a test runner that runs each of those test files on a Lox interpreter, parses the result, and validates that that the test does what it's expected to do.

There are various interpreters you can run the tests against:

$ make test       # The final versions of clox and jlox.
$ make test_clox  # The final version of clox.
$ make test_jlox  # The final version of jlox.
$ make test_c     # Every chapter's version of clox.
$ make test_java  # Every chapter's version of jlox.
$ make test_all   # All of the above.

Testing your implementation

You are welcome to use the test suite and the test runner to test your own Lox implementation. The test runner is at tool/bin/test.dart and can be given a custom interpreter executable to run using --interpreter. For example, if you had an interpreter executable at my_code/boblox, you could test it like:

$ dart tool/bin/test.dart clox --interpreter my_code/boblox

You still need to tell it which suite of tests to run because that determines the test expectations. If your interpreter should behave like jlox, use "jlox" as the suite name. If it behaves like clox, use "clox". If your interpreter is only complete up to the end of one of the chapters in the book, you can use that chapter as the suite, like "chap10_functions". See the Makefile for the names of all of the chapters.

If your interpreter needs other command line arguments passed to use, pass them to the test runner using --arguments and it will forward to your interpreter.

Repository Layout

craftinginterpreters (munificent/craftinginterpreters)

Searching Database…

这是什么?这是《Crafting Interpreters》的代码仓库,这本书教你如何构建解释器。仓库里包含这本书的完整正文、书中两个 Lox 解释器的完整源代码,以及支撑 craftinginterpreters.com 网站的构建系统。
这只是一本用来读的书,还是也附带能运行的代码?两者都有。book/ 目录存放 Markdown 格式的章节,java/ 和 c/ 目录是完整的 jlox 和 clox 解释器,还有一个静态网站生成器,把正文和代码片段编织成 HTML,提交在 site/ 目录下。
我只想读这本书,也需要自己构建什么吗?不需要——生成好的网站已经提交在 site/ 目录下,和 craftinginterpreters.com 完全一致,所以你不用安装任何东西就能在网上读完整本书。
用这个仓库构建解释器需要准备什么?构建和测试脚本需要 Dart,另外你的系统路径里还要有 C 编译器和 javac。大部分工作由 make 来编排,它会生成图书网站并编译 clox 和 jlox 这两个解释器。
这个构建在 Windows 上能用吗?也许可以,但只能靠你自己:作者是 macOS 用户,认为任何 POSIX 系统都应该没问题,并表示 Windows 需要额外的工作,他无法提供帮助。
Lox 是什么?一种专门为这本书设计的小型脚本语言。你要把它实现两次:先实现 jlox,一个用 Java 写的解释器;再实现 clox,一个用 C 写的字节码虚拟机。书中的每一部分都是从零开始带你构建其中一个。
jlox 和 clox 有什么区别?jlox 是用 Java 写的解释器,clox 是用 C 写的解释器;它们的源代码分别放在 java/ 和 c/ 目录里。书中为它们各安排了一个部分,你还可以按任意一章结尾时的代码状态来构建。
每章末尾的挑战题有答案吗?有——挑战题的示例答案放在 note/answers 目录里,作者唯一的警告是“不许作弊!”。note/ 目录还收录了他的研究笔记和待办事项。
我可以分享自己用其他语言写的 Lox 移植版吗?可以。README 明确欢迎移植版本,并指出这特别有价值,因为不是每个读者都喜欢 Java 和 C,同时请你把你的实现添加到 wiki 的 Lox 实现列表里。
What are similar projects?hello-algo, quarkdown, chibicc, c4, mquickjs, free-books, lua, ruby
Creator
munificent
Size
22.9 MB
Stars
11.1K
Updated
CommitAug 01
Age
9y
Commits · 90 Days