1 | import { isFunctionNode, isNode, isOperatorNode, isParenthesisNode, isSymbolNode } from '../../../utils/is'
|
2 | import { factory } from '../../../utils/factory'
|
3 |
|
4 | const name = 'resolve'
|
5 | const dependencies = [
|
6 | 'parse',
|
7 | 'FunctionNode',
|
8 | 'OperatorNode',
|
9 | 'ParenthesisNode'
|
10 | ]
|
11 |
|
12 | export const createResolve = factory(name, dependencies, ({
|
13 | parse,
|
14 | FunctionNode,
|
15 | OperatorNode,
|
16 | ParenthesisNode
|
17 | }) => {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function resolve (node, scope) {
|
36 | if (!scope) {
|
37 | return node
|
38 | }
|
39 | if (isSymbolNode(node)) {
|
40 | const value = scope[node.name]
|
41 | if (isNode(value)) {
|
42 | return resolve(value, scope)
|
43 | } else if (typeof value === 'number') {
|
44 | return parse(String(value))
|
45 | }
|
46 | } else if (isOperatorNode(node)) {
|
47 | const args = node.args.map(function (arg) {
|
48 | return resolve(arg, scope)
|
49 | })
|
50 | return new OperatorNode(node.op, node.fn, args, node.implicit)
|
51 | } else if (isParenthesisNode(node)) {
|
52 | return new ParenthesisNode(resolve(node.content, scope))
|
53 | } else if (isFunctionNode(node)) {
|
54 | const args = node.args.map(function (arg) {
|
55 | return resolve(arg, scope)
|
56 | })
|
57 | return new FunctionNode(node.name, args)
|
58 | }
|
59 | return node
|
60 | }
|
61 |
|
62 | return resolve
|
63 | })
|