ast_test.go | ||
ast.go | ||
byte-slider.go | ||
context.go | ||
funcs-math.go | ||
go.mod | ||
helpers_test.go | ||
helpers.go | ||
it-range.go | ||
iterator.go | ||
operand-const.go | ||
operand-func.go | ||
operand-list.go | ||
operand-var.go | ||
operator-bool.go | ||
operator-fact.go | ||
operator-prod.go | ||
operator-rel.go | ||
operator-sign.go | ||
operator-sum.go | ||
parser_test.go | ||
parser.go | ||
README.adoc | ||
scanner_test.go | ||
scanner.go | ||
simple-func-store.go | ||
simple-var-store.go | ||
symbol.go | ||
term_test.go | ||
term-constuctor-registry.go | ||
term.go | ||
token_test.go | ||
token.go | ||
utils.go |
Expr
1. Expr
Expr is a GO package capable of analysing, interpreting and calculating expressions.
1.1. Usage
import (
"fmt"
"strings"
"git.portale-stac.it/go-pkg/expr"
)
func main() {
ctx := expr.NewSimpleVarStore()
ctx.SetValue("var", int64(4))
source := `(3-1)*(10/5) == var`
r := strings.NewReader(source)
scanner := expr.NewScanner(r, DefaultTranslations())
parser := expr.NewParser(ctx)
if ast, err := parser.parse(scanner); err == nil {
if result, err := ast.eval(ctx); err == nil {
fmt.Printf("%q -> %v [%T]\n", source, result, result)
} else {
fmt.Println("Error calculating the expression:", err)
}
} else {
fmt.Println("Error parsing the expression:", err)
}
}
The above program is equivalent to the following one.
import (
"fmt"
"git.portale-stac.it/go-pkg/expr"
)
func main() {
ctx := expr.NewSimpleVarStore()
ctx.SetValue("var", int64(4))
source := `(3-1)*(10/5) == var`
if result, err := expr.evalString(ctx, source); err == nil {
fmt.Printf("%q -> %v [%T]\n", source, result, result)
} else {
fmt.Println("Error calculating the expression:", err)
}
}
1.2. Data types
Expr supports numerical, string, relational, and boolean expressions.
1.2.1. Numbers
Numbers can be integers (GO int64) and float (GO float64). In mixed operations involving integers and floats, integers are automatically promoted to floats.
Symbol | Operation | Description | Examples |
---|---|---|---|
|
change sign |
Change the sign of values |
|
|
sum |
Add two values |
|
|
subtracion |
Subtract the right value from the left one |
|
|
product |
Multiply two values |
|
|
Division |
Divide the left value by the right one |
|
|
Float division |
Force float division |
|
|
Modulo |
Remainder of the integer division |
|
1.2.2. String
Strings are character sequences enclosed between two double quote "
.
1.2.3. Boolean
Boolean data type has two values only: true and false. Relational and Boolean expressions produce a Boolean values.
Symbol | Operation | Description | Examples |
---|---|---|---|
|
Less |
True if the left value is less than the right one |
|
|
Less or Greater |
True if the left value is less or greater than the right one |
|
Symbol | Operation | Description | Examples |
---|---|---|---|
|
Not |
True if the right value is false |
|
|
And |
True if both left and right values are true |
|
|
Or |
True if at least one of the left and right values integers true |
|