module.exports = (() => {
const id = require('../apg-lib/identifiers');
let state = null;
const scan = (ruleCount, ruleDeps, index, isScanned) => {
let i = 0;
let j = 0;
const rdi = ruleDeps[index];
isScanned[index] = true;
const op = rdi.rule.opcodes;
for (i = 0; i < op.length; i += 1) {
const opi = op[i];
if (opi.type === id.RNM) {
rdi.refersTo[opi.index] = true;
if (!isScanned[opi.index]) {
scan(ruleCount, ruleDeps, opi.index, isScanned);
}
for (j = 0; j < ruleCount; j += 1) {
if (ruleDeps[opi.index].refersTo[j]) {
rdi.refersTo[j] = true;
}
}
} else if (opi.type === id.UDT) {
rdi.refersToUdt[opi.index] = true;
} else if (opi.type === id.BKR) {
if (opi.index < ruleCount) {
rdi.refersTo[opi.index] = true;
if (!isScanned[opi.index]) {
scan(ruleCount, ruleDeps, opi.index, isScanned);
}
} else {
rdi.refersToUdt[ruleCount - opi.index] = true;
}
}
}
};