Add support for labels

This commit is contained in:
2026-03-14 14:52:44 +00:00
parent cc51b2d7be
commit 22dc1fc0ca

8
asm.js
View File

@@ -112,6 +112,7 @@ const Action = Object.freeze({
DATA: 11,
ALIGN: 12,
DEF: 13,
LABEL: 14,
});
const types = {
@@ -224,6 +225,8 @@ class Parser {
this.state = state;
return;
}
if (token.endsWith(":"))
return { type: Action.LABEL, name: token.slice(0, -1) };
const code = this.translate_code(token);
if (code)
return { type: Action.APPEND, code };
@@ -630,6 +633,7 @@ export class Assembler {
[Action.DATA]: (action) => this.action_data(action),
[Action.ALIGN]: (action) => this.action_align(action),
[Action.DEF]: (action) => this.action_def(action),
[Action.LABEL]: (action) => this.action_label(action),
};
this.exports = [];
@@ -741,6 +745,10 @@ export class Assembler {
this.defs[action.def.name] = action.def.value;
}
action_label(action) {
this.defs[action.name] = this.pos.addr;
}
push(chunk) {
const text = this.decoder.decode(chunk, { stream: true });
for (const action of this.parser.handle(text))