Allow for blank lines and comment-only lines in assembly code
This commit is contained in:
parent
b90300a257
commit
afecab8ce3
|
|
@ -22,27 +22,39 @@ function decodeMultipleInstructions(str) {
|
||||||
let lines = str.split(/\n/); // returns an array of lines
|
let lines = str.split(/\n/); // returns an array of lines
|
||||||
let output = [];
|
let output = [];
|
||||||
lines.forEach( (l) => {
|
lines.forEach( (l) => {
|
||||||
let stripped = stripWhitespaceFromEnds(stripComments(l));
|
let decoded = decodeInstruction(l);
|
||||||
let decoded = decodeInstruction(stripped);
|
if (decoded) {
|
||||||
output.push(decoded.op);
|
output.push(decoded.op);
|
||||||
output.push(decoded.arg);
|
output.push(decoded.arg);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return new Uint8Array(output);
|
return new Uint8Array(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} line - A line of assembly code
|
||||||
|
* @returns {(object|false)} Either {op: machineOp, arg: 0}, or false if the line was blank
|
||||||
|
*/
|
||||||
function decodeInstruction(line) {
|
function decodeInstruction(line) {
|
||||||
let addressingMode = "direct"; // Must be "direct" or "indirect"
|
line = stripWhitespaceFromEnds(stripComments(line));
|
||||||
op_arg_array = line.split(" ");
|
let op_arg_array = line.split(" "); // split line into an array of [op, arg]
|
||||||
let opName = op_arg_array[0].toLowerCase();
|
let opName = op_arg_array[0].toLowerCase();
|
||||||
|
let addressingMode = 'direct'; // Must be "direct" or "indirect"
|
||||||
|
|
||||||
// Handle mnemonics without arguments (eg END)
|
// Handle blank lines and mnemonics without arguments (eg END)
|
||||||
if (op_arg_array.length < 2) { // No argument
|
if (op_arg_array.length < 2) { // No argument
|
||||||
|
// handle blank lines, or lines that just contain a comment:
|
||||||
|
if (line.length === 0) { return false; }
|
||||||
|
|
||||||
|
// handle mnemonics that aren't paired with an argument:
|
||||||
if (mnemonicsWithOptionalArgs.indexOf(opName) < 0) {
|
if (mnemonicsWithOptionalArgs.indexOf(opName) < 0) {
|
||||||
throw error("Missing opcode");
|
console.error(`Missing opcode: ${line}`);
|
||||||
|
throw new Error("Missing opcode");
|
||||||
}
|
}
|
||||||
let machineOp = mnemonics2opcodes[opName][addressingMode];
|
let machineOp = mnemonics2opcodes[opName][addressingMode];
|
||||||
return { op: machineOp, arg: 0 };
|
return { op: machineOp, arg: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle mnemonics with arguments (eg ADD $FF)
|
// Handle mnemonics with arguments (eg ADD $FF)
|
||||||
let arg_str = op_arg_array[1];
|
let arg_str = op_arg_array[1];
|
||||||
if (arg_str.startsWith("(")) {
|
if (arg_str.startsWith("(")) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue