From 2972030d0ae834b1706e9eba16ebceacb0f45019 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 14 Mar 2026 13:48:07 +0000 Subject: [PATCH] Add .def support --- asm.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/asm.js b/asm.js index 47ac5d1..5a013d0 100644 --- a/asm.js +++ b/asm.js @@ -93,6 +93,8 @@ const State = Object.freeze({ WORD: 21, UTF8: 22, ALIGN: 23, + DEF_NAME: 24, + DEF_VALUE: 25, }); const Action = Object.freeze({ @@ -109,6 +111,7 @@ const Action = Object.freeze({ AT: 10, DATA: 11, ALIGN: 12, + DEF: 13, }); const types = { @@ -157,6 +160,7 @@ class Parser { ".word": State.WORD, ".utf8": State.UTF8, ".align": State.ALIGN, + ".def": State.DEF_NAME, }; this.handlers = { [State.TOP]: (token) => this.token_top(token), @@ -183,6 +187,8 @@ class Parser { [State.WORD]: (token) => this.token_word(token), [State.UTF8]: (token) => this.token_utf8(token), [State.ALIGN]: (token) => this.token_align(token), + [State.DEF_NAME]: (token) => this.token_def_name(token), + [State.DEF_VALUE]: (token) => this.token_def_value(token), }; this.results = []; @@ -538,6 +544,40 @@ class Parser { return action; } + token_def_name(token) { + if (token == LINE_END) { + console.error("ERROR: Unexpected end of line, expected name"); + this.state = State.TOP; + return; + } + this.def_name = token; + this.state = State.DEF_VALUE; + } + + token_def_value(token) { + if (token == LINE_END) { + console.error("ERROR: Unexpected end of line, expected value"); + this.def_name = undefined; + this.state = State.TOP; + return; + } + const value = this.integer(token); + if (value == null) { + console.error( + `ERROR: Unexpected token ${token}, expected value`); + this.def_name = undefined; + this.state = State.TOP; + return; + } + const action = { + type: Action.DEF, + def: { name: this.def_name, value }, + }; + this.def_name = undefined; + this.state = State.TOP; + return action; + } + mem_action() { const action = { type: Action.MEM, @@ -593,6 +633,7 @@ export class Assembler { [Action.AT]: (action) => this.action_at(action), [Action.DATA]: (action) => this.action_data(action), [Action.ALIGN]: (action) => this.action_align(action), + [Action.DEF]: (action) => this.action_def(action), }; this.exports = []; @@ -602,6 +643,7 @@ export class Assembler { this.globals = {}; this.pos = { mem: 0, addr: 0 }; this.data = []; + this.defs = {}; } action_append(action) { @@ -694,6 +736,10 @@ export class Assembler { } } + action_def(action) { + this.defs[action.def.name] = action.def.value; + } + push(chunk) { const text = this.decoder.decode(chunk, { stream: true }); for (const action of this.parser.handle(text))