private Map<Integer, Integer> buildLoopIndex(List<Token> tokens) {
        Map<Integer, Integer> loopIndex = new HashMap<>();

        Stack<Integer> startIndices = new Stack<>();
        for (int i = 0; i < tokens.size(); i++) {
            switch (tokens.get(i).getType()) {
                case LOOP_START:
                    startIndices.push(i);
                    break;
                case LOOP_END:
                    if (startIndices.isEmpty()) {
                        throw new UnbalancedLoopException(i);
                    }

                    loopIndex.put(startIndices.pop(), i);
                    break;
            }
        }

        if (startIndices.size() > 0) {
            throw new UnbalancedLoopException(startIndices.pop());
        }

        return loopIndex;
    }