Skip to content

Commit 5a9dd20

Browse files
committed
refactor: simplify compile.ts by merging loops and reducing redundancy
1 parent be675ab commit 5a9dd20

1 file changed

Lines changed: 19 additions & 46 deletions

File tree

src/compile.ts

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -80,107 +80,81 @@ export function compile<TResult>(
8080
): CompiledData {
8181
const { shortCircuit = true } = options;
8282

83-
// 序列化:支持 Proxy, Object, Array 和原始值
8483
const ast = serializeArgumentToAST(expression);
8584

8685
// 建立变量名到索引的映射
8786
const variableOrder: string[] = [];
8887
const variableToIndex = new Map<string, number>();
88+
const descToName = new Map<string, string>();
8989

90-
for (const name of Object.keys(variables)) {
90+
for (const [name, value] of Object.entries(variables)) {
9191
if (!variableToIndex.has(name)) {
9292
variableToIndex.set(name, variableOrder.length);
9393
variableOrder.push(name);
9494
}
95-
}
96-
97-
// 建立 Symbol description -> 变量名 映射(用于占位符替换)
98-
const descToName = new Map<string, string>();
99-
for (const [name, value] of Object.entries(variables)) {
10095
const id = getVariableId(value);
101-
if (id && id.description) {
96+
if (id?.description) {
10297
descToName.set(id.description, name);
10398
}
10499
}
105100

106101
// 转换 AST:将占位符替换为变量名,然后替换为 $N
107102
const undefinedVars: string[] = [];
108103
const transformed = transformIdentifiers(ast, (name) => {
109-
// 1. 如果是占位符,先替换为变量名
104+
// 占位符替换为变量名
110105
const placeholderMatch = name.match(/^\$\$VAR_(.+)\$\$$/);
111106
if (placeholderMatch) {
112-
const desc = placeholderMatch[1];
113-
const varName = descToName.get(desc!);
114-
if (!varName) {
115-
throw new Error(`Unknown variable placeholder: ${name}`);
116-
}
107+
const varName = descToName.get(placeholderMatch[1]!);
108+
if (!varName) throw new Error(`Unknown variable placeholder: ${name}`);
117109
name = varName;
118110
}
119111

120-
// 2. 将变量名替换为 $N
112+
// 变量名替换为 $N
121113
const index = variableToIndex.get(name);
122-
if (index !== undefined) {
123-
return `$${index}`;
124-
}
114+
if (index !== undefined) return `$${index}`;
125115

126-
// 3. 检查是否为允许的全局对象
127-
if (!ALLOWED_GLOBALS.has(name)) {
128-
undefinedVars.push(name);
129-
}
116+
// 检查是否为允许的全局对象
117+
if (!ALLOWED_GLOBALS.has(name)) undefinedVars.push(name);
130118
return name;
131119
});
132120

133121
if (undefinedVars.length > 0) {
134-
throw new Error(`Undefined variable(s): ${[...new Set(undefinedVars)].join(", ")}`);
122+
const uniqueVars = [...new Set(undefinedVars)];
123+
throw new Error(`Undefined variable(s): ${uniqueVars.join(", ")}`);
135124
}
136125

137126
// 生成编译后的表达式
138127
const expressions: CompiledExpression[] = [];
139128

140129
if (shortCircuit) {
141-
// 短路求值模式:生成控制流节点
142130
let nextIndex = variableOrder.length;
143131

144132
function compileAst(node: ASTNode): number {
145-
// 检查是否需要短路处理
146133
if (node.type === "BinaryExpr" && (node.operator === "||" || node.operator === "&&" || node.operator === "??")) {
147134
return compileShortCircuit(node);
148135
}
149-
150136
if (node.type === "ConditionalExpr") {
151137
return compileConditional(node);
152138
}
153-
154-
// 普通表达式:直接生成
155139
const exprStr = generate(node);
156-
const idx = nextIndex++;
157140
expressions.push(exprStr);
158-
return idx;
141+
return nextIndex++;
159142
}
160143

161144
function compileShortCircuit(node: ASTNode & { type: "BinaryExpr" }): number {
162-
// 递归编译左操作数
163145
const leftIdx = compileAst(node.left);
164146

165-
// 生成跳转条件
166-
let branchCondition: string;
167-
switch (node.operator) {
168-
case "||":
169-
branchCondition = `$${leftIdx}`;
170-
break;
171-
case "&&":
172-
branchCondition = `!$${leftIdx}`;
173-
break;
174-
default:
175-
branchCondition = `$${leftIdx}!=null`;
176-
}
147+
const branchConditions: Record<string, string> = {
148+
"||": `$${leftIdx}`,
149+
"&&": `!$${leftIdx}`,
150+
"??": `$${leftIdx}!=null`,
151+
};
177152

178153
const branchIdx = expressions.length;
179-
expressions.push(["br", branchCondition, 0] as BranchNode);
154+
expressions.push(["br", branchConditions[node.operator], 0] as BranchNode);
180155
nextIndex++;
181156

182157
compileAst(node.right);
183-
184158
const skipCount = expressions.length - branchIdx - 1;
185159
(expressions[branchIdx] as BranchNode)[2] = skipCount;
186160

@@ -217,7 +191,6 @@ export function compile<TResult>(
217191

218192
compileAst(transformed);
219193
} else {
220-
// 原始模式:直接生成表达式字符串
221194
expressions.push(generate(transformed));
222195
}
223196

0 commit comments

Comments
 (0)