-
-
Notifications
You must be signed in to change notification settings - Fork 38
Calculate
After tokenizing the formula, the tokens will be put into the Formula
class to calculate. You can have a look to src/compiler/Evaluator.ts
.
First, it will look through the tokens, extract some important details of the formula, such as the numbers and the operators, and calculate the value inside brackets and the value of functions. And then put these further details into some arrays.
Next, it iterates through the array of operator for 3 times.
- The first time is to calculate the logical operator.
- The second time is for multiplying & dividing.
- The third time is for adding & subbing.
Let's continue taking the formula for example:
After extracting the details, it creates an array of number:
[35, -24, 20]
The number
-24
is calculated from the large bracket.
an array of operator:
["×", "-"]
In the source code, the two arrays don't store the value directly. Instead, they store tokens.
var numbers: List<NumberToken> = new List(); var operators: List<OperatorToken> = new List();
It traverses the array of operator, then find the corresponding number by the index of the operator, so that it can calculate the value of the formula.
Finally, it will return a number. In this example, it's -860
.
Copyright © 2024 NriotHrreion <[email protected]>