~groctel/lox

A simple language written from Crafting Interpreters
feat(clox): Debug trace options and toggles
feat(clox): Disassemble strings, improve debug print style
refactor(clox): Make Vm a non-static object

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~groctel/lox
read/write
git@git.sr.ht:~groctel/lox

You can also use your local clone with git send-email.

#lox

A simple language written from Crafting Interpreters

#Grammar

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, ")";

#Operator precedence

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
Do not follow this link