public void compileSubroutineBody(string classType, string functionName, string functionType, int countParameters) { finalInstructions.Add("<subroutineBody>"); string currentInstruction; currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // { int varDecs = compileVarDec(); currentVMWriter.writeFunction(classType, functionName, varDecs); if (functionType == "constructor") { currentVMWriter.WritePush("constant", countParameters.ToString()); currentVMWriter.writeCall("Memory.alloc", 1); currentVMWriter.WritePop("pointer", 0); } if (functionType == "method") { currentVMWriter.WritePush("argument", "0"); currentVMWriter.WritePop("pointer", 0); } compileStatements(); currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // } finalInstructions.Add("</subroutineBody>"); }
public void CompileSubRoutine() { jtoken.Advance(); if (jtoken.Symbol() == ')' && jtoken.TokenType().Equals("SYMBOL")) { return; } string strKeyword = ""; if (jtoken.KeyWord().Equals("function") || jtoken.KeyWord().Equals("method") || jtoken.KeyWord().Equals("constructor")) { strKeyword = jtoken.KeyWord(); // new subroutine - reset symbol table symbolTable.startSubroutine(); if (jtoken.KeyWord().Equals("method")) { symbolTable.define("this", strClassName, "argument"); } jtoken.Advance(); } string strType = ""; if (jtoken.TokenType().Equals("KEYWORD") && jtoken.KeyWord().Equals("void")) { strType = "void"; jtoken.Advance(); } else if (jtoken.TokenType().Equals("KEYWORD") && (jtoken.KeyWord().Equals("int") || jtoken.KeyWord().Equals("boolean") || jtoken.KeyWord().Equals("char"))) { strType = jtoken.KeyWord(); jtoken.Advance(); } else { strType = jtoken.Identifier(); jtoken.Advance(); } if (jtoken.TokenType().Equals("IDENTIFIER")) { strSubRoutineName = jtoken.Identifier(); jtoken.Advance(); } if (jtoken.Symbol() == '(') { CompileParameterList(); } jtoken.Advance(); if (jtoken.Symbol() == '{') { jtoken.Advance(); } while (jtoken.KeyWord().Equals("var") && (jtoken.TokenType().Equals("KEYWORD"))) { jtoken.DecrementPointer(); CompileVarDec(); } string strFunction = ""; if (strClassName.Length != 0 && strSubRoutineName.Length != 0) { strFunction += strClassName + "." + strSubRoutineName; } vmWriter.WriteFunction(strFunction, symbolTable.varCount("var")); if (strKeyword.Equals("method")) { vmWriter.WritePush("argument", 0); vmWriter.WritePop("pointer", 0); } else if (strKeyword.Equals("constructor")) { vmWriter.WritePush("constant", symbolTable.varCount("field")); vmWriter.WriteCall("Memory.alloc", 1); vmWriter.WritePop("pointer", 0); } CompileStatements(); CompileSubRoutine(); }