1 | const {isExpression, isToken} = require('./lang');
|
2 | const _ = require('lodash');
|
3 |
|
4 | function searchExpressions(callback, ...expressions) {
|
5 | const nextExpr = expressions;
|
6 | const visited = new Set();
|
7 | expressions.forEach(e => visited.add(e))
|
8 | while (nextExpr.length) {
|
9 | const currentExpr = nextExpr.shift();
|
10 | callback(currentExpr);
|
11 | currentExpr.forEach(subExpression => {
|
12 | if (isExpression(subExpression) && !visited.has(subExpression)) {
|
13 | nextExpr.push(subExpression);
|
14 | visited.add(subExpression)
|
15 | }
|
16 | });
|
17 | }
|
18 | }
|
19 |
|
20 |
|
21 | function searchExpressionsWithoutInnerFunctions(callback, ...expressions) {
|
22 | const nextExpr = expressions;
|
23 | const visited = new Set();
|
24 | expressions.forEach(e => visited.add(e))
|
25 | while (nextExpr.length) {
|
26 | const currentExpr = nextExpr.shift();
|
27 | callback(currentExpr);
|
28 | currentExpr.forEach(subExpression => {
|
29 | if (isExpression(subExpression) && !visited.has(subExpression) && subExpression[0].$type !== 'func') {
|
30 | nextExpr.push(subExpression);
|
31 | visited.add(subExpression)
|
32 | }
|
33 | });
|
34 | }
|
35 | }
|
36 |
|
37 | function flattenExpression(...expressions) {
|
38 | const output = [];
|
39 | searchExpressions((expr) => output.push(expr), ...expressions);
|
40 | return output;
|
41 | }
|
42 |
|
43 | function flattenExpressionWithoutInnerFunctions(...expr) {
|
44 | const output = [];
|
45 | searchExpressionsWithoutInnerFunctions((expr) => output.push(expr), ...expr);
|
46 | return output;
|
47 | }
|
48 |
|
49 |
|
50 | function getAllFunctions(expr) {
|
51 | const output = [expr];
|
52 | searchExpressions((e) => {
|
53 | if (e[0].$type === 'func') {
|
54 | output.push(e)
|
55 | }
|
56 | }, [expr]);
|
57 | return output;
|
58 | }
|
59 |
|
60 | function processByExecutionOrder(callback, context, recurIntoFunctions, expr) {
|
61 | if (!isExpression(expr)) {
|
62 | return context;
|
63 | }
|
64 |
|
65 | context = callback(expr[0], context);
|
66 | if (expr[0].$type === 'ternary') {
|
67 | const newContext = callback(expr[1], context);
|
68 | processByExecutionOrder(callback, callback(expr[2], newContext), recurIntoFunctions, expr[2])
|
69 | processByExecutionOrder(callback, callback(expr[3], newContext), recurIntoFunctions, expr[3])
|
70 | } else {
|
71 | expr.slice(1).map(t => {
|
72 | if (isToken(t)) {
|
73 | context = callback(t, context);
|
74 | } else if (isExpression(t)) {
|
75 | if (expr[0].$type === 'func' && !recurIntoFunctions) {
|
76 | return;
|
77 | }
|
78 | context = processByExecutionOrder(callback, callback(expr[2], context), recurIntoFunctions, t)
|
79 | }
|
80 | })
|
81 | }
|
82 | return context;
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 | module.exports = {
|
88 | searchExpressions,
|
89 | searchExpressionsWithoutInnerFunctions,
|
90 | flattenExpression,
|
91 | flattenExpressionWithoutInnerFunctions,
|
92 | getAllFunctions,
|
93 | processByExecutionOrder
|
94 | }; |
\ | No newline at end of file |