Skip to content

C++ implementation of an interpreter for the Lox language

License

Notifications You must be signed in to change notification settings

SebastienWae/cpplox

Repository files navigation

cpplox

cpplox is an interpreter for the Lox language written in C++.

Roadmap

  • Lexer
  • Expressions
  • Interpreter
  • Statements
  • Environment
  • Scopes
  • Control flow
  • Functions
  • Class

Installing

Building from source

Dependencies:

  • GCC 12+
  • CMake 3.22+

Steps:

  • 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
    

Usage

REPL

$ cpplox
cpplox: Lox interpreter - v0.0.1
To exit, press Ctrl+d or type "exit"
>> var a = 1;
>> print a + 2;
3

File

cpplox </path/to/file.lox>

Lox language

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.

Grammar

Lexical

NUMBER         → DIGIT+ ( "." DIGIT+ )? ;
STRING         → "\"" <any char except "\"">* "\"" ;
IDENTIFIER     → ALPHA ( ALPHA | DIGIT )* ;
ALPHA          → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT          → "0" ... "9" ;

Syntax

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 ;

Examples

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);
}

Running the tests

cmake --preset config-debug
cmake --build --preset test-debug
ctest --output-on-failure --preset test-debug

License

GNU General Public License v3.0

About

C++ implementation of an interpreter for the Lox language

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published