// // varDec: 'var' type varName (',' varName)* ';' public void CompileVarDec(int depth) { // compile: 'var' var varToken = Eat("var"); // compile: type var typeToken = EatType(); // compile: varName var varNameToken = EatIdentifier(); // Add to symbol table SymbolTableManager.AddToSubroutineSymbolTable(new SymbolTableItem { Kind = VarKindType.LOCAL, Name = varNameToken.Value, Scope = VarScopeType.SUBROUTINE_LEVEL, Type = typeToken.Value }); // compile: (',' varName)* while (_tokenizer.CurrentToken.Value == ",") { // compile: ',' var commaToken = Eat(","); // compile varName varNameToken = EatIdentifier(); // Add to symbol table SymbolTableManager.AddToSubroutineSymbolTable(new SymbolTableItem { Kind = VarKindType.LOCAL, Name = varNameToken.Value, Scope = VarScopeType.SUBROUTINE_LEVEL, Type = typeToken.Value }); } // compile: ';' var semiColonToken = Eat(";"); }
// // ('constructor'|'function'|'method') ('void'|type) subtroutineName '(' parameterList ')' subroutineBody public void CompileSubroutineDec(int depth, string className) { // Reset SymbolTable SymbolTableManager.ResetSubroutineSymbolTable(); // compile: ('constructor'|'function'|'method') var subToken = EatSubroutine(); if (subToken.Value != "function" && subToken.Value != "constructor") { SymbolTableManager.AddToSubroutineSymbolTable(new SymbolTableItem { Name = "this", Type = className, Scope = VarScopeType.SUBROUTINE_LEVEL, Kind = VarKindType.ARGUMENT }); } // compile: ('void'|type) var subTypeToken = SoftEatType() ?? Eat("void"); // compile: subtroutineName var subNameToken = EatIdentifier(); // compile: '(' var leftParenToken = Eat("("); // compile: parameterList CompileParameterList(depth + 1); // compile: ')' var rightParenToken = Eat(")"); // compile: subroutineBody CompileSubroutineBody(depth + 1, subToken.Value, subNameToken.Value, className); }
// // ( (type varName) (',' type varName)* )? public void CompileParameterList(int depth) { bool commaEncountered = false; // compile: ( (type varName) (',' type varName)* )? while (_tokenizer.CurrentToken.GetKeywordType() == Types.KeywordType.BOOLEAN || _tokenizer.CurrentToken.GetKeywordType() == Types.KeywordType.INT || _tokenizer.CurrentToken.GetKeywordType() == Types.KeywordType.CHAR || _tokenizer.CurrentToken.TokenType == TokenType.IDENTIFIER || // Handle Array and Object types commaEncountered) { // compile: type var typeToken = EatType(); // compile: varName var varNameToken = EatIdentifier(); // Add to symbol table SymbolTableManager.AddToSubroutineSymbolTable(new SymbolTableItem { Kind = VarKindType.ARGUMENT, Scope = VarScopeType.SUBROUTINE_LEVEL, Type = typeToken.Value, Name = varNameToken.Value }); // compile: ',' if (_tokenizer.CurrentToken.Value == ",") { var commaToken = Eat(","); commaEncountered = true; continue; } else { commaEncountered = false; } break; } }