feat(clox): Debug trace options and toggles
feat(clox): Disassemble strings, improve debug print style
refactor(clox): Make Vm a non-static object
A simple language written from Crafting Interpreters
program = { declaration }, EOF;
declaration = ( classDecl | funDecl | varDecl | statement ), ";";
classDecl = "class", IDENTIFIER, [ "<", IDENTIFIER ], "{", { function }, "}";
funDecl = "fun", function;
varDecl = "var", IDENTIFIER, [ "=", expression ], ";";
statement = block | exprStmt | ifStmt | forStmt | printStmt | returnStmt | whileStmt;
function = IDENTIFIER, "(", [ parameters ], ")", block;
arguments = expression, { ",", expression };
parameters = IDENTIFIER, { ",", IDENTIFIER };
block = "{", { declaration }, "}";
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;
expression = assignment;
assignment = [ call, "." ], IDENTIFIER, "=", ( assignment | logicOr );
logicOr = logicAnd, { "or", logicAnd };
logicAnd = equality, { "and", equality };
equality = comparison, { ( "!=" | "==" ), comparison };
comparison = term, { ( ">" | ">=" | "<" | "<=" ), term };
term = factor, { ( "-" | "+" ), factor };
factor = unary, { ( "/" | "*" ), unary }; (* Same as [ factor, ( "/" | "*" ) ], unary; *)
unary = ( "!" | "-" ), unary | call;
call = primary, { ( "(", [ arguments ], ")" ) | ".", IDENTIFIER };
primary = IDENTIFIER | NUMBER | STRING
| "true" | "false" | "nil" | "this"
| ( "super", ".", IDENTIFIER ) | "(", expression, ")";
https://craftinginterpreters.com/parsing-expressions.html#ambiguity-and-the-parsing-game
Note that, just like in the EBNF grammar, upper rows have lower precedence:
Name | Operators | Associates |
---|---|---|
Equality | == != |
Left |
Comparison | > >= < <= |
Left |
Term | - + |
Left |
Factor | / * |
Left |
Unary | ! - |
Right |