所以,我目前正在尝试进入模拟器编程,我选择了 JavaScript,因为它是我完全流利的语言。
基本上我的问题归结为:我有一个数组,它是我的操作码表,因此它将我的函数绑定到一个数字。
例如:
function function_one() {
alert("hello");
}
function function_two() {
alert("World")
}
var optable = [
function_one,
function_two
];
optable[0](); //runs function_one()
optable[1](); //runs function_two();
现在,我想编写一个简单而原始的编译器,它使用汇编代码行的一部分,搜索此数组中的第一个字符串并返回 optable 中函数的索引,但我尝试的所有操作都只返回NaN. currentLine[] 编码如下: ["function_one", "foo", "bar"] (作为字符串,是的)
例如:
var currentLine = byLine[i].split(" ");
console.log(currentLine[0]);
currentProgram += optable.findIndex(currentLine[0]); //<-- NaN
console.log(currentProgram);
我知道这不是它的工作方式,因为 optable 数组包含函数并且我搜索一个字符串,但是我怎样才能让它工作?我有一些可以使用 eval() 的东西,但我已经可以看到这可能会导致什么......(安全等)
斯蒂芬大帝
qq_遁去的一_1
相关分类