• Stars
    star
    8,160
  • Rank 4,316 (Top 0.09 %)
  • Language
    HTML
  • License
    Other
  • Created over 7 years ago
  • Updated 30 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Repository for the book "Crafting Interpreters"

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

  • asset/ – Sass files and jinja2 templates used to generate the site.
  • book/ - Markdown files for the text of each chapter.
  • build/ - Intermediate files and other build output (except for the site itself) go here. Not committed to Git.
  • c/ – Source code of clox, the interpreter written in C. Also contains an XCode project, if that's your thing.
  • gen/ – Java source files generated by GenerateAst.java go here. Not committed.
  • java/ – Source code of jlox, the interpreter written in Java.
  • note/ – Various research, notes, TODOs, and other miscellanea.
  • note/answers – Sample answers for the challenges. No cheating!
  • site/ – The final generated site. The contents of this directory directly mirror craftinginterpreters.com. Most content here is generated by build.py, but fonts, images, and JS only live here. Everything is committed, even the generated content.
  • test/ – Test cases for the Lox implementations.
  • tool/ – Dart package containing the build, test, and other scripts.

More Repositories

1

game-programming-patterns

Source repo for the book
HTML
3,967
star
2

vigil

Vigil, the eternal morally vigilant programming language
Python
2,840
star
3

hauberk

A web-based roguelike written in Dart.
Dart
1,951
star
4

mark-sweep

A simple mark-sweep garbage collector in C
C
709
star
5

magpie

The Magpie programming language
Python
351
star
6

bantam

A simple demo app for Pratt parsing
Java
298
star
7

finch

The Finch programming language
C++
213
star
8

jasic

A complete interpreter for a dialect of BASIC in one Java sourcefile
Java
147
star
9

rooms-and-mazes

Animated demo code for a random dungeon generator
Dart
137
star
10

ui-as-code

Design work on improving Dart's syntax for UI code
Dart
121
star
11

amaranth

An open source roguelike written in C#
C#
115
star
12

journal

My blog, in all its statically-generated glory.
HTML
93
star
13

malison

A little terminal emulation package for Dart.
Dart
71
star
14

lark

The Lark programming language.
Java
57
star
15

piecemeal

Basic data structures your 2D Dart game may need.
Dart
51
star
16

wrenalyzer

Static analyzer for the Wren programming language
41
star
17

lisp2-gc

A simple implementation of the LISP2 mark-compact GC algorithm
C
37
star
18

malison-dotnet

A terminal emulation library in C#
C#
36
star
19

a-turn-based-game-loop

Demo code for http://journal.stuffwithstuff.com/2014/07/15/a-turn-based-game-loop/
Dart
27
star
20

markymark

A tiny static web server for previewing Markdown files
Dart
23
star
21

bulfinch

Just a little toy language to learn about register-based VMs.
Java
20
star
22

fov

Demo code for a blog post about field-of-view calculation
Dart
19
star
23

pattern_matching

A simple ML-like pattern-matching library in C#.
C#
18
star
24

bramble-dotnet

A set of utility classes and extension methods in C#
C#
17
star
25

bits

Tiny web page for converting between binary, decimal, hex and UTF-8
HTML
9
star
26

dep-external-libraries

Proposal for handling platform-specific code in Dart
Dart
8
star
27

wren-sublime

A Sublime Package for the Wren programming language
6
star
28

settings

My personal settings, configurations, and dotfiles.
Shell
5
star
29

multipleinheritance.js

A little hack to get multiple inheritance working in JavaScript.
JavaScript
5
star
30

wren-atom

Atom package for Wren
CoffeeScript
4
star
31

linkedlist

Reversing a linked list
Go
4
star
32

magpie-optionally-typed

The old single-dispatch optionally-typed flavor of the Magpie programming language.
C#
3
star
33

temp-flutter-examples

Temporary repo looking at some Flutter examples
Dart
2
star
34

dep-interface-libraries

DEP for configuration-specific code in Dart
2
star
35

magpie-csharp

The old C#-based Magpie programming language
C#
2
star
36

gallery-constructors

Temporary repo to test out some potential changes around constructors in Dart
Dart
1
star
37

format-flutter

Temporary repo to apply an experimental dart format style to Flutter.
Dart
1
star
38

module_scrape

Temporary code for investigating a module system for Dart
Dart
1
star
39

support

My personal repository for support files: things like color schemes, preferences, and other settings files that it's handy to have centrally accessible.
1
star