README.adoc: example programs updated

This commit is contained in:
Celestino Amoroso 2024-04-09 06:29:44 +02:00
parent 024ff42be0
commit a9b143d012

View File

@ -63,18 +63,17 @@ package main
import (
"fmt"
"strings"
"git.portale-stac.it/go-pkg/expr"
)
func main() {
ctx := expr.NewSimpleVarStore()
ctx.SetValue("var", int64(4))
ctx.SetVar("var", int64(4))
source := `(3-1)*(10/5) == var`
r := strings.NewReader(source)
scanner := expr.NewScanner(r, DefaultTranslations())
scanner := expr.NewScanner(r, expr.DefaultTranslations())
parser := expr.NewParser(ctx)
if ast, err := parser.Parse(scanner); err == nil {
@ -88,10 +87,13 @@ func main() {
}
}
----
The above program is equivalent to the following one.
[source,go]
----
package main
import (
"fmt"
"git.portale-stac.it/go-pkg/expr"
@ -99,7 +101,7 @@ import (
func main() {
ctx := expr.NewSimpleVarStore()
ctx.SetValue("var", int64(4))
ctx.SetVar("var", int64(4))
source := `(3-1)*(10/5) == var`
@ -111,6 +113,29 @@ func main() {
}
----
Here is another equivalent version.
[source,go]
----
package main
import (
"fmt"
"git.portale-stac.it/go-pkg/expr"
)
func main() {
source := `(3-1)*(10/5) == var`
if result, err := expr.EvalStringA(source, expr.Arg{"var", int64(4)}); err == nil {
fmt.Printf("%q -> %v [%T]\n", source, result, result)
} else {
fmt.Println("Error calculating the expression:", err)
}
}
----
=== Data types
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.