cardiograph - Change to programmatically infer whether the input includes annotations or not

This commit is contained in:
n loewen 2023-09-23 20:20:42 -07:00
parent 91cba57aa1
commit 3e32cb97e1
1 changed files with 8 additions and 10 deletions

View File

@ -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);