Implement .utf directive
This commit is contained in:
22
asm.js
22
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),
|
||||
|
||||
Reference in New Issue
Block a user