An implementation of Crafting Interpreters's Lox in C++20.
// fibonacci.lox
fun fib(n) {
if (n <= 1) return n;
return fib(n - 2) + fib(n - 1);
}
print fib(7); // prints 13
Extensions:
- Properly nesting C-style
/*...*/
block comments. - Added support for the ternary operator and the comma operator.
- Implemented break statements inside loops.
Prerequisites:
cmake
v3.25llvm
v15 (forclangd
andclang-format
)ninja
(optional)rlwrap
(optional)
mkdir build
cd build
cmake -GNinja ..
ninja
This will produce two binaries in bin/
which you can run with
# Run interpreter
bin/lox
# Use rlwrap to scroll through history
rlwrap bin/lox
# Run tests (expects to be called from the build/ dir)
(cd bin && ./tests)
So far, I've been able to build and run on WSL and my M1 MacBook using clang 15.0.6
.
- Write about the visitor pattern.
- Try using
mpark/patterns
and refactor code with variants. - Try to merge literals and values (don't need both) - make everything a literal