evaluator.go | |
---|---|
|
|
Package evaluator contains expression evaluator with the ability to provide named values into expressions. Evaluator supports all arithmetic operators, logical operators, arithmetic operators, and it is possible to use parenthesis to change priority of operations. |
|
Documentation in literate-programming-style is available at: https://redhatinsights.github.io/insights-operator-utils/packages/evaluator/evaluator.html |
|
Operator type for all functions that implements any dyadic operator |
|
toint function convert Boolean value to integer |
|
tobool function convert integer value to Boolean |
|
evaluate function evaluates expression represented as token sequence in postfix notation |
|
all implemented dyadic (arithmetic) operators |
|
operand stack is empty at beginning |
|
token sequence processing |
|
does the token represents dyadic operator? |
|
we found and operator -> evaluate the dyadic operation (with two operands) -> store result onto the stack |
|
token does not represent operator: -> is it integer value or identifier then? |
|
we found integer value so store it to the operand stack |
|
we found identifier name so try to find the value + store the value onto the operand stack |
|
token of different type (shall it happen?) |
|
now the operand stack should contain just one value -> the result |
|
performArithmeticOperation function perform selected arithmetic operator against two values taken from stack |
|
read the second operand from the stack + check for empty stack |
|
read the first operand from the stack + check for empty stack |
|
divide by zero is not supported |
|
perform the selected arithmeric operation |
|
store result (one value) back onto the stack |
|
no error |
|
Evaluate function evaluates given algebraic expression and return its result |
|
scanner object (lexer) |
|
structure that represents set of source file(s) |
|
info about source file |
|
initialize the scanner |
|
transform input expression into postfix notation |
|
evaluate the expression represented in postfix notation |
|
stack should not be empty |
|
stack size should be exactly 1 |
|
stack is not empty, so its TOP is the result |
|