MCPcopy
hub / github.com/autoNumeric/autoNumeric / evaluate

Method evaluate

src/maths/Evaluator.js:43–69  ·  view source on GitHub ↗
(subtree)

Source from the content-addressed store, hash-verified

41 }
42
43 evaluate(subtree) {
44 if (subtree === void(0) || subtree === null) {
45 throw new Error(`Invalid AST sub-tree`);
46 }
47
48 if (subtree.type === 'number') {
49 return subtree.value;
50 } else if (subtree.type === 'unaryMinus') {
51 return -this.evaluate(subtree.left);
52 } else {
53 const left = this.evaluate(subtree.left);
54 const right = this.evaluate(subtree.right);
55
56 switch (subtree.type) {
57 case 'op_+':
58 return Number(left) + Number(right);
59 case 'op_-':
60 return left - right;
61 case 'op_*':
62 return left * right;
63 case 'op_/':
64 return left / right;
65 default :
66 throw new Error(`Invalid operator '${subtree.type}'`);
67 }
68 }
69 }
70}

Callers 2

testParserFunction · 0.80
_exitFormulaModeMethod · 0.80

Calls

no outgoing calls

Tested by 1

testParserFunction · 0.64