Following the amazing book by Robert Nystorm, Crafting the Interpreter. This repo closely follows the book, including enhancement proposed as challenge questions.
Required Java >= 13
if one does not want to refactor the code.
program → declaration* EOF ;
declaration → classDecl
| funDecl
| varDecl
| statement ;
classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )?
"{" function* "}" ;
funDecl → "fun" function ;
varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;
statement → exprStmt
| forStmt
| ifStmt
| printStmt
| returnStmt
| whileStmt
| block ;
exprStmt → expression ";" ;
forStmt → "for" "(" ( varDecl | exprStmt | ";" )
expression? ";"
expression? ")" statement ;
ifStmt → "if" "(" expression ")" statement
( "else" statement )? ;
printStmt → "print" expression ";" ;
returnStmt → "return" expression? ";" ;
whileStmt → "while" "(" expression ")" statement ;
block → "{" declaration* "}" ;`
Note that block
is a statement rule, but is also used as a nonterminal in a couple of other rules for things like function bodies.
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 → "true" | "false" | "nil" | "this"
| NUMBER | STRING | IDENTIFIER | "(" expression ")"
| "super" "." IDENTIFIER ;
Keep the other rules cleaner.
function → IDENTIFIER "(" parameters? ")" block ;
parameters → IDENTIFIER ( "," IDENTIFIER )* ;
arguments → expression ( "," expression )* ;
NUMBER → DIGIT+ ( "." DIGIT+ )? ;
STRING → "\"" <any char except "\"">* "\"" ;
IDENTIFIER → ALPHA ( ALPHA | DIGIT )* ;
ALPHA → "a" ... "z" | "A" ... "Z" | "_" ;
DIGIT → "0" ... "9" ;
20230830
: Lexer done!- Added support for C-style
/* ... */
block comments (challenge problem!)
- Added support for C-style
20230831
: Building AST with Visitor Pattern20230901
Expression parser done!- TODO: challenge problems --
comma
operator support, Ternary?:
operator support
- TODO: challenge problems --
20230902
: Expression evaluator done.20230903
: Take #1 -- statement parsing and evaluation done. Global variable handling is done.20220904
: Take #2 -- variable assignment done. Next TODO: Scoping.20230908
: Take #3 -- completed statement execution with scoping.- TODO: Fix REPL to execute expressions and statements
- TODO: Fix implicit assignment to
nil
-- make it an error if a var is not assigned in a scope.
20230908
: Completed control flow (if
/else
,while
,for
)20230910
: Completed function decls and calls. Starting with closures, resolving and binding."20230910
: Closures work!20230912
: Scope resolution and semantic analysis works.- TODO: Extend the resolver to report an error if a local variable is never used.
20230912
:class
syntax works now!20230912
: Try #1 -- Supportingthis
not working yet!20230914
: Try #2 -- Fixedthis
.20230914
: Try #3 -- Addedinit
as a special method -- constructor (likepython
's__init__
). Still an error resolving this properly. Will come back to later to resolve the problem after inheritance.20230914
: Try #1 -- Added basic support for inheritance.20230914
: Try #2 -- Done!- TODO:
- Complete challenges.
- Remove all errors using LOX scripts from
lox-test