GO package for analysis and calculation of expressions
Go to file
2024-07-19 17:03:03 +02:00
doc Doc: little changes 2024-06-21 09:06:25 +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
ast.go Expr embeds Typer and ast implements it 2024-07-13 17:11:39 +02:00
bind-expr-functions.go Impemented Typer interface 2024-07-13 17:14:25 +02:00
bind-go-functions.go Impemented Typer interface 2024-07-13 17:14:25 +02:00
builtin-base.go builtin-base.go: unused function iteratorFunc() commented out 2024-07-07 15:52:16 +02:00
builtin-fmt.go builtin-fmt.go: print() and println() can write data to a generic Writer. They fetch the writer from the control variable '_stdout'. 2024-06-25 10:55:54 +02:00
builtin-import.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +02:00
builtin-iterator.go removed/commented unused code 2024-07-19 17:03:03 +02:00
builtin-math-arith.go iterator.go: exported some const identifier 2024-07-18 07:27:02 +02:00
builtin-os-file.go builtin-os-file.go: changed read and write function names; added fileReadTextAll 2024-06-17 06:59:15 +02:00
builtin-string.go builtin-string.go: removed useless err == nil check 2024-07-07 15:53:29 +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 new operator "?!" (alternate value) 2024-07-06 05:54:53 +02:00
common-params.go New builtin module 'iterator' 2024-07-14 16:53:32 +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 Function call moved from operand-func.go to function.go 2024-06-17 06:54:50 +02: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 iterator.go: exported some const identifier 2024-07-18 07:27:02 +02:00
dict-type.go dict-type.go: exported NewDataCursor 2024-07-18 07:25:51 +02:00
expr-context.go ExprContext: new functions VarCount(), DeleteVar(), FuncCount(), DeleteFunc() 2024-07-13 08:59:15 +02:00
expr-function.go Impemented Typer interface 2024-07-13 17:14:25 +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 fraction-type.go: use of strings.TrimSuffix() in place of check suffix and slice 2024-07-07 15:57:17 +02:00
function.go function.go: removed useless param != nil check 2024-07-07 15:58:29 +02:00
global-context.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +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 exported some identifier 2024-06-09 10:13:37 +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 iterator.go: exported some const identifier 2024-07-18 07:27:02 +02:00
list-iterator.go iterator.go: exported some const identifier 2024-07-18 07:27:02 +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 self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operand-iterator.go iterator.go: exported some const identifier 2024-07-18 07:27:02 +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 self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-assign.go operator-assign.go -- Fix: Assigning a functor to a collection's item didn't work 2024-07-11 06:49:02 +02: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 self param replaced as opTerm 2024-07-09 07:50:06 +02: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 self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-fact.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-fraction.go self param replaced as opTerm 2024-07-09 07:50:06 +02: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 self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-insert.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-iter-value.go operator-iter-value.go: prefix operator () used to get the current value of an iterator replaced by ^ 2024-07-13 17:17:16 +02:00
operator-length.go iterator.go: exported some const identifier 2024-07-18 07:27:02 +02:00
operator-plugin.go New function for searching and importing plugin 2024-07-19 15:37:00 +02:00
operator-post-inc.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-prod.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-range.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-rel.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-selector.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-sign.go self param replaced as opTerm 2024-07-09 07:50:06 +02:00
operator-sum.go operator-sum.go: replaced self receiver 2024-07-08 07:29:42 +02:00
operator-unset.go operator-unset.go: fixed function removal 2024-07-13 09:07:33 +02:00
parser.go parser.go: renamed self receiver 2024-07-07 16:14:52 +02: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 scanner: disabled prefix operator '()' 2024-07-13 18:10:04 +02:00
simple-store.go ExprContext: new functions VarCount(), DeleteVar(), FuncCount(), DeleteFunc() 2024-07-13 08:59:15 +02:00
symbol.go New operator unset to delete variables and functions from current context 2024-07-13 09:01:59 +02:00
t_ast_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +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 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-fmt_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-import_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-iterator_test.go removed/commented unused code 2024-07-19 17:03:03 +02:00
t_builtin-math-arith_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-os-file_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-string_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_common_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_dict_test.go refactored dict's item access 2024-06-17 14:06:33 +02:00
t_expr_test.go a lot oh changes to the test framework and new test files t_builtin-fmt_test.go and t_plugin_test.go added 2024-06-25 10:59:03 +02:00
t_fractions_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_funcs_test.go scanner: disabled prefix operator '()' 2024-07-13 18:10:04 +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 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_iter-list_test.go new test file t_iter-list.go 2024-06-06 05:33:35 +02:00
t_iterator_test.go New builtin module 'iterator' 2024-07-14 16:53:32 +02:00
t_list_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_module-register_test.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +02:00
t_operator_test.go operator-unset.go: fixed function removal 2024-07-13 09:07:33 +02:00
t_parser_test.go t_parser_test.go: more tests on ??, ?= and ?! operators 2024-07-06 17:01:23 +02:00
t_plugin_test.go removed/commented unused code 2024-07-19 17:03:03 +02:00
t_relational_test.go a lot oh changes to the test framework and new test files t_builtin-fmt_test.go and t_plugin_test.go added 2024-06-25 10:59:03 +02: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 a lot oh changes to the test framework and new test files t_builtin-fmt_test.go and t_plugin_test.go added 2024-06-25 10:59:03 +02: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 Rename priority label priCoalesce as priDefault 2024-07-13 17:13:16 +02:00
test-file.txt fixed all errors in test files 2024-04-20 06:04:35 +02:00
token.go token.go: replaced self receiver 2024-07-07 16:19:58 +02: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