Add symbol resolution (params only)
This commit is contained in:
20
asm.js
20
asm.js
@@ -61,6 +61,7 @@ const Action = Object.freeze({
|
|||||||
FUNC: 2,
|
FUNC: 2,
|
||||||
RESULT: 3,
|
RESULT: 3,
|
||||||
PARAM: 4,
|
PARAM: 4,
|
||||||
|
SYMBOL: 5,
|
||||||
});
|
});
|
||||||
|
|
||||||
const types = {
|
const types = {
|
||||||
@@ -99,8 +100,13 @@ class Parser {
|
|||||||
this.params = {};
|
this.params = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
integer(token) {
|
||||||
|
const x = parseInt(token);
|
||||||
|
return Number.isNaN(x) ? null : x;
|
||||||
|
}
|
||||||
|
|
||||||
translate_code(token) {
|
translate_code(token) {
|
||||||
return opcodes[token] ?? parseInt(token);
|
return opcodes[token] ?? this.integer(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
translate_type(token) {
|
translate_type(token) {
|
||||||
@@ -116,7 +122,10 @@ class Parser {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const code = this.translate_code(token);
|
const code = this.translate_code(token);
|
||||||
|
if (code)
|
||||||
return { type: Action.APPEND, code };
|
return { type: Action.APPEND, code };
|
||||||
|
else
|
||||||
|
return { type: Action.SYMBOL, symbol: token };
|
||||||
}
|
}
|
||||||
|
|
||||||
token_export(token) {
|
token_export(token) {
|
||||||
@@ -155,7 +164,7 @@ class Parser {
|
|||||||
token_param_type(token) {
|
token_param_type(token) {
|
||||||
if (token == LINE_END) {
|
if (token == LINE_END) {
|
||||||
console.error(
|
console.error(
|
||||||
"ERROR: Unexpected newline in .params: expected type");
|
"ERROR: Unexpected newline in .param: expected type");
|
||||||
this.state = State.TOP;
|
this.state = State.TOP;
|
||||||
this.params = {};
|
this.params = {};
|
||||||
} else {
|
} else {
|
||||||
@@ -192,6 +201,7 @@ export class Assembler {
|
|||||||
[Action.FUNC]: (action) => this.action_func(action),
|
[Action.FUNC]: (action) => this.action_func(action),
|
||||||
[Action.RESULT]: (action) => this.action_result(action),
|
[Action.RESULT]: (action) => this.action_result(action),
|
||||||
[Action.PARAM]: (action) => this.action_param(action),
|
[Action.PARAM]: (action) => this.action_param(action),
|
||||||
|
[Action.SYMBOL]: (action) => this.action_symbol(action),
|
||||||
};
|
};
|
||||||
|
|
||||||
this.exports = [];
|
this.exports = [];
|
||||||
@@ -225,6 +235,12 @@ export class Assembler {
|
|||||||
Object.assign(this.funcs[this.current_func].params, action.params);
|
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) {
|
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