GO package for analysis and calculation of expressions
Go to file
2024-05-25 03:30:26 +02:00
doc Doc: more details on some operators 2024-05-20 15:50:45 +02:00
tools func-bolerplate.bash: generate Go source for a new functions module 2024-05-01 07:13:52 +02:00
ast_test.go the function call procedure now check the number of actual parameters against the numer of formal parameters 2024-05-04 22:35:03 +02:00
ast.go context.go: setVar() renamed as UnsafeSetVar() 2024-05-19 01:27:44 +02:00
byte-slider.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
common-errors.go common-errors.go: moved here some errors 2024-05-15 22:01:13 +02:00
common-params.go New interface and implementation to model function parameters 2024-05-22 20:52:44 +02:00
common-type-names.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
context-helpers.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
context.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
control.go added the copywrite comment header in source files that lacked it 2024-04-13 06:00:22 +02:00
data-cursor.go list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
dict_test.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
expr_test.go simple-var-store.go and simple-func-store.go merged in a single file named simple-store.go 2024-05-23 07:46:31 +02:00
file-reader.expr some expression and data files used by tests 2024-05-03 08:48:29 +02:00
formatter.go formatter.go: Truncate function and number type names 2024-05-20 05:25:04 +02:00
func-base.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
func-import.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
func-math.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
func-os.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
func-string.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
funcs_test.go New interface and implementation to model function parameters 2024-05-22 20:52:44 +02:00
function.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
global-context.go simple-var-store.go and simple-func-store.go merged in a single file named simple-store.go 2024-05-23 07:46:31 +02:00
go.mod added go.mod 2024-03-26 08:56:20 +01:00
graph_test.go added the graph source files 2024-04-13 05:54:34 +02:00
graph.go graph.go: remove unused Reticle code 2024-04-13 06:05:04 +02:00
helpers_test.go simple-var-store.go and simple-func-store.go merged in a single file named simple-store.go 2024-05-23 07:46:31 +02:00
helpers.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
int.list some expression and data files used by tests 2024-05-03 08:48:29 +02:00
it-range.go added the copywrite comment header in source files that lacked it 2024-04-13 06:00:22 +02:00
iter-list.go list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
iterator_test.go new test files and some refactorings 2024-05-06 05:52:25 +02:00
iterator.expr some expression and data files used by tests 2024-05-03 08:48:29 +02:00
iterator.go list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
list_test.go list_test.go: test nr 15,16,17 fixed 2024-05-25 03:28:01 +02:00
module-register.go Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +02:00
operand-dict.go New interface to Typer: the function TypeName() returns a more readable type name 2024-05-19 02:23:28 +02:00
operand-expr.go The content of round bracket now returns an expressione term. This way the content is decoupled from external terms. 2024-05-19 01:34:07 +02:00
operand-func.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
operand-iterator.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
operand-list.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
operand-literal.go operand-const.go -> operand-literal.go 2024-05-14 04:59:55 +02:00
operand-selector-case.go operand-selector-case.go: String() function added to the selectorCase type 2024-04-13 04:32:54 +02:00
operand-var.go Added virtual symbol SymVariable translated from general real SymIdentifier symbol 2024-05-20 06:05:24 +02:00
operator-assign.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
operator-bool.go control: exported all control variables by renaming them in upper case 2024-04-13 05:16:23 +02:00
operator-builtin.go Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +02:00
operator-but.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-coalesce.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
operator-context.go New interface and implementation to model function parameters 2024-05-22 20:52:44 +02:00
operator-ctrl.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-dot.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
operator-fact.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-fraction.go operator-fraction.go: Typer interface implementation 2024-05-20 05:31:20 +02:00
operator-in.go new operator 'in': it returns true if a item belongs to a list or if a key belongs to a dict 2024-05-19 01:44:50 +02:00
operator-include.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-index.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
operator-insert.go Added virtual symbol SymVariable translated from general real SymIdentifier symbol 2024-05-20 06:05:24 +02:00
operator-iter-value.go removed commented code 2024-05-04 00:57:21 +02:00
operator-length.go adapted and enhanced the dict operations to make them compatible with the new DictType 2024-05-19 01:38:07 +02:00
operator-post-inc.go Added virtual symbol SymVariable translated from general real SymIdentifier symbol 2024-05-20 06:05:24 +02:00
operator-prod.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-rel.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-selector.go removed commented code 2024-04-27 06:14:09 +02:00
operator-sign.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-sum.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
parser_test.go simple-var-store.go and simple-func-store.go merged in a single file named simple-store.go 2024-05-23 07:46:31 +02:00
parser.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
README.adoc doc: Syntax of expressions moved from README.adoc to doc/Expr.doc 2024-04-14 08:16:01 +02:00
sample-export-all.expr Operator '@@' (export-all) added. Experimental include() function also added 2024-04-06 03:06:07 +02:00
scanner_test.go scanner: Fixed decimal number parser; it didn't save parenthesis around period part 2024-05-14 05:41:10 +02:00
scanner.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
simple-func-store.go.bak simple-var-store.go and simple-func-store.go merged in a single file named simple-store.go 2024-05-23 07:46:31 +02:00
simple-store.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
simple-var-store.go.bak simple-var-store.go and simple-func-store.go merged in a single file named simple-store.go 2024-05-23 07:46:31 +02:00
strings_test.go strings_test.go: test nr 5,6 fixed 2024-05-25 03:30:26 +02:00
symbol.go new syntax to get items from collection: collection[index]. Supported collections are string, list and dict 2024-05-24 22:51:01 +02:00
term_test.go utils_test.go: new test file 2024-05-18 07:06:06 +02:00
term-constuctor-registry.go Remove the unused 'parent' param from the function newTerm(). 2024-05-11 06:41:06 +02:00
term.go term.go: Error messagge about incompatible types now truncates long values 2024-05-20 05:26:33 +02:00
test-file.txt fixed all errors in test files 2024-04-20 06:04:35 +02:00
test-funcs.expr Operator '@@' (export-all) added. Experimental include() function also added 2024-04-06 03:06:07 +02:00
token_test.go fixed all errors in test files 2024-04-20 06:04:35 +02:00
token.go token.go: function IsSymbol() 2024-05-04 19:08:02 +02:00
utils_test.go utils_test.go: new test file 2024-05-18 07:06:06 +02:00
utils.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00

