A Lox interpreter from Part II of the book "Crafting Interpreters" by Bob Nystrom (website, git repo), written in C++ and using flexc++/bisonc++. The Java code for jlox
from the book has been adapted without significant modification, using similar visitor pattern logic, the same class hierarchy, and identical naming conventions where possible.
Under Windows run: loxcpp [/?|/h|/v] [/i [/a]|/b] [filename]
, under Linux run ./loxcpp [-?|-h|-v] [-i [-a]|-b] [filename]
. The flags are:
/b
,-b
run in batch mode (this is the default), use Ctrl-Z (Windows) or Ctrl-D (Linux) to end input/i
,-i
run an interactive interpreter, executing each statement when entered/a
,-a
print the AST for expressions entered in interactive mode[filename]
read from file instead of standard input/v
,-v
print version string and exit/?
,/h
,-?
,-h
print usage info and exit
Error handling and reporting is very basic, the parser only outputs "syntax error" and the interpreter can throw exceptions.
The Windows Makefile can be run within a Visual Studio 2019/2022 Command Prompt with:
nmake /f Makefile.nmake
The GNU Makefile can be run wih
make [all] [flexcpp] [bisoncpp] [all]
For the flexcpp
and bisoncpp
targets to work, the flexc++
and bisonc++
programs must be installed (these are Linux only and a port to Windows is unlikely). I recommend building from scratch using the source from GitLab:
To obtain the prerequisites for building these under Debian, use:
sudo apt install icmake libbobcat-dev yodl
Modifying the files loxer
and loxgram
requires running make flexcpp
and make bisoncpp
respectively for changes to be reflected in the auto-generated C++ files.
The lexer
directory contains the scanner logic, note that some manual changes have been made to Loxer.h
even though this is a generated file. The parser
directory contains the grammar logic, note that some manual changes have been made to Loxgram.h
even though again this is a generated file.
The visitor
directory contains the tree-walk code for the AST generated by parser/loxgram
, contained entirely in C++ header files. The top level .hpp
files contiain the code for the types used to build the AST, the .cpp
files (other than main.cpp
) in general contain the minimal code needed to break the circular dependencies involved.
The Loxer
and Loxgram
classes are fairly self-contained, with an instance of the former a parameter to the latter's initializer. The files ExprVisitor.hpp
and StmtVisitor.hpp
are pure-virtual definitions for the other files in directory visitor
. The files FwdTypes.hpp
/Value.cpp
use a std::variant
where Java Object
is used in the book and this is the type for the symbol table in Environment.hpp
. The base class SType
is empty and allows std::shared_ptr<SType>
to be used as the semantic type for the parser. Only classes Expr
and Stmt
derive from this directly.
Heavy use of std::make_shared
and std::dynamic_pointer_cast
is made, particularly in the file loxgram
. There should be no memory leaks over the running time of the interpreter, which is the motivation for using these over raw pointers. Type Value
can store a std::shared_ptr
for two of its variant types; use of std::unique_ptr
is not possible here for technical reasons.
Don't be fooled by the 1.00+ version number—this is alpha-development quality software. The project does contain a large chunk of working code which may be of interest to others wanting to use flexc++
and bisonc++
to create an interpreter, or who have worked through the book and want to gain greater knowledge of Modern C++. It is assumed that much of the existing code can be reused/adapted to create a JIT version, in the style of clox
, as a future improvement.
-
2023/08/27: 1.00 First upload to GitHub with Windows executable. Many bugs, features and defects: please raise issues and submit pull requests.
-
2023/08/30: 1.01 Second upload to GitHub with Windows executable. Believed to be complete and correct coverage of all of the material in Part II.
-
2024/06/01: 1.02 Third upload to GitHub with Windows executable. Fix to
LoxFunction::call()
to handle recursive functions correctly.