Assembler { public static Assembly makeLabelMap(List<Instruction> instructions) { List<Instruction> updatedAssembly = new ArrayList<>(); Map<String, Long> labelMap = new LinkedHashMap<>(); long lineNumber = 0; for (Instruction instruction : instructions) { if (instruction.label != null) { labelMap.put(instruction.label, lineNumber); } if (instruction.opcode != null) { updatedAssembly.add(instruction); lineNumber++; } } return new Assembly(updatedAssembly, labelMap); } private  Assembler(); @Nullable static AssemblerConfig loadConfig(@Nullable Reader configReader); static List<String> assemble(@Nonnull String input, @Nullable AssemblerConfig config, @Nonnull PrintStream logger, boolean insertIOConfiguration); @Nonnull static List<Instruction> parseAssembly(@Nonnull String input); static List<Instruction> insertIOConfiguration(List<Instruction> instructions, AssemblerConfig config); static List<Instruction> processDollarLabelOperands(List<Instruction> instructions); static List<Instruction> processChanDirectives(List<Instruction> instructions, AssemblerConfig config); static List<Instruction> processDataDirectives(List<Instruction> instructions); static Assembly makeLabelMap(List<Instruction> instructions); static Map<String, Long> getConfigLabels(@Nonnull AssemblerConfig config); static Assembly patchLabels(Assembly assembly); static List<Byte> processIndirectInstruction(Instruction instruction); static List<Byte> processDirectInstructionWithLabelOperand(Instruction instruction, Map<String, Long> labelMap); static byte processDirectInstructionWithConstantOperand(Instruction instruction); static List<Instruction> prefix(Instruction instruction); static ParseTree parseExpression(String expression, Function<AssemblerExpressionParser, ParseTree> startRule); final static long maxInt; final static long minInt; final static Pattern inputComment; final static Pattern instructionPattern; final static Pattern directivePattern; final static Pattern dollarPattern; static Map<String, Opcode> opcodes; }
@Test public void testMakeLabelMap() { List<Instruction> instructions = Arrays.asList( new Instruction(InstructionType.INSTRUCTION, "init", Assembler.opcodes.get("ldc"), 89L, null, null, null, 1), new Instruction(InstructionType.INSTRUCTION, "~configuration", Assembler.opcodes.get("confio"), null, null, null, null, 2), new Instruction(InstructionType.INSTRUCTION, "alabel", Assembler.opcodes.get("ajw"), -10L, null, null, null, 3), new Instruction(InstructionType.INSTRUCTION, null, Assembler.opcodes.get("ldlp"), 0L, null, null, null, 4), new Instruction(InstructionType.INSTRUCTION, "justcomment", null, null, null, null, null, 5), new Instruction(InstructionType.INSTRUCTION, "jump", Assembler.opcodes.get("j"), null, "~configuration", null, null, 6), new Instruction(InstructionType.INSTRUCTION, "~end", null, null, null, "exit program", null, 7) ); Assembly labelMappedAssembly = Assembler.makeLabelMap(instructions); assertEquals(new Long(0), labelMappedAssembly.labelMap.get("init")); assertEquals(new Long(1), labelMappedAssembly.labelMap.get("~configuration")); assertEquals(new Long(2), labelMappedAssembly.labelMap.get("alabel")); assertEquals(new Long(4), labelMappedAssembly.labelMap.get("justcomment")); assertEquals(new Long(4), labelMappedAssembly.labelMap.get("jump")); assertEquals(new Long(5), labelMappedAssembly.labelMap.get("~end")); assertEquals(5, labelMappedAssembly.instructions.size()); }