package de.unitrier.st.uap.w25.tram;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LoggedMachine extends AbstractMachine {
    private static final Logger logger = LogManager.getLogger(LoggedMachine.class);
    private int stepCounter = -1;

    public LoggedMachine(Instruction[] prog) { super(prog); }
    public LoggedMachine(Instruction[] prog, Integer[] stack) { super(prog, stack); }

    @Override
    public void execute() {
        logger.trace(stepStr());
        while (!isHalted()) {
            executeStep();
        }
    }

    @Override
    public void executeStep() {
        super.executeStep();
        logger.trace(stepStr());
    }

    public String stepStr() {
        int step = ++stepCounter;
        String format = "Step %3d | TOP=%3d | PC=%3d | PP=%3d | FP=%3d | STACK=%s";
        return String.format(format, step, this.TOP, this.PC, this.PP, this.FP, stackStr());
    }
}
