From 3e32cb97e114046485dc48e95cf0ccfdc41e0a24 Mon Sep 17 00:00:00 2001 From: n loewen Date: Sat, 23 Sep 2023 20:20:42 -0700 Subject: [PATCH] cardiograph - Change to programmatically infer whether the input includes annotations or not --- src/cardiograph.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/cardiograph.js b/src/cardiograph.js index 47d89b0..e2fbff0 100755 --- a/src/cardiograph.js +++ b/src/cardiograph.js @@ -19,14 +19,9 @@ main(); async function main() { const opter = new Opter(); - opter.addOption('-d', '--debug'); opter.addOption('-i', '--in', false, true, 1); const opts = opter.parse(process.argv); - const sourcecodeHasAnnotations = 'debug' in opts; - // if true, then the input is JSON {sourceInfo, machineCode} - // otherwise, the input is a string of space-separated numbers - let input = null; if ('in' in opts) { // Read from file input = fs.readFileSync(opts.in[0], 'utf8'); @@ -36,16 +31,19 @@ async function main() { let code = null; let sourceInfo = null; - if (!sourcecodeHasAnnotations) { - code = new Uint8Array(input.split(' ')); - } else { + + try { const parsedInput = JSON.parse(input); - code = new Uint8Array(parsedInput.machineCode); sourceInfo = parsedInput.sourceInfo; + code = new Uint8Array(parsedInput.machineCode); + } catch (error) { + if (error.name === 'SyntaxError') { + code = new Uint8Array(input.split(' ')); + } } cpu.loadMemory(code); - if (sourcecodeHasAnnotations) { cpu.loadSourceInfo(sourceInfo); } + if (sourceInfo !== null) { cpu.loadSourceInfo(sourceInfo); } cpu.onCycleEnd(tick); cpu.onCycleEnd(logCPUState);