Giter Club home page Giter Club logo

olox's Introduction

Olox

Olox is a tree-walk interpreter for the Lox language after the first half of Crafting Interpreters, implemented in OCaml (hence the name).

Implementation

There are some differences to the original book implementation in Java. First of all, instead of writing everything from scratch, olox uses ocamllex and menhir for lexing and parsing, respectively. The reason for this is that I have written recursive descent parser before but had never used a parser generator.

Instead of using classes and the visitor pattern for AST traversal, Olox encodes the AST in variants (ADTs), which is the natural solution in OCaml (even though the "o" in olox comes from OCaml's "Objective"). However, the resulting structure of the code is similar enough to the book's solution.

For the environments, olox uses a persistent data structure (Map.t of the OCaml standard library). Thus, a "resolver" is not strictly needed for lexical scopes. Nevertheless, there is a static analysis pass which runs before the interpreter. Like the resolver of jlox, it makes sure that all variables can be resolved and throws the some static errors (usage of this outside of method, super in a class without a superclass, etc.).

Jlox compliance

For the most part, olox behaves exactly like jlox. In particular, olox passes the test suites for "functions", "closures" and "classes" from the original repo, see test directory.

There are two exceptions:

1. Mutual recursion

In olox, the global environment is simply the first environment, but not special otherwise. As a result, variables in the global environment cannot be resolved before they are declared. This breaks naive mutual recursion, because the first function won't be able to see the second function. To use mutual recursion, the user can pass the second function into the first one as a parameter.

2. Parameter limit

I did not implement an upper limit of 255 parameters / arguments. Predictably, these tests cannot pass.

Building and running

Olox uses the dune build system, so the usual dune commands work

dune build

Similarly, to run the tests

dune runtest

olox's People

Contributors

tjammer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

t-abbott

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.