GO package for analysis and calculation of expressions
Go to file
2024-04-28 06:43:57 +02:00
doc Expr.adoc: 'work in progress' added 2024-04-19 14:30:40 +02:00
ast_test.go Expressions now support function definition 2024-04-02 04:36:03 +02:00
ast.go Fix ast.go: the insert() didn't check the returned error 2024-04-26 04:26:20 +02:00
byte-slider.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
context-helpers.go merged with datasource-context 2024-04-27 06:19:12 +02:00
context.go context.go: added exists return value to the GetFuncInfo() 2024-04-26 04:28: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 Fixed Clean() and added error message when calling optional missing functions 2024-04-28 04:41:43 +02:00
expr_test.go temporary 2024-04-26 21:03:22 +02:00
func-base.go modules are now represented by a struct that holds import-function, module description and importa status. 2024-04-28 05:41:13 +02:00
func-common.go new convert function int() 2024-04-20 07:29:42 +02:00
func-import.go modules are now represented by a struct that holds import-function, module description and importa status. 2024-04-28 05:41:13 +02:00
func-math.go modules are now represented by a struct that holds import-function, module description and importa status. 2024-04-28 05:41:13 +02:00
func-os.go modules are now represented by a struct that holds import-function, module description and importa status. 2024-04-28 05:41:13 +02:00
funcs_test.go funcs_test.go, more tests on the int() function 2024-04-20 07:41:58 +02:00
go.mod added go.mod 2024-03-26 08:56:20 +01:00
graph_test.go added the graph source files 2024-04-13 05:54:34 +02:00
graph.go graph.go: remove unused Reticle code 2024-04-13 06:05:04 +02:00
helpers_test.go added the copywrite comment header in source files that lacked it 2024-04-13 06:00:22 +02:00
helpers.go helpers.go: two new functions: EvalStream() and EvalFile() 2024-04-27 09:46:03 +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 the FlatArrayIterator has been moved to the new file iter-list.go 2024-04-26 04:30:43 +02:00
iterator.go the FlatArrayIterator has been moved to the new file iter-list.go 2024-04-26 04:30:43 +02:00
module-register.go modules are now represented by a struct that holds import-function, module description and importa status. 2024-04-28 05:41:13 +02:00
operand-const.go all constant value are now stored in the same data struct (same constructor). Also nil const added 2024-04-20 05:39:49 +02:00
operand-dict.go operand-dict.go: removed commented code 2024-04-26 08:02:22 +02:00
operand-expr.go removed commented code 2024-04-27 06:14:09 +02:00
operand-func.go removed commented code 2024-04-27 06:14:09 +02:00
operand-iterator.go operand-iterator.go: accepts two new optional functions 'reset' and 'clean' from data-source 2024-04-27 22:32:57 +02:00
operand-list.go changed the structer of the selector components: now all case are a list value assigned as right operand of the selector operator 2024-04-13 10:10:25 +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 adapted to the new GetFuncInfo() specification 2024-04-26 04:36:03 +02:00
operator-assign.go operator-assign.go: little refactor 2024-04-26 04:31:31 +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 corrected a comment 2024-04-27 09:47:24 +02:00
operator-but.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-coalesce.go coalesce operators '??' and '?=' now accepts function definitions too 2024-04-20 09:40:07 +02:00
operator-context.go new operator '111123' that returns the content of the current context or the context of an iterator 2024-04-27 06:12:30 +02:00
operator-ctrl.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-dot.go accessing to the Reset() and Clean()functions of an iterator is now done by identifiers, i.e. reset, not by string, i.e. "reset". 2024-04-28 04:44:19 +02:00
operator-fact.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-include.go new prefix operator 'include' 2024-04-27 09:48:45 +02:00
operator-insert.go new operator dot '.' used to select an item or character from a list or a string respectively 2024-04-19 09:05:26 +02:00
operator-iter-value.go first working implementation of iterators and some fixes to the parser around lists analysis 2024-04-26 04:45:42 +02:00
operator-length.go operator-length.go: Fix: the returned value was int, instead of int64 2024-04-27 22:37:56 +02:00
operator-post-inc.go provisional implementation of the postfix ++ operator 2024-04-26 04:37:50 +02:00
operator-prod.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-rel.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-selector.go removed commented code 2024-04-27 06:14:09 +02:00
operator-sign.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-sum.go two new operators added: '<<' and '>> 2024-04-16 03:54:50 +02:00
parser_test.go updated some error messages; add some tests on the dict data-type; Use of reflect.DeepEqual() to compare the test results with the desired results. 2024-04-26 04:43:36 +02:00
parser.go first working implementation of iterators and some fixes to the parser around lists analysis 2024-04-26 04:45:42 +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_test.go fixed all errors in test files 2024-04-20 06:04:35 +02:00
scanner.go new operator '111123' that returns the content of the current context or the context of an iterator 2024-04-27 06:12:30 +02:00
simple-func-store.go adapted to the new GetFuncInfo() specification 2024-04-26 04:36:03 +02:00
simple-var-store.go adapted to the new GetFuncInfo() specification 2024-04-26 04:36:03 +02:00
symbol.go symbol.go: typo 2024-04-27 22:30:18 +02:00
term_test.go term_test.go: changed some fmt.Println() to t.Log() 2024-04-13 05:07:38 +02:00
term-constuctor-registry.go Added copyrighr note to all sources 2024-03-26 08:45:18 +01:00
term.go termo.go: changed the name of a variable 2024-04-27 09:48:05 +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_test.go fixed all errors in test files 2024-04-20 06:04:35 +02:00
token.go expressions now support intger value in bin (0b), oct (0o), and hex (0x) form 2024-04-17 14:15:50 +02:00
utils.go New function isFunctor() 2024-04-28 06:43:57 +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