GO package for analysis and calculation of expressions
Go to file
2026-04-30 07:04:03 +02:00
doc moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
kern kern/dict-type.go: added GetItem() 2026-04-30 07:04:03 +02: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
util util files moved form kern to util package 2026-04-29 11:03:36 +02:00
ast.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
bind-expr-functions.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-base.go util files moved form kern to util package 2026-04-29 11:03:36 +02:00
builtin-fmt.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-import.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-iterator.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-math-arith.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-os-file-iter.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-os-file.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtin-string.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
builtins-register.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
byte-slider.go byte-slider.go: renamed function receiver from self to slider 2024-07-07 15:54:26 +02:00
data-cursor.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
dict-iterator.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
expr.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
function.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
global-context.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +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 moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
helpers.go util files moved form kern to util package 2026-04-29 11:03:36 +02:00
import-utils.go util files moved form kern to util package 2026-04-29 11:03:36 +02:00
it-range.go.unused it-range.go renamed as it-range.go.unused 2024-06-05 08:05:42 +02:00
iter-factory.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
lib-ext-darwin.go Plugin file name extension adapted to the host OS convention (.so on linux, .dll on windows, .dylib on Darwin/MacOS) 2026-04-23 19:06:11 +02:00
lib-ext-linux.go Plugin file name extension adapted to the host OS convention (.so on linux, .dll on windows, .dylib on Darwin/MacOS) 2026-04-23 19:06:11 +02:00
lib-ext-windows.go Plugin file name extension adapted to the host OS convention (.so on linux, .dll on windows, .dylib on Darwin/MacOS) 2026-04-23 19:06:11 +02:00
list-iterator.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-dict.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-expr.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-func.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-iterator.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-list.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-literal.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-selector-case.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operand-var.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-assign.go util files moved form kern to util package 2026-04-29 11:03:36 +02:00
operator-bitwise.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-bool.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-builtin.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-but.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-context.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-ctrl.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-default.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-digest.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-dot.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-fact.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-filter.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-fraction.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-in.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-include.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-index.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-insert.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-iter-value.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-join.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-length.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-map.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-plugin.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-post-inc-dec.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-pre-inc-dec.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-prod.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-range.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-rel.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-selector.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-shift.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-sign.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-sum.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
operator-unset.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
parser.go parser: token '()' returns error 2026-04-30 07:03:28 +02:00
plugins.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
README.adoc README: added link to the main Expr documentation hosted on cdn.paas.portale-stac.it 2026-04-21 06:47:15 +02:00
scanner.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
simple-store.go util files moved form kern to util package 2026-04-29 11:03:36 +02:00
symbol-map.go new operator 'join' 2026-04-25 07:00:30 +02:00
symbol.go new operator 'join' 2026-04-25 07:00:30 +02:00
t_ast_test.go Added support to op-assign operators like '+='. 2024-07-23 15:35:57 +02:00
t_bool_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_builtin-base_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_builtin-fmt_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +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 builtin-os-file: add function fileReadIterator() that creates an iterator over lines of a text file 2026-04-23 19:01:07 +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 increased test coverage and splitted some utility functions by OS 2026-04-26 06:16:43 +02:00
t_builtin-string_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_common_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_context_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_dict_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_expr_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_fractions_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_funcs_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +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 moved a subset of source file to the kern package 2026-04-27 19:43:37 +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 moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_iterator_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_list_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02: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 moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_parser_test.go parser: token '()' returns error 2026-04-30 07:03:28 +02:00
t_plugin_test.go t_plugin_test.go: TestLoadPluginName() removed due to linkage mismatch 2026-04-23 22:02:41 +02: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 moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_simple-store_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_strings_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
t_template_test.go tests improved 2024-06-26 04:29:40 +02:00
t_term_test.go moved a subset of source file to the kern package 2026-04-27 19:43:37 +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 util files moved form kern to util package 2026-04-29 11:03:36 +02:00
t_utils-unix_test.go util files moved form kern to util package 2026-04-29 11:03:36 +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 moved a subset of source file to the kern package 2026-04-27 19:43:37 +02:00
test-file.txt fixed all errors in test files 2024-04-20 06:04:35 +02:00
token.go token.go: little change to ErrorExpectedGotStringWithPrefix() 2026-04-30 07:01:02 +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 Expr documentation for a complete reference of the expression syntax and available functions.