assembler - Change to use new argument parsing library

This commit is contained in:
n loewen 2023-09-02 13:40:56 -04:00
parent 16f85d3b9f
commit b3d10a4197
2 changed files with 37 additions and 14 deletions

@ -1 +1 @@
Subproject commit 216cdcae53e86d6908aa1b4d86549b1ca5e7bb53
Subproject commit 86e16a9855901b96d09d613f431fd7ee4690c6af

View File

@ -2,6 +2,7 @@
const fs = require('fs');
const argparser = require('./argparser/argparser.js');
const { logMemory } = require('./logging.js');
const { num2hex, hex2num, bin2num } = require('./conversions.js');
const DBG = require('./dbg.js');
@ -406,14 +407,39 @@ function stripWhitespaceFromEnds(line) {
// Initialize debugger
let dbg = new DBG('nitpick');
let returnOnStdout = true; // FIXME - set this using a CLI arg
if (returnOnStdout) { dbg = new DBG('none'); }
// Get input
const filename = process.argv[2]; // FIXME - Get filename in a more robust way
const outputFilename = process.argv[3]; // FIXME - Get filename in a more robust way
const inputFile_str = fs.readFileSync(filename, 'utf8');
assemble(inputFile_str, outputFilename);
// Handle command-line options...
const args = argparser(process.argv);
dbg.d('args', args);
if (typeof args.i === 'undefined' || args.i.length === 0) {
console.error('Input file required (-i prog.asm)');
process.exit();
}
const inputFilename = args.i[0];
const inputFile_str = fs.readFileSync(inputFilename, 'utf8');
if ('o' in args && args.o.length === 0) {
console.error('Missing output file name (-o prog.asm)');
process.exit();
}
let outputFilename = null;
let returnOnStdout = 'o' in args ? false : true;
if (!returnOnStdout) {
outputFilename = args.o[0];
} else {
dbg = new DBG('none');
}
let outputWithMetadata = false;
if ('d' in args || 'debug' in args) {
outputWithMetadata = true;
}
// TODO: maybe check for too many args?
assemble(inputFile_str, outputFilename, outputWithMetadata);
/**
@ -422,8 +448,8 @@ assemble(inputFile_str, outputFilename);
* both machine code and metadata is written to the output file.
* Otherwise, a string of decimal numbers is written.
* @arg {string} sourceCode - Source code to assemble
* @arg {string} [outputFile='out.txt'] - Output file for machine code (and optional metadata)
* @arg {boolean} [includeMetadata=false] - Include metadata for use when debugging using the simulator?
* @arg {string} [outputFile] - Output file for machine code (and optional metadata)
* @arg {boolean} [includeMetadata=false] - Include metadata when writing output to a file? (for use when debugging using the simulator)
**/
function assemble(sourceCode, outputFile='out.txt', includeMetadata=false) {
const out = decodeInstructions(sourceCode);
@ -431,10 +457,7 @@ function assemble(sourceCode, outputFile='out.txt', includeMetadata=false) {
if (returnOnStdout) {
const debugJSON = JSON.stringify(out);
console.log(debugJSON);
return;
}
if (!includeMetadata) {
} else if (!includeMetadata) {
const asciiMachineCode = out.machineCode.toString().replace(/,/g, ' ');
fs.writeFileSync(outputFile, asciiMachineCode);
} else {