static void FirstPass(string inputFile, SymbolTable symbolTable) { using (var parser = new Parser(inputFile)) { int counter = 0; while (parser.HasMoreCommands()) { parser.Advance(); if (parser.CommandType() == Parser.L_COMMAND) { symbolTable.AddEntry(parser.GetSymbol(), counter); } else { counter++; } } } }
static void MainProcess(StreamWriter writer, string inputFile, SymbolTable symbolTable) { using (var parser = new Parser(inputFile)) { string line = ""; int counter = 16; while (parser.HasMoreCommands()) { parser.Advance(); if (parser.CommandType() == Parser.A_COMMAND) { int address = 0; string symbol = parser.GetSymbol(); if (!Int32.TryParse(symbol, out address)) { if (symbolTable.Contains(symbol)) { address = symbolTable.GetAddress(parser.GetSymbol()); } else { address = counter++; symbolTable.AddEntry(symbol, address); } } line = Convert.ToString(address, 2).PadLeft(16, '0'); } else if (parser.CommandType() == Parser.C_COMMAND) { var dest = Code.Dest(parser.GetDest()); var comp = Code.Comp(parser.GetComp()); var jump = Code.Jump(parser.GetJump()); line = "111" + comp + dest + jump; } else { continue; // skip labels } writer.WriteLine(line); } } }