hzengine-core 0.1.2-dev
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/async/index.js +162 -0
- package/dist/async/zeppos_timer.js +58 -0
- package/dist/audio/index.js +260 -0
- package/dist/config/index.js +57 -0
- package/dist/debug/index.js +8 -0
- package/dist/index.js +103 -0
- package/dist/platform/index.js +1 -0
- package/dist/plugins/basic_command/$.js +8 -0
- package/dist/plugins/basic_command/audio.js +40 -0
- package/dist/plugins/basic_command/basic.js +124 -0
- package/dist/plugins/basic_command/character.js +112 -0
- package/dist/plugins/basic_command/conditional.js +260 -0
- package/dist/plugins/basic_command/config.js +22 -0
- package/dist/plugins/basic_command/decorator.js +24 -0
- package/dist/plugins/basic_command/eval.js +67 -0
- package/dist/plugins/basic_command/img.js +249 -0
- package/dist/plugins/basic_command/index.js +22 -0
- package/dist/plugins/basic_command/menu.js +140 -0
- package/dist/plugins/global_gesture/index.js +25 -0
- package/dist/plugins/transform/animation.js +440 -0
- package/dist/plugins/transform/commands.js +38 -0
- package/dist/plugins/transform/example_profiles.js +32 -0
- package/dist/plugins/transform/hz_anime.js +211 -0
- package/dist/plugins/transform/index.js +93 -0
- package/dist/script/index.js +537 -0
- package/dist/script/readscript.js +15 -0
- package/dist/script/strtools.js +157 -0
- package/dist/storage/decorator.js +260 -0
- package/dist/storage/fs.js +96 -0
- package/dist/storage/index.js +442 -0
- package/dist/system/index.js +144 -0
- package/dist/ui/index.js +535 -0
- package/dist/utils/path.js +289 -0
- package/license.txt +202 -0
- package/package.json +26 -0
- package/src/async/index.ts +124 -0
- package/src/async/zeppos_timer.js +65 -0
- package/src/audio/index.ts +224 -0
- package/src/config/index.ts +80 -0
- package/src/debug/index.ts +11 -0
- package/src/index.ts +122 -0
- package/src/platform/index.ts +158 -0
- package/src/plugins/basic_command/$.ts +11 -0
- package/src/plugins/basic_command/audio.ts +53 -0
- package/src/plugins/basic_command/basic.ts +145 -0
- package/src/plugins/basic_command/character.ts +144 -0
- package/src/plugins/basic_command/conditional.ts +349 -0
- package/src/plugins/basic_command/config.ts +29 -0
- package/src/plugins/basic_command/decorator.ts +29 -0
- package/src/plugins/basic_command/eval.ts +88 -0
- package/src/plugins/basic_command/img.ts +317 -0
- package/src/plugins/basic_command/index.ts +24 -0
- package/src/plugins/basic_command/menu.ts +178 -0
- package/src/plugins/global_gesture/index.ts +29 -0
- package/src/plugins/transform/animation.ts +542 -0
- package/src/plugins/transform/commands.ts +53 -0
- package/src/plugins/transform/example_profiles.ts +36 -0
- package/src/plugins/transform/hz_anime.ts +214 -0
- package/src/plugins/transform/index.ts +141 -0
- package/src/plugins/transform/readme.md +1 -0
- package/src/script/index.ts +623 -0
- package/src/script/readscript.ts +17 -0
- package/src/script/strtools.ts +159 -0
- package/src/storage/decorator.ts +473 -0
- package/src/storage/fs.ts +104 -0
- package/src/storage/index.ts +541 -0
- package/src/system/index.ts +95 -0
- package/src/ui/index.ts +699 -0
- package/src/utils/path.js +338 -0
- package/tsconfig.json +111 -0
- package/types/async/index.d.ts +24 -0
- package/types/async/zeppos_timer.d.ts +14 -0
- package/types/audio/index.d.ts +64 -0
- package/types/config/index.d.ts +9 -0
- package/types/debug/index.d.ts +6 -0
- package/types/index.d.ts +41 -0
- package/types/platform/index.d.ts +134 -0
- package/types/plugins/basic_command/$.d.ts +2 -0
- package/types/plugins/basic_command/audio.d.ts +2 -0
- package/types/plugins/basic_command/basic.d.ts +3 -0
- package/types/plugins/basic_command/character.d.ts +2 -0
- package/types/plugins/basic_command/conditional.d.ts +2 -0
- package/types/plugins/basic_command/config.d.ts +2 -0
- package/types/plugins/basic_command/decorator.d.ts +2 -0
- package/types/plugins/basic_command/eval.d.ts +2 -0
- package/types/plugins/basic_command/img.d.ts +2 -0
- package/types/plugins/basic_command/index.d.ts +2 -0
- package/types/plugins/basic_command/menu.d.ts +2 -0
- package/types/plugins/global_gesture/index.d.ts +2 -0
- package/types/plugins/transform/animation.d.ts +131 -0
- package/types/plugins/transform/commands.d.ts +7 -0
- package/types/plugins/transform/example_profiles.d.ts +2 -0
- package/types/plugins/transform/hz_anime.d.ts +51 -0
- package/types/plugins/transform/index.d.ts +13 -0
- package/types/script/index.d.ts +123 -0
- package/types/script/readscript.d.ts +2 -0
- package/types/script/strtools.d.ts +31 -0
- package/types/storage/decorator.d.ts +41 -0
- package/types/storage/fs.d.ts +1 -0
- package/types/storage/index.d.ts +86 -0
- package/types/system/index.d.ts +35 -0
- package/types/ui/index.d.ts +167 -0
- package/types/utils/path.d.ts +84 -0
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
36
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
37
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
38
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
39
|
+
};
|
|
40
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
41
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
42
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
43
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
44
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
45
|
+
};
|
|
46
|
+
import { mergeObjs2Str as joinObjs2Str, parseInterpolatedStr, removeComment, splitStr2Objs, } from "./strtools.js";
|
|
47
|
+
import { readline } from "./readscript.js";
|
|
48
|
+
import { Save } from "../storage/decorator.js";
|
|
49
|
+
let Script = (() => {
|
|
50
|
+
var _a, _Script__callStack_accessor_storage, _Script__statementStack_accessor_storage, _Script__nextRunPosition_accessor_storage;
|
|
51
|
+
let __callStack_decorators;
|
|
52
|
+
let __callStack_initializers = [];
|
|
53
|
+
let __callStack_extraInitializers = [];
|
|
54
|
+
let __statementStack_decorators;
|
|
55
|
+
let __statementStack_initializers = [];
|
|
56
|
+
let __statementStack_extraInitializers = [];
|
|
57
|
+
let __nextRunPosition_decorators;
|
|
58
|
+
let __nextRunPosition_initializers = [];
|
|
59
|
+
let __nextRunPosition_extraInitializers = [];
|
|
60
|
+
return _a = class Script {
|
|
61
|
+
constructor(_core) {
|
|
62
|
+
this._core = _core;
|
|
63
|
+
_Script__callStack_accessor_storage.set(this, __runInitializers(this, __callStack_initializers, []));
|
|
64
|
+
_Script__statementStack_accessor_storage.set(this, (__runInitializers(this, __callStack_extraInitializers), __runInitializers(this, __statementStack_initializers, [])));
|
|
65
|
+
_Script__nextRunPosition_accessor_storage.set(this, (__runInitializers(this, __statementStack_extraInitializers), __runInitializers(this, __nextRunPosition_initializers, null)));
|
|
66
|
+
// Middleware
|
|
67
|
+
this._middlewares = (__runInitializers(this, __nextRunPosition_extraInitializers), []);
|
|
68
|
+
// Statement Analyse
|
|
69
|
+
this._statementAnalyseStack = [];
|
|
70
|
+
this._analyseStatementMiddlewares = [];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 调用栈
|
|
74
|
+
* 在call时保存当前执行位置和语句栈,在return时恢复执行位置和语句栈
|
|
75
|
+
*/
|
|
76
|
+
get _callStack() { return __classPrivateFieldGet(this, _Script__callStack_accessor_storage, "f"); }
|
|
77
|
+
set _callStack(value) { __classPrivateFieldSet(this, _Script__callStack_accessor_storage, value, "f"); }
|
|
78
|
+
/**
|
|
79
|
+
* 语句栈
|
|
80
|
+
* 比如while, if,会在语句开始时入栈,语句结束时出栈
|
|
81
|
+
*/
|
|
82
|
+
get _statementStack() { return __classPrivateFieldGet(this, _Script__statementStack_accessor_storage, "f"); }
|
|
83
|
+
set _statementStack(value) { __classPrivateFieldSet(this, _Script__statementStack_accessor_storage, value, "f"); }
|
|
84
|
+
/**
|
|
85
|
+
* 下一次执行的脚本位置
|
|
86
|
+
* 注意:存储该值的时候应总是拷贝赋值而非直接引用赋值
|
|
87
|
+
*/
|
|
88
|
+
get _nextRunPosition() { return __classPrivateFieldGet(this, _Script__nextRunPosition_accessor_storage, "f"); }
|
|
89
|
+
set _nextRunPosition(value) { __classPrivateFieldSet(this, _Script__nextRunPosition_accessor_storage, value, "f"); }
|
|
90
|
+
// Script Run
|
|
91
|
+
/**
|
|
92
|
+
* 执行_nextRunPosition,并返回下一行_nextRunPosition是否不为null
|
|
93
|
+
*/
|
|
94
|
+
runSingleLine() {
|
|
95
|
+
if (!this._nextRunPosition) {
|
|
96
|
+
// 文件尾隐式执行 return
|
|
97
|
+
this.return();
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
let nowRunPosition = [
|
|
101
|
+
...this._nextRunPosition,
|
|
102
|
+
];
|
|
103
|
+
this.incrementNextPosition();
|
|
104
|
+
let rawCommand = readline(this._core, nowRunPosition[0], nowRunPosition[1]);
|
|
105
|
+
// remove comment
|
|
106
|
+
rawCommand = removeComment(rawCommand);
|
|
107
|
+
if (rawCommand.trim().length && !rawCommand.trim().startsWith("#")) {
|
|
108
|
+
if (rawCommand.trim().startsWith("*")) {
|
|
109
|
+
if (this._statementStack.length) {
|
|
110
|
+
throw `label between statement is not allowed, at file [${nowRunPosition[0]}] line [${nowRunPosition[1]}]`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
this._core.debug.log("Run cmd: " + rawCommand);
|
|
115
|
+
// Process Command
|
|
116
|
+
this._processCmd(rawCommand, [...nowRunPosition]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return !!this._nextRunPosition;
|
|
120
|
+
}
|
|
121
|
+
// Label Point Control
|
|
122
|
+
/**
|
|
123
|
+
* 跳转到目标标签
|
|
124
|
+
* @param targetLabel
|
|
125
|
+
*/
|
|
126
|
+
jumpLabel(targetLabel) {
|
|
127
|
+
let labelPosition = this._locateLabel(targetLabel);
|
|
128
|
+
this._nextRunPosition = labelPosition;
|
|
129
|
+
this._statementStack = [];
|
|
130
|
+
}
|
|
131
|
+
jump(path, index, clearStatementStack = false) {
|
|
132
|
+
this._nextRunPosition = [path, index];
|
|
133
|
+
if (clearStatementStack) {
|
|
134
|
+
this._statementStack = [];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 调用目标标签
|
|
139
|
+
* 保存当前执行位置至调用栈,跳转到目标位置,直到return返回
|
|
140
|
+
* @param targetLabel
|
|
141
|
+
*/
|
|
142
|
+
callLabel(targetLabel) {
|
|
143
|
+
// console.log(
|
|
144
|
+
// `pending to call label ${targetLabel}, stack=${JSON.stringify(
|
|
145
|
+
// this._routeStack
|
|
146
|
+
// )}`
|
|
147
|
+
// );
|
|
148
|
+
let labelPosition = this._locateLabel(targetLabel);
|
|
149
|
+
this._callStack.push({
|
|
150
|
+
position: this._nextRunPosition ? [...this._nextRunPosition] : null,
|
|
151
|
+
statementStack: this._statementStack,
|
|
152
|
+
});
|
|
153
|
+
this._nextRunPosition = labelPosition;
|
|
154
|
+
this._statementStack = [];
|
|
155
|
+
// console.log(
|
|
156
|
+
// `finished call label ${targetLabel}, stack=${JSON.stringify(
|
|
157
|
+
// this._routeStack
|
|
158
|
+
// )}`
|
|
159
|
+
// );
|
|
160
|
+
}
|
|
161
|
+
hasLabel(targetLabel) {
|
|
162
|
+
var _b, _c, _d;
|
|
163
|
+
return (((_d = (_c = (_b = this._core.storage.preloadedData) === null || _b === void 0 ? void 0 : _b.script) === null || _c === void 0 ? void 0 : _c.labelMap) === null || _d === void 0 ? void 0 : _d[targetLabel]) != null);
|
|
164
|
+
}
|
|
165
|
+
return() {
|
|
166
|
+
// console.log(`pending to return, stack=${JSON.stringify(this._routeStack)}`);
|
|
167
|
+
let stackItem = this._callStack.pop();
|
|
168
|
+
if (stackItem) {
|
|
169
|
+
this._statementStack = stackItem.statementStack;
|
|
170
|
+
this._nextRunPosition = stackItem.position;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
this._nextRunPosition = null;
|
|
174
|
+
this._statementStack = [];
|
|
175
|
+
// Game End
|
|
176
|
+
this._core.end();
|
|
177
|
+
}
|
|
178
|
+
// console.log(`finished return, stack=${JSON.stringify(this._routeStack)}`);
|
|
179
|
+
}
|
|
180
|
+
clear() {
|
|
181
|
+
this._nextRunPosition = null;
|
|
182
|
+
this._callStack = [];
|
|
183
|
+
}
|
|
184
|
+
_locateLabel(labelName) {
|
|
185
|
+
if (this._core.storage.preloadedData == null)
|
|
186
|
+
throw "Preloaded Data is Null";
|
|
187
|
+
if (!this._core.storage.preloadedData.script.labelMap[labelName])
|
|
188
|
+
throw `Error: Label [${labelName}] not found`;
|
|
189
|
+
let labelData = [
|
|
190
|
+
...this._core.storage.preloadedData.script.labelMap[labelName],
|
|
191
|
+
];
|
|
192
|
+
return labelData;
|
|
193
|
+
}
|
|
194
|
+
incrementNextPosition() {
|
|
195
|
+
if (this._core.storage.preloadedData == null)
|
|
196
|
+
throw "Preloaded Data is Null";
|
|
197
|
+
if (!this._nextRunPosition)
|
|
198
|
+
throw "_nextRunPosition is null";
|
|
199
|
+
// console.log(`hzsInfoMap=${JSON.stringify(this._core.storage.preloadedData.script.hzsInfoMap)}`);
|
|
200
|
+
let hzsInfo = this._core.storage.preloadedData.script.hzsInfoMap[this._nextRunPosition[0]];
|
|
201
|
+
if (!hzsInfo)
|
|
202
|
+
throw `Preloaded hzsInfo of path(${this._nextRunPosition[0]}) not found`;
|
|
203
|
+
this._nextRunPosition[1]++;
|
|
204
|
+
if (this._nextRunPosition[1] >= hzsInfo.totalLines)
|
|
205
|
+
this._nextRunPosition = null;
|
|
206
|
+
}
|
|
207
|
+
use(middleware, add_front = false) {
|
|
208
|
+
if (add_front) {
|
|
209
|
+
this._middlewares.unshift(middleware);
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
this._middlewares.push(middleware);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
_processCmd(cmd, nowRunPosition) {
|
|
216
|
+
let ctx = this._buildContext(cmd, nowRunPosition);
|
|
217
|
+
if (this._middlewares.length === 0) {
|
|
218
|
+
this._processUnsolvedCmd(cmd);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
let i = 0, len = this._middlewares.length;
|
|
222
|
+
let nextFunc = () => {
|
|
223
|
+
i++;
|
|
224
|
+
if (i >= len) {
|
|
225
|
+
this._processUnsolvedCmd(cmd);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this._middlewares[i](ctx, nextFunc);
|
|
229
|
+
};
|
|
230
|
+
this._middlewares[0](ctx, nextFunc);
|
|
231
|
+
}
|
|
232
|
+
_processUnsolvedCmd(cmd) {
|
|
233
|
+
if (cmd.trim().length === 0)
|
|
234
|
+
return; // Empty Line
|
|
235
|
+
else if (cmd.trim().startsWith("*"))
|
|
236
|
+
return; // Label Command
|
|
237
|
+
else {
|
|
238
|
+
throw `Can not parse command: ${cmd}`;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
_buildContext(cmd, nowRunPosition) {
|
|
242
|
+
return new _a.Context(this._core, cmd, nowRunPosition[0], nowRunPosition[1], this._statementStack);
|
|
243
|
+
}
|
|
244
|
+
_buildAnalyseStatementContext(cmd, nowRunPosition) {
|
|
245
|
+
return new _a.ContextForAnalyseStatement(this._core, cmd, nowRunPosition[0], nowRunPosition[1], this._statementAnalyseStack);
|
|
246
|
+
}
|
|
247
|
+
useAnalyseStatement(middleware, add_front) {
|
|
248
|
+
if (add_front) {
|
|
249
|
+
this._analyseStatementMiddlewares.unshift(middleware);
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
this._analyseStatementMiddlewares.push(middleware);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* 分析statement
|
|
257
|
+
*
|
|
258
|
+
* Analyze the statement syntax and record the script point location and related information in advance.
|
|
259
|
+
* When the script executes a statement, the regular middleware corresponding to that statement will load
|
|
260
|
+
* the information saved by analyseStatement before the statement, and if there is no analysis, it will call
|
|
261
|
+
* analyseStatement to analyze, and the corresponding analysis middleware will process and save the information.
|
|
262
|
+
* After analysis is complete, reset _nextRunPosition to the location before the call, switch back to the normal mode,
|
|
263
|
+
* and continue executing.
|
|
264
|
+
*/
|
|
265
|
+
analyseStatement(ctx) {
|
|
266
|
+
// this._core.debug.log("[HZEngine] Start statement analyse mode");
|
|
267
|
+
// Backup _nextRunPosition
|
|
268
|
+
let _nextRunPositionBackup = this
|
|
269
|
+
._nextRunPosition
|
|
270
|
+
? [...this._nextRunPosition]
|
|
271
|
+
: null;
|
|
272
|
+
let covered = false;
|
|
273
|
+
// Set _nextRunPosition to the current position of the statement
|
|
274
|
+
this._nextRunPosition = [ctx.currentPath, ctx.currentLineIndex];
|
|
275
|
+
while (this._nextRunPosition) {
|
|
276
|
+
let rawCommand = readline(this._core, this._nextRunPosition[0], this._nextRunPosition[1]);
|
|
277
|
+
if (rawCommand == null)
|
|
278
|
+
throw `Readline Error(got ${rawCommand}), at file [${this._nextRunPosition[0]}] line [${this._nextRunPosition[1] + 1}]`;
|
|
279
|
+
// If the command is not empty and not a comment
|
|
280
|
+
if (rawCommand.trim().length && !rawCommand.trim().startsWith("#")) {
|
|
281
|
+
// If it is a label command, check if the statement stack is empty
|
|
282
|
+
if (rawCommand.trim().startsWith("*")) {
|
|
283
|
+
if (this._statementStack.length) {
|
|
284
|
+
throw `label between statement is not allowed, at file [${this._nextRunPosition[0]}] line [${this._nextRunPosition[1] + 1}]`;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
// Build context for analyzing statement
|
|
289
|
+
let sub_ctx = this._buildAnalyseStatementContext(rawCommand, [
|
|
290
|
+
...this._nextRunPosition,
|
|
291
|
+
]);
|
|
292
|
+
// console.log(`[HZEngine] analyse statement command [${rawCommand}]`);
|
|
293
|
+
// Process command
|
|
294
|
+
if (this._analyseStatementMiddlewares.length === 0) {
|
|
295
|
+
// TODO do nothing
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
let i = 0, len = this._analyseStatementMiddlewares.length;
|
|
299
|
+
let nextFunc = () => {
|
|
300
|
+
i++;
|
|
301
|
+
if (i >= len) {
|
|
302
|
+
// TODO do nothing
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
this._analyseStatementMiddlewares[i](sub_ctx, nextFunc);
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
this._analyseStatementMiddlewares[0](sub_ctx, nextFunc);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// Move to the next line
|
|
313
|
+
this.incrementNextPosition();
|
|
314
|
+
if (covered) {
|
|
315
|
+
if (this._statementAnalyseStack.length === 0) {
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
if (this._statementAnalyseStack.length > 0) {
|
|
321
|
+
covered = true;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
this._nextRunPosition = _nextRunPositionBackup;
|
|
326
|
+
// Check if the statement stack is empty
|
|
327
|
+
if (this._statementAnalyseStack.length > 0) {
|
|
328
|
+
throw `statement not closed, at file [${this._statementAnalyseStack[this._statementAnalyseStack.length - 1][1][0]}] line [${this._statementAnalyseStack[this._statementAnalyseStack.length - 1][1][1] + 1}]`;
|
|
329
|
+
}
|
|
330
|
+
// Reset _nextRunPosition to the backup value, and switch back to normal mode, and continue executing
|
|
331
|
+
// this._core.debug.logconsole.log("[HZEngine] Finished analyse statement mode ");
|
|
332
|
+
}
|
|
333
|
+
// eval
|
|
334
|
+
evalScope(code) {
|
|
335
|
+
try {
|
|
336
|
+
return new Function("sd", "gd", "hz", `${code}`)(this._core.storage.sd, this._core.storage.gd, this._core);
|
|
337
|
+
}
|
|
338
|
+
catch (e) {
|
|
339
|
+
this._core.debug.log(`Error in evalScope: ${e}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
evalExpression(code) {
|
|
343
|
+
this._core.debug.log(`evalExpression: ${code}`);
|
|
344
|
+
try {
|
|
345
|
+
return new Function("sd", "gd", "hz", `return (${code})`)(this._core.storage.sd, this._core.storage.gd, this._core);
|
|
346
|
+
}
|
|
347
|
+
catch (e) {
|
|
348
|
+
this._core.debug.log(`Error in evalExpression: ${e}`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// parse string
|
|
352
|
+
parseString(str) {
|
|
353
|
+
let parsedInterpolated = parseInterpolatedStr(str);
|
|
354
|
+
let res = "";
|
|
355
|
+
for (let item of parsedInterpolated) {
|
|
356
|
+
if (item.isExpression) {
|
|
357
|
+
res += this.evalExpression(item.str);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
res += item.str;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return res;
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
_Script__callStack_accessor_storage = new WeakMap(),
|
|
367
|
+
_Script__statementStack_accessor_storage = new WeakMap(),
|
|
368
|
+
_Script__nextRunPosition_accessor_storage = new WeakMap(),
|
|
369
|
+
(() => {
|
|
370
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
371
|
+
__callStack_decorators = [Save("script.callStack")];
|
|
372
|
+
__statementStack_decorators = [Save("script.statementStack")];
|
|
373
|
+
__nextRunPosition_decorators = [Save("script.nextRunPosition")];
|
|
374
|
+
__esDecorate(_a, null, __callStack_decorators, { kind: "accessor", name: "_callStack", static: false, private: false, access: { has: obj => "_callStack" in obj, get: obj => obj._callStack, set: (obj, value) => { obj._callStack = value; } }, metadata: _metadata }, __callStack_initializers, __callStack_extraInitializers);
|
|
375
|
+
__esDecorate(_a, null, __statementStack_decorators, { kind: "accessor", name: "_statementStack", static: false, private: false, access: { has: obj => "_statementStack" in obj, get: obj => obj._statementStack, set: (obj, value) => { obj._statementStack = value; } }, metadata: _metadata }, __statementStack_initializers, __statementStack_extraInitializers);
|
|
376
|
+
__esDecorate(_a, null, __nextRunPosition_decorators, { kind: "accessor", name: "_nextRunPosition", static: false, private: false, access: { has: obj => "_nextRunPosition" in obj, get: obj => obj._nextRunPosition, set: (obj, value) => { obj._nextRunPosition = value; } }, metadata: _metadata }, __nextRunPosition_initializers, __nextRunPosition_extraInitializers);
|
|
377
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
378
|
+
})(),
|
|
379
|
+
_a;
|
|
380
|
+
})();
|
|
381
|
+
export { Script };
|
|
382
|
+
(function (Script) {
|
|
383
|
+
class Context {
|
|
384
|
+
constructor(_core, _rawtext, currentPath, currentLineIndex, _statementStack) {
|
|
385
|
+
this._core = _core;
|
|
386
|
+
this._rawtext = _rawtext;
|
|
387
|
+
this.currentPath = currentPath;
|
|
388
|
+
this.currentLineIndex = currentLineIndex;
|
|
389
|
+
this._statementStack = _statementStack;
|
|
390
|
+
this._rawtextChanged = false;
|
|
391
|
+
this._slicedArgs = null;
|
|
392
|
+
}
|
|
393
|
+
get rawtext() {
|
|
394
|
+
return this._rawtext;
|
|
395
|
+
}
|
|
396
|
+
set rawtext(rawtext) {
|
|
397
|
+
this._rawtext = rawtext;
|
|
398
|
+
this._rawtextChanged = true;
|
|
399
|
+
}
|
|
400
|
+
get slicedArgs() {
|
|
401
|
+
if (!this._slicedArgs || this._rawtextChanged)
|
|
402
|
+
this._slicedArgs = splitStr2Objs(this.rawtext);
|
|
403
|
+
this._rawtextChanged = false;
|
|
404
|
+
return this._slicedArgs;
|
|
405
|
+
}
|
|
406
|
+
// 注意只有在修改在触发slicedArgs的时候才会更新rawtext
|
|
407
|
+
set slicedArgs(slicedArgs) {
|
|
408
|
+
this._slicedArgs = JSON.parse(JSON.stringify(slicedArgs)); // TODO 深拷贝 性能
|
|
409
|
+
this._rawtext = joinObjs2Str(slicedArgs);
|
|
410
|
+
// TODO this._rawtextChanged = true ???
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* 開始一個新的Statement,返回該Statement的數據
|
|
414
|
+
* Start a new statement and return the data of the new statement
|
|
415
|
+
* @param identifier the identifier of the statement
|
|
416
|
+
* @returns the data of the new statement
|
|
417
|
+
*/
|
|
418
|
+
startStatement(identifier, data) {
|
|
419
|
+
let statement_data = data !== null && data !== void 0 ? data : this.getStatementData();
|
|
420
|
+
let statementStackItem = [
|
|
421
|
+
identifier,
|
|
422
|
+
[this.currentPath, this.currentLineIndex],
|
|
423
|
+
statement_data,
|
|
424
|
+
];
|
|
425
|
+
this._statementStack.push(statementStackItem);
|
|
426
|
+
return statement_data;
|
|
427
|
+
}
|
|
428
|
+
endStatement(identifier) {
|
|
429
|
+
if (this._statementStack.length === 0)
|
|
430
|
+
throw `statement not open, at file [${this.currentPath}] line [${this.currentLineIndex + 1}]`;
|
|
431
|
+
if (this._statementStack[this._statementStack.length - 1][0] !== identifier)
|
|
432
|
+
throw `the last statement in the stack is not ${identifier}, at file [${this.currentPath}] line [${this.currentLineIndex + 1}]`;
|
|
433
|
+
return this._statementStack.pop()[2];
|
|
434
|
+
}
|
|
435
|
+
get statementStack() {
|
|
436
|
+
return this._statementStack;
|
|
437
|
+
}
|
|
438
|
+
// statement data will stored in core.storage.globalData.script.statement_data
|
|
439
|
+
// the key of the statement data is the line index of the start statement,
|
|
440
|
+
// for example: the key of the statement data of "menu ... end menu"
|
|
441
|
+
// is stored in the key of the line index of the "menu" statement
|
|
442
|
+
getStatementData() {
|
|
443
|
+
let statement_data_in_file = this._core.storage.getSaveableData(this._core.storage.globalData, true, "script", "statement_data", this.currentPath);
|
|
444
|
+
if (!statement_data_in_file["" + this.currentLineIndex]) {
|
|
445
|
+
this._core.script.analyseStatement(this);
|
|
446
|
+
statement_data_in_file = this._core.storage.getSaveableData(this._core.storage.globalData, true, "script", "statement_data", this.currentPath);
|
|
447
|
+
if (!statement_data_in_file[this.currentLineIndex])
|
|
448
|
+
throw `analyse statement failed as statement data not found, at file [${this.currentPath}] line [${this.currentLineIndex + 1}]`;
|
|
449
|
+
}
|
|
450
|
+
return statement_data_in_file[this.currentLineIndex];
|
|
451
|
+
}
|
|
452
|
+
setStatementData(statement_data, start_position) {
|
|
453
|
+
let statement_data_in_file = this._core.storage.getSaveableData(this._core.storage.globalData, true, "script", "statement_data", start_position[0]);
|
|
454
|
+
statement_data_in_file["" + start_position[1]] = statement_data;
|
|
455
|
+
this._core.storage.saveGlobalData();
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
Script.Context = Context;
|
|
459
|
+
let Utils;
|
|
460
|
+
(function (Utils) {
|
|
461
|
+
Utils.joinSlicedArgs = joinObjs2Str;
|
|
462
|
+
Utils.splitRawtext = splitStr2Objs;
|
|
463
|
+
function splitCommas(rawtext) {
|
|
464
|
+
let slicedArgs = splitStr2Objs(rawtext);
|
|
465
|
+
// console.log(`splitCommas rawtext: ${rawtext}, slicedArgs: ${JSON.stringify(slicedArgs)}`);
|
|
466
|
+
let res = [];
|
|
467
|
+
for (let i = 0; i < slicedArgs.length; i++) {
|
|
468
|
+
if (slicedArgs[i].isQuoted)
|
|
469
|
+
res.push(`"${slicedArgs[i].str}"`);
|
|
470
|
+
else if (slicedArgs[i].isSquared)
|
|
471
|
+
res.push(`[${slicedArgs[i].str}]`);
|
|
472
|
+
else if (slicedArgs[i].isRounded)
|
|
473
|
+
res.push(`(${slicedArgs[i].str})`);
|
|
474
|
+
else {
|
|
475
|
+
slicedArgs[i].str.split(",").forEach((str) => {
|
|
476
|
+
str = str.trim();
|
|
477
|
+
if (str)
|
|
478
|
+
res.push(str);
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
// console.log(`splitCommas res: ${JSON.stringify(res)}`);
|
|
483
|
+
return res;
|
|
484
|
+
}
|
|
485
|
+
Utils.splitCommas = splitCommas;
|
|
486
|
+
function parseTuple(rawtext) {
|
|
487
|
+
if (rawtext.length < 2 ||
|
|
488
|
+
rawtext[0] !== "(" ||
|
|
489
|
+
rawtext[rawtext.length - 1] !== ")") {
|
|
490
|
+
throw `invalid tuple: ${rawtext}`;
|
|
491
|
+
}
|
|
492
|
+
rawtext = rawtext.slice(1, rawtext.length - 1);
|
|
493
|
+
// console.log(`parseTuple rawtext: ${rawtext}`);
|
|
494
|
+
return parseHzsArgs(rawtext);
|
|
495
|
+
}
|
|
496
|
+
Utils.parseTuple = parseTuple;
|
|
497
|
+
function parseArray(rawtext) {
|
|
498
|
+
if (rawtext.length < 2 ||
|
|
499
|
+
rawtext[0] !== "[" ||
|
|
500
|
+
rawtext[rawtext.length - 1] !== "]") {
|
|
501
|
+
throw `invalid array: ${rawtext}`;
|
|
502
|
+
}
|
|
503
|
+
rawtext = rawtext.slice(1, rawtext.length - 1);
|
|
504
|
+
return parseHzsArgs(rawtext);
|
|
505
|
+
}
|
|
506
|
+
Utils.parseArray = parseArray;
|
|
507
|
+
function parseHzsArgs(rawtext) {
|
|
508
|
+
rawtext = rawtext.trim();
|
|
509
|
+
let arr = splitCommas(rawtext);
|
|
510
|
+
let res = arr.map((str) => {
|
|
511
|
+
if (str.startsWith("("))
|
|
512
|
+
return parseTuple(str);
|
|
513
|
+
else if (str.startsWith("["))
|
|
514
|
+
return parseArray(str);
|
|
515
|
+
else
|
|
516
|
+
return str;
|
|
517
|
+
});
|
|
518
|
+
// console.log(`parseHzsArgs from: "${rawtext}" ; res: ${JSON.stringify(res)}`);
|
|
519
|
+
return res;
|
|
520
|
+
}
|
|
521
|
+
Utils.parseHzsArgs = parseHzsArgs;
|
|
522
|
+
})(Utils = Script.Utils || (Script.Utils = {}));
|
|
523
|
+
class ContextForAnalyseStatement extends Context {
|
|
524
|
+
startStatement(identifier, data = {}) {
|
|
525
|
+
return super.startStatement(identifier, data !== null && data !== void 0 ? data : {});
|
|
526
|
+
}
|
|
527
|
+
endStatement(identifier) {
|
|
528
|
+
let statement_data = super.endStatement(identifier);
|
|
529
|
+
// save analysed statement data
|
|
530
|
+
let statement_data_in_file = this._core.storage.getSaveableData(this._core.storage.globalData, true, "script", "statement_data", this.currentPath);
|
|
531
|
+
statement_data_in_file["" + this.currentLineIndex] = statement_data;
|
|
532
|
+
this._core.storage.saveGlobalData();
|
|
533
|
+
return statement_data;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
Script.ContextForAnalyseStatement = ContextForAnalyseStatement;
|
|
537
|
+
})(Script || (Script = {}));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// import * as hmFS from '@zos/fs'
|
|
2
|
+
let cache = new Map();
|
|
3
|
+
export function readline(_core, path, line_index) {
|
|
4
|
+
if (cache.has(path)) {
|
|
5
|
+
return cache.get(path)[line_index];
|
|
6
|
+
}
|
|
7
|
+
// Get Command
|
|
8
|
+
let hzsContent = _core.platform.readFileSync({
|
|
9
|
+
path,
|
|
10
|
+
options: { encoding: "utf8" },
|
|
11
|
+
});
|
|
12
|
+
let file = hzsContent.split(/\r?\n|(?<!\n)\r/);
|
|
13
|
+
cache.set(path, file);
|
|
14
|
+
return file[line_index];
|
|
15
|
+
}
|