Assemble kernel on the client #1
46
asm.js
46
asm.js
@@ -93,6 +93,8 @@ const State = Object.freeze({
|
|||||||
WORD: 21,
|
WORD: 21,
|
||||||
UTF8: 22,
|
UTF8: 22,
|
||||||
ALIGN: 23,
|
ALIGN: 23,
|
||||||
|
DEF_NAME: 24,
|
||||||
|
DEF_VALUE: 25,
|
||||||
});
|
});
|
||||||
|
|
||||||
const Action = Object.freeze({
|
const Action = Object.freeze({
|
||||||
@@ -109,6 +111,7 @@ const Action = Object.freeze({
|
|||||||
AT: 10,
|
AT: 10,
|
||||||
DATA: 11,
|
DATA: 11,
|
||||||
ALIGN: 12,
|
ALIGN: 12,
|
||||||
|
DEF: 13,
|
||||||
});
|
});
|
||||||
|
|
||||||
const types = {
|
const types = {
|
||||||
@@ -157,6 +160,7 @@ class Parser {
|
|||||||
".word": State.WORD,
|
".word": State.WORD,
|
||||||
".utf8": State.UTF8,
|
".utf8": State.UTF8,
|
||||||
".align": State.ALIGN,
|
".align": State.ALIGN,
|
||||||
|
".def": State.DEF_NAME,
|
||||||
};
|
};
|
||||||
this.handlers = {
|
this.handlers = {
|
||||||
[State.TOP]: (token) => this.token_top(token),
|
[State.TOP]: (token) => this.token_top(token),
|
||||||
@@ -183,6 +187,8 @@ class Parser {
|
|||||||
[State.WORD]: (token) => this.token_word(token),
|
[State.WORD]: (token) => this.token_word(token),
|
||||||
[State.UTF8]: (token) => this.token_utf8(token),
|
[State.UTF8]: (token) => this.token_utf8(token),
|
||||||
[State.ALIGN]: (token) => this.token_align(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 = [];
|
this.results = [];
|
||||||
@@ -538,6 +544,40 @@ class Parser {
|
|||||||
return action;
|
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() {
|
mem_action() {
|
||||||
const action = {
|
const action = {
|
||||||
type: Action.MEM,
|
type: Action.MEM,
|
||||||
@@ -593,6 +633,7 @@ export class Assembler {
|
|||||||
[Action.AT]: (action) => this.action_at(action),
|
[Action.AT]: (action) => this.action_at(action),
|
||||||
[Action.DATA]: (action) => this.action_data(action),
|
[Action.DATA]: (action) => this.action_data(action),
|
||||||
[Action.ALIGN]: (action) => this.action_align(action),
|
[Action.ALIGN]: (action) => this.action_align(action),
|
||||||
|
[Action.DEF]: (action) => this.action_def(action),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.exports = [];
|
this.exports = [];
|
||||||
@@ -602,6 +643,7 @@ export class Assembler {
|
|||||||
this.globals = {};
|
this.globals = {};
|
||||||
this.pos = { mem: 0, addr: 0 };
|
this.pos = { mem: 0, addr: 0 };
|
||||||
this.data = [];
|
this.data = [];
|
||||||
|
this.defs = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
action_append(action) {
|
action_append(action) {
|
||||||
@@ -694,6 +736,10 @@ export class Assembler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
action_def(action) {
|
||||||
|
this.defs[action.def.name] = action.def.value;
|
||||||
|
}
|
||||||
|
|
||||||
push(chunk) {
|
push(chunk) {
|
||||||
const text = this.decoder.decode(chunk, { stream: true });
|
const text = this.decoder.decode(chunk, { stream: true });
|
||||||
for (const action of this.parser.handle(text))
|
for (const action of this.parser.handle(text))
|
||||||
|
|||||||
Reference in New Issue
Block a user