Created
June 3, 2023 11:27
-
-
Save evaporei/090f889656f127af0442086a28cef4b1 to your computer and use it in GitHub Desktop.
Jack grammar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- 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" ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slide 42 https://drive.google.com/file/d/1CM_w6cxQpYnYHcP-OhNkNU6oD5rMnjzv/view