cpplox
is an interpreter for the Lox language written in C++.
- Lexer
- Expressions
- Interpreter
- Statements
- Environment
- Scopes
- Control flow
- Functions
- Class
- GCC 12+
- CMake 3.22+
- clone repository
git clone --recurse-submodules https://github.com/SebastienWae/cpplox
- build project
cd ccplox ./externals/vcpkg/bootstrap-vcpkg.sh cmake --preset config-release cmake --build --preset build-release
$ cpplox
cpplox: Lox interpreter - v0.0.1
To exit, press Ctrl+d or type "exit"
>> var a = 1;
>> print a + 2;
3
cpplox </path/to/file.lox>
Lox is a language with a C-like syntax that was created by Robert Nystrom for his book Crafting Interpreters.
A detailed introduction can be found in chapther 3 and the grammar in the Appendix I.
cpplox
also implement block comments, as well as the comma and ternary conditional operators.
NUMBER → DIGIT+ ( "." DIGIT+ )? ;
STRING → "\"" <any char except "\"">* "\"" ;
IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ;
ALPHA → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT → "0" ... "9" ;
program → declaration* EOF ;
declaration → varDecl
| statement ;
varDecl → "var" IDENTIFIER ( "=" assign )? ( "," IDENTIFIER ( "=" assign )? )* ";"
statement → exprStmt
| printStmt
| block ;
block → "{" declaration* "}" ;
exprStmt → expression ";" ;
printStmt → "print" expression ";" ;
NUMBER → DIGIT+ ( "." DIGIT+ )? ;
STRING → "\"" <any char except "\"">* "\"" ;
IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ;
ALPHA → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT → "0" ... "9" ;
expression → assignment ( "," expression )* ;
assignment → IDENTIFIER "=" assignment
| equality "?" assignment ":" assignment
| equality ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term → factor ( ( "-" | "+" ) factor )* ;
factor → unary ( ( "/" | "*" ) unary )* ;
unary → ( "!" | "-" ) unary
| primary ;
primary → NUMBER | STRING | "true" | "false" | "nil"
| "(" expression ")"
| IDENTIFIER ;
More examples are available here.
var imAVariable = "here is my value";
print "Hello, world!";
var a = 1;
while (a < 10) {
print a;
a = a + 1;
}
fun returnFunction() {
var outside = "outside";
fun inner() {
print outside;
}
return inner;
}
class Breakfast {
cook() {
print "Eggs a-fryin'!";
}
serve(who) {
print "Enjoy your breakfast, " + who + ".";
}
}
var a = 1, b = 2, c = 3;
var x = (a, b, c);
print x;
fun fib(n) {
return n < 2 ? n : fib(n - 1) + fib(n - 2);
}
cmake --preset config-debug
cmake --build --preset test-debug
ctest --output-on-failure --preset test-debug