private void CompileSubroutineBody(string subroutineName, JackTokenizer.JackKeywordTypes subroutineKind) { xml += "<subroutineBody>\n"; // '{' varDec* statements '}' Nx().ExpectSymbol('{'); this.CompileVarDecs(subroutineName, subroutineKind); switch (subroutineKind) { case JackTokenizer.JackKeywordTypes.CONSTRUCTOR: this.vm.WritePush(VMWriter.Segment.CONST, this.symbolTable.VarCount(SymbolTable.Kind.FIELD)); this.vm.WriteCall("Memory.alloc", 1); this.vm.WritePop(VMWriter.Segment.POINTER, 0); break; case JackTokenizer.JackKeywordTypes.METHOD: this.vm.WritePush(VMWriter.Segment.ARG, 0); this.vm.WritePop(VMWriter.Segment.POINTER, 0); break; } this.CompileStatements(subroutineName); Nx().ExpectSymbol('}'); xml += "</subroutineBody>\n"; }
private void CompileVarDecs(string subroutineName, JackTokenizer.JackKeywordTypes subroutineKind) { while (Nx(increment: false).ExpectKeyword(JackTokenizer.JackKeywordTypes.VAR, throwException: false)) { this.CompileVarDec(subroutineName); } if (subroutineKind == JackTokenizer.JackKeywordTypes.CONSTRUCTOR) { } this.vm.WriteFunction($"{this.className}.{subroutineName}", this.symbolTable.VarCount(SymbolTable.Kind.VAR)); }
private void CompileClassVarDecs() { var expectedKeywords = new JackTokenizer.JackKeywordTypes[] { JackTokenizer.JackKeywordTypes.STATIC, JackTokenizer.JackKeywordTypes.FIELD }; while (Nx(increment: false).ExpectKeyword(expectedKeywords, throwException: false)) { this.CompileClassVarDec(); } }
private void CompileSubroutineDecs() { var expectedKeywords = new JackTokenizer.JackKeywordTypes[] { JackTokenizer.JackKeywordTypes.CONSTRUCTOR, JackTokenizer.JackKeywordTypes.FUNCTION, JackTokenizer.JackKeywordTypes.METHOD }; while (Nx(false).ExpectKeyword(expectedKeywords, throwException: false)) { this.CompileSubroutineDec(); } }
private void CompileStatements(string subroutineName) { xml += "<statements>\n"; var expectedKeywords = new JackTokenizer.JackKeywordTypes[] { JackTokenizer.JackKeywordTypes.LET, JackTokenizer.JackKeywordTypes.IF, JackTokenizer.JackKeywordTypes.WHILE, JackTokenizer.JackKeywordTypes.DO, JackTokenizer.JackKeywordTypes.RETURN }; while (Nx(increment: false).ExpectKeyword(expectedKeywords, throwException: false)) { this.CompileStatement(subroutineName); } xml += "</statements>\n"; }
public static bool ExpectKeyword(this JackTokenizer.ClassifiedJackToken token, JackTokenizer.JackKeywordTypes keyword, bool throwException = true) { if (token.TokenType() == JackTokenizer.JackTokenType.keyword && token.ParseKeyWord() == keyword) { return(true); } else { if (throwException) { throw new Exception("Expected keyword 'class' but found '" + token.value + "'."); } else { return(false); } } }