README

1. Expr

Expr is a GO package capable of analysing, interpreting and calculating expressions.

A few examples to get started.

Examples taken from parser_test.go
`1.0 / 2`                      // 0.5
`435 + 105 * 2 - 1`            // 644
`4 == (3-1)*(10/5)`            // true
`"uno" * (2+1)`                // `unounouno`
`2+3 but 5*2`                  // 10 (1)
`add(add(1+4),3+2,5*(3-2))`    // 15 (2)
`a=5; b=2; add(a, b*3)`        // 11 (3)
`two=func(){2}; two()`         // 2  (4)
`double=func(x){2*x}; a=4+1; two=func() {2}; (double(3+a) + 1) * two()`  // 34
`import("./test-funcs.expr"); (double(3+a) + 1) * two()`  // 34 (5)
`[1,2,"hello"]`                // Mixed types list
`[1,2]+[3]`                    // append list, result: [1,2,3]
`add([1,[2,2],3,2])`           // Deep list sum, result: 10 (2)
`[a=1,b=2,c=3] but a+b+c`      // 6
1 but operator.
2 The add() function definition may be changed in the future.
3 Multiple expression. Only the last expression value will returned.
4 Simple function definition: two() returns 2.
5 import() function imports expressions from the specified files. See file test-funcs.expr.

1.1. Usage

package main

import (
	"fmt"
	"strings"
	"git.portale-stac.it/go-pkg/expr"
)

func main() {
	ctx := expr.NewSimpleVarStore()
	ctx.SetVar("var", 4)

	source := `(3-1)*(10/5) == var`

	r := strings.NewReader(source)
	scanner := expr.NewScanner(r, expr.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.

package main

import (
	"fmt"
	"git.portale-stac.it/go-pkg/expr"
)

func main() {
	ctx := expr.NewSimpleVarStore()
	ctx.SetVar("var", 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)
	}
}

Here is another equivalent version.

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", 4}); err == nil {
		fmt.Printf("%q -> %v [%T]\n", source, result, result)
	} else {
		fmt.Println("Error calculating the expression:", err)
	}
}

2. Context of evaluation

Unless helpers functions like expr.EvalStringA() are used, a context is required to compute an expession.

A context is an object that implements the expr.ExprContext interface. This interface specifies a set of function to handle variables and functions.

Variables and functions can be added to a context both programmatically and ad an effect of the expression computation.

3. Expressions syntax

See TODO link to doc/Expr.html