public void compileLetStatement() { bool array = false; string currentInstruction; finalInstructions.Add("<letStatement>"); currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // let currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // count string valtoPop = currentInstruction; if (nextChar() == "[") { array = true; currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // [ finalInstructions.Add("<expression>"); compileExpression(); finalInstructions.Add("</expression>"); currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // ] currentVMWriter.WritePushValue(valtoPop); currentVMWriter.WriteArithmetic("+"); } currentInstruction = printAndAdvance("XML"); finalInstructions.Add(currentInstruction); // = finalInstructions.Add("<expression>"); compileExpression(); finalInstructions.Add("</expression>"); currentInstruction = printAndAdvance("XML"); if (array == true) { currentVMWriter.WritePop("temp", 0); currentVMWriter.WritePop("pointer", 1); currentVMWriter.WritePush("temp", "0"); currentVMWriter.WritePop("that", 0); } else { currentVMWriter.WritePopValue(valtoPop); } finalInstructions.Add(currentInstruction); // ; finalInstructions.Add("</letStatement>"); }
public void CompileLet() { jtoken.Advance(); string strVariableName = jtoken.Identifier(); jtoken.Advance(); bool array = false; if ((jtoken.TokenType().Equals("SYMBOL")) && (jtoken.Symbol() == '[')) { array = true; vmWriter.WritePush(symbolTable.KindOf(strVariableName), symbolTable.IndexOf(strVariableName)); CompileExpressionList(); jtoken.Advance(); if ((jtoken.TokenType().Equals("SYMBOL")) && (jtoken.Symbol() == ']')) { vmWriter.WriteArithmetic("add"); jtoken.Advance(); } } CompileExpressionList(); jtoken.Advance(); if (array) { vmWriter.WritePop("temp", 0); vmWriter.WritePop("pointer", 1); vmWriter.WritePush("temp", 0); vmWriter.WritePop("that", 0); } else { vmWriter.WritePop(symbolTable.KindOf(strVariableName), symbolTable.IndexOf(strVariableName)); } }