Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created June 3, 2023 11:27
Show Gist options
  • Save evaporei/090f889656f127af0442086a28cef4b1 to your computer and use it in GitHub Desktop.
Save evaporei/090f889656f127af0442086a28cef4b1 to your computer and use it in GitHub Desktop.
Jack grammar
--- program structure ---
// a Jack program is a collection of files, each should represent a class like the one below
class = "class" className '{' classVarDec* fnDec* '}' ;
classVarDec = ("static" | "field") type varName (',' varName)* ';' ;
fnDec = ("constructor" | "function" | "method") ("void" | type) fnName '(' paramList ')' fnBody ;
paramList = ( (type varName) (',' type varName)* )? ;
fnBody = '{' varDec* statements '}' ;
varDec = "var" type varName (',' type varName)* ';' ;
type = primitive | className ;
primitive = "int" | "char" | "boolean" ;
--- statements ---
statements = statement* ;
statement = let | if | while | do | return ;
let = "let" varName ('[' expression ']')? '=' expression ';' ;
if = "if" '(' expression ')' '{' statements '}' ("else" '{' statements '}')? ;
while = "while" '(' expression ')' '{' statements '}' ;
do = "do" fnCall ';' ;
return = "return" expression? ';' ;
--- expressions ---
expression = equality ;
equality = comparison ( ('=') comparison )* ;
comparison = term ( ('<' | '>') term )* ;
term = unary ( ('+' | '-' | '*' | '/' | '&' | '|') unary )* ;
unary = ('-' | '~') unary | primary ;
primary = '(' expression ')'
| varName
| indexed
| fnCall
| literal ;
varName = IDENTIFIER ;
fnName = IDENTIFIER ;
className = IDENTIFIER ;
indexed = IDENTIFIER '[' expression ']' ;
fnCall = ( (className | varName) '.' )? fnName '(' exprList ')' ;
exprList = ( expression (',' expression)* )? ;
literal = STRING | INTEGER | "true" | "false" | "null" | "this" ;
@evaporei
Copy link
Author

evaporei commented Jun 3, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment