先定义语法与词法规则,通过 tokenizer 将源码转为 tokens,再由 parser 构建 AST,最后 evaluate 函数遍历 AST 执行指令,实现变量赋值、表达式计算与打印输出。

实现一个基于 JavaScript 的简单虚拟机或解释器,核心是定义语言的语法、解析代码并执行指令。不需要复杂的编译原理知识,借助 JavaScript 的灵活性,可以快速搭建一个可运行的小型解释器。重点在于理解词法分析、语法树构建和执行过程。
先设计一种极简语言,比如支持变量赋值、算术运算和打印语句。例如:
x = 10接下来进行词法分析(Tokenizer),把源码拆成一个个有意义的标记(tokens):
示例 tokenizer 函数:
立即学习“Java免费学习笔记(深入)”;
function tokenize(input) {
const tokens = [];
const regex = /\s*(\d+|[a-zA-Z_]\w*|=.|\+|\(|\)|;|\n)\s*/g;
let match;
while ((match = regex.exec(input))) {
const value = match[1];
if (value === '\n') {
tokens.push({ type: 'newline' });
} else if (/^[a-zA-Z_]\w*$/.test(value)) {
tokens.push({ type: 'identifier', value });
} else if (/^\d+$/.test(value)) {
tokens.push({ type: 'number', value: parseInt(value) });
} else if (value === '=' || value === '+' || value === '(' || value === ')') {
tokens.push({ type: 'operator', value });
} else if (value === 'print') {
tokens.push({ type: 'keyword', value });
}
}
return tokens;
}将 token 流转换为树形结构,便于后续遍历执行。比如上面的例子可生成如下结构:
{
type: "Program",
body: [
{ type: "Assignment", name: "x", value: { type: "Number", value: 10 } },
{ type: "Assignment", name: "y", value: { type: "BinaryExpression", left: { type: "Identifier", name: "x" }, operator: "+", right: { type: "Number", value: 5 } } },
{ type: "Print", argument: { type: "Identifier", name: "y" } }
]
}通过 parser 遍历 tokens 构建 AST:
function parse(tokens) {
let current = 0;
<p>function walk() {
let token = tokens[current];</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">if (token.type === 'identifier') {
// 可能是赋值或表达式
const name = token.value;
token = tokens[++current];
if (token && token.type === 'operator' && token.value === '=') {
current++;
const value = walk();
return {
type: 'Assignment',
name,
value
};
}
return { type: 'Identifier', name };
}
if (token.type === 'number') {
current++;
return { type: 'Number', value: token.value };
}
if (token.type === 'operator' && token.value === '+') {
current++;
return {
type: 'BinaryExpression',
left: { type: 'Identifier', name: tokens[current - 2].value },
operator: '+',
right: walk()
};
}
if (token.type === 'keyword' && token.value === 'print') {
current++;
const arg = walk();
return { type: 'Print', argument: arg };
}
throw new Error(`Unknown token: ${token.type}`);}
const ast = { type: 'Program', body: [] };
while (current < tokens.length) { const node = walk(); if (node) ast.body.push(node); }
return ast; }
解释器遍历 AST 并执行每个节点。需要维护一个变量环境(变量存储):
function evaluate(ast) {
const env = {};
<p>function exec(node) {
switch (node.type) {
case 'Program':
node.body.forEach(exec);
break;</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;"> case 'Assignment':
env[node.name] = exec(node.value);
break;
case 'Number':
return node.value;
case 'Identifier':
if (!(node.name in env)) throw new Error(`Undefined variable: ${node.name}`);
return env[node.name];
case 'BinaryExpression':
const left = exec(node.left);
const right = exec(node.right);
if (node.operator === '+') return left + right;
break;
case 'Print':
console.log(exec(node.argument));
break;
default:
throw new Error(`Unknown node type: ${node.type}`);
}}
exec(ast); }
将三部分串联起来:
const input = ` x = 10 y = x + 5 print(y) `; <p>const tokens = tokenize(input); const ast = parse(tokens); evaluate(ast); // 输出 15
基本上就这些。你可以在此基础上扩展功能:支持函数、条件语句、作用域等。关键是理清“输入 → 分词 → 构建树 → 执行”的流程。不复杂但容易忽略细节,比如 token 位置管理和错误提示。用 JavaScript 实现解释器门槛低,适合学习语言设计基础。
以上就是怎样实现一个基于JavaScript的简单虚拟机或解释器?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号