classFile
importDec* class+
importDec
'import' classPath ';'
class
'class' className '{' classVarDec* subroutineDec* '}'
classVarDec
'static' type varName (',' varName)* ';'
'field' type varName (',' varName)* ';'
type
'int' | 'char' | 'boolean' | className
suborutineDec
('constructor' | 'function' | 'method' ) ('void' | type) subroutineName '(' parameterList ')' subroutineBody
parameterList
((type varName) (',' type varName)*)?
subroutineBody
'{' statements '}'
statements
statement*
statement
letStatement | ifStatement | whileStatement | doStatement | returnStatement
letStatement
'let' varName ('[' expression ']')? '=' expression ';'
ifStatement
'if' '(' expression ')' '{' statements '}'
varDecStatement
'var' type varName (',' varName)* ';'
whileStatement
'while' '(' expression ')' '{' statements '}'
doStatement
'do' subroutineCall ';'
returnStatement
'return' expression? ';'
expression
term (op term)*
term
integerConstant | stringConstant | keywordConstant | charConstant | varName | varName '[' expression ']' | subroutineCall | '(' expression ')' | unaryOp term
subroutineCall
subroutineName '(' expressionList ')'
(className | varName) '.' subroutineName '(' expressionList ')'
expressionList
(expression (',' expression)*)?
op
'+' | '-' | '*' | '/' | '&' | '&&' | '|' | '||' | '<' | '<=' | '>' | '>=' | '==' | '!='
unaryOp
'-' | '~'
keywordConstant
'true' | 'false' | 'this' | 'null'
- function Array new(int size);
- method char charAt(int index);
- method int length();
- function int readInt();
- function String readLn();
- function void printLn(JackObject object);
基本的语法参见Example.jack
运行解释器的方法:
java Main Example.jack