|
1 | | -export namespace ProgramFunctions { |
2 | | - export const variables: Record<string, string> = {}; |
| 1 | +import { Project } from "@/core/Project"; |
3 | 2 |
|
| 3 | +export namespace ProgramFunctions { |
4 | 4 | /** |
5 | 5 | * 核心代码的获取变量值的方法 |
6 | 6 | * @param varName |
7 | 7 | * @returns |
8 | 8 | */ |
9 | | - export function getVarInCore(varName: string): string { |
10 | | - if (varName in variables) { |
11 | | - return variables[varName]; |
12 | | - } |
13 | | - return "NaN"; |
| 9 | + export function getVarInCore(project: Project, varName: string): string { |
| 10 | + return project.autoCompute.variables.get(varName) || "NaN"; |
14 | 11 | } |
15 | 12 |
|
16 | | - export function isHaveVar(varName: string): boolean { |
17 | | - return varName in variables; |
| 13 | + export function isHaveVar(project: Project, varName: string): boolean { |
| 14 | + return project.autoCompute.variables.has(varName); |
18 | 15 | } |
19 | 16 |
|
20 | 17 | /** |
21 | 18 | * 设置变量,变量名不能是逻辑节点名称 |
22 | 19 | * @param args |
23 | 20 | * @returns |
24 | 21 | */ |
25 | | - export function setVar(args: string[]): string[] { |
| 22 | + export function setVar(project: Project, args: string[]): string[] { |
26 | 23 | if (args.length !== 2) { |
27 | 24 | return ["error", "参数数量错误,必须保证两个"]; |
28 | 25 | } |
29 | 26 | const varName = args[0]; |
30 | | - if (!/^[a-zA-Z\u4e00-\u9fa5]/.test(varName)) { |
31 | | - return ["error", "变量名必须以字母或汉字开头"]; |
32 | | - } |
33 | | - variables[varName] = args[1]; |
| 27 | + project.autoCompute.variables.set(varName, args[1]); |
34 | 28 | return ["success"]; |
35 | 29 | } |
36 | 30 |
|
37 | 31 | /** |
38 | 32 | * 获取现存变量,如果没有,则返回NaN |
39 | 33 | * @param args |
40 | 34 | */ |
41 | | - export function getVar(args: string[]): string[] { |
| 35 | + export function getVar(project: Project, args: string[]): string[] { |
42 | 36 | if (args.length === 1) { |
43 | 37 | const varName = args[0]; |
44 | | - return [getVarInCore(varName)]; |
| 38 | + return [project.autoCompute.variables.get(varName) || "NaN"]; |
45 | 39 | } |
46 | 40 | return ["error", "参数数量错误"]; |
47 | 41 | } |
|
0 commit comments