Contents

alexito4/slox

Swift implementation of a Lox interpreter

A TREE-WALK INTERPRETER

- [x] Challenge 4: C-style / ... / block comments.

- [x] Challenge 3: AST Printer In Reverse Polish Notation. - [x] GenerateAst tool - Things to explore: - Is there a better way to metaprogram the expression classes? Or is there even a need to metaprogram them with Swift cleaner syntax? - Does Swift offer a better model for defining the expressions? - Implemented Enums with methods and pattern matching in another branch.

- [x] Helper method for parsing left-associative series of binary operators. Swift can't pass variadic arguments between functions (no array splatting), so it's a little bit hugly. - [ ] Challenge 1: Add prefix and postfix ++ and -- operators. - [ ] Challenge 2: Add support for the C-style conditional or “ternary” operator ?: - [ ] Challenge 3: Add error productions to handle each binary operator appearing without a left-hand operand.

- [ ] Challenge 1: Allowing comparisons on types other than numbers could be useful. - [x] Challenge 2: Many languages define + such that if either operand is a string, the other is converted to a string and the results are then concatenated. - [ ] Challenge 3: Change the implementation in visitBinary() to detect and report a runtime error when dividing by 0.

- [ ] Challenge 1: Add support to the REPL to let users type in both statements and expressions. - [ ] Challenge 2: Make it a runtime error to access a variable that has not been initialized or assigned to - Challenge 3: Nothing to implement.

- Challenge 1: Nothing to implement. - Challenge 2: Nothing to implement. - [x] Challenge 3: Add support for break statements.

- Challenge 1: Nothing to implement. - [x] Challenge 2: Add anonymous function (lambdas) syntax. - Challenge 3: Nothing to implement.

- Challenge 1: Nothing to implement. - Challenge 2: Nothing to implement. - [ ] Challenge 3: Extend the resolver to report an error if a local variable is never used. - [ ] Challenge 4: Store local variables in an array and look them up by index.

- [ ] Challenge 1: Add class methods. - [ ] Challenge 2: Support getter methods. - Challenge 3: Nothing to implement.

- [ ] Challenge 1: Multiple inheritance. Nothing to implement...? - [ ] Challenge 2: Reverse method lookup order in class hierarchy. - Challenge 3: Add your own features!

Tests

I integrated Bob tests in order to be able to make sure this implementation behaves in the same way as the original implementation. You can find the test in this project with the test.py script modified to work with this project. You can also find a diff with the main modifications.

Example of usage: sh build.sh; ./tools/test.py chap13_inheritance

Project structure

This project now uses SPM to manage the executables, framework and dependencies.

The bulk of the interpreter is implemented in a framework and the executable is just a small CLI program that uses that framework.

  • slox: The executable. Can be used as a CLI to run the interpreter.
  • LoxCore: The main framework that contains the implementation of the Lox language in Swift.
  • GenerateAst: A small executable that generates the AST for LoxCore.

Author

Alejandro Martinez | http://alejandromp.com | @alexito4

Package Metadata

Repository: alexito4/slox

Stars: 51

Forks: 8

Open issues: 0

Default branch: master

Primary language: swift

License: MIT

Topics: interpreter, language, lox, lox-interpreter, lox-language, programming-language, swift

README: README.md