diff --git a/asm.js b/asm.js index c687de7..87ce8fa 100644 --- a/asm.js +++ b/asm.js @@ -61,6 +61,7 @@ const Action = Object.freeze({ FUNC: 2, RESULT: 3, PARAM: 4, + SYMBOL: 5, }); const types = { @@ -99,8 +100,13 @@ class Parser { this.params = {}; } + integer(token) { + const x = parseInt(token); + return Number.isNaN(x) ? null : x; + } + translate_code(token) { - return opcodes[token] ?? parseInt(token); + return opcodes[token] ?? this.integer(token); } translate_type(token) { @@ -116,7 +122,10 @@ class Parser { return; } const code = this.translate_code(token); - return { type: Action.APPEND, code }; + if (code) + return { type: Action.APPEND, code }; + else + return { type: Action.SYMBOL, symbol: token }; } token_export(token) { @@ -155,7 +164,7 @@ class Parser { token_param_type(token) { if (token == LINE_END) { console.error( - "ERROR: Unexpected newline in .params: expected type"); + "ERROR: Unexpected newline in .param: expected type"); this.state = State.TOP; this.params = {}; } else { @@ -192,6 +201,7 @@ export class Assembler { [Action.FUNC]: (action) => this.action_func(action), [Action.RESULT]: (action) => this.action_result(action), [Action.PARAM]: (action) => this.action_param(action), + [Action.SYMBOL]: (action) => this.action_symbol(action), }; this.exports = []; @@ -225,6 +235,12 @@ export class Assembler { Object.assign(this.funcs[this.current_func].params, action.params); } + action_symbol(action) { + const func = this.funcs[this.current_func]; + const index = Object.keys(func.params).indexOf(action.symbol); + func.body.push(index); + } + push(chunk) { const text = this.decoder.decode(chunk, { stream: true }); for (const action of this.parser.handle(text))