@cloudpss/expression 0.6.0-alpha.9 → 0.6.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analyze.d.ts +6 -3
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +13 -33
- package/dist/analyze.js.map +1 -1
- package/dist/definitions/argument.d.ts +3 -11
- package/dist/definitions/argument.d.ts.map +1 -1
- package/dist/definitions/constraint.d.ts +2 -2
- package/dist/definitions/constraint.d.ts.map +1 -1
- package/dist/definitions/constraint.js +1 -1
- package/dist/definitions/constraint.js.map +1 -1
- package/dist/definitions/parameter-group.js +4 -4
- package/dist/definitions/parameter-group.js.map +1 -1
- package/dist/definitions/parameter.d.ts +22 -14
- package/dist/definitions/parameter.d.ts.map +1 -1
- package/dist/definitions/parameter.js +10 -1
- package/dist/definitions/parameter.js.map +1 -1
- package/dist/definitions/utils.d.ts +28 -0
- package/dist/definitions/utils.d.ts.map +1 -0
- package/dist/definitions/utils.js +272 -0
- package/dist/definitions/utils.js.map +1 -0
- package/dist/definitions/variable.js +1 -1
- package/dist/definitions/variable.js.map +1 -1
- package/dist/definitions.d.ts +1 -2
- package/dist/definitions.d.ts.map +1 -1
- package/dist/definitions.js +1 -1
- package/dist/definitions.js.map +1 -1
- package/dist/eval.d.ts +3 -5
- package/dist/eval.d.ts.map +1 -1
- package/dist/eval.js +12 -20
- package/dist/eval.js.map +1 -1
- package/dist/expression.d.ts +10 -4
- package/dist/expression.d.ts.map +1 -1
- package/dist/expression.js +6 -6
- package/dist/expression.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/interface.d.ts +30 -0
- package/dist/interface.d.ts.map +1 -0
- package/dist/interface.js +6 -0
- package/dist/interface.js.map +1 -0
- package/dist/main.d.ts +41 -28
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +165 -149
- package/dist/main.js.map +1 -1
- package/dist/migrate.d.ts +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +25 -4
- package/dist/migrate.js.map +1 -1
- package/dist/migrator/access.d.ts.map +1 -1
- package/dist/migrator/access.js +64 -29
- package/dist/migrator/access.js.map +1 -1
- package/dist/migrator/call.d.ts.map +1 -1
- package/dist/migrator/call.js +298 -201
- package/dist/migrator/call.js.map +1 -1
- package/dist/migrator/concat.d.ts.map +1 -1
- package/dist/migrator/concat.js +15 -2
- package/dist/migrator/concat.js.map +1 -1
- package/dist/migrator/function.d.ts +6 -0
- package/dist/migrator/function.d.ts.map +1 -0
- package/dist/migrator/function.js +25 -0
- package/dist/migrator/function.js.map +1 -0
- package/dist/migrator/interface.d.ts +3 -1
- package/dist/migrator/interface.d.ts.map +1 -1
- package/dist/migrator/interface.js.map +1 -1
- package/dist/migrator/node.d.ts.map +1 -1
- package/dist/migrator/node.js +55 -7
- package/dist/migrator/node.js.map +1 -1
- package/dist/migrator/operator.d.ts.map +1 -1
- package/dist/migrator/operator.js +107 -60
- package/dist/migrator/operator.js.map +1 -1
- package/dist/migrator/serialize.d.ts +4 -0
- package/dist/migrator/serialize.d.ts.map +1 -0
- package/dist/migrator/serialize.js +21 -0
- package/dist/migrator/serialize.js.map +1 -0
- package/dist/migrator/special.d.ts.map +1 -1
- package/dist/migrator/special.js +31 -0
- package/dist/migrator/special.js.map +1 -1
- package/dist/migrator/state.d.ts +4 -4
- package/dist/migrator/state.d.ts.map +1 -1
- package/dist/migrator/state.js +29 -31
- package/dist/migrator/state.js.map +1 -1
- package/dist/migrator/symbol.d.ts.map +1 -1
- package/dist/migrator/symbol.js +34 -12
- package/dist/migrator/symbol.js.map +1 -1
- package/dist/migrator/to-type.d.ts.map +1 -1
- package/dist/migrator/to-type.js +21 -4
- package/dist/migrator/to-type.js.map +1 -1
- package/dist/migrator/utils.d.ts +6 -0
- package/dist/migrator/utils.d.ts.map +1 -1
- package/dist/migrator/utils.js +49 -1
- package/dist/migrator/utils.js.map +1 -1
- package/dist/parser.d.ts +2 -2
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +27 -8
- package/dist/parser.js.map +1 -1
- package/dist/re-exports.d.ts +4 -0
- package/dist/re-exports.d.ts.map +1 -0
- package/dist/re-exports.js +3 -0
- package/dist/re-exports.js.map +1 -0
- package/dist/scope.d.ts +16 -17
- package/dist/scope.d.ts.map +1 -1
- package/dist/scope.js +84 -53
- package/dist/scope.js.map +1 -1
- package/dist/type.d.ts +22 -10
- package/dist/type.d.ts.map +1 -1
- package/dist/type.js +21 -24
- package/dist/type.js.map +1 -1
- package/package.json +8 -5
- package/src/analyze.ts +20 -39
- package/src/definitions/argument.ts +3 -13
- package/src/definitions/constraint.ts +3 -3
- package/src/definitions/parameter-group.ts +4 -4
- package/src/definitions/parameter.ts +47 -24
- package/src/definitions/utils.ts +288 -0
- package/src/definitions/variable.ts +1 -1
- package/src/definitions.ts +1 -28
- package/src/eval.ts +16 -25
- package/src/expression.ts +16 -8
- package/src/index.ts +3 -1
- package/src/interface.ts +35 -0
- package/src/main.ts +232 -200
- package/src/migrate.ts +27 -4
- package/src/migrator/access.ts +67 -37
- package/src/migrator/call.ts +287 -190
- package/src/migrator/concat.ts +15 -2
- package/src/migrator/function.ts +27 -0
- package/src/migrator/interface.ts +3 -1
- package/src/migrator/node.ts +56 -6
- package/src/migrator/operator.ts +110 -64
- package/src/migrator/serialize.ts +21 -0
- package/src/migrator/special.ts +31 -0
- package/src/migrator/state.ts +31 -34
- package/src/migrator/symbol.ts +33 -12
- package/src/migrator/to-type.ts +23 -4
- package/src/migrator/utils.ts +49 -1
- package/src/parser.ts +33 -8
- package/src/re-exports.ts +47 -0
- package/src/scope.ts +98 -62
- package/src/type.ts +40 -25
- package/tests/analyze.ts +45 -6
- package/tests/compile.ts +65 -0
- package/tests/condition.ts +33 -17
- package/tests/definition.ts +237 -18
- package/tests/eval-complex.ts +19 -11
- package/tests/eval.ts +59 -12
- package/tests/import.ts +21 -7
- package/tests/main.ts +58 -0
- package/tests/migrate.ts +317 -0
- package/tests/scope.ts +3 -3
- package/tests/template.ts +36 -0
- package/dist/context.d.ts +0 -41
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -18
- package/dist/context.js.map +0 -1
- package/src/context.ts +0 -54
package/src/main.ts
CHANGED
|
@@ -1,211 +1,297 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { klona } from 'klona';
|
|
5
|
-
import { Context, type Options } from './context.js';
|
|
1
|
+
import { isVmFunction, wrapToVmValue, type VmValue } from '@mirascript/mirascript';
|
|
2
|
+
import { serialize } from '@mirascript/mirascript/subtle';
|
|
3
|
+
import { defaultOptions, type Logger, type Options } from './interface.js';
|
|
6
4
|
import { parse } from './parser.js';
|
|
7
|
-
import { analyze } from './analyze.js';
|
|
5
|
+
import { analyze, type GlobalReferenceChain } from './analyze.js';
|
|
8
6
|
import {
|
|
9
7
|
type CompiledExpression,
|
|
10
|
-
Expression,
|
|
8
|
+
type Expression,
|
|
9
|
+
type ExpressionFunction,
|
|
11
10
|
type ExpressionOrValue,
|
|
11
|
+
type ExpressionSource,
|
|
12
12
|
ExpressionTag,
|
|
13
13
|
isExpression,
|
|
14
14
|
} from './expression.js';
|
|
15
15
|
import { evaluate } from './eval.js';
|
|
16
16
|
import { TypeInfo } from './type.js';
|
|
17
17
|
import { Scope } from './scope.js';
|
|
18
|
-
import
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
import {
|
|
19
|
+
argumentConverter,
|
|
20
|
+
choiceParameterType,
|
|
21
|
+
fillArgumentMap,
|
|
22
|
+
type ArgumentMap,
|
|
23
|
+
type ArgumentValue,
|
|
24
|
+
type Choice,
|
|
25
|
+
type ChoiceParameterType,
|
|
26
|
+
type ConditionExpression,
|
|
27
|
+
type Parameter,
|
|
28
|
+
type ParameterGroup,
|
|
29
|
+
type ParameterMap,
|
|
28
30
|
} from './definitions.js';
|
|
29
31
|
const { isArray } = Array;
|
|
30
|
-
const {
|
|
32
|
+
const { defineProperty } = Object;
|
|
33
|
+
|
|
34
|
+
/** 选项类型 */
|
|
35
|
+
export type EvaluatedChoices<T extends ChoiceParameterType> = {
|
|
36
|
+
[K in T]: ParameterMap[K] extends { choices?: infer C } ? (C extends readonly unknown[] ? C : never) : never;
|
|
37
|
+
}[T];
|
|
31
38
|
|
|
32
39
|
/**
|
|
33
40
|
* 表达式求值
|
|
34
41
|
*/
|
|
35
42
|
export class Evaluator {
|
|
36
|
-
constructor(options?: Partial<Options>) {
|
|
37
|
-
this.
|
|
38
|
-
}
|
|
39
|
-
/** 选项 */
|
|
40
|
-
get options(): Options {
|
|
41
|
-
return this.context.options;
|
|
43
|
+
constructor(options?: Partial<Options> | null) {
|
|
44
|
+
this.options = options ?? defaultOptions;
|
|
42
45
|
}
|
|
43
46
|
|
|
44
|
-
/**
|
|
45
|
-
|
|
47
|
+
/** Logger */
|
|
48
|
+
get logger(): Logger {
|
|
49
|
+
return this.options.logger ?? console;
|
|
50
|
+
}
|
|
51
|
+
/** 选项 */
|
|
52
|
+
readonly options: Readonly<Partial<Options>>;
|
|
46
53
|
|
|
47
54
|
/** 默认执行环境 */
|
|
48
|
-
private readonly scope = new Scope(
|
|
55
|
+
private readonly scope = new Scope(null, true, '<evaluator>');
|
|
49
56
|
|
|
50
|
-
|
|
57
|
+
private readonly __imported = new Map<string, VmValue>();
|
|
58
|
+
/** 执行环境 */
|
|
59
|
+
get imported(): ReadonlyMap<string, VmValue> {
|
|
60
|
+
return this.__imported;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 导入到执行环境
|
|
64
|
+
*
|
|
65
|
+
* 使用 `undefined` 删除已导入的变量
|
|
66
|
+
*/
|
|
51
67
|
import(values: Record<string, unknown>): void {
|
|
52
68
|
for (const [key, value] of Object.entries(values)) {
|
|
53
69
|
if (value === undefined) {
|
|
54
|
-
this.
|
|
70
|
+
this.__imported.delete(key);
|
|
55
71
|
} else {
|
|
56
|
-
this.
|
|
72
|
+
this.__imported.set(key, wrapToVmValue(value, null));
|
|
57
73
|
}
|
|
58
74
|
}
|
|
59
75
|
}
|
|
60
76
|
|
|
61
|
-
/**
|
|
62
|
-
* 编译表达式
|
|
63
|
-
*/
|
|
77
|
+
/** 编译表达式 */
|
|
64
78
|
compile<T extends VmValue>(expression: Expression<T>, throws = true): CompiledExpression<T> {
|
|
65
|
-
const value = parse(this
|
|
79
|
+
const value = parse(this, expression.source, throws, false);
|
|
66
80
|
const tag = expression[ExpressionTag];
|
|
67
|
-
const type = tag ? TypeInfo.parse(tag) :
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
const type = tag ? TypeInfo.parse(tag) : '';
|
|
82
|
+
let compiled: ExpressionFunction<T>;
|
|
83
|
+
if (!type) {
|
|
84
|
+
compiled = (scope, evaluator) => evaluate<T>(evaluator, value, scope);
|
|
85
|
+
} else {
|
|
86
|
+
const to = TypeInfo.converter(type);
|
|
87
|
+
compiled = (scope, evaluator) => {
|
|
88
|
+
const ret = evaluate<T>(evaluator, value, scope);
|
|
89
|
+
if (ret == null) return null;
|
|
90
|
+
return to(ret) as T;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
defineProperty(compiled, ExpressionTag, { value: type, enumerable: true });
|
|
94
|
+
defineProperty(compiled, 'source', { value: value.source, enumerable: true });
|
|
95
|
+
return compiled as CompiledExpression<T>;
|
|
75
96
|
}
|
|
76
97
|
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
98
|
+
/** 执行 MiraScript 模板 */
|
|
99
|
+
template(template: string | null | undefined, scope: Scope | undefined): string {
|
|
100
|
+
if (template == null) return '';
|
|
101
|
+
const throws = scope?.throws ?? true;
|
|
102
|
+
const value = parse(this, template, throws, true);
|
|
103
|
+
const result = evaluate<string>(this, value, scope ?? this.scope) ?? template;
|
|
104
|
+
if (typeof result == 'string') return result;
|
|
105
|
+
return TypeInfo.toString(result);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 求值 */
|
|
109
|
+
evaluate<T extends VmValue>(
|
|
110
|
+
expression: ExpressionOrValue<T> | ExpressionFunction<T> | null | undefined,
|
|
111
|
+
scope?: Scope | null,
|
|
112
|
+
): T | null;
|
|
113
|
+
/** 求值 */
|
|
114
|
+
evaluate<T extends VmValue, const D = NonNullable<T>>(
|
|
115
|
+
expression: ExpressionOrValue<T> | ExpressionFunction<T> | null | undefined,
|
|
116
|
+
scope: Scope | null | undefined,
|
|
87
117
|
defaults: D,
|
|
88
|
-
): T | D;
|
|
89
|
-
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
evaluate<T
|
|
93
|
-
expression:
|
|
94
|
-
scope: Scope | undefined,
|
|
95
|
-
defaults
|
|
96
|
-
): T | D
|
|
118
|
+
): NonNullable<T> | D;
|
|
119
|
+
/** 求值 */
|
|
120
|
+
evaluate<T>(expression: ExpressionFunction<T> | T | null | undefined, scope?: Scope | null): T | null;
|
|
121
|
+
/** 求值 */
|
|
122
|
+
evaluate<T, const D = NonNullable<T>>(
|
|
123
|
+
expression: ExpressionFunction<T> | T | null | undefined,
|
|
124
|
+
scope: Scope | null | undefined,
|
|
125
|
+
defaults: D,
|
|
126
|
+
): NonNullable<T> | D;
|
|
127
|
+
|
|
128
|
+
/** 求值 */
|
|
129
|
+
evaluate<T extends VmValue, const D = NonNullable<T>>(
|
|
130
|
+
expression: ExpressionOrValue<T> | ExpressionFunction<T> | null | undefined,
|
|
131
|
+
scope: Scope | null | undefined,
|
|
132
|
+
defaults: D | null = null,
|
|
133
|
+
): T | D | null {
|
|
97
134
|
if (expression == null) return defaults;
|
|
98
|
-
|
|
99
|
-
|
|
135
|
+
|
|
136
|
+
if (typeof expression == 'function' && !isVmFunction(expression)) {
|
|
137
|
+
try {
|
|
138
|
+
const ret = expression(scope ?? this.scope, this);
|
|
139
|
+
if (ret == null) return defaults;
|
|
140
|
+
return ret;
|
|
141
|
+
} catch (ex) {
|
|
142
|
+
if (scope?.throws ?? true) {
|
|
143
|
+
throw ex;
|
|
144
|
+
}
|
|
145
|
+
return defaults;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!isExpression<T>(expression)) {
|
|
150
|
+
return expression;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const result = this.evaluateSource(expression.source, scope);
|
|
154
|
+
if (result == null) return defaults;
|
|
155
|
+
|
|
100
156
|
const tag = expression[ExpressionTag];
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
157
|
+
if (!tag) return result;
|
|
158
|
+
return TypeInfo.to(result, tag) as T;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** 求值 */
|
|
162
|
+
evaluateSource<T extends VmValue>(
|
|
163
|
+
expression: ExpressionSource<T> | T | null | undefined,
|
|
164
|
+
scope: Scope | null | undefined,
|
|
165
|
+
): T | null;
|
|
166
|
+
/** 求值 */
|
|
167
|
+
evaluateSource<T extends VmValue, const D = NonNullable<T>>(
|
|
168
|
+
expression: ExpressionSource<T> | T | null | undefined,
|
|
169
|
+
scope: Scope | null | undefined,
|
|
170
|
+
defaults: D,
|
|
171
|
+
): NonNullable<T> | D;
|
|
172
|
+
|
|
173
|
+
/** 求值 */
|
|
174
|
+
evaluateSource<T extends VmValue, const D = NonNullable<T>>(
|
|
175
|
+
expression: ExpressionSource<T> | T | null | undefined,
|
|
176
|
+
scope: Scope | null | undefined,
|
|
177
|
+
defaults: D | null = null,
|
|
178
|
+
): NonNullable<T> | D | null {
|
|
179
|
+
if (expression == null) return defaults;
|
|
180
|
+
if (typeof expression != 'string') return expression;
|
|
181
|
+
|
|
182
|
+
scope ??= this.scope;
|
|
183
|
+
const { throws } = scope;
|
|
184
|
+
const value = parse(this, expression, throws, false);
|
|
185
|
+
const result = evaluate<T>(this, value, scope);
|
|
186
|
+
if (result == null) return defaults;
|
|
187
|
+
|
|
104
188
|
return result;
|
|
105
189
|
}
|
|
106
190
|
|
|
107
191
|
/** 求条件 */
|
|
108
|
-
evaluateCondition(
|
|
192
|
+
evaluateCondition(
|
|
193
|
+
condition: ConditionExpression | null | undefined,
|
|
194
|
+
scope: Scope | null | undefined,
|
|
195
|
+
defaults = true,
|
|
196
|
+
): boolean {
|
|
197
|
+
if (condition == null || condition === '') return defaults;
|
|
109
198
|
if (typeof condition == 'boolean') return condition;
|
|
110
|
-
if (condition == null) return defaults;
|
|
111
|
-
condition = (
|
|
112
|
-
typeof condition == 'string' ? condition : lib.to_string(condition)
|
|
113
|
-
).trim() as ConditionExpression & string;
|
|
114
|
-
if (condition === '') return defaults;
|
|
115
199
|
if (condition === 'true') return true;
|
|
116
200
|
if (condition === 'false') return false;
|
|
117
201
|
try {
|
|
118
|
-
|
|
202
|
+
const result = this.evaluateSource(condition, scope, defaults);
|
|
203
|
+
if (typeof result == 'boolean') return result;
|
|
204
|
+
throw new Error(`Condition evaluates to \`${serialize(result)}\``);
|
|
119
205
|
} catch (ex) {
|
|
120
206
|
if (scope?.throws) {
|
|
121
207
|
throw ex;
|
|
122
208
|
}
|
|
123
|
-
this.
|
|
209
|
+
this.logger.warn(`Invalid condition`, ex);
|
|
124
210
|
return defaults;
|
|
125
211
|
}
|
|
126
212
|
}
|
|
127
213
|
|
|
128
214
|
/** 求选项列表 */
|
|
129
|
-
evaluateChoices<
|
|
130
|
-
|
|
131
|
-
scope: Scope | undefined,
|
|
132
|
-
):
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
215
|
+
evaluateChoices<T extends ChoiceParameterType>(
|
|
216
|
+
definition: Pick<ParameterMap[T], 'type' | 'choices' | 'value'>,
|
|
217
|
+
scope: Scope | null | undefined,
|
|
218
|
+
): EvaluatedChoices<T> {
|
|
219
|
+
const ret: Choice[] = [];
|
|
220
|
+
const { type, choices } = definition;
|
|
221
|
+
const isLogical = type === 'logical';
|
|
222
|
+
const choiceType = choiceParameterType(definition);
|
|
223
|
+
const fallbackKeys = !isLogical
|
|
224
|
+
? []
|
|
225
|
+
: choiceType === 'boolean'
|
|
226
|
+
? [false, true]
|
|
227
|
+
: choiceType === 'number'
|
|
228
|
+
? [0, 1]
|
|
229
|
+
: ['0', '1'];
|
|
230
|
+
if (choices != null) {
|
|
231
|
+
const evaluatedChoices = this.evaluate(choices, scope);
|
|
232
|
+
if (evaluatedChoices == null) {
|
|
233
|
+
// do nothing
|
|
234
|
+
} else if (!isArray(evaluatedChoices as unknown[])) {
|
|
235
|
+
if (scope?.throws ?? true) {
|
|
236
|
+
throw new Error(`Choices is not an array`);
|
|
237
|
+
}
|
|
238
|
+
} else if (evaluatedChoices.length > 0) {
|
|
239
|
+
const to = TypeInfo.converter(choiceType);
|
|
240
|
+
let len = evaluatedChoices.length;
|
|
241
|
+
if (isLogical && len > 2) len = 2;
|
|
242
|
+
for (let i = 0; i < len; i++) {
|
|
243
|
+
try {
|
|
244
|
+
const item = evaluatedChoices[i];
|
|
245
|
+
const key = to(item?.key ?? fallbackKeys[i] ?? i);
|
|
246
|
+
const name = item?.name ?? (isLogical ? '' : String(key));
|
|
247
|
+
const description = item?.description ?? '';
|
|
248
|
+
const condition = item?.condition || undefined;
|
|
249
|
+
ret[i] = { key, name, description, condition };
|
|
250
|
+
} catch (ex) {
|
|
251
|
+
if (scope?.throws ?? true) {
|
|
252
|
+
throw new Error(`Invalid choice value: ${serialize(evaluatedChoices[i])}`, {
|
|
253
|
+
cause: ex,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
ret[i] = { key: to(i, i), name: '', description: '', condition: undefined };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
139
259
|
}
|
|
140
|
-
return [];
|
|
141
260
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
key: (item.key ?? index) as V,
|
|
151
|
-
name: item.name ?? String(item.key ?? index),
|
|
152
|
-
description: item.description ?? '',
|
|
153
|
-
condition: item.condition,
|
|
154
|
-
};
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** 转换参数值的类型 */
|
|
159
|
-
toArg<V extends ArgumentValue>(value: VmValue, definition: Parameter): V {
|
|
160
|
-
switch (definition.type as LiteralUnion<ParameterType, string>) {
|
|
161
|
-
case 'real':
|
|
162
|
-
case 'integer':
|
|
163
|
-
return to_number(value) as V;
|
|
164
|
-
case 'file':
|
|
165
|
-
case 'text':
|
|
166
|
-
case 'code':
|
|
167
|
-
case 'pinLike':
|
|
168
|
-
case 'cssColor':
|
|
169
|
-
case 'cssBackground':
|
|
170
|
-
case 'resourceId':
|
|
171
|
-
return to_string(value) as V;
|
|
172
|
-
|
|
173
|
-
case 'choice':
|
|
174
|
-
case 'logical':
|
|
175
|
-
return value as V;
|
|
176
|
-
|
|
177
|
-
case 'multiSelect':
|
|
178
|
-
case 'table': {
|
|
179
|
-
if (isVmArray(value)) {
|
|
180
|
-
return value as V;
|
|
181
|
-
}
|
|
182
|
-
return (value != null ? [value] : []) as ArgumentValue as V;
|
|
261
|
+
if (isLogical) {
|
|
262
|
+
while (ret.length < 2) {
|
|
263
|
+
ret.push({
|
|
264
|
+
key: fallbackKeys[ret.length]!,
|
|
265
|
+
name: '',
|
|
266
|
+
description: '',
|
|
267
|
+
condition: undefined,
|
|
268
|
+
});
|
|
183
269
|
}
|
|
184
|
-
case 'grouped':
|
|
185
|
-
case 'record':
|
|
186
|
-
default:
|
|
187
|
-
return value as V;
|
|
188
270
|
}
|
|
271
|
+
return ret as EvaluatedChoices<T>;
|
|
189
272
|
}
|
|
190
273
|
|
|
191
274
|
/** 求参数值 */
|
|
192
275
|
evaluateArg<V extends ArgumentValue>(
|
|
193
|
-
expression: ExpressionOrValue<V> | undefined,
|
|
194
|
-
scope: Scope | undefined,
|
|
276
|
+
expression: ExpressionOrValue<V> | null | undefined,
|
|
277
|
+
scope: Scope | null | undefined,
|
|
195
278
|
definition: Parameter,
|
|
196
279
|
): V {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
280
|
+
// 默认值有可能为表达式,因此需要先填充再求值
|
|
281
|
+
expression ??= definition.value as ExpressionOrValue<V>;
|
|
282
|
+
// 求值失败时返回默认值
|
|
283
|
+
const result = this.evaluate<V>(expression, scope, definition.value as V);
|
|
284
|
+
const converter = argumentConverter(definition);
|
|
285
|
+
// 转换失败时返回转换前的值
|
|
286
|
+
return converter(result, result) as V;
|
|
201
287
|
}
|
|
202
288
|
/** 求参数表的所有参数值 */
|
|
203
289
|
evaluateArgs<T extends Record<string, ArgumentValue>>(
|
|
204
|
-
args: ArgumentMap<T> | undefined,
|
|
205
|
-
scope: Scope | undefined,
|
|
290
|
+
args: ArgumentMap<T> | null | undefined,
|
|
291
|
+
scope: Scope | null | undefined,
|
|
206
292
|
definition: ReadonlyArray<Pick<ParameterGroup, 'items'>>,
|
|
207
293
|
): T {
|
|
208
|
-
const a =
|
|
294
|
+
const a = fillArgumentMap(definition, args);
|
|
209
295
|
const ret: Record<string, ArgumentValue> = {};
|
|
210
296
|
for (const group of definition) {
|
|
211
297
|
for (const param of group.items) {
|
|
@@ -217,63 +303,6 @@ export class Evaluator {
|
|
|
217
303
|
}
|
|
218
304
|
return ret as T;
|
|
219
305
|
}
|
|
220
|
-
/** 获取参数默认值,修复现有的参数,并填充缺失的参数 */
|
|
221
|
-
defaultArgs<T extends Record<string, ArgumentValue>>(
|
|
222
|
-
definition: ReadonlyArray<Pick<ParameterGroup, 'items'>> | undefined,
|
|
223
|
-
args: ArgumentMap<T> = {} as ArgumentMap<T>,
|
|
224
|
-
): ArgumentMap<T> {
|
|
225
|
-
if (definition == null || !isArray(definition as unknown[]) || definition.length === 0) {
|
|
226
|
-
return args;
|
|
227
|
-
}
|
|
228
|
-
/** 参数键的类型 */
|
|
229
|
-
type K = keyof T & string;
|
|
230
|
-
/** 参数值的类型 */
|
|
231
|
-
type V = NonNullable<T[K]>;
|
|
232
|
-
const keys = new Set(Object.keys(args) as K[]);
|
|
233
|
-
|
|
234
|
-
// 根据定义填充或修复参数
|
|
235
|
-
for (const group of definition) {
|
|
236
|
-
for (const param of group.items) {
|
|
237
|
-
const key = param.key as K;
|
|
238
|
-
if (!key) continue;
|
|
239
|
-
const currentValue = keys.has(key) ? args[key] : undefined;
|
|
240
|
-
if (currentValue == null) {
|
|
241
|
-
// 使用默认值填充
|
|
242
|
-
const defaultValue = param.value;
|
|
243
|
-
if (isExpression(defaultValue)) {
|
|
244
|
-
args[key] = { [ExpressionTag]: '', source: defaultValue.source } as Expression<V>;
|
|
245
|
-
} else {
|
|
246
|
-
const value =
|
|
247
|
-
defaultValue != null && typeof defaultValue == 'object'
|
|
248
|
-
? klona(defaultValue)
|
|
249
|
-
: defaultValue;
|
|
250
|
-
args[key] = this.toArg<V>(value, param);
|
|
251
|
-
}
|
|
252
|
-
} else {
|
|
253
|
-
// 已存在的参数值
|
|
254
|
-
if (isExpression(currentValue)) {
|
|
255
|
-
// 保留表达式
|
|
256
|
-
} else {
|
|
257
|
-
// 修复现有的参数值
|
|
258
|
-
const castedValue = this.toArg<V>(currentValue, param);
|
|
259
|
-
if (castedValue != null && castedValue !== currentValue) {
|
|
260
|
-
args[key] = castedValue;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
keys.delete(key);
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// 删除多余的参数
|
|
269
|
-
for (const key of keys) {
|
|
270
|
-
// 保留特殊参数和私有字段
|
|
271
|
-
if (key.startsWith('_') || key.startsWith('$') || key.startsWith('@')) continue;
|
|
272
|
-
delete args[key];
|
|
273
|
-
}
|
|
274
|
-
return args;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
306
|
/** 解析表达式 */
|
|
278
307
|
validate<T extends VmValue>(expression: Expression<T>): void {
|
|
279
308
|
if (!isExpression(expression) || !expression.source) {
|
|
@@ -281,15 +310,18 @@ export class Evaluator {
|
|
|
281
310
|
}
|
|
282
311
|
const tag = expression[ExpressionTag];
|
|
283
312
|
if (tag) TypeInfo.parse(tag);
|
|
284
|
-
parse(this
|
|
313
|
+
parse(this, expression.source, true, false);
|
|
285
314
|
}
|
|
286
315
|
|
|
287
316
|
/** 分析表达式 */
|
|
288
|
-
analyze<T extends VmValue>(expression: Expression<T
|
|
317
|
+
analyze<T extends VmValue>(expression: Expression<T>, scope?: Scope | null): readonly GlobalReferenceChain[] {
|
|
289
318
|
if (!isExpression(expression) || !expression.source) {
|
|
290
319
|
throw new Error(`${expression.source || String(expression)} is not a valid expression`);
|
|
291
320
|
}
|
|
292
|
-
|
|
293
|
-
|
|
321
|
+
scope ??= this.scope;
|
|
322
|
+
const exp = parse(this, expression.source, scope.throws, false);
|
|
323
|
+
// 如果 throws=true,则 parse 会抛出错误,此处有 error 一定是 throws=false 的情况
|
|
324
|
+
if (exp.error) return [];
|
|
325
|
+
return analyze(exp, this, scope);
|
|
294
326
|
}
|
|
295
327
|
}
|
package/src/migrate.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { VmValue } from '@mirascript/mirascript';
|
|
2
|
+
import type { Scope } from './scope.js';
|
|
2
3
|
import type { ExpressionSource } from './expression.js';
|
|
3
4
|
import { BaseState } from './migrator/state.js';
|
|
4
|
-
import {
|
|
5
|
+
import { Evaluator } from './main.js';
|
|
6
|
+
import { DefaultVmContext } from '@mirascript/mirascript/subtle';
|
|
5
7
|
|
|
8
|
+
const evaluator = new Evaluator();
|
|
6
9
|
/** MathJs 表达式转为 MiraScript 表达式 */
|
|
7
10
|
export function migrateMathJs<const T extends VmValue>(
|
|
8
11
|
expr: ExpressionSource<T>,
|
|
@@ -16,11 +19,31 @@ export function migrateMathJs<const T extends VmValue>(
|
|
|
16
19
|
if (expr === 'null') return 'nil' as ExpressionSource<T>;
|
|
17
20
|
if (expr === 'true') return 'true' as ExpressionSource<T>;
|
|
18
21
|
if (expr === 'false') return 'false' as ExpressionSource<T>;
|
|
22
|
+
if (condition) {
|
|
23
|
+
// 兼容旧的 evaluateCondition 的宽容处理
|
|
24
|
+
if (expr === 'True' || expr === 'TRUE' || expr === '1' || expr === '"1"' || expr === "'1'")
|
|
25
|
+
return 'true' as ExpressionSource<T>;
|
|
26
|
+
if (
|
|
27
|
+
expr === 'False' ||
|
|
28
|
+
expr === 'FALSE' ||
|
|
29
|
+
expr === '0' ||
|
|
30
|
+
expr === '"0"' ||
|
|
31
|
+
expr === "'0'" ||
|
|
32
|
+
expr === '""' ||
|
|
33
|
+
expr === "''"
|
|
34
|
+
)
|
|
35
|
+
return 'false' as ExpressionSource<T>;
|
|
36
|
+
}
|
|
19
37
|
if (/^[-+]?(\d+(?:\.\d+)?(?:[eE][+-]?\d+)?)$/.test(expr)) {
|
|
20
38
|
return expr;
|
|
21
39
|
}
|
|
22
40
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
41
|
+
const dispose = context?.reset(evaluator);
|
|
42
|
+
try {
|
|
43
|
+
const state = new BaseState(expr, condition, context ? context.proxy : DefaultVmContext);
|
|
44
|
+
state.migrate();
|
|
45
|
+
return state.result() as ExpressionSource<T>;
|
|
46
|
+
} finally {
|
|
47
|
+
dispose?.();
|
|
48
|
+
}
|
|
26
49
|
}
|