mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-21 09:09:01 +00:00
Add interpret command
This commit is contained in:
parent
d940c7db14
commit
4bf7b41ca6
2 changed files with 67 additions and 1 deletions
65
experiments/maki-interpreter/cli.js
Normal file
65
experiments/maki-interpreter/cli.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { parse } = require("./");
|
||||
|
||||
function parseFile(relativePath) {
|
||||
const buffer = fs.readFileSync(path.join(__dirname, relativePath));
|
||||
return parse(buffer);
|
||||
}
|
||||
|
||||
class Group {}
|
||||
|
||||
const System = {
|
||||
getScriptGroup() {
|
||||
return new Group();
|
||||
}
|
||||
};
|
||||
|
||||
function interpret({ commands: rawCommands, variables, types }) {
|
||||
const commands = rawCommands.slice(0, 5);
|
||||
const environment = {
|
||||
System
|
||||
};
|
||||
const stack = [];
|
||||
let i = 0;
|
||||
while (i < commands.length) {
|
||||
const command = commands[i];
|
||||
switch (command.opcode) {
|
||||
// push
|
||||
case 1: {
|
||||
// What are these? Do they have names?
|
||||
const offsetIntoVariables = command.arguments[0];
|
||||
const variable = variables[offsetIntoVariables];
|
||||
console.log(variable);
|
||||
stack.push(variable);
|
||||
break;
|
||||
}
|
||||
// call
|
||||
case 24: {
|
||||
const funcDefinition = command.arguments[0];
|
||||
const { name } = funcDefinition;
|
||||
const variable = stack.pop();
|
||||
const type = types[variable.type];
|
||||
const obj = environment[type.name];
|
||||
stack.push(obj[name]());
|
||||
break;
|
||||
}
|
||||
// mov
|
||||
case 48: {
|
||||
const a = stack.pop();
|
||||
const b = stack.pop();
|
||||
stack.push(a);
|
||||
break;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
const relativePath = process.argv[2];
|
||||
const { commands, variables, types } = parseFile(relativePath);
|
||||
interpret({ commands, variables, types });
|
||||
}
|
||||
|
||||
main();
|
||||
|
|
@ -7,7 +7,8 @@
|
|||
"scripts": {
|
||||
"test": "jest -u",
|
||||
"tdd": "jest --watch",
|
||||
"decompile": "cd reference/maki_decompiler_1.1 && perl mdc.pl ../../fixtures/standardframe.maki"
|
||||
"decompile": "cd reference/maki_decompiler_1.1 && perl mdc.pl ../../fixtures/standardframe.maki",
|
||||
"interpret": "node ./cli.js ./fixtures/standardframe.maki"
|
||||
},
|
||||
"devDependencies": {
|
||||
"glob": "^7.1.4",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue