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 fs = require('fs');
const argparser = require('./argparser/argparser.js');
const { logMemory } = require('./logging.js'); const { logMemory } = require('./logging.js');
const { num2hex, hex2num, bin2num } = require('./conversions.js'); const { num2hex, hex2num, bin2num } = require('./conversions.js');
const DBG = require('./dbg.js'); const DBG = require('./dbg.js');
@ -406,14 +407,39 @@ function stripWhitespaceFromEnds(line) {
// Initialize debugger // Initialize debugger
let dbg = new DBG('nitpick'); let dbg = new DBG('nitpick');
let returnOnStdout = true; // FIXME - set this using a CLI arg
if (returnOnStdout) { dbg = new DBG('none'); }
// Get input // Handle command-line options...
const filename = process.argv[2]; // FIXME - Get filename in a more robust way const args = argparser(process.argv);
const outputFilename = process.argv[3]; // FIXME - Get filename in a more robust way dbg.d('args', args);
const inputFile_str = fs.readFileSync(filename, 'utf8');
assemble(inputFile_str, outputFilename); 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. * both machine code and metadata is written to the output file.
* Otherwise, a string of decimal numbers is written. * Otherwise, a string of decimal numbers is written.
* @arg {string} sourceCode - Source code to assemble * @arg {string} sourceCode - Source code to assemble
* @arg {string} [outputFile='out.txt'] - Output file for machine code (and optional metadata) * @arg {string} [outputFile] - Output file for machine code (and optional metadata)
* @arg {boolean} [includeMetadata=false] - Include metadata for use when debugging using the simulator? * @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) { function assemble(sourceCode, outputFile='out.txt', includeMetadata=false) {
const out = decodeInstructions(sourceCode); const out = decodeInstructions(sourceCode);
@ -431,10 +457,7 @@ function assemble(sourceCode, outputFile='out.txt', includeMetadata=false) {
if (returnOnStdout) { if (returnOnStdout) {
const debugJSON = JSON.stringify(out); const debugJSON = JSON.stringify(out);
console.log(debugJSON); console.log(debugJSON);
return; } else if (!includeMetadata) {
}
if (!includeMetadata) {
const asciiMachineCode = out.machineCode.toString().replace(/,/g, ' '); const asciiMachineCode = out.machineCode.toString().replace(/,/g, ' ');
fs.writeFileSync(outputFile, asciiMachineCode); fs.writeFileSync(outputFile, asciiMachineCode);
} else { } else {