assembler - Fix 'Too many arguments' error so that it works for normal lines as well as lines with `*` (which might include an optional 2nd arg)

This commit is contained in:
n loewen 2023-08-24 16:32:29 +01:00
parent d30050a292
commit 1bf2144a67
1 changed files with 16 additions and 3 deletions

View File

@ -99,8 +99,19 @@ function preparseSourceCode(source) {
info.argument = op_arg_array[1]; info.argument = op_arg_array[1];
info.extraArgument = op_arg_array[2]; info.extraArgument = op_arg_array[2];
} }
if (op_arg_array.length > 3) { // If there's too many arguments, throw an error
console.error('Error on line ${info.number}: Too many arguments'); // NB. there's a special case:
// lines with the ASM_IP_LABEL can take an extra argument
let maxArgs = 2;
if (op_arg_array.length > 2 && op_arg_array[1].startsWith(ASM_IP_LABEL)) {
maxArgs = 3;
}
if (op_arg_array.length > maxArgs) {
console.error();
console.error(`Error: Too many arguments`);
console.error(` at line ${info.number}`);
console.error();
throw new Error("Too many arguments");
} }
} }
return info; return info;
@ -226,7 +237,9 @@ function decodeInstructions(source) {
// then it's an error // then it's an error
if (!line.operation.startsWith('@')) { if (!line.operation.startsWith('@')) {
if (mnemonicsWithOptionalArgs.indexOf(line.operation.toLowerCase()) < 0) { if (mnemonicsWithOptionalArgs.indexOf(line.operation.toLowerCase()) < 0) {
console.error(`Missing operand for line ${line.number}: ${line.source}`); console.error('');
console.error(`Error: Missing operand ${line.source}`);
console.error(` at line ${line.number}`);
console.error(''); console.error('');
throw new Error("Missing operand"); throw new Error("Missing operand");
} else { } else {