function lowerGCode(gcodeArray) {
let IR = [];
let opcodeRe = /(G[0-9]+|M[0-9]+)/;
let opXRe = /X(-?[0-9]+.[0-9]+)/;
let opYRe = /Y(-?[0-9]+.[0-9]+)/;
let opZRe = /Z(-?[0-9]+.[0-9]+)/;
let opFRe = /F(-?[0-9]+.[0-9]+)/;
let findOpcode = (instruction, argRe) => {
let maybeArgResults = instruction.match(argRe);
if (!maybeArgResults) {
return "";
}
return maybeArgResults[0];
};
let findArg = (instruction, argRe) => {
let maybeArgResults = instruction.match(argRe);
if (!maybeArgResults || maybeArgResults.length < 2) {
return null;
}
return parseFloat(maybeArgResults[1]) || null;
};
gcodeArray.forEach(function (instruction) {
if (!instruction || instruction[0] == "''") {
return;
}
let tokens = instruction.trim().split(" ");
let newPosition;
let opcode = findOpcode(tokens[0], opcodeRe);
if (opcode === "G0" || opcode === "G1") {
let opx = findArg(tokens[1], opXRe);
let opy = findArg(tokens[2], opYRe);
let opz = findArg(tokens[3], opZRe);
let opf = findArg(tokens[4], opFRe);
newPosition = new Op("move", opx, opy, opz, opf);
IR.push(newPosition);
}
});
return IR;
}