README.adoc: example programs updated
This commit is contained in:
parent
024ff42be0
commit
a9b143d012
33
README.adoc
33
README.adoc
@ -63,18 +63,17 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.portale-stac.it/go-pkg/expr"
|
"git.portale-stac.it/go-pkg/expr"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := expr.NewSimpleVarStore()
|
ctx := expr.NewSimpleVarStore()
|
||||||
ctx.SetValue("var", int64(4))
|
ctx.SetVar("var", int64(4))
|
||||||
|
|
||||||
source := `(3-1)*(10/5) == var`
|
source := `(3-1)*(10/5) == var`
|
||||||
|
|
||||||
r := strings.NewReader(source)
|
r := strings.NewReader(source)
|
||||||
scanner := expr.NewScanner(r, DefaultTranslations())
|
scanner := expr.NewScanner(r, expr.DefaultTranslations())
|
||||||
parser := expr.NewParser(ctx)
|
parser := expr.NewParser(ctx)
|
||||||
|
|
||||||
if ast, err := parser.Parse(scanner); err == nil {
|
if ast, err := parser.Parse(scanner); err == nil {
|
||||||
@ -88,10 +87,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
The above program is equivalent to the following one.
|
The above program is equivalent to the following one.
|
||||||
|
|
||||||
[source,go]
|
[source,go]
|
||||||
----
|
----
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.portale-stac.it/go-pkg/expr"
|
"git.portale-stac.it/go-pkg/expr"
|
||||||
@ -99,7 +101,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := expr.NewSimpleVarStore()
|
ctx := expr.NewSimpleVarStore()
|
||||||
ctx.SetValue("var", int64(4))
|
ctx.SetVar("var", int64(4))
|
||||||
|
|
||||||
source := `(3-1)*(10/5) == var`
|
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
|
=== Data types
|
||||||
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
|
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user