Esempio n. 1
0
void CompilationEngine::compileSubroutine()
{
    /* ('constructor' | 'function' | 'method')
       ('void' | type) subroutineName '(' parameterList ')'
       subroutineBody */

    tagNonTerminal("subroutineDec");

    if (jt.keyword() == Keyword::kCONSTRUCTOR)
        readKeyword("constructor", Keyword::kCONSTRUCTOR);
    else if (jt.keyword() == Keyword::kFUNCTION)
        readKeyword("function", Keyword::kFUNCTION);
    else
        readKeyword("method", Keyword::kMETHOD);

    nextToken();

    if (jt.tokenType() == TokenType::kKEYWORD && jt.keyword() == Keyword::kVOID)
        readKeyword("void", Keyword::kVOID);
    else
        readType();

    nextToken();
    readIdentifier();
    nextToken();
    readSymbol('(');
    nextToken();
    compileParameterList();
    readSymbol(')');
    nextToken();
    compileSubroutineBody();
    untagNonTerminal("subroutineDec");
}
void CompilationEng::compileSubroutine()
{
	while(true)
	{
		getToken();
		if(m_token == "}")
		{
			m_bPutback = true;
			break;
		}
		else
		{
			if(m_token == "constructor" || m_token == "function" || m_token == "method")
			{
				printNonterminal("subroutineDec");
				numOfTab++;
				printCurrentToken(false); //<keyword> constructor|function|method </keyword>
				printCurrentToken(true);  //<keyword> void|type </keyword>  void is also a keyword
				printCurrentToken(true);  //<symbol> subroutineName </keyword>
				printCurrentToken(true);  //<symbol> ( </symbol>
				compileParameterList();
				printCurrentToken(false); // <symbol> ) </symbol>
				compileSubroutineBody();
				numOfTab--;
				printNonterminal("subroutineDec",false); //end of subroutine
				}
				else
				{
					m_ofs << m_token << endl;
					break;
				}
			}
			
		}
}