- Scanner
- Print Abstract Syntax Tree (AST)
- Parser
- Variable Resolution Pass
- Expressions & Arithmetic
- Branching & Looping
- Variables, Functions & Function calls
- Parameter binding & Function return statements
- Classes, Inheritance, Instance variables, & Methods
- Ability to evaluate string interpolation
- Add support for the C-style conditional or “ternary” operator
?:
.
class Cake {
taste() {
var adjective = "delicious";
print "The " + this.flavor + " cake is " + adjective + "!";
}
}
var cake = Cake();
cake.flavor = "German chocolate";
cake.taste(); // Prints "The German chocolate cake is delicious!".
program → declaration* EOF ;
declaration → classDecl
| funDecl
| varDecl
| statement ;
classDecl → "class" IDENTIFIER ( "<" IDENTIFIER )? "{" function* "}" ;
funDecl → "fun" function ;
function → IDENTIFIER "(" parameters? ")" block ;
parameters → IDENTIFIER ( "," IDENTIFIER )* ;
varDecl → "var" IDENTIFIER ( "=" expression )? ";" ;
statement → exprStmt
| forStmt
| ifStmt
| printStmt
| returnStmt
| whileStmt
| block ;
block → "{" declaration* "}" ;
ifStmt → "if" "(" expression ")" statement
( "else" statement )? ;
whileStmt → "while" "(" expression ")" statement ;
forStmt → "for" "(" ( varDecl | exprStmt | ";" )
expression? ";"
expression? ")" statement ;
exprStmt → expression ";" ;
printStmt → "print" expression ";" ;
returnStmt → "return" expression? ";" ;
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 )* ;
arguments → expression ( "," expression )* ;
primary → "true" | "false" | "nil"
| NUMBER | STRING | IDENTIFIER
| "(" expression ")"
| "super" "." IDENTIFIER ;
- Crafting Interpreters by Robert Nystrom
- Building Recursive Descent Parsers by Supriyo Biswas