GO package for analysis and calculation of expressions
Go to file
Celestino Amoroso f347b15146 Control vars are now stored in the globalCtx only.
However, it is still allowed to store control var in a local context in special situation
2024-06-08 05:49:28 +02:00
doc Doc: start of function documentation 2024-06-03 06:27:14 +02:00
tools func-bolerplate.bash: generate Go source for a new functions module 2024-05-01 07:13:52 +02:00
ast.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +02:00
byte-slider.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
common-errors.go use of typeName() in error messages 2024-06-05 05:48:02 +02:00
common-params.go typeFilepath and typeDirpath renamed as paramFilepath and paramDirpath respectively 2024-06-02 11:32:47 +02:00
common-type-names.go common-type-names.go: added type name list-of 2024-06-07 08:59:04 +02:00
context-helpers.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +02:00
context.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
control.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +02:00
data-cursor.go list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
dict-type.go DictType moved from operand-dict.go to the dict-type.go file 2024-06-04 10:59:25 +02:00
file-reader.expr some expression and data files used by tests 2024-05-03 08:48:29 +02:00
formatter.go use of typeName() in error messages 2024-06-05 05:48:02 +02:00
fraction-type.go some identier exported; new file import-utils.go 2024-06-06 05:31:35 +02:00
func-base.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
func-fmt.go some identier exported; new file import-utils.go 2024-06-06 05:31:35 +02:00
func-import.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
func-math.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
func-os.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
func-string.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
function.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
global-context.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +02:00
go.mod added go.mod 2024-03-26 08:56:20 +01:00
graph.go graph.go: remove unused Reticle code 2024-04-13 06:05:04 +02:00
helpers.go member ctx removed from struct parser because it is unused 2024-06-07 09:03:42 +02:00
import-utils.go import-utils.go: addPresetDirs() replaced by addSearchDirs() 2024-06-07 09:01:18 +02:00
int.list some expression and data files used by tests 2024-05-03 08:48:29 +02:00
it-range.go.unused it-range.go renamed as it-range.go.unused 2024-06-05 08:05:42 +02:00
iter-list.go fixed some defects in the iter-list implementation 2024-06-05 08:06:39 +02:00
iterator.expr some expression and data files used by tests 2024-05-03 08:48:29 +02:00
iterator.go fixed some defects in the iter-list implementation 2024-06-05 08:06:39 +02:00
list-type.go list-type.go: two special constructors, MakeList() and ListFromStrigns(), added 2024-06-07 09:02:14 +02:00
module-register.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
operand-dict.go DictType moved from operand-dict.go to the dict-type.go file 2024-06-04 10:59:25 +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 IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
operand-iterator.go fixed some defects in the iter-list implementation 2024-06-05 08:06:39 +02:00
operand-list.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +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 Expr's functions now support parameters with default value 2024-06-01 19:56:40 +02:00
operator-assign.go some identier exported; new file import-utils.go 2024-06-06 05:31:35 +02:00
operator-bool.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +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 IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
operator-context.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +02:00
operator-ctrl.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +02:00
operator-dot.go operator-dot.go: the '.' (dot) operator can now only be used to call member functions of iterators 2024-05-25 03:35:17 +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 A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
operator-in.go deep list inclusion and item membership implemented 2024-05-29 13:03:58 +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 test index/5 moved to list/26 because reflection's deep-equal function returns false even though computed and wanted lists are equal 2024-05-26 06:30:42 +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-range.go some identier exported; new file import-utils.go 2024-06-06 05:31:35 +02:00
operator-rel.go deep list inclusion and item membership implemented 2024-05-29 13:03:58 +02:00
operator-selector.go IsOptional() function of ExprFuncParam renamed as IdDefault(). A new IsOptional() function created to check if a param is optional without requiring a default value 2024-06-07 09:45:02 +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.go member ctx removed from struct parser because it is unused 2024-06-07 09:03:42 +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.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-store.go Control vars are now stored in the globalCtx only. 2024-06-08 05:49:28 +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
t_ast_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_dict_test.go member ctx removed from struct parser because it is unused 2024-06-07 09:03:42 +02:00
t_expr_test.go Expr's functions now support parameters with default value 2024-06-01 19:56:40 +02:00
t_fractions_test.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
t_func-base_test.go changes to adapt errors using typeName() functions 2024-06-05 05:52:27 +02:00
t_func-import_test.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
t_func-math-arith_test.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
t_func-os_test.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
t_func-string_test.go changes to adapt errors using typeName() functions 2024-06-05 05:52:27 +02:00
t_funcs_test.go some identier exported; new file import-utils.go 2024-06-06 05:31:35 +02:00
t_graph_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_helpers_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_index_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_iter-list_test.go new test file t_iter-list.go 2024-06-06 05:33:35 +02:00
t_iterator_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_list_test.go t_list_test.go: removed commented code 2024-06-07 08:54:59 +02:00
t_module-register_test.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
t_parser_test.go member ctx removed from struct parser because it is unused 2024-06-07 09:03:42 +02:00
t_relational_test.go deep list inclusion and item membership implemented 2024-05-29 13:03:58 +02:00
t_scanner_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_strings_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_template_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_term_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_token_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_utils_test.go New test file for specific code section or data type 2024-06-01 16:31:50 +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 use of typeName() in error messages 2024-06-05 05:48:02 +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.go token.go: function IsSymbol() 2024-05-04 19:08:02 +02:00
utils.go fixed some defects in the iter-list implementation 2024-06-05 08:06:39 +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