From 1bf2144a6703719c145828faa8e10071dabf9498 Mon Sep 17 00:00:00 2001 From: n loewen Date: Thu, 24 Aug 2023 16:32:29 +0100 Subject: [PATCH] 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) --- assembler.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/assembler.js b/assembler.js index ca998d2..1c5e0a7 100644 --- a/assembler.js +++ b/assembler.js @@ -99,8 +99,19 @@ function preparseSourceCode(source) { info.argument = op_arg_array[1]; info.extraArgument = op_arg_array[2]; } - if (op_arg_array.length > 3) { - console.error('Error on line ${info.number}: Too many arguments'); + // If there's too many arguments, throw an error + // 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; @@ -226,7 +237,9 @@ function decodeInstructions(source) { // then it's an error if (!line.operation.startsWith('@')) { 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(''); throw new Error("Missing operand"); } else {