GO package for analysis and calculation of expressions
Go to file
2025-01-03 07:33:17 +01:00
doc Doc: bitwise operators in the main operator table and special assignment operators table 2025-01-03 05:43:50 +01:00
test-resources Test on iterator filter and map 2024-07-13 15:30:04 +02:00
tools func-bolerplate.bash: generate Go source for a new functions module 2024-05-01 07:13:52 +02:00
ast.go Added support to op-assign operators like '+='. 2024-07-23 15:35:57 +02:00
bind-expr-functions.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
bind-go-functions.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
builtin-base.go builtin-base.go: new eval() function 2024-12-27 07:14:26 +01:00
builtin-fmt.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
builtin-import.go New, more flexible, parser context datatype that includes and extends 2024-12-19 14:48:27 +01:00
builtin-iterator.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
builtin-math-arith.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
builtin-os-file.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
builtin-string.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
builtins-register.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +02:00
byte-slider.go byte-slider.go: renamed function receiver from self to slider 2024-07-07 15:54:26 +02:00
common-errors.go common-errors.go: undefinte article selection in error messages 2024-12-27 07:13:03 +01:00
common-params.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
common-type-names.go common-type-names.go: TypeNil and TyperDict added 2024-09-09 15:16:42 +02:00
context-helpers.go context-helpers.go: export functions no longer export control flags 2024-12-27 07:22:28 +01:00
control.go control.go: use of UnsafeSetVar() in place of SetVar(). SetCtrl() added 2024-06-25 10:53:05 +02:00
data-cursor.go enhanced and simplified Reset(), Clean() and Next() methods 2024-09-09 15:23:07 +02:00
dict-type.go Added a test to the context type 2025-01-03 05:40:24 +01:00
expr-context.go RegisterFunc() also returns the funcInfo object 2024-08-01 00:09:49 +02:00
expr-function.go CallFunction() has been replaced by three new functions: 2024-08-02 06:39:33 +02:00
expr.go Expr embeds Typer and ast implements it 2024-07-13 17:11:39 +02:00
formatter.go Formatter option is now composed of two data: flags (lower 16 bits) and indentation size (higher 16 bits). 2024-06-11 16:32:01 +02:00
fraction-type.go solved a number of problems highlighted by the syntax analyzer 2024-12-27 07:46:11 +01:00
function.go Fix function.go: CallFunctionByParams() dit not pass correctly received actual params 2024-08-23 10:29:57 +02:00
global-context.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
go.mod New, more flexible, parser context datatype that includes and extends 2024-12-19 14:48:27 +01:00
go.sum New operator %= 2024-12-19 15:14:30 +01:00
graph.go graph.go: remove unused Reticle code 2024-04-13 06:05:04 +02:00
helpers.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
import-utils.go import-utils.go: Paths are now expanded with respect to env-vars and shell ~ 2024-07-19 15:33:15 +02:00
it-range.go.unused it-range.go renamed as it-range.go.unused 2024-06-05 08:05:42 +02:00
iterator.go solved a number of problems highlighted by the syntax analyzer 2024-12-27 07:46:11 +01:00
list-iterator.go Reset() and Clean() have new, simpler signature 2024-09-12 05:44:29 +02:00
list-type.go list-type.go: commented out an unused fuction 2024-07-07 16:08:45 +02:00
operand-dict.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operand-expr.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operand-func.go CallFunction() has been replaced by three new functions: 2024-08-02 06:39:33 +02:00
operand-iterator.go Reset() and Clean() have new, simpler signature 2024-09-12 05:44:29 +02:00
operand-list.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operand-literal.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operand-selector-case.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operand-var.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
operator-assign.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
operator-bitwise.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
operator-bool.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-builtin.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-but.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-context.go solved a number of problems highlighted by the syntax analyzer 2024-12-27 07:46:11 +01:00
operator-ctrl.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-default.go Rename priority label priCoalesce as priDefault 2024-07-13 17:13:16 +02:00
operator-dot.go Changed some function names and param types 2024-07-31 09:11:57 +02:00
operator-fact.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-fraction.go new string operator '/' 2024-12-28 19:16:45 +01:00
operator-in.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-include.go operator-include.go: Fixed inclusion of a list of files. 2024-07-13 09:00:53 +02:00
operator-index.go Completed transition of the symbol '|' from fraction to operator binary or. New fraction symbol is ':'. 2024-12-23 06:59:39 +01:00
operator-insert.go The list operator '<<' (append) and '>>' (prepend) have been replaced 2024-12-28 09:17:27 +01:00
operator-iter-value.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
operator-length.go Iterator: new function Count() 2024-07-21 16:34:52 +02:00
operator-plugin.go New function for searching and importing plugin 2024-07-19 15:37:00 +02:00
operator-post-inc.go operator-post-inc.go: new post int decrement operator '--' 2024-07-24 06:39:35 +02:00
operator-prod.go new string operator '/' 2024-12-28 19:16:45 +01:00
operator-range.go operator-range.go: range-term registered with symbol SymRange 2024-12-23 06:55:57 +01:00
operator-rel.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-selector.go operator-selector.go: Simplified selector for Boolean expressions 2024-10-01 06:37:35 +02:00
operator-shift.go Added new special operators like &= and <<=. 2024-12-29 19:26:02 +01:00
operator-sign.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-sum.go operator-sum.go: sum of two fraction fixed 2024-12-23 06:52:10 +01:00
operator-unset.go operator-unset.go: fixed function removal 2024-07-13 09:07:33 +02:00
parser.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
plugins.go New function for searching and importing plugin 2024-07-19 15:37:00 +02:00
README.adoc doc: Syntax of expressions moved from README.adoc to doc/Expr.doc 2024-04-14 08:16:01 +02:00
scanner.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
simple-store.go RegisterFunc() also returns the funcInfo object 2024-08-01 00:09:49 +02:00
symbol-map.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
symbol.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
t_ast_test.go Added support to op-assign operators like '+='. 2024-07-23 15:35:57 +02:00
t_bool_test.go the common test framework now supports error, string and nil as value of the wantErr field 2024-07-06 16:43:13 +02:00
t_builtin-base_test.go builtin-base.go: new eval() function 2024-12-27 07:14:26 +01:00
t_builtin-fmt_test.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_builtin-import_test.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_builtin-iterator_test.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_builtin-math-arith_test.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_builtin-os-file_test.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_builtin-string_test.go t_builtin-string_test.go: corrected an undefinite article 2024-12-27 07:14:01 +01:00
t_common_test.go The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_dict_test.go %q replaced by %s in some error messages 2024-07-21 05:33:06 +02:00
t_expr_test.go Added a test to the context type 2025-01-03 05:40:24 +01:00
t_fractions_test.go new string operator '/' 2024-12-28 19:16:45 +01:00
t_funcs_test.go t_funcs_test.go: A test add on parameters check about two params with the same name 2024-10-05 05:25:29 +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 The function parameter model has been modified to support the passing of named parameters 2024-07-28 18:49:08 +02:00
t_index_test.go Completed transition of the symbol '|' from fraction to operator binary or. New fraction symbol is ':'. 2024-12-23 06:59:39 +01:00
t_iter-list_test.go Fixed type (expcted -> expected) 2024-07-21 05:43:55 +02:00
t_iterator_test.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
t_list_test.go Added new special operators like &= and <<=. 2024-12-29 19:26:02 +01:00
t_module-register_test.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +02:00
t_operator_test.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
t_parser_test.go New bitwise XOR operator specified by symbol ^ (caret). Iterator dereference is now done by prefixed * (star) 2025-01-03 07:33:17 +01:00
t_plugin_test.go solved a number of problems highlighted by the syntax analyzer 2024-12-27 07:46:11 +01:00
t_relational_test.go Completed transition of the symbol '|' from fraction to operator binary or. New fraction symbol is ':'. 2024-12-23 06:59:39 +01:00
t_scanner_test.go t_scanner_test.go: replaced t.Log(fmtStringf()) with t.Logf() 2024-07-07 16:18:39 +02:00
t_strings_test.go Added new special operators like &= and <<=. 2024-12-29 19:26:02 +01:00
t_template_test.go tests improved 2024-06-26 04:29:40 +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 tests improved 2024-06-26 04:29:40 +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: new priority priDereference 2025-01-03 07:28:30 +01:00
test-file.txt fixed all errors in test files 2024-04-20 06:04:35 +02:00
token.go token.go: new member function Token.SetSymbol() 2025-01-03 07:27:49 +01:00
utils.go utils.go: added function ExpandPath 2024-07-19 15:30:26 +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