1.核心流程
整体 paser + transformer(转化成目标ast) + gen
- Tokenizer(词法分析):字符串 → tokens[](paren / name / number / string)。
- Parser(语法分析):tokens → Lisp 风格 AST(根节点 Program,子树主要是 CallExpression + 字面量)。
- Transformer(变换):遍历旧 AST,边访问边往 _context 数组里“吐”出新 AST 节点 → 偏 JS/C 调用风格的 AST(callee/arguments、ExpressionStatement 包装等)。
- Code Generator(代码生成):新 AST → 目标代码字符串(这里是带分号的 C 风格调用)。
2. 语法解析器 核心
ts
// ...
while (
(token.type !== 'paren') ||
(token.type === 'paren' && token.value !== ')')
) {
// we'll call the `walk` function which will return a `node` and we'll
// push it into our `node.params`.
node.params.push(walk());
token = tokens[current];
}