-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.ts
More file actions
292 lines (277 loc) · 9.51 KB
/
Copy patharray.ts
File metadata and controls
292 lines (277 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as util from '../util';
import {Expression, type ExpressionResult, Literal} from '../codegen-steps';
import {deepEqualCodegen} from '@jsonjoy.com/util/lib/json-equal/deepEqualCodegen';
import type * as types from '../types';
import type {Vars} from '../Vars';
const {isArray} = Array;
const objectKeys = Object.keys;
/**
* Creates a deep clone of any JSON-like object.
*
* @param obj Any plain POJO object.
* @returns A deep copy of the object.
*/
export const clone = <T = unknown>(obj: T): T => {
if (!obj) return obj;
if (isArray(obj)) {
const arr: unknown[] = [];
const length = obj.length;
for (let i = 0; i < length; i++) arr.push(clone(obj[i]));
return arr as unknown as T;
} else if (typeof obj === 'object') {
const keys = objectKeys(obj!);
const length = keys.length;
const newObject: any = {};
for (let i = 0; i < length; i++) {
const key = keys[i];
newObject[key] = clone((obj as any)[key]);
}
return newObject;
}
return obj;
};
const createSubExpressionOperator = <N extends string>(
name: N,
fn: (arr: unknown[], varname: string, vars: Vars, run: () => unknown) => unknown,
) => {
return [
name,
[],
3,
(expr: types.TernaryExpression<N>, ctx) => {
const arr = util.asArr(ctx.eval(expr[1], ctx));
const varname = util.asStr(util.asLiteral(expr[2]));
const expression = expr[3];
const run = () => ctx.eval(expression, ctx);
return fn(arr, varname, ctx.vars, run);
},
(ctx: types.OperatorCodegenCtx<types.TernaryExpression<N>>): ExpressionResult => {
ctx.link(util.asArr, 'asArr');
ctx.link(fn, name);
const varname = util.asStr(util.asLiteral(ctx.expr[2]));
const d = ctx.link(ctx.subExpression(ctx.expr[3]));
const operand1 = ctx.operands[0];
const arr =
operand1 instanceof Literal && operand1.val instanceof Array
? JSON.stringify(operand1.val)
: `asArr(${operand1})`;
const js = `${name}(${arr},${JSON.stringify(varname)},vars,function(){return ${d}(vars)})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.TernaryExpression<N>>;
};
export const arrayOperators: types.OperatorDefinition<any>[] = [
[
'concat',
['++'],
-1,
(expr: types.ExprConcat, ctx) => {
const arrays = expr.slice(1).map((e) => ctx.eval(e, ctx));
return util.concat(arrays);
},
(ctx: types.OperatorCodegenCtx<types.ExprConcat>): ExpressionResult => {
ctx.link(util.concat, 'concat');
const js = `concat([(${ctx.operands.join('),(')})])`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprConcat>,
[
'push',
[],
-1,
(expr: types.ExprPush, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
const arr = clone(util.asArr(operand1));
for (let i = 2; i < expr.length; i++) arr.push(ctx.eval(expr[i], ctx));
return arr;
},
(ctx: types.OperatorCodegenCtx<types.ExprPush>): ExpressionResult => {
const arrOperand = ctx.operands[0];
let arr: Literal | Expression;
if (arrOperand instanceof Literal) {
arr = new Literal(clone(util.asArr(arrOperand.val)));
} else {
ctx.link(util.asArr, 'asArr');
arr = new Expression(`asArr(${arrOperand})`);
}
const rArr = ctx.var('' + arr);
const pushes: string[] = [];
for (let i = 1; i < ctx.operands.length; i++) {
const operand = ctx.operands[i];
pushes.push(`(${rArr}.push(${operand}))`);
}
return new Expression(`(${pushes.join(',')},${rArr})`);
},
] as types.OperatorDefinition<types.ExprPush>,
[
'head',
[],
2,
(expr: types.ExprHead, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
const operand2 = ctx.eval(expr[2], ctx);
return util.head(operand1, operand2);
},
(ctx: types.OperatorCodegenCtx<types.ExprHead>): ExpressionResult => {
ctx.link(util.head, 'head');
const js = `head((${ctx.operands[0]}),(${ctx.operands[1]}))`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprHead>,
[
'sort',
[],
1,
(expr: types.ExprSort, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
const arr = util.asArr(operand1);
/** @todo use `.toSorted()`, once it is more common. */
return [...arr].sort();
},
(ctx: types.OperatorCodegenCtx<types.ExprSort>): ExpressionResult => {
ctx.link(util.asArr, 'asArr');
const js = `[...asArr(${ctx.operands[0]})].sort()`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprSort>,
[
'reverse',
[],
1,
(expr: types.ExprReverse, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
const arr = util.asArr(operand1);
/** @todo use `.toReversed()`, once it is more common. */
return [...arr].reverse();
},
(ctx: types.OperatorCodegenCtx<types.ExprReverse>): ExpressionResult => {
ctx.link(util.asArr, 'asArr');
const js = `[...asArr(${ctx.operands[0]})].reverse()`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprReverse>,
[
'in',
[],
2,
(expr: types.ExprIn, ctx) => {
const arr = ctx.eval(expr[1], ctx);
const val = ctx.eval(expr[2], ctx);
return util.isInArr(arr, val);
},
(ctx: types.OperatorCodegenCtx<types.ExprIn>): ExpressionResult => {
const arr = ctx.operands[0];
const val = ctx.operands[1];
if (val instanceof Literal) {
const fnJs = deepEqualCodegen(val.val);
const d = ctx.const(fnJs);
ctx.link(util.isInArr2, 'isInArr2');
const js = `isInArr2((${ctx.operands[0]}),${d})`;
return new Expression(js);
}
ctx.link(util.isInArr, 'isInArr');
const js = `isInArr((${ctx.operands[0]}),(${ctx.operands[1]}))`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprIn>,
[
'fromEntries',
[],
1,
(expr: types.ExprFromEntries, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
return util.fromEntries(operand1);
},
(ctx: types.OperatorCodegenCtx<types.ExprFromEntries>): ExpressionResult => {
ctx.link(util.fromEntries, 'fromEntries');
const js = `fromEntries(${ctx.operands[0]})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprFromEntries>,
[
'indexOf',
[],
2,
(expr: types.ExprIndexOf, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
const operand2 = ctx.eval(expr[2], ctx);
return util.indexOf(operand1, operand2);
},
(ctx: types.OperatorCodegenCtx<types.ExprIndexOf>): ExpressionResult => {
const val = ctx.operands[1];
if (val instanceof Literal) {
const fnJs = deepEqualCodegen(val.val);
const d = ctx.const(fnJs);
ctx.link(util.indexOf2, 'indexOf2');
const js = `indexOf2((${ctx.operands[0]}),${d})`;
return new Expression(js);
}
ctx.link(util.indexOf, 'indexOf');
const js = `indexOf((${ctx.operands[0]}),(${ctx.operands[1]}))`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprIndexOf>,
[
'slice',
[],
3,
(expr: types.ExprSlice, ctx) => {
const operand1 = util.asArr(ctx.eval(expr[1], ctx));
const operand2 = util.int(ctx.eval(expr[2], ctx));
const operand3 = util.int(ctx.eval(expr[3], ctx));
return operand1.slice(operand2, operand3);
},
(ctx: types.OperatorCodegenCtx<types.ExprSlice>): ExpressionResult => {
ctx.link(util.asArr, 'asArr');
const js = `asArr(${ctx.operands[0]}).slice((${ctx.operands[1]}),(${ctx.operands[2]}))`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprSlice>,
[
'zip',
[],
2,
(expr: types.ExprZip, ctx) => {
const operand1 = ctx.eval(expr[1], ctx);
const operand2 = ctx.eval(expr[2], ctx);
return util.zip(operand1, operand2);
},
(ctx: types.OperatorCodegenCtx<types.ExprZip>): ExpressionResult => {
ctx.link(util.zip, 'zip');
const js = `zip((${ctx.operands[0]}),(${ctx.operands[1]}))`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprZip>,
createSubExpressionOperator<'filter'>('filter', util.filter),
createSubExpressionOperator<'map'>('map', util.map),
[
'reduce',
[],
5,
(expr: types.ExprReduce, ctx) => {
const arr = util.asArr(ctx.eval(expr[1], ctx));
const initialValue = ctx.eval(expr[2], ctx);
const accname = util.asStr(util.asLiteral(expr[3]));
const varname = util.asStr(util.asLiteral(expr[4]));
const expression = expr[5];
const run = () => ctx.eval(expression, ctx);
return util.reduce(arr, initialValue, accname, varname, ctx.vars, run);
},
(ctx: types.OperatorCodegenCtx<types.ExprReduce>): ExpressionResult => {
ctx.link(util.asArr, 'asArr');
ctx.link(util.reduce, 'reduce');
const accname = util.asStr(util.asLiteral(ctx.expr[3]));
const varname = util.asStr(util.asLiteral(ctx.expr[4]));
const d = ctx.link(ctx.subExpression(ctx.expr[5]));
const operand1 = ctx.operands[0];
const arr =
operand1 instanceof Literal && operand1.val instanceof Array
? JSON.stringify(operand1.val)
: `asArr(${operand1})`;
const js = `reduce((${arr}),(${ctx.operands[1]}),${JSON.stringify(accname)},${JSON.stringify(
varname,
)},vars,function(){return ${d}(vars)})`;
return new Expression(js);
},
] as types.OperatorDefinition<types.ExprReduce>,
];