GO package for analysis and calculation of expressions
Go to file
2024-06-26 04:25:49 +02:00
doc Doc: little changes 2024-06-21 09:06:25 +02:00
test-resources 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
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
bind-expr-functions.go Improved closure context persistence. Now it is possibile to define counters like this func(base){func(){@base=base+1}} 2024-06-24 07:20:17 +02:00
bind-go-functions.go splitted go and expr function bindings in dedicated source files 2024-06-09 10:41:06 +02:00
builtin-base.go common-errors.go: exported all error functions. 2024-06-10 20:37:58 +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-math-arith.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +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 common-errors.go: exported all error functions. 2024-06-10 20:37:58 +02:00
builtins-register.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +02:00
byte-slider.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
common-errors.go common-errors.go: commented out some unused error functions 2024-06-26 04:23:14 +02:00
common-params.go common-params.go: added the constant ConstLastIndex 2024-06-19 09:17:46 +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
context.go ExprContext: removed Merge() method 2024-06-25 10:45:54 +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 list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
dict-type.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
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 common-errors.go: exported all error functions. 2024-06-10 20:37:58 +02:00
function.go function.go: commented out some unused error functions 2024-06-26 04:23:48 +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 formatter.go: typeName() renamed as TypeName() 2024-06-10 09:37:27 +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 replaced ToInt() with ToGoInt() 2024-06-26 04:25:49 +02:00
iterator.go fixed some defects in the iter-list implementation 2024-06-05 08:06:39 +02:00
list-type.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
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 Function call moved from operand-func.go to function.go 2024-06-17 06:54:50 +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 formatter.go: typeName() renamed as TypeName() 2024-06-10 09:37:27 +02:00
operator-bool.go utils.go: toInt() -> ToInt(); toBool() -> ToBool() 2024-06-10 19:03:39 +02:00
operator-builtin.go formatter.go: typeName() renamed as TypeName() 2024-06-10 09:37:27 +02:00
operator-but.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-coalesce.go exported some identifier 2024-06-09 10:13:37 +02:00
operator-context.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
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 replaced ToInt() with ToGoInt() 2024-06-26 04:25:49 +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 replaced ToInt() with ToGoInt() 2024-06-26 04:25:49 +02:00
operator-plugin.go formatter.go: typeName() renamed as TypeName() 2024-06-10 09:37:27 +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 operator-range.go: Fixed priority bug; when range only has the left limit, right limit is set to ConstLastIndex constant 2024-06-19 09:20:02 +02:00
operator-rel.go utils.go: toInt() -> ToInt(); toBool() -> ToBool() 2024-06-10 19:03:39 +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 parser.go: SymColon resets the firstToken flag; that is needed to specify range indeces that can define negative limits 2024-06-19 09:22:40 +02:00
plugins.go plugin.go: support Liteide and VScode to debug executable file name 2024-06-17 06:57:47 +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.go: Strings can be enclosed between two single-quotes too 2024-06-10 18:52:13 +02:00
simple-store.go ExprContext: removed Merge() method 2024-06-25 10:45:54 +02:00
symbol.go first plugin support. 2024-06-09 07:41:56 +02:00
t_ast_test.go A lot of changes. Main ones are: 2024-05-28 07:26:05 +02:00
t_builtin-base_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_builtin-fmt_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_builtin-import_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_builtin-math-arith_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_builtin-os-file_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_builtin-string_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_common_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_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 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_funcs_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_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 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_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 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_list_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_module-register_test.go func-*.go modules renamed as builtin-*.go. 2024-06-09 10:28:51 +02:00
t_parser_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_plugin_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_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: Test nr 25 changed because now single-quotes can enclose strings 2024-06-10 20:36:03 +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 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_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 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
term-constuctor-registry.go Remove the unused 'parent' param from the function newTerm(). 2024-05-11 06:41:06 +02:00
term.go formatter.go: typeName() renamed as TypeName() 2024-06-10 09:37:27 +02:00
test-file.txt fixed all errors in test files 2024-04-20 06:04:35 +02:00
token.go token.go: new function IsOneOf() 2024-06-19 09:16:19 +02:00
utils.go utils.go: ToInt() renamed as ToGoInt() 2024-06-26 04:22:29 +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