Refactor (assembler): Define label prefix in a constant, for easy re-configuration
This commit is contained in:
parent
3fd862a55d
commit
4dda5881ef
12
assembler.js
12
assembler.js
|
|
@ -13,8 +13,10 @@ exports.assemble = (str, debug = false) => {
|
||||||
return decodeInstructions(str);
|
return decodeInstructions(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Configure pseudo-ops:
|
||||||
const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr';
|
const POINTER_TO_CURRENT_ADDR_PSEUDO_OPERAND = '*addr';
|
||||||
const CONSTANT_PREFIX = '#';
|
const CONSTANT_PREFIX = '#';
|
||||||
|
const LABEL_PREFIX = '@';
|
||||||
|
|
||||||
const mnemonicsWithOptionalArgs = ['end', 'nop'];
|
const mnemonicsWithOptionalArgs = ['end', 'nop'];
|
||||||
const mnemonics2opcodes = {
|
const mnemonics2opcodes = {
|
||||||
|
|
@ -60,10 +62,10 @@ function decodeInstructions(line) {
|
||||||
// HANDLE OPS
|
// HANDLE OPS
|
||||||
|
|
||||||
// Handle label definitions
|
// Handle label definitions
|
||||||
if (line.startsWith('@')) {
|
if (line.startsWith(LABEL_PREFIX)) {
|
||||||
// TODO: validate label
|
// TODO: validate label
|
||||||
// validateLabel(line);
|
// validateLabel(line);
|
||||||
let label = line.substring(1); // strip '@'
|
let label = line.substring(1); // strip label prefix
|
||||||
|
|
||||||
if (label in labels) {
|
if (label in labels) {
|
||||||
labels[label].pointsToByte = IP;
|
labels[label].pointsToByte = IP;
|
||||||
|
|
@ -112,9 +114,9 @@ function decodeInstructions(line) {
|
||||||
// HANDLE OPERANDS
|
// HANDLE OPERANDS
|
||||||
|
|
||||||
// Handle references to labels
|
// Handle references to labels
|
||||||
} else if (arg_str.startsWith('@')) {
|
} else if (arg_str.startsWith(LABEL_PREFIX)) {
|
||||||
// TODO: validate label // validateLabel(line);
|
// TODO: validate label // validateLabel(line);
|
||||||
let label = arg_str.substring(1); // strip '@'
|
let label = arg_str.substring(1); // strip label prefix
|
||||||
arg_num = 0;
|
arg_num = 0;
|
||||||
|
|
||||||
if (label in labels) {
|
if (label in labels) {
|
||||||
|
|
@ -189,7 +191,7 @@ function decodeInstructions(line) {
|
||||||
|
|
||||||
// Backfill label references
|
// Backfill label references
|
||||||
for (let k of Object.keys(labels)) {
|
for (let k of Object.keys(labels)) {
|
||||||
dbgGroup(2, `@${k}`);
|
dbgGroup(2, `${LABEL_PREFIX}${k}`);
|
||||||
let label = labels[k];
|
let label = labels[k];
|
||||||
dbg(2, `pointsToByte: ${label.pointsToByte}`);
|
dbg(2, `pointsToByte: ${label.pointsToByte}`);
|
||||||
dbg(2, `bytesToReplace: ${label.bytesToReplace}`);
|
dbg(2, `bytesToReplace: ${label.bytesToReplace}`);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue