Add interpret command

This commit is contained in:
Jordan Eldredge 2019-06-23 16:59:11 -07:00
parent d940c7db14
commit 4bf7b41ca6
2 changed files with 67 additions and 1 deletions

View 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();

View file

@ -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",