Skip to content

Latest commit

 

History

169 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crafting Interpreters: An Implementation in C++

This is a bytecode compiler and virtual machine interpreter for the Lox programming language. Lox is dynamically typed, garbage collected, with first-class function closures, and classes and inheritance.

A gentle, friendly introduction to Lox:

print "Hello, world!";

var imAVariable = "here is my value";
var iAmNil;

if (iAmNil) {
    print "yes";
} else {
    print "no";
}

var a = 1;
while (a < 10) {
    print a;
    a = a + 1;
}

for (var a = 1; a < 10; a = a + 1) {
    print a;
}

fun returnSum(a, b) {
    return a + b;
}

fun returnFunction() {
    var outside = "outside";

    return fun() {
        print outside;
    };
}

var innerFn = returnFunction();
innerFn();

class Breakfast {
    init(meat, bread) {
        this.meat = meat;
        this.bread = bread;
    }

    serve(who) {
        print "Enjoy your " + this.meat + " and " +
            this.bread + ", " + who + ".";
    }
}

class Brunch < Breakfast {
    drink() {
        print "How about a Bloody Mary?";
    }
}

var benedict = Brunch("ham", "English muffin");
benedict.serve("Noble Reader");

Build

docker build --tag=cpploximg --target=build .

Options:

--tag=<name> The tag can be whatever name you want for your image.

--target=<stage> The stage can be one of: deps, build, bench, perf, test, or debug.

--build-arg CC=<compiler> The compiler can be one of: gcc or clang. Defaults to clang.

Use REPL in container shell

docker run -it --rm cpploximg

> print "Hello, Lox!";

Run a script outside the container

In this example, I mount $(pwd)/test/lox into the container as /host, and I run a Lox script from that mounted folder.

docker run -it --rm -v $(pwd)/test/lox:/host:ro cpploximg ./cpploxbc /host/hello.lox

Development

Docker will re-run the entire build stage when a single source changes. To get incremental builds -- very handy during development -- we can mount our host files and run cmake from within a container.

docker run -it --rm -v $(pwd):/project/src:ro cpploximg bash
# cmake --build .
# ctest

Profile

The perf target will prepare the project and tools, then you must run in a container with privileged access.

docker run -it --privileged --rm cpploximg bash
# ./perf record -g ./perf_test
# ./perf report -g graph,0.5,caller

About

Following Nystrom's book Crafting Interpreters, implemented in C++.

Resources

Stars

102 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages