GO package for analysis and calculation of expressions
Go to file
2024-06-05 05:52:27 +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 New test file for specific code section or data type 2024-06-01 16:31:50 +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 Eprx now supports range of indeces to extract parts of strings or lists 2024-05-26 06:19:08 +02:00
context-helpers.go function definition and usage rationalized 2024-05-24 06:28:48 +02:00
context.go New test file for specific code section or data type 2024-06-01 16:31:50 +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-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 A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
func-base.go New test file for specific code section or data type 2024-06-01 16:31:50 +02:00
func-fmt.go func-fmt.go: print() and println() 2024-06-02 12:32:08 +02:00
func-import.go typeFilepath changed 2024-06-02 11:54:43 +02:00
func-math.go Expr's functions now support parameters with default value 2024-06-01 19:56:40 +02:00
func-os.go typeFilepath and typeDirpath renamed as paramFilepath and paramDirpath respectively 2024-06-02 11:32:47 +02:00
func-string.go func-string.go: ad least one extra prefix and suffix for startsWithStr() and endsWitchStr() 2024-06-02 12:30:17 +02:00
function.go function.go: if the right most parameter is repeteable (pfRepeat), then it does not increment the min-arg count 2024-06-02 11:30:24 +02:00
global-context.go global-context.go: variables holding function values can be invoked as function now 2024-06-04 11:01:04 +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 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.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-type.go list-type.go: constructor newListA(), called without arguments, now creates an empty list, not a nil object 2024-06-05 05:03:37 +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 Expr's functions now support parameters with default value 2024-06-01 19:56:40 +02:00
operand-iterator.go function definition and usage rationalized 2024-05-24 06:28:48 +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 operator-assign.go: some errors message changed to report the Expr's type names, not the Go ones 2024-06-05 05:05:40 +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 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 Eprx now supports range of indeces to extract parts of strings or lists 2024-05-26 06:19:08 +02:00
operator-rel.go deep list inclusion and item membership implemented 2024-05-29 13:03:58 +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.go parser.go: commented out some useless code 2024-06-04 11:02:26 +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-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 Call() implementation can invoke varaibles holding functors 2024-06-04 11:03:24 +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
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 t_dict_test.go: some tests added 2024-06-05 05:49:07 +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 little changes to test source files 2024-06-02 12:33:32 +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_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: some tests added 2024-06-05 05:50:37 +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 changes to adapt errors using typeName() functions 2024-06-05 05:52:27 +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 utils.go/typeName() and formatter.go/getTipeName() have been merged in formatter.go/typeName() 2024-06-05 05:42:37 +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