Untitled

mail@pastecode.io avatar
unknown
java
14 days ago
4.0 kB
1
Indexable
Never
package org.opcode.service;

import org.opcode.model.Register;
import org.opcode.model.RegisterState;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class OpcodeSimulatorImpl implements OpcodeSimulator {

    private Map<Character, Register> registerMap;

    public OpcodeSimulatorImpl() {
        initializeRegisters();
    }

    private void initializeRegisters() {
        registerMap = new HashMap<>();
        registerMap.put('A', new Register('A'));
        registerMap.put('B', new Register('B'));
        registerMap.put('C', new Register('C'));
        registerMap.put('D', new Register('D'));
    }

    @Override
    public RegisterState execute(List<String> instructions) {
        for (String instruction : instructions) {
            String[] parts = instruction.split(" ");
            String command = parts[0];

            switch (command) {
                case "SET":
                    setRegister(parts[1].charAt(0), Integer.parseInt(parts[2]));
                    break;
                case "ADD":
                    addToRegister(parts[1].charAt(0), Integer.parseInt(parts[2]));
                    break;
                case "ADR":
                    addRegisters(parts[1].charAt(0), parts[2].charAt(0));
                    break;
                case "MOV":
                    moveRegister(parts[1].charAt(0), parts[2].charAt(0));
                    break;
                case "INR":
                    incrementRegister(parts[1].charAt(0));
                    break;
                case "DCR":
                    decrementRegister(parts[1].charAt(0));
                    break;
                case "RST":
                    resetRegisters();
                    break;
                default:
                    throw new UnsupportedOperationException("Unsupported operation: " + command);
            }
        }

        return new RegisterState(new ArrayList<>(registerMap.values()));
    }

    private void setRegister(char register, int value) {
        registerMap.get(register).setValue(value);
    }

    private void addToRegister(char register, int value) {
        Register reg = registerMap.get(register);
        reg.setValue(reg.getValue() + value);
    }

    private void addRegisters(char register1, char register2) {
        Register reg1 = registerMap.get(register1);
        Register reg2 = registerMap.get(register2);
        reg1.setValue(reg1.getValue() + reg2.getValue());
    }

    private void moveRegister(char dest, char src) {
        Register regSrc = registerMap.get(src);
        registerMap.get(dest).setValue(regSrc.getValue());
    }

    private void incrementRegister(char register) {
        Register reg = registerMap.get(register);
        reg.setValue(reg.getValue() + 1);
    }

    private void decrementRegister(char register) {
        Register reg = registerMap.get(register);
        reg.setValue(reg.getValue() - 1);
    }

    private void resetRegisters() {
        for (Register register : registerMap.values()) {
            register.setValue(0);
        }
    }

    public static void main(String[] args) {
        // Initialize the simulator
        OpcodeSimulator simulator = new OpcodeSimulatorImpl();

        // Define the instructions to be executed
        List<String> instructions = new ArrayList<>();
        instructions.add("SET A 10");
        instructions.add("ADD A 12");
        instructions.add("INR C");
        instructions.add("DCR A");
        instructions.add("RST");

        // Execute the instructions
        RegisterState state = simulator.execute(instructions);

        // Print the final state of the registers
        System.out.println("Register A: " + state.getRegister('A').getValue());
        System.out.println("Register B: " + state.getRegister('B').getValue());
        System.out.println("Register C: " + state.getRegister('C').getValue());
        System.out.println("Register D: " + state.getRegister('D').getValue());
    }
}
Leave a Comment