GO package for analysis and calculation of expressions
Go to file
2024-04-02 06:49:16 +02:00
ast_test.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
ast.go preset definitions and functions were moved to the new preset.go file 2024-04-02 05:00:48 +02:00
byte-slider.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
context.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
func-import.go import() function added 2024-04-02 06:49:16 +02:00
func-math.go import() function added 2024-04-02 06:49:16 +02:00
go.mod added go.mod 2024-03-26 08:56:20 +01:00
helpers_test.go helpers_test.go: changed some comments 2024-03-28 06:34:40 +01:00
helpers.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
it-range.go Added Iterator interface and two implementation: list itrator and range iterator 2024-03-30 06:56:12 +01:00
iterator.go Added Iterator interface and two implementation: list itrator and range iterator 2024-03-30 06:56:12 +01:00
operand-const.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
operand-expr.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
operand-func.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
operand-list.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
operand-var.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
operator-assign.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
operator-bool.go 'and' and 'or' operators are now evaluated using logic shortcut (this behaviour can be changed setting system var '_bool_shortcut' to false 2024-03-31 05:09:24 +02:00
operator-but.go New operators 'but' and assignment ('=') 2024-03-30 08:09:41 +01:00
operator-fact.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
operator-prod.go added operator '%' (division remainder) and test 2024-03-26 09:27:14 +01:00
operator-rel.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
operator-sign.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
operator-sum.go The plus and minus operators now support lists for join and difference respectively 2024-03-30 07:05:22 +01:00
parser_test.go import() function added 2024-04-02 06:49:16 +02:00
parser.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
preset.go import() function added 2024-04-02 06:49:16 +02:00
README.adoc Second incomplete draft 2024-03-27 08:44:47 +01:00
scanner_test.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
scanner.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
simple-func-store.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
simple-var-store.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
symbol.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
term_test.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
term-constuctor-registry.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
term.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
test-funcs.expr import() function added 2024-04-02 06:49:16 +02:00
token_test.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
token.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
utils.go Expressions now support function definition 2024-04-02 04:36:03 +02:00

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.

Table 1. Arithmetic operators
Symbol Operation Description Examples

+ / -

change sign

Change the sign of values

-1 [-1], -(+2) [-2]

+

sum

Add two values

-1 + 2 [1], 4 + 0.5 [4.5]

-

subtracion

Subtract the right value from the left one

3 - 1 [2], 4 - 0.5 [3.5]

*

product

Multiply two values

-1 * 2 [-2], 4 * 0.5 [2.0]

/

Division

Divide the left value by the right one

-1 / 2 [0], 1.0 / 2 [0.5]

./

Float division

Force float division

-1 ./ 2 [-0.5]

%

Modulo

Remainder of the integer division

5 % 2 [1]

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.

Table 2. Relational operators
Symbol Operation Description Examples

<

Less

True if the left value is less than the right one

5 < 2 [false], "a" < "b" [true]

<=

Less or Greater

True if the left value is less or greater than the right one

5 ⇐ 2 [false], "b" ⇐ "b" [true]

Table 3. Boolean operators
Symbol Operation Description Examples

NOT

Not

True if the right value is false

NOT true [false], NOT (2 < 1) [true]

AND / &&

And

True if both left and right values are true

false && true [false], "a" < "b" AND NOT (2 < 1) [true]

OR / ||

Or

True if at least one of the left and right values integers true

false or true [true], "a" == "b" OR (2 == 1) [false]