From 93f3dd1f416884f307e02efa529dcd650d54e0ff Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 14 Mar 2026 13:10:48 +0000 Subject: [PATCH] Implement .utf directive --- asm.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/asm.js b/asm.js index e79d62a..068e8eb 100644 --- a/asm.js +++ b/asm.js @@ -91,6 +91,7 @@ const State = Object.freeze({ AT_ADDR: 19, BYTE: 20, WORD: 21, + UTF8: 22, }); const Action = Object.freeze({ @@ -135,7 +136,8 @@ const const_opcodes = { }; class Parser { - constructor() { + constructor(encoder) { + this.encoder = encoder; this.tokens = []; this.tokenizer = new Tokenizer(); this.state = State.TOP; @@ -151,6 +153,7 @@ class Parser { ".at": State.AT_MEM, ".byte": State.BYTE, ".word": State.WORD, + ".utf8": State.UTF8, }; this.handlers = { [State.TOP]: (token) => this.token_top(token), @@ -175,6 +178,7 @@ class Parser { [State.AT_ADDR]: (token) => this.token_at_addr(token), [State.BYTE]: (token) => this.token_byte(token), [State.WORD]: (token) => this.token_word(token), + [State.UTF8]: (token) => this.token_utf8(token), }; this.results = []; @@ -499,6 +503,20 @@ class Parser { return action; } + token_utf8(token) { + if (token == LINE_END) { + this.state = State.TOP; + return; + } else if (token.string == undefined) { + console.error( + `ERROR: unexpected token ${token}, expected string`); + return; + } + const value = this.encoder.encode(token.string); + const action = { type: Action.DATA, size: value.length, value }; + return action; + } + mem_action() { const action = { type: Action.MEM, @@ -539,7 +557,7 @@ export class Assembler { constructor() { this.encoder = new TextEncoder("utf-8"); this.decoder = new TextDecoder("utf-8"); - this.parser = new Parser(); + this.parser = new Parser(this.encoder); this.handlers = { [Action.APPEND]: (action) => this.action_append(action), [Action.EXPORT]: (action) => this.action_export(action),