Skip to content

Commit e8f48d7

Browse files
committed
refactor(evaluate): remove unused V1 format code and simplify evaluator
Removed isV2Format detection function and legacy buildEvaluatorFunctionBody since V2 format is now the default. This simplifies the codebase and removes dead code paths.
1 parent 9396b0e commit e8f48d7

1 file changed

Lines changed: 2 additions & 39 deletions

File tree

src/evaluate.ts

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ import type { BranchNode, CompiledData, CompiledExpression, ControlFlowNode, Jum
55
*/
66
const evaluatorCache = new Map<string, (values: unknown[]) => unknown>();
77

8-
/**
9-
* 检测编译数据是否包含控制流节点(V2 格式)
10-
*/
11-
function isV2Format(expressions: CompiledExpression[]): boolean {
12-
return expressions.some((expr) => Array.isArray(expr));
13-
}
14-
158
/**
169
* 分析并验证控制流结构,将 br/jmp/phi 翻译为 if-else 语句
1710
*
@@ -198,10 +191,8 @@ export function evaluate<TResult = unknown>(data: CompiledData, values: Record<s
198191
let evaluator = evaluatorCache.get(cacheKey);
199192

200193
if (!evaluator) {
201-
// 根据格式选择合适的函数体构造器
202-
const functionBody = isV2Format(expressions)
203-
? buildEvaluatorFunctionBodyV2(expressions, variableNames.length)
204-
: buildEvaluatorFunctionBody(expressions as string[], variableNames.length);
194+
// 默认使用 V2 格式的函数体构造器
195+
const functionBody = buildEvaluatorFunctionBodyV2(expressions, variableNames.length);
205196
// eslint-disable-next-line @typescript-eslint/no-implied-eval
206197
evaluator = new Function("$values", functionBody) as (values: unknown[]) => unknown;
207198
evaluatorCache.set(cacheKey, evaluator);
@@ -216,34 +207,6 @@ export function evaluate<TResult = unknown>(data: CompiledData, values: Record<s
216207
}
217208
}
218209

219-
/**
220-
* 构造求值函数体
221-
*
222-
* @param expressions - 表达式列表
223-
* @param variableCount - 变量数量
224-
* @returns 函数体字符串
225-
*
226-
* @example
227-
* ```ts
228-
* buildEvaluatorFunctionBody(["$0+$1", "$2*2"], 2)
229-
* // 返回执行 $0+$1 并存储到 $values[2],然后执行 $2*2 的函数体
230-
* ```
231-
*/
232-
function buildEvaluatorFunctionBody(expressions: string[], variableCount: number): string {
233-
if (expressions.length === 0) throw new Error("No expressions to evaluate");
234-
235-
const lines = [
236-
...Array.from({ length: variableCount }, (_, i) => `const $${i} = $values[${i}];`),
237-
...expressions.map((expr, i) => {
238-
const idx = variableCount + i;
239-
return `const $${idx} = ${expr}; $values[${idx}] = $${idx};`;
240-
}),
241-
`return $values[$values.length - 1];`,
242-
];
243-
244-
return lines.join("\n");
245-
}
246-
247210
/**
248211
* 构造带控制流支持的求值函数体(V2 格式)
249212
*

0 commit comments

Comments
 (0)