GO package for analysis and calculation of expressions
Go to file
2024-05-14 04:56:24 +02:00
doc Doc: some typo and better examples 2024-05-11 20:14:54 +02:00
tools func-bolerplate.bash: generate Go source for a new functions module 2024-05-01 07:13:52 +02:00
ast_test.go the function call procedure now check the number of actual parameters against the numer of formal parameters 2024-05-04 22:35:03 +02:00
ast.go Remove the unused 'parent' param from the function newTerm(). 2024-05-11 06:41:06 +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: added errDivisionByZero() 2024-05-13 14:23:21 +02:00
common-params.go common definitions collections 2024-05-01 07:10:11 +02:00
common-type-names.go common-type-names.go: typeFraction added 2024-05-01 21:43:42 +02:00
context-helpers.go commented tracing code 2024-05-01 05:57:08 +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 list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
dict_test.go the sum operation now supports dicts too 2024-05-14 04:56:24 +02:00
expr_test.go expr_test.go: removed temporary test 2024-05-06 15:31:28 +02:00
file-reader.expr some expression and data files used by tests 2024-05-03 08:48:29 +02:00
formatter.go formatter.go: number base options added 2024-05-08 07:51:38 +02:00
func-base.go added functions to get the fenerating fraction of a decimal number 2024-05-13 14:24:37 +02:00
func-import.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
func-math.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
func-os.go func-os.go: fixed the handling of err==io.EOF 2024-05-01 05:48:37 +02:00
func-string.go func-string.go: added three new functions (splitStr, startsWithStr, endsWithString) to the 'string' builtin module 2024-05-06 15:29:13 +02:00
funcs_test.go added functions to get the fenerating fraction of a decimal number 2024-05-13 14:24:37 +02:00
global-context.go Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +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
int.list some expression and data files used by tests 2024-05-03 08:48:29 +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 list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
iterator_test.go new test files and some refactorings 2024-05-06 05:52:25 +02:00
iterator.expr some expression and data files used by tests 2024-05-03 08:48:29 +02:00
iterator.go list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
list_test.go extended funcs and list tests 2024-05-10 09:18:32 +02:00
module-register.go Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +02:00
operand-const.go literals of rational number, e.g. 1.2(3), are now supported and are evaluated as fractions 2024-05-14 04:55:16 +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 Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +02:00
operand-iterator.go list iterators now support start, stop e step 2024-05-04 08:07:49 +02:00
operand-list.go operand-list.go: ListType constructor functions newList() and newListA() 2024-05-06 15:30:23 +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 Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +02:00
operator-assign.go operator-assign.go: improvements that allow to define function aliases (TODO: maybe *funcDefFunctor else-if section can be removed 2024-05-06 16:01:50 +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 Since now builtin functions are registared in a new global context. This reduces the effort to copy the whole set of builtin functions in the context of a function call; only the called function will be copied, if it is global. 2024-05-11 10:45:38 +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 operator-context.go: changed priPrePost with priIncDec 2024-05-06 04:14:54 +02:00
operator-ctrl.go class and kind types removed 2024-04-09 05:32:50 +02:00
operator-dot.go operator-dot.go: fixed string index 2024-05-10 07:32:45 +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 added functions to get the fenerating fraction of a decimal number 2024-05-13 14:24:37 +02:00
operator-include.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-insert.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-iter-value.go removed commented code 2024-05-04 00:57:21 +02:00
operator-length.go dict: implemented length of dict 2024-05-10 09:25:18 +02:00
operator-post-inc.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-prod.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-rel.go Some data-type check functions (e.g. IsInteger()) exported 2024-05-08 07:53:01 +02:00
operator-selector.go removed commented code 2024-04-27 06:14:09 +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 the sum operation now supports dicts too 2024-05-14 04:56:24 +02:00
parser_test.go new string test file 2024-05-10 07:33:59 +02:00
parser.go Remove the unused 'parent' param from the function newTerm(). 2024-05-11 06:41:06 +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 literals of rational number, e.g. 1.2(3), are now supported and are evaluated as fractions 2024-05-14 04:55:16 +02:00
simple-func-store.go improved the string representation of the content 2024-05-03 06:33:47 +02:00
simple-var-store.go improved the string representation of the content 2024-05-03 06:33:47 +02:00
strings_test.go new string test file 2024-05-10 07:33:59 +02:00
symbol.go literals of rational number, e.g. 1.2(3), are now supported and are evaluated as fractions 2024-05-14 04:55:16 +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 Remove the unused 'parent' param from the function newTerm(). 2024-05-11 06:41:06 +02:00
term.go term.go: priPrePost renamed as priIncDec 2024-05-06 04:11:43 +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 token.go: function IsSymbol() 2024-05-04 19:08:02 +02:00
utils.go new function isRational() that return true is the passed value is integere or fraction 2024-05-10 09:17:51 +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