Refactor (assembler): Define label prefix in a constant, for easy re-configuration

This commit is contained in:
n loewen 2023-08-16 09:38:18 +01:00
parent 3fd862a55d
commit 4dda5881ef
1 changed files with 7 additions and 5 deletions

View File

@ -13,8 +13,10 @@ exports.assemble = (str, debug = false) => {
return decodeInstructions(str);
}
// Configure pseudo-ops:
const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr';
const CONSTANT_PREFIX = '#';
const LABEL_PREFIX = '@';
const mnemonicsWithOptionalArgs = ['end', 'nop'];
const mnemonics2opcodes = {
@ -60,10 +62,10 @@ function decodeInstructions(line) {
// HANDLE OPS
// Handle label definitions
if (line.startsWith('@')) {
if (line.startsWith(LABEL_PREFIX)) {
// TODO: validate label
// validateLabel(line);
let label = line.substring(1); // strip '@'
let label = line.substring(1); // strip label prefix
if (label in labels) {
labels[label].pointsToByte = IP;
@ -112,9 +114,9 @@ function decodeInstructions(line) {
// HANDLE OPERANDS
// Handle references to labels
} else if (arg_str.startsWith('@')) {
} else if (arg_str.startsWith(LABEL_PREFIX)) {
// TODO: validate label // validateLabel(line);
let label = arg_str.substring(1); // strip '@'
let label = arg_str.substring(1); // strip label prefix
arg_num = 0;
if (label in labels) {
@ -189,7 +191,7 @@ function decodeInstructions(line) {
// Backfill label references
for (let k of Object.keys(labels)) {
dbgGroup(2, `@${k}`);
dbgGroup(2, `${LABEL_PREFIX}${k}`);
let label = labels[k];
dbg(2, `pointsToByte: ${label.pointsToByte}`);
dbg(2, `bytesToReplace: ${label.bytesToReplace}`);