https://github.com/liautraver/codecrafters-interpreter-cpp
an exercise for building an interpreter on codecrafter
https://github.com/liautraver/codecrafters-interpreter-cpp
cpp cpp23 crafting-interpreters
Last synced: 25 days ago
JSON representation
an exercise for building an interpreter on codecrafter
- Host: GitHub
- URL: https://github.com/liautraver/codecrafters-interpreter-cpp
- Owner: LiAuTraver
- Created: 2024-11-30T18:30:40.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-12T19:14:02.000Z (about 1 year ago)
- Last Synced: 2025-04-12T19:38:47.913Z (about 1 year ago)
- Topics: cpp, cpp23, crafting-interpreters
- Language: C++
- Homepage:
- Size: 1.45 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://app.codecrafters.io/users/codecrafters-bot?r=2qF)
This is a C++ interpreter for Lox, made while taking on the
["Build your own Interpreter" Challenge](https://app.codecrafters.io/courses/interpreter/overview) by [CodeCrafters](https://codecrafters.io).
> from [Crafting Interpreters](http://www.craftinginterpreters.com/appendix-i.html) -- Lox is a language created solely for the purpose of the book and learning.
This is the repo for codecrafters' online judge exercise, a more detailed version see [here](https://github.com/LiAuTraver/loxcpp).
## Build
A C++23 compiler is required to build the project; tested with gcc, clang, and MSVC. Bazel or CMake is required to build the project.
### Debug mode
```powershell
$env:AC_CPP_DEBUG=1 # set the environment variable to enable debug mode
```
#### with Bazel(currently only configured for Windows)
```powershell
bazel build //tools:interpreter # build the interpreter
bazel test //tests:... # run the tests
```
#### with CMake
vcpkg is required to install the dependencies.
Make sure the option `AC_CPP_DEBUG` is set to `ON` in the CMakeLists.txt file.
```powershell
cmake --preset= # see available presets via `cmake --list-presets` make sure to alter the toolchain file
```
### Release mode
No external dependencies are required for release mode.
You can run Bazel or CMake as mentioned above(only for target `interpreter`),
or run the `run.sh` script in the root directory(Linux).
## Run
### Run the interpreter
```powershell
interpreter tokenize
interpreter parse
interpreter evaluate
interpreter run
# repl was on the way, but not in a forseeable future...
```
## Grammar
### Syntax
```cpp
program → declaration* EOF ;
```
### Declaration
```cpp
declaration -> varDecl
| statement
| funcDecl
| classDecl ;
varDecl -> "var" IDENTIFIER ( "=" expression )? ";" ;
funcDecl -> "fun" function ;
classDecl -> "class" IDENTIFIER ("<" IDENTIFIER )? ( "{" function* "}" )? ;
```
### Statement
```cpp
statement -> exprStmt
| printStmt
| block
| ifStmt
| whileStmt
| forStmt
| returnStmt ;
exprStmt -> expression ";" ;
printStmt -> "print" expression ";" ;
block -> "{" declaration* "}" ;
ifStmt -> "if" "(" expression ")" statement ( "else" statement )? ;
whileStmt -> "while" "(" expression ")" statement ;
forStmt -> "for" "(" ( varDecl | exprStmt | ";" )
expression? ";"
expression? ")" statement ;
returnStmt -> "return" expression? ";" ;
```
### Expression
```cpp
expression -> assignment ;
assignment -> ( call "." )? IDENTIFIER "=" assignment
| logic_or ;
logic_or -> logic_and ( "or" logic_and )* ;
logic_and -> equality ( "and" equality )* ;
equality -> comparison ( ( "!=" | "==" ) comparison )* ;
comparison -> term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term -> factor ( ( "-" | "+" ) factor )* ;
factor -> unary ( ( "/" | "*" ) unary )* ;
unary -> ( "!" | "-" ) unary
| call ;
call -> primary ( "(" arguments? ")" | "." IDENTIFIER )* ;
primary -> NUMBER | STRING
| "true" | "false" | "nil" | "this"
| "(" expression ")" | IDENTIFIER | "super" "." IDENTIFIER;
```
### Miscellaneous
```cpp
arguments -> expression ( "," expression )* ;
function -> IDENTIFIER "(" parameters? ")" block ;
parameters -> IDENTIFIER ( "," IDENTIFIER )* ;
alnums -> [a-zA-Z0-9]
| [.!@#$%^&*()]
| [...] ;
```
### Lexical
```cpp
number -> digit + ( "." digit + )? ;
string -> "\"" + ([[alnums]])* + "\"" ;
identifier -> [a-zA-Z_] + [a-zA-Z0-9_]* ;
```
> note: the `cpp` was just for syntax highlighting in vscode to make it look prettier than plain text.
## Project Structure
[dependencies for interpreter](image/interpreter.png), generated by Bazel
## Notes
This exercise is still in active development, and I will be updating it as I progress through the book.
TODO(pirority from high to low)
- [ ] Desugar `for` statement(currently handles it saparately)
- [x] Binding and resolving
- [x] Add `class` support
- [x] Add class inheritance support
- [ ] JVM bytecode generation
- [ ] LLVM IR generation
- [ ] Machine code generation(x86_64, risc-v)