Skip to content

owolabioromidayo/cpplox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cpplox

A C++ implementation of the tree-walking interpreter from Crafting Interpreters.

Usage:

git clone https://github.com/owolabioromidayo/cpplox
cd cpplox
g++ src/main.cpp -o lox

REPL Mode :./lox
Run a Lox file : ./lox filepath

Parser grammar

cpplox implements most of the original Lox definition except classes and inheritance. Here's the parser grammar:

program      => declaration* EOF
declaration  => funcDecl | varDecl | statement
funDecl      => "fun" function
function     => IDENTIFIER "(" parameters? ")" block
parameters   => IDENTIFIER ( "," IDENTIFIER )*
varDecl      => "var" IDENTIFIER ( "=" expression )? ";"
statement    => exprStmt | ifStmt | forStmt | printStmt | returnStmt | whileStmt
                         | breakStmt | continueStmt | block
exprStmt     => expression ";"
ifStmt       => "if" "(" expression ")" statement ( "else" statement )?
forStmt      => "for" "(" ( varDecl | exprStmt | ";" ) expression? ";" expression? ")" statement
printStmt    => "print" expression ";"
returnStmt   => "return" expression? ";"
whileStmt    => "while" "(" expression ")" statement
block        => "{" declaration* "}" ;
expression   => series
series       => assignment ( "," assignment )*
assignment   => ( call "." )? IDENTIFIER "=" assignment | ternary
ternary      => logic_or ( "?" ternary ":" ternary )*
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 )*
arguments    => expression ( "," expression )*
primary      => NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")"
                                | IDENTIFIER | functionExpr | "super" . IDENTIFIER
functionExpr => "fun" IDENTIFIER? "(" parameters? ")" block

About

Fast Tree Walk Interpreter for Lox in C++

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages