diff --git a/asm.js b/asm.js index 705f04a..97f62b7 100644 --- a/asm.js +++ b/asm.js @@ -104,6 +104,7 @@ const State = Object.freeze({ TABLE_SIZE: 32, ELEM_TABLE: 33, ELEM_ELEM: 34, + ELEM_LABEL: 35, }); const Action = Object.freeze({ @@ -260,6 +261,7 @@ class Parser { [State.TABLE_SIZE]: (token) => this.token_table_size(token), [State.ELEM_TABLE]: (token) => this.token_elem_table(token), [State.ELEM_ELEM]: (token) => this.token_elem_elem(token), + [State.ELEM_LABEL]: (token) => this.token_elem_label(token), }; this.results = []; @@ -813,6 +815,12 @@ class Parser { } this.elem.elem = token; + this.state = State.ELEM_LABEL; + } + + token_elem_label(token) { + if (token != LINE_END) + this.elem.label = token; const action = { type: Action.ELEM, elem: this.elem }; this.elem = undefined this.state = State.TOP; @@ -1055,12 +1063,14 @@ export class Assembler { action_elem(action) { const table = this.tables[action.elem.table]; - const index = Object.keys(this.funcs).indexOf(action.elem.elem); - if (index == -1) { + const fn = Object.keys(this.funcs).indexOf(action.elem.elem); + if (fn == -1) { console.error(`ERROR: ${action.elem.elem}: no such function`); return; } - table.elems.push(index); + const index = table.elems.push(fn) - 1; + if (action.elem.label) + this.defs[action.elem.label] = index; } push(chunk) {