Skip to content
/ lox Public

typescript and lox tree-walk lox interpreter with some extensions to the base lox language

Notifications You must be signed in to change notification settings

mtso/lox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lox implementations

This repo contains typescript and lox tree-walk interpreter implementations of lox (an educational programming language described in Crafting Interpreters by Bob Nystrom). There are a couple additional extensions to the base language.

The extensions are intended to experiment with some additional language features. And the lox implementation makes heavy use of the dynamic property access extension to implement collections (List/Map). A purer lox implementation with vanilla lox was demonstrated in https://github.com/benhoyt/loxlox.

Aside from the extensions and a couple omissions (REPL and AST Printer), both implementations follow the structure of the book, with: AST code generator, visitor pattern, etc. Parse and runtime errors in the lox implementation are bubbled up via a Result{ok,value} class to simulate raised exceptions.

Though character escaping was added, the meta usage of running lox.lox with lox.lox is a work in progress.

Typescript implementation

The typescript implementation expects the Deno runtime, which you can install with:

curl -fsSL https://deno.land/x/install/install.sh | sh

Run with:

deno run --allow-read lox.ts <path to lox file>

Extensions to base lox in typescript implementation

  • dynamic property access
  • lambdas (anonymous functions)

Additional builtins

  • str
  • strlen
  • strslice
  • parse_float
  • readfile
  • process_exit
  • process_args
  • eprint (output to stderr)

Lox example demonstrating extensions

class Object { }
class Stack {
  init() {
    this.items = Object();
    this.count = 0;
  }
  push(item) {
    this.items.(this.count) = item;
    this.count = this.count + 1;
  }
  pop() {
    this.count = this.count - 1;
    var item = this.items.(this.count);
    this.items.(this.count) = nil;
    return item;
  }
  forEach(callback) {
    for (var i = 0; i < this.count; i = i + 1) {
      callback(this.items.(i));
    }
  }
}
var stack = Stack();
stack.push("foo");
stack.push("bar");
stack.forEach(fun(item) { print(item); });
// foo
// bar

Testing

With the official repo set up, run the following command:

dart tool/bin/test.dart jlox --interpreter <path to repo>/tslox

Link to repo: https://github.com/munificent/craftinginterpreters

Lox implementation

The lox implementation uses the dynamic property access extension, and is expected to be run using it, like so:

deno run --allow-read lox.ts lox.lox -- <path to lox file>

The -- is needed since process_args builtin is coded to remove the first two command line arguments (to skip past the --).

Testing

With the official repo set up as above, run the following command to test the lox implementation of lox:

dart tool/bin/test.dart jlox --interpreter <path to repo>/loxlox

Link to repo: https://github.com/munificent/craftinginterpreters

Extensions to base lox in lox implementation

  • dynamic property access

Additional builtins

The same builtins in the typescript implementation are made available in the lox implementation.

Meta Usage (WIP)

The backslash escaping in Parser::string() supports running lox.lox with the lox implementation (lox.lox itself).

The command to run lox with the lox implementation of lox:

deno run --allow-read lox.ts lox.lox -- lox.lox -- <path to lox file>

Limitations

  • The REPL prompt in the book is not implemented.
  • AST Printer class omitted in both implementations.

About

typescript and lox tree-walk lox interpreter with some extensions to the base lox language

Topics

Resources

Stars

Watchers

Forks