package fabrik.simulator.pic16f84;

import java.util.ArrayList;
import java.util.List;

public class ProgramStack {
    private static List<Integer> returnStack = new ArrayList<>(8);
    private static int stackIndex = 0;

    private static void incIndex(){
        if (stackIndex == 7){
            stackIndex = 0;
        }
        else{
            stackIndex++;
        }
    }

    public static void push(int value) {
        if ((returnStack.size() != 8 ) && (returnStack.size() <= stackIndex)){
            returnStack.add(value);
            incIndex();
        }
        else {
            returnStack.set(stackIndex, value);
            incIndex();
        }
    }

    public static int pop() {
        stackIndex =  (stackIndex > 0) ? --stackIndex : 7;
        return returnStack.get(stackIndex)-1;
    }

    public static int getStackPointer(){
        return stackIndex;
    }

    public static List<Integer> getStack() {
        return returnStack;
    }
}
