Compare commits
40 Commits
v0.4.0
...
hook-training
| Author | SHA1 | Date | |
|---|---|---|---|
| ba1d887a05 | |||
| faff5a7e2c | |||
| 36f6846a3f | |||
| 591b4ffc19 | |||
| fe9ab9ebd2 | |||
| 7198749063 | |||
| b76481bbf2 | |||
| 4f05e5c90a | |||
| 8ad25afdc4 | |||
| 54bc759f70 | |||
| b6887af77a | |||
| 353d495c50 | |||
| f45b2c0a88 | |||
| 624e3ac0f2 | |||
| 35fcbd2bce | |||
| 2150181303 | |||
| d643e24a1b | |||
| 43e631f2e8 | |||
| f1afbf9b49 | |||
| ed6af6603a | |||
| 51e740d243 | |||
| 53dacd5332 | |||
| e297f2a1d3 | |||
| 70cdb9367e | |||
| efd9af9030 | |||
| b67d896415 | |||
| f03ae555fc | |||
| f36ae33acc | |||
| b9ea96f649 | |||
| 838ab9fd7e | |||
| ad6b4b872f | |||
| c75e6485e0 | |||
| 7b80f7f03b | |||
| 7f9fd570b2 | |||
| b17d2250a4 | |||
| dda10749d0 | |||
| 07ca84170e | |||
| 55e136e9bc | |||
| e493c40c7b | |||
| 95605232ab |
+8
-227
@@ -1,5 +1,5 @@
|
||||
= Expr
|
||||
Expressions calculator
|
||||
= README
|
||||
README about the Expressions calculator
|
||||
:authors: Celestino Amoroso
|
||||
:docinfo: shared
|
||||
:encoding: utf-8
|
||||
@@ -135,232 +135,13 @@ func main() {
|
||||
}
|
||||
----
|
||||
|
||||
=== Data types
|
||||
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
|
||||
== Context of evaluation
|
||||
Unless helpers functions like _expr.EvalStringA()_ are used, a _context_ is required to compute an expession.
|
||||
|
||||
==== Numbers
|
||||
Numbers can be integers (GO int64) or float (GO float64). In mixed operations involving integers and floats, integers are automatically promoted to floats.
|
||||
|
||||
.Arithmetic operators
|
||||
[cols="^1,^2,6,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`+` / [blue]`-` | _change sign_ | Change the sign of values | [blue]`-1` _[-1]_ +
|
||||
[blue]`-(+2)` _[-2]_
|
||||
|
||||
| [blue]`+` | _sum_ | Add two values | [blue]`-1 + 2` _[1]_ +
|
||||
[blue]`4 + 0.5` _[4.5]_
|
||||
|
||||
| [blue]`-` | _subtraction_ | Subtract the right value from the left one | [blue]`3 - 1` _[2]_ +
|
||||
[blue]`4 - 0.5` _[3.5]_
|
||||
|
||||
| [blue]`*` | _product_ | Multiply two values | `-1 * 2` _[-2]_ +
|
||||
[blue]`4 * 0.5` _[2.0]_
|
||||
|
||||
| [blue]`/` | _Division_ | Divide the left value by the right one | [blue]`-1 / 2` _[0]_ +
|
||||
[blue]`1.0 / 2` _[0.5]_
|
||||
|
||||
| [blue]`./` | _Float division_ | Force float division | [blue]`-1 ./ 2` _[-0.5]_
|
||||
|
||||
| [blue]`%` | _Modulo_ | Remainder of the integer division | [blue]`5 % 2` _[1]_
|
||||
|
||||
|===
|
||||
|
||||
==== String
|
||||
Strings are character sequences enclosed between two double quote [blue]`"`. Example: [blue]`"I'm a string"`.
|
||||
|
||||
Some arithmetic operators can also be used with strings.
|
||||
|
||||
.String operators
|
||||
[cols="^1,^2,6,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`+` | _concatenation_ | Join two strings or two _stringable_ values | [blue]`"one" + "two"` _["onetwo"]_ +
|
||||
[blue]`"one" + 2` _["one2"]_
|
||||
|
||||
| [blue]`*` | _repeat_ | Make _n_ copy of a string | [blue]`"one" * 2` _["oneone"]_
|
||||
|===
|
||||
|
||||
|
||||
==== Boolean
|
||||
Boolean data type has two values only: _true_ and _false_. Relational and Boolean expressions produce Boolean values.
|
||||
|
||||
|
||||
.Relational operators
|
||||
[cols="^1,^2,6,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`==` | _Equal_ | True if the left value is equal to the right one | [blue]`5 == 2` _[false]_ +
|
||||
[blue]`"a" == "a"` _[true]_
|
||||
| [blue]`!=` | _Not Equal_ | True if the left value is NOT equal to the right one | [blue]`5 != 2` _[true]_ +
|
||||
[blue]`"a" != "a"` _[false]_
|
||||
| [blue]`<` | _Less_ | True if the left value is less than the right one | [blue]`5 < 2` _[false]_ +
|
||||
[blue]`"a" < "b"` _[true]_
|
||||
| [blue]`\<=` | _Less or Equal_ | True if the left value is less than or equal to the right one | [blue]`5 \<= 2` _[false]_ +
|
||||
[blue]`"b" \<= "b"` _[true]_
|
||||
| [blue]`>` | _Greater_ | True if the left value is greater than the right one | [blue]`5 > 2` _[true]_ +
|
||||
[blue]`"a" < "b"` _[false]_
|
||||
| [blue]`>=` | _Greater or Equal_ | True if the left value is greater than or equal to the right one | [blue]`5 >= 2` _[true]_ +
|
||||
[blue]`"b" \<= "b"` _[true]_
|
||||
|===
|
||||
|
||||
|
||||
.Boolean operators
|
||||
[cols="^2,^2,5,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`NOT` | _Not_ | True if the right value is false | [blue]`NOT true` _[false]_ +
|
||||
[blue]`NOT (2 < 1)` _[true]_
|
||||
|
||||
| [blue]`AND` / [blue]`&&` | _And_ | True if both left and right values are true | [blue]`false && true` _[false]_ +
|
||||
[blue]`"a" < "b" AND NOT (2 < 1)` _[true]_
|
||||
|
||||
| [blue]`OR` / [blue]`\|\|` | _Or_ | True if at least one of the left and right values integers true| [blue]`false or true` _[true]_ +
|
||||
[blue]`"a" == "b" OR (2 == 1)` _[false]_
|
||||
|===
|
||||
|
||||
[CAUTION]
|
||||
====
|
||||
Currently, boolean operations are evaluated using _short cut evaluation_. This means that, if the left expression of operators [blue]`and` and [blue]`or` is sufficient to establish the result of the whole operation, the right expression would not evaluated at all.
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
2 > (a=1) or (a=8) > 0; a // <1>
|
||||
----
|
||||
<1> This multi-expression returns _1_ because in the first expression the left value of [blue]`or` is _true_ and as a conseguence its right value is not computed. Therefore the _a_ variable only receives the integer _1_.
|
||||
====
|
||||
|
||||
==== List
|
||||
_Expr_ supports list of mixed-type values, also specified by normal expressions.
|
||||
|
||||
.List examples
|
||||
[source,go]
|
||||
----
|
||||
[1, 2, 3] // List of integers
|
||||
["one", "two", "three"] // List of strings
|
||||
["one", 2, false, 4.1] // List of mixed-types
|
||||
["one"+1, 2.0*(9-2)] // List of expressions
|
||||
[ [1,"one"], [2,"two"]] // List of lists
|
||||
----
|
||||
|
||||
|
||||
.List operators
|
||||
[cols="^2,^2,5,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`+` | _Join_ | Joins two lists | [blue]`[1,2] + [3]` _[ [1,2,3] ]_
|
||||
|
||||
| [blue]`-` | _Difference_ | Left list without elements in the right list | [blue]`[1,2,3] - [2]` _[ [1,3] ]_
|
||||
|===
|
||||
|
||||
=== Variables
|
||||
#TODO: variables#
|
||||
|
||||
=== Other operations
|
||||
|
||||
==== [blue]`;` operator
|
||||
The semicolon operator [blue]`;` is an infixed operator. It evaluates the left expression first and then the right expression. The latter is the final result.
|
||||
|
||||
IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions.
|
||||
|
||||
TIP: [blue]`;` can be used to set some variables before the final calculation.
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
a=1; b=2; c=3; a+b+c // returns 6
|
||||
----
|
||||
|
||||
==== [blue]`but` operator
|
||||
[blue]`but` is an infixed operator. Its operands can be any type of expression. It evaluates the left expression first, then the right expression. The value of the right expression is the final result. Examples: [blue]`5 but 2` returns 2, [blue]`x=2*3 but x-1` returns 5.
|
||||
|
||||
[blue]`but` is very similar to [blue]`;`. The only difference is that [blue]`;` can't be used inside parenthesis [blue]`;(` and [blue]`)`.
|
||||
|
||||
==== Assignment operator [blue]`=`
|
||||
The assignment operator [blue]`=` is used to define variable in the evaluation context or to change their value (see _ExprContext_).
|
||||
The value on the left side of [blue]`=` must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation.
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
a=15+1 // returns 16
|
||||
----
|
||||
|
||||
==== Selector operator [blue]`? : ::`
|
||||
The _selector operator_ is very similar to the _switch/case/default_ statement available in many programming languages.
|
||||
|
||||
.Syntax
|
||||
[source,bnf]
|
||||
----
|
||||
<selector-operator> ::= <expression> "?" <selector-case> { ":" <selector-case> } ["::" <case-value>]
|
||||
<selector-case> ::= [<list>] <case-value>
|
||||
<case-value> ::= "{" <multi-expr> "}"
|
||||
<multi-expr> ::= <expr> {";" <expr>}
|
||||
----
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
1 ? {"a"} : {"b"} // returns "b"
|
||||
10 ? {"a"} : {"b"} :: {"c"} // returns "c"
|
||||
10 ? {"a"} :[true, 2+8] {"b"} :: {"c"} // returns "b"
|
||||
10 ? {"a"} :[true, 2+8] {"b"} ::[10] {"c"} // error: "... case list in default clause"
|
||||
10 ? {"a"} :[10] {x="b" but x} :: {"c"} // returns "b"
|
||||
10 ? {"a"} :[10] {x="b"; x} :: {"c"} // returns "b"
|
||||
10 ? {"a"} : {"b"} // error: "... no case catches the value (10) of the selection expression
|
||||
----
|
||||
|
||||
=== Priorities of operators
|
||||
The table below shows all supported operators by decreasing priorities.
|
||||
|
||||
.Operators priorities
|
||||
[cols="^2,^2,^2,^5,<5"]
|
||||
|===
|
||||
| Priority | Operators | Position | Operation | Operands and results
|
||||
|
||||
1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ "!" -> _integer_
|
||||
1+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| ("+"\|"-") _number_ -> _number_
|
||||
.5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ "*" _number_ -> _number_
|
||||
| [blue]`*` | _Infix_ | _String-repeat_ | _string_ "*" _integer_ -> _string_
|
||||
| [blue]`/` | _Infix_ | _Division_ | _number_ "/" _number_ -> _number_
|
||||
| [blue]`./` | _Infix_ | _Float-division_ | __number__ "./" _number_ -> _float_
|
||||
| [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ "%" _integer_ -> _integer_
|
||||
.5+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ "+" _number_ -> _number_
|
||||
| [blue]`+` | _Infix_ | _String-concat_ | (_string_\|_number_) "+" (_string_\|_number_) -> _string_
|
||||
| [blue]`+` | _Infix_ | _List-join_ | _list_ "+" _list_ -> _list_
|
||||
| [blue]`-` | _Infix_ | _Subtraction_ | _number_ "-" _number_ -> _number_
|
||||
| [blue]`-` | _Infix_ | _List-difference_ | _list_ "-" _list_ -> _list_
|
||||
.6+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ "<" _comparable_ -> _boolean_
|
||||
| [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ "\<=" _comparable_ -> _boolean_
|
||||
| [blue]`>` | _Infix_ | _greater_ | _comparable_ ">" _comparable_ -> _boolean_
|
||||
| [blue]`>=` | _Infix_ | _greater-equal_ | _comparable_ ">=" _comparable_ -> _boolean_
|
||||
| [blue]`==` | _Infix_ | _equal_ | _comparable_ "==" _comparable_ -> _boolean_
|
||||
| [blue]`!=` | _Infix_ | _not-equal_ | _comparable_ "!=" _comparable_ -> _boolean_
|
||||
.1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | "not" _boolean_ -> _boolean_
|
||||
.2+|*AND*| [blue]`and` | _Infix_ | _and_ | _boolean_ "and" _boolean_ -> _boolean_
|
||||
| [blue]`&&` | _Infix_ | _and_ | _boolean_ "&&" _boolean_ -> _boolean_
|
||||
.2+|*OR*| [blue]`or` | _Infix_ | _or_ | _boolean_ "or" _boolean_ -> _boolean_
|
||||
| [blue]`||` | _Infix_ | _or_ | _boolean_ "||" _boolean_ -> _boolean_
|
||||
.1+|*ASSIGN*| [blue]`=` | _Infix_ | _assignment_ | _identifier_ "=" _any_ -> _any_
|
||||
.1+|*BUT*| [blue]`but` | _Infix_ | _but_ | _any_ "but" _any_ -> _any_
|
||||
|===
|
||||
|
||||
=== Functions
|
||||
|
||||
==== Function calls
|
||||
#TODO: function calls operations#
|
||||
|
||||
==== Function definitions
|
||||
#TODO: function definitions operations#
|
||||
|
||||
==== Builtins
|
||||
#TODO: builtins#
|
||||
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.
|
||||
|
||||
== Expressions syntax
|
||||
|
||||
See #TODO link to doc/Expr.html#
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
type Expr interface {
|
||||
Eval(ctx ExprContext) (result any, err error)
|
||||
eval(ctx ExprContext, preset bool) (result any, err error)
|
||||
String() string
|
||||
}
|
||||
|
||||
//-------- ast
|
||||
@@ -118,7 +119,7 @@ func (self *ast) eval(ctx ExprContext, preset bool) (result any, err error) {
|
||||
if self.forest != nil {
|
||||
for _, root := range self.forest {
|
||||
if result, err = root.compute(ctx); err == nil {
|
||||
ctx.setVar(control_last_result, result)
|
||||
ctx.setVar(ControlLastResult, result)
|
||||
} else {
|
||||
//err = fmt.Errorf("error in expression nr %d: %v", i+1, err)
|
||||
break
|
||||
|
||||
+13
-10
@@ -1,13 +1,16 @@
|
||||
// preset.go
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// control.go
|
||||
package expr
|
||||
|
||||
import "strings"
|
||||
|
||||
// Preset control variables
|
||||
const (
|
||||
control_last_result = "_last"
|
||||
control_bool_shortcut = "_bool_shortcut"
|
||||
control_import_path = "_import_path"
|
||||
ControlLastResult = "last"
|
||||
ControlBoolShortcut = "_bool_shortcut"
|
||||
ControlImportPath = "_import_path"
|
||||
)
|
||||
|
||||
// Other control variables
|
||||
@@ -21,23 +24,23 @@ const (
|
||||
)
|
||||
|
||||
func initDefaultVars(ctx ExprContext) {
|
||||
ctx.setVar(control_bool_shortcut, true)
|
||||
ctx.setVar(control_import_path, init_import_path)
|
||||
ctx.SetVar(ControlBoolShortcut, true)
|
||||
ctx.SetVar(ControlImportPath, init_import_path)
|
||||
}
|
||||
|
||||
func enable(ctx ExprContext, name string) {
|
||||
if strings.HasPrefix(name, "_") {
|
||||
ctx.setVar(name, true)
|
||||
ctx.SetVar(name, true)
|
||||
} else {
|
||||
ctx.setVar("_"+name, true)
|
||||
ctx.SetVar("_"+name, true)
|
||||
}
|
||||
}
|
||||
|
||||
func disable(ctx ExprContext, name string) {
|
||||
if strings.HasPrefix(name, "_") {
|
||||
ctx.setVar(name, false)
|
||||
ctx.SetVar(name, false)
|
||||
} else {
|
||||
ctx.setVar("_"+name, false)
|
||||
ctx.SetVar("_"+name, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+278
@@ -0,0 +1,278 @@
|
||||
= Expr
|
||||
Expressions calculator
|
||||
:authors: Celestino Amoroso
|
||||
:docinfo: shared
|
||||
:encoding: utf-8
|
||||
:toc: right
|
||||
:toclevels: 4
|
||||
//:toc-title: Indice Generale
|
||||
:icons: font
|
||||
:icon-set: fi
|
||||
:numbered:
|
||||
//:table-caption: Tabella
|
||||
//:figure-caption: Diagramma
|
||||
:docinfo1:
|
||||
:sectlinks:
|
||||
:sectanchors:
|
||||
:source-highlighter: rouge
|
||||
// :rouge-style: ThankfulEyes
|
||||
:rouge-style: gruvbox
|
||||
// :rouge-style: colorful
|
||||
//:rouge-style: monokay
|
||||
|
||||
toc::[]
|
||||
|
||||
#TODO: Work in progress#
|
||||
|
||||
== Expr
|
||||
_Expr_ is a GO package capable of analysing, interpreting and calculating expressions.
|
||||
|
||||
|
||||
=== Concepts and terminology
|
||||
#TODO#
|
||||
|
||||
== Data types
|
||||
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
|
||||
|
||||
=== Numbers
|
||||
Numbers can be integers (GO int64) or float (GO float64). In mixed operations involving integers and floats, integers are automatically promoted to floats.
|
||||
|
||||
.Arithmetic operators
|
||||
[cols="^1,^2,6,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`+` / [blue]`-` | _change sign_ | Change the sign of values | [blue]`-1` _[-1]_ +
|
||||
[blue]`-(+2)` _[-2]_
|
||||
|
||||
| [blue]`+` | _sum_ | Add two values | [blue]`-1 + 2` _[1]_ +
|
||||
[blue]`4 + 0.5` _[4.5]_
|
||||
|
||||
| [blue]`-` | _subtraction_ | Subtract the right value from the left one | [blue]`3 - 1` _[2]_ +
|
||||
[blue]`4 - 0.5` _[3.5]_
|
||||
|
||||
| [blue]`*` | _product_ | Multiply two values | `-1 * 2` _[-2]_ +
|
||||
[blue]`4 * 0.5` _[2.0]_
|
||||
|
||||
| [blue]`/` | _Division_ | Divide the left value by the right one | [blue]`-1 / 2` _[0]_ +
|
||||
[blue]`1.0 / 2` _[0.5]_
|
||||
|
||||
| [blue]`./` | _Float division_ | Force float division | [blue]`-1 ./ 2` _[-0.5]_
|
||||
|
||||
| [blue]`%` | _Modulo_ | Remainder of the integer division | [blue]`5 % 2` _[1]_
|
||||
|
||||
|===
|
||||
|
||||
=== String
|
||||
Strings are character sequences enclosed between two double quote [blue]`"`. Example: [blue]`"I'm a string"`.
|
||||
|
||||
Some arithmetic operators can also be used with strings.
|
||||
|
||||
.String operators
|
||||
[cols="^1,^2,6,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`+` | _concatenation_ | Join two strings or two _stringable_ values | [blue]`"one" + "two"` _["onetwo"]_ +
|
||||
[blue]`"one" + 2` _["one2"]_
|
||||
|
||||
| [blue]`*` | _repeat_ | Make _n_ copy of a string | [blue]`"one" * 2` _["oneone"]_
|
||||
|===
|
||||
|
||||
|
||||
=== Boolean
|
||||
Boolean data type has two values only: _true_ and _false_. Relational and Boolean expressions produce Boolean values.
|
||||
|
||||
|
||||
.Relational operators
|
||||
[cols="^1,^2,6,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`==` | _Equal_ | True if the left value is equal to the right one | [blue]`5 == 2` _[false]_ +
|
||||
[blue]`"a" == "a"` _[true]_
|
||||
| [blue]`!=` | _Not Equal_ | True if the left value is NOT equal to the right one | [blue]`5 != 2` _[true]_ +
|
||||
[blue]`"a" != "a"` _[false]_
|
||||
| [blue]`<` | _Less_ | True if the left value is less than the right one | [blue]`5 < 2` _[false]_ +
|
||||
[blue]`"a" < "b"` _[true]_
|
||||
| [blue]`\<=` | _Less or Equal_ | True if the left value is less than or equal to the right one | [blue]`5 \<= 2` _[false]_ +
|
||||
[blue]`"b" \<= "b"` _[true]_
|
||||
| [blue]`>` | _Greater_ | True if the left value is greater than the right one | [blue]`5 > 2` _[true]_ +
|
||||
[blue]`"a" < "b"` _[false]_
|
||||
| [blue]`>=` | _Greater or Equal_ | True if the left value is greater than or equal to the right one | [blue]`5 >= 2` _[true]_ +
|
||||
[blue]`"b" \<= "b"` _[true]_
|
||||
|===
|
||||
|
||||
|
||||
.Boolean operators
|
||||
[cols="^2,^2,5,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`NOT` | _Not_ | True if the right value is false | [blue]`NOT true` _[false]_ +
|
||||
[blue]`NOT (2 < 1)` _[true]_
|
||||
|
||||
| [blue]`AND` / [blue]`&&` | _And_ | True if both left and right values are true | [blue]`false && true` _[false]_ +
|
||||
[blue]`"a" < "b" AND NOT (2 < 1)` _[true]_
|
||||
|
||||
| [blue]`OR` / [blue]`\|\|` | _Or_ | True if at least one of the left and right values integers true| [blue]`false or true` _[true]_ +
|
||||
[blue]`"a" == "b" OR (2 == 1)` _[false]_
|
||||
|===
|
||||
|
||||
[CAUTION]
|
||||
====
|
||||
Currently, boolean operations are evaluated using _short cut evaluation_. This means that, if the left expression of operators [blue]`and` and [blue]`or` is sufficient to establish the result of the whole operation, the right expression would not evaluated at all.
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
2 > (a=1) or (a=8) > 0; a // <1>
|
||||
----
|
||||
<1> This multi-expression returns _1_ because in the first expression the left value of [blue]`or` is _true_ and as a conseguence its right value is not computed. Therefore the _a_ variable only receives the integer _1_.
|
||||
====
|
||||
|
||||
=== List
|
||||
_Expr_ supports list of mixed-type values, also specified by normal expressions.
|
||||
|
||||
.List examples
|
||||
[source,go]
|
||||
----
|
||||
[1, 2, 3] // List of integers
|
||||
["one", "two", "three"] // List of strings
|
||||
["one", 2, false, 4.1] // List of mixed-types
|
||||
["one"+1, 2.0*(9-2)] // List of expressions
|
||||
[ [1,"one"], [2,"two"]] // List of lists
|
||||
----
|
||||
|
||||
|
||||
.List operators
|
||||
[cols="^2,^2,5,4"]
|
||||
|===
|
||||
| Symbol | Operation | Description | Examples
|
||||
|
||||
| [blue]`+` | _Join_ | Joins two lists | [blue]`[1,2] + [3]` _[ [1,2,3] ]_
|
||||
|
||||
| [blue]`-` | _Difference_ | Left list without elements in the right list | [blue]`[1,2,3] - [2]` _[ [1,3] ]_
|
||||
|===
|
||||
|
||||
== Variables
|
||||
A variable is an identifier with an assigned value. Variables are stored in the object that implements the _ExprContext_ interface.
|
||||
|
||||
.Examples
|
||||
[source,go]
|
||||
----
|
||||
a=1
|
||||
x = 5.2 * (9-3)
|
||||
x = 1; y = 2*x
|
||||
----
|
||||
|
||||
== Other operations
|
||||
|
||||
=== [blue]`;` operator
|
||||
The semicolon operator [blue]`;` is an infixed operator. It evaluates the left expression first and then the right expression. The latter is the final result.
|
||||
|
||||
IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions.
|
||||
|
||||
TIP: [blue]`;` can be used to set some variables before the final calculation.
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
a=1; b=2; c=3; a+b+c // returns 6
|
||||
----
|
||||
|
||||
=== [blue]`but` operator
|
||||
[blue]`but` is an infixed operator. Its operands can be any type of expression. It evaluates the left expression first, then the right expression. The value of the right expression is the final result. Examples: [blue]`5 but 2` returns 2, [blue]`x=2*3 but x-1` returns 5.
|
||||
|
||||
[blue]`but` is very similar to [blue]`;`. The only difference is that [blue]`;` can't be used inside parenthesis [blue]`(` and [blue]`)`.
|
||||
|
||||
=== Assignment operator [blue]`=`
|
||||
The assignment operator [blue]`=` is used to define variable in the evaluation context or to change their value (see _ExprContext_).
|
||||
The value on the left side of [blue]`=` must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation.
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
a=15+1 // returns 16
|
||||
----
|
||||
|
||||
=== Selector operator [blue]`? : ::`
|
||||
The _selector operator_ is very similar to the _switch/case/default_ statement available in many programming languages.
|
||||
|
||||
.Syntax
|
||||
[source,bnf]
|
||||
----
|
||||
<selector-operator> ::= <expression> "?" <selector-case> { ":" <selector-case> } ["::" <case-value>]
|
||||
<selector-case> ::= [<list>] <case-value>
|
||||
<case-value> ::= "{" <multi-expr> "}"
|
||||
<multi-expr> ::= <expr> {";" <expr>}
|
||||
----
|
||||
|
||||
.Example
|
||||
[source,go]
|
||||
----
|
||||
1 ? {"a"} : {"b"} // returns "b"
|
||||
10 ? {"a"} : {"b"} :: {"c"} // returns "c"
|
||||
10 ? {"a"} :[true, 2+8] {"b"} :: {"c"} // returns "b"
|
||||
10 ? {"a"} :[true, 2+8] {"b"} ::[10] {"c"} // error: "... case list in default clause"
|
||||
10 ? {"a"} :[10] {x="b" but x} :: {"c"} // returns "b"
|
||||
10 ? {"a"} :[10] {x="b"; x} :: {"c"} // returns "b"
|
||||
10 ? {"a"} : {"b"} // error: "... no case catches the value (10) of the selection expression
|
||||
----
|
||||
|
||||
== Priorities of operators
|
||||
The table below shows all supported operators by decreasing priorities.
|
||||
|
||||
.Operators priorities
|
||||
[cols="^2,^2,^2,^5,<5"]
|
||||
|===
|
||||
| Priority | Operators | Position | Operation | Operands and results
|
||||
|
||||
1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ "!" -> _integer_
|
||||
1+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| ("+"\|"-") _number_ -> _number_
|
||||
.5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ "*" _number_ -> _number_
|
||||
| [blue]`*` | _Infix_ | _String-repeat_ | _string_ "*" _integer_ -> _string_
|
||||
| [blue]`/` | _Infix_ | _Division_ | _number_ "/" _number_ -> _number_
|
||||
| [blue]`./` | _Infix_ | _Float-division_ | __number__ "./" _number_ -> _float_
|
||||
| [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ "%" _integer_ -> _integer_
|
||||
.5+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ "+" _number_ -> _number_
|
||||
| [blue]`+` | _Infix_ | _String-concat_ | (_string_\|_number_) "+" (_string_\|_number_) -> _string_
|
||||
| [blue]`+` | _Infix_ | _List-join_ | _list_ "+" _list_ -> _list_
|
||||
| [blue]`-` | _Infix_ | _Subtraction_ | _number_ "-" _number_ -> _number_
|
||||
| [blue]`-` | _Infix_ | _List-difference_ | _list_ "-" _list_ -> _list_
|
||||
.6+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ "<" _comparable_ -> _boolean_
|
||||
| [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ "\<=" _comparable_ -> _boolean_
|
||||
| [blue]`>` | _Infix_ | _greater_ | _comparable_ ">" _comparable_ -> _boolean_
|
||||
| [blue]`>=` | _Infix_ | _greater-equal_ | _comparable_ ">=" _comparable_ -> _boolean_
|
||||
| [blue]`==` | _Infix_ | _equal_ | _comparable_ "==" _comparable_ -> _boolean_
|
||||
| [blue]`!=` | _Infix_ | _not-equal_ | _comparable_ "!=" _comparable_ -> _boolean_
|
||||
.1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | "not" _boolean_ -> _boolean_
|
||||
.2+|*AND*| [blue]`and` | _Infix_ | _and_ | _boolean_ "and" _boolean_ -> _boolean_
|
||||
| [blue]`&&` | _Infix_ | _and_ | _boolean_ "&&" _boolean_ -> _boolean_
|
||||
.2+|*OR*| [blue]`or` | _Infix_ | _or_ | _boolean_ "or" _boolean_ -> _boolean_
|
||||
| [blue]`\|\|` | _Infix_ | _or_ | _boolean_ "\|\|" _boolean_ -> _boolean_
|
||||
.1+|*ASSIGN*| [blue]`=` | _Infix_ | _assignment_ | _identifier_ "=" _any_ -> _any_
|
||||
.1+|*BUT*| [blue]`but` | _Infix_ | _but_ | _any_ "but" _any_ -> _any_
|
||||
|===
|
||||
|
||||
== Functions
|
||||
Functions in _Expr_ are very similar to functions in many programming languages.
|
||||
|
||||
In _Expr_ functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator [blue]`@` it is possibile to export local definition to the calling context.
|
||||
|
||||
=== Function calls
|
||||
#TODO: function calls operations#
|
||||
|
||||
=== Function definitions
|
||||
#TODO: function definitions operations#
|
||||
|
||||
== Builtins
|
||||
#TODO: builtins#
|
||||
|
||||
=== Builtin functions
|
||||
|
||||
=== [blue]_import()_
|
||||
[blue]_import([grey]#<source-file>#)_ loads the multi-expression contained in the specified source and returns its value.
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// expr_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExpr(t *testing.T) {
|
||||
type inputType struct {
|
||||
source string
|
||||
wantResult any
|
||||
wantErr error
|
||||
}
|
||||
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`fact=func(n){(n)?{1}::{n*fact(n-1)}}; fact(5)`, int64(120), nil},
|
||||
}
|
||||
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
inputs1 := []inputType{
|
||||
{`f=openFile("/tmp/test2.txt"); line=readFile(f); closeFile(f); line`, "ciao", nil},
|
||||
//{`f = func(op){op()}; f(func(){2})`, int64(2), nil},
|
||||
}
|
||||
|
||||
for i, input := range inputs1 {
|
||||
var expr Expr
|
||||
var gotResult any
|
||||
var gotErr error
|
||||
|
||||
ctx := NewSimpleFuncStore()
|
||||
// ImportMathFuncs(ctx)
|
||||
// ImportImportFunc(ctx)
|
||||
ImportOsFuncs(ctx)
|
||||
parser := NewParser(ctx)
|
||||
|
||||
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
|
||||
|
||||
r := strings.NewReader(input.source)
|
||||
scanner := NewScanner(r, DefaultTranslations())
|
||||
|
||||
good := true
|
||||
if expr, gotErr = parser.Parse(scanner); gotErr == nil {
|
||||
gotResult, gotErr = expr.Eval(ctx)
|
||||
}
|
||||
|
||||
if gotResult != input.wantResult {
|
||||
t.Errorf("%d: %q -> result = %v [%T], want %v [%T]", i+1, input.source, gotResult, gotResult, input.wantResult, input.wantResult)
|
||||
good = false
|
||||
}
|
||||
|
||||
if gotErr != input.wantErr {
|
||||
if input.wantErr == nil || gotErr == nil || (gotErr.Error() != input.wantErr.Error()) {
|
||||
t.Errorf("%d: %q -> err = <%v>, want <%v>", i+1, input.source, gotErr, input.wantErr)
|
||||
good = false
|
||||
}
|
||||
}
|
||||
|
||||
if good {
|
||||
succeeded++
|
||||
} else {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed))
|
||||
}
|
||||
+5
-2
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-import.go
|
||||
package expr
|
||||
|
||||
@@ -51,7 +54,7 @@ func addEnvImportDirs(dirList []string) []string {
|
||||
}
|
||||
|
||||
func addPresetImportDirs(ctx ExprContext, dirList []string) []string {
|
||||
if dirSpec, exists := getControlString(ctx, control_import_path); exists {
|
||||
if dirSpec, exists := getControlString(ctx, ControlImportPath); exists {
|
||||
dirs := strings.Split(dirSpec, ":")
|
||||
if dirList == nil {
|
||||
dirList = dirs
|
||||
@@ -133,7 +136,7 @@ func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (resu
|
||||
return
|
||||
}
|
||||
|
||||
func importImportFunc(ctx ExprContext) {
|
||||
func ImportImportFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("import", &simpleFunctor{f: importFunc}, 1, -1)
|
||||
ctx.RegisterFunc("include", &simpleFunctor{f: includeFunc}, 1, -1)
|
||||
}
|
||||
|
||||
+8
-1
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// funcs-math.go
|
||||
package expr
|
||||
|
||||
@@ -101,7 +104,11 @@ func mulFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func importMathFuncs(ctx ExprContext) {
|
||||
func ImportMathFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("add", &simpleFunctor{f: addFunc}, 0, -1)
|
||||
ctx.RegisterFunc("mul", &simpleFunctor{f: mulFunc}, 0, -1)
|
||||
}
|
||||
|
||||
func init() {
|
||||
registerImport("math.arith", ImportMathFuncs)
|
||||
}
|
||||
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// func-os.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type osHandle interface {
|
||||
getFile() *os.File
|
||||
}
|
||||
|
||||
type osWriter struct {
|
||||
fh *os.File
|
||||
writer *bufio.Writer
|
||||
}
|
||||
|
||||
func (h *osWriter) getFile() *os.File {
|
||||
return h.fh
|
||||
}
|
||||
|
||||
type osReader struct {
|
||||
fh *os.File
|
||||
reader *bufio.Reader
|
||||
}
|
||||
|
||||
func (h *osReader) getFile() *os.File {
|
||||
return h.fh
|
||||
}
|
||||
|
||||
func createFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var filePath string
|
||||
if len(args) > 0 {
|
||||
filePath, _ = args[0].(string)
|
||||
}
|
||||
|
||||
if len(filePath) > 0 {
|
||||
var fh *os.File
|
||||
if fh, err = os.Create(filePath); err == nil {
|
||||
result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("%s(): missing the file path", name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func openFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var filePath string
|
||||
if len(args) > 0 {
|
||||
filePath, _ = args[0].(string)
|
||||
}
|
||||
|
||||
if len(filePath) > 0 {
|
||||
var fh *os.File
|
||||
if fh, err = os.Open(filePath); err == nil {
|
||||
result = &osReader{fh: fh, reader: bufio.NewReader(fh)}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("%s(): missing the file path", name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func appendFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var filePath string
|
||||
if len(args) > 0 {
|
||||
filePath, _ = args[0].(string)
|
||||
}
|
||||
|
||||
if len(filePath) > 0 {
|
||||
var fh *os.File
|
||||
if fh, err = os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660); err == nil {
|
||||
result = &osWriter{fh: fh, writer: bufio.NewWriter(fh)}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("%s(): missing the file path", name)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func closeFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var handle osHandle
|
||||
|
||||
if len(args) > 0 {
|
||||
handle, _ = args[0].(osHandle)
|
||||
}
|
||||
|
||||
if handle != nil {
|
||||
if fh := handle.getFile(); fh != nil {
|
||||
if w, ok := handle.(*osWriter); ok {
|
||||
err = w.writer.Flush()
|
||||
}
|
||||
if err == nil {
|
||||
err = fh.Close()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("%s(): invalid file handle", name)
|
||||
}
|
||||
result = err == nil
|
||||
return
|
||||
}
|
||||
|
||||
func writeFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var handle osHandle
|
||||
|
||||
if len(args) > 0 {
|
||||
handle, _ = args[0].(osHandle)
|
||||
}
|
||||
|
||||
if handle != nil {
|
||||
if fh := handle.getFile(); fh != nil {
|
||||
if w, ok := handle.(*osWriter); ok {
|
||||
result, err = fmt.Fprint(w.writer, args[1:]...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func readFileFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
var handle osHandle
|
||||
|
||||
if len(args) > 0 {
|
||||
handle, _ = args[0].(osHandle)
|
||||
}
|
||||
|
||||
if handle != nil {
|
||||
if fh := handle.getFile(); fh != nil {
|
||||
if r, ok := handle.(*osReader); ok {
|
||||
var limit byte = '\n'
|
||||
var v string
|
||||
if len(args) > 1 {
|
||||
if s, ok := args[1].(string); ok && len(s) > 0 {
|
||||
limit = s[0]
|
||||
}
|
||||
}
|
||||
if v, err = r.reader.ReadString(limit); err == nil || err == io.EOF {
|
||||
if len(v) > 0 && v[len(v)-1] == limit {
|
||||
result = v[0 : len(v)-1]
|
||||
} else {
|
||||
result = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ImportOsFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("openFile", &simpleFunctor{f: openFileFunc}, 1, 1)
|
||||
ctx.RegisterFunc("appendFile", &simpleFunctor{f: appendFileFunc}, 1, 1)
|
||||
ctx.RegisterFunc("createFile", &simpleFunctor{f: createFileFunc}, 1, 1)
|
||||
ctx.RegisterFunc("writeFile", &simpleFunctor{f: writeFileFunc}, 1, -1)
|
||||
ctx.RegisterFunc("readFile", &simpleFunctor{f: readFileFunc}, 1, 2)
|
||||
ctx.RegisterFunc("closeFile", &simpleFunctor{f: closeFileFunc}, 1, 1)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// function-register.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var functionRegister map[string]func(ExprContext)
|
||||
|
||||
func registerImport(name string, importFunc func(ExprContext)) {
|
||||
if functionRegister == nil {
|
||||
functionRegister = make(map[string]func(ExprContext))
|
||||
}
|
||||
functionRegister[name] = importFunc
|
||||
}
|
||||
|
||||
func ImportInContext(ctx ExprContext, name string) (exists bool) {
|
||||
var importFunc func(ExprContext)
|
||||
if importFunc, exists = functionRegister[name]; exists {
|
||||
importFunc(ctx)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ImportInContextByGlobPattern(ctx ExprContext, pattern string) (count int, err error) {
|
||||
var matched bool
|
||||
for name, importFunc := range functionRegister {
|
||||
if matched, err = filepath.Match(pattern, name); err == nil {
|
||||
if matched {
|
||||
count++
|
||||
importFunc(ctx)
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
if functionRegister == nil {
|
||||
functionRegister = make(map[string]func(ExprContext))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// graph.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func BuildGraph(root *term) (g string) {
|
||||
//var sb strings.Builder
|
||||
g = fmt.Sprintf("tree: %v", root)
|
||||
|
||||
r := NewReticle(root)
|
||||
fmt.Println(r)
|
||||
return
|
||||
}
|
||||
|
||||
type NodeRef struct {
|
||||
node *term
|
||||
pos int
|
||||
label string
|
||||
}
|
||||
|
||||
type Level []*NodeRef
|
||||
|
||||
type Reticle struct {
|
||||
levels []Level
|
||||
left, right int
|
||||
colsWidth []int
|
||||
}
|
||||
|
||||
func NewExprReticle(e Expr) *Reticle {
|
||||
tree, _ := e.(*ast)
|
||||
return NewReticle(tree.root)
|
||||
}
|
||||
|
||||
func NewReticle(tree *term) *Reticle {
|
||||
r := &Reticle{levels: make([]Level, 0)}
|
||||
r.build(tree, 0, 0)
|
||||
r.computeCharWidth()
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Reticle) computeCharWidth() {
|
||||
numCol := r.right - r.left + 1
|
||||
r.colsWidth = make([]int, numCol)
|
||||
for _, level := range r.levels {
|
||||
for _, ref := range level {
|
||||
c := ref.pos - r.left
|
||||
if v := ref.node.value(); v != nil {
|
||||
ref.label = fmt.Sprintf("%v", v)
|
||||
} else {
|
||||
ref.label = ref.node.source()
|
||||
}
|
||||
r.colsWidth[c] = max(r.colsWidth[c], len(ref.label)+2) // +2 to make room for brakets
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reticle) build(node *term, depth, pos int) {
|
||||
var level Level
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
if len(r.levels) == depth {
|
||||
level = make(Level, 0)
|
||||
r.levels = append(r.levels, level)
|
||||
} else {
|
||||
level = r.levels[depth]
|
||||
}
|
||||
ref := &NodeRef{node: node, pos: pos}
|
||||
level = append(level, ref)
|
||||
if r.left > pos {
|
||||
r.left = pos
|
||||
}
|
||||
if r.right < pos {
|
||||
r.right = pos
|
||||
}
|
||||
if node.children != nil {
|
||||
halfPos := len(node.children) / 2
|
||||
childPos := pos - halfPos - 1
|
||||
for _, child := range node.children {
|
||||
childPos++
|
||||
if childPos == pos && (len(node.children)&1) == 0 {
|
||||
childPos++
|
||||
}
|
||||
r.build(child, depth+1, childPos)
|
||||
}
|
||||
}
|
||||
r.levels[depth] = level
|
||||
}
|
||||
|
||||
func (r *Reticle) nodeInLevel(level []*NodeRef, index int) (node *NodeRef) {
|
||||
for _, ref := range level {
|
||||
if ref.pos-r.left == index {
|
||||
node = ref
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Reticle) String() string {
|
||||
var sb strings.Builder
|
||||
for _, level := range r.levels {
|
||||
for j, w := range r.colsWidth {
|
||||
var label string
|
||||
if ref := r.nodeInLevel(level, j); ref != nil {
|
||||
s := "(" + ref.label + ")"
|
||||
label = fmt.Sprintf("%[1]*s", -w, fmt.Sprintf("%[1]*s", (w+len(s))/2, s))
|
||||
} else {
|
||||
label = strings.Repeat(" ", w)
|
||||
}
|
||||
sb.WriteString(label)
|
||||
}
|
||||
sb.WriteByte('\n')
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// graph_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGraph(t *testing.T) {
|
||||
// tk0 := NewToken(0, 0, SymChangeSign, "-")
|
||||
tk1 := NewValueToken(0, 0, SymInteger, "100", 100)
|
||||
tk2 := NewToken(0, 0, SymPlus, "+")
|
||||
tk3 := NewValueToken(0, 0, SymInteger, "200", 200)
|
||||
|
||||
tree := NewAst()
|
||||
// tree.addToken(tk0)
|
||||
tree.addToken(tk1)
|
||||
tree.addToken(tk2)
|
||||
tree.addToken(tk3)
|
||||
tk4 := NewToken(0, 0, SymPlus, "-")
|
||||
tk5 := NewValueToken(0, 0, SymInteger, "50", 50)
|
||||
tree.addToken(tk4)
|
||||
tree.addToken(tk5)
|
||||
|
||||
g := BuildGraph(tree.root)
|
||||
fmt.Println(g)
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// helpers.go
|
||||
package expr
|
||||
|
||||
|
||||
+3
-2
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// helpers_test.go
|
||||
package expr
|
||||
|
||||
@@ -45,7 +48,6 @@ func TestEvalStringA(t *testing.T) {
|
||||
t.Errorf("Source %q got %v, want %v", source, gotResult, wantResult)
|
||||
t.Errorf("Error: %v", gotErr)
|
||||
}
|
||||
fmt.Println("Hello World!")
|
||||
}
|
||||
|
||||
func TestEvalString(t *testing.T) {
|
||||
@@ -76,5 +78,4 @@ func TestEvalString(t *testing.T) {
|
||||
t.Errorf("Source %q got %v, want %v", source, gotResult, wantResult)
|
||||
t.Errorf("Error: %v", gotErr)
|
||||
}
|
||||
fmt.Println("Hello World!")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
First test
|
||||
next commit
|
||||
nr 3
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// it-range.go
|
||||
package expr
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// iterator.go
|
||||
package expr
|
||||
|
||||
|
||||
+6
-1
@@ -89,7 +89,12 @@ type funcDefFunctor struct {
|
||||
func (functor *funcDefFunctor) Invoke(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
for i, p := range functor.params {
|
||||
if i < len(args) {
|
||||
ctx.setVar(p, args[i])
|
||||
arg := args[i]
|
||||
if functor, ok := arg.(Functor); ok {
|
||||
ctx.RegisterFunc(p, functor, 0, -1)
|
||||
} else {
|
||||
ctx.setVar(p, arg)
|
||||
}
|
||||
} else {
|
||||
ctx.setVar(p, nil)
|
||||
}
|
||||
|
||||
+37
-4
@@ -5,11 +5,15 @@
|
||||
package expr
|
||||
|
||||
// -------- list term
|
||||
func newListTermA(args ...*term) *term {
|
||||
return newListTerm(args)
|
||||
}
|
||||
|
||||
func newListTerm(args []*term) *term {
|
||||
return &term{
|
||||
tk: *NewToken(0, 0, SymList, "[]"),
|
||||
tk: *NewValueToken(0, 0, SymList, "[]", args),
|
||||
parent: nil,
|
||||
children: args,
|
||||
children: nil,
|
||||
position: posLeaf,
|
||||
priority: priValue,
|
||||
evalFunc: evalList,
|
||||
@@ -18,8 +22,9 @@ func newListTerm(args []*term) *term {
|
||||
|
||||
// -------- list func
|
||||
func evalList(ctx ExprContext, self *term) (v any, err error) {
|
||||
items := make([]any, len(self.children))
|
||||
for i, tree := range self.children {
|
||||
list, _ := self.value().([]*term)
|
||||
items := make([]any, len(list))
|
||||
for i, tree := range list {
|
||||
var param any
|
||||
if param, err = tree.compute(ctx); err != nil {
|
||||
break
|
||||
@@ -31,3 +36,31 @@ func evalList(ctx ExprContext, self *term) (v any, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// // -------- list term
|
||||
// func newListTerm(args []*term) *term {
|
||||
// return &term{
|
||||
// tk: *NewToken(0, 0, SymList, "[]"),
|
||||
// parent: nil,
|
||||
// children: args,
|
||||
// position: posLeaf,
|
||||
// priority: priValue,
|
||||
// evalFunc: evalList,
|
||||
// }
|
||||
// }
|
||||
|
||||
// // -------- list func
|
||||
// func evalList(ctx ExprContext, self *term) (v any, err error) {
|
||||
// items := make([]any, len(self.children))
|
||||
// for i, tree := range self.children {
|
||||
// var param any
|
||||
// if param, err = tree.compute(ctx); err != nil {
|
||||
// break
|
||||
// }
|
||||
// items[i] = param
|
||||
// }
|
||||
// if err == nil {
|
||||
// v = items
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
// operand-selector-case.go
|
||||
package expr
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// -------- selector case term
|
||||
|
||||
@@ -13,6 +16,18 @@ type selectorCase struct {
|
||||
caseExpr Expr
|
||||
}
|
||||
|
||||
func (sc *selectorCase) String() string {
|
||||
var sb strings.Builder
|
||||
if sc.filterList != nil {
|
||||
sc.filterList.toString(&sb)
|
||||
sb.WriteByte(' ')
|
||||
}
|
||||
sb.WriteByte('{')
|
||||
sb.WriteString(sc.caseExpr.String())
|
||||
sb.WriteByte('}')
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func newSelectorCaseTerm(row, col int, filterList *term, caseExpr Expr) *term {
|
||||
tk := NewValueToken(row, col, SymSelectorCase, "", &selectorCase{filterList: filterList, caseExpr: caseExpr})
|
||||
return &term{
|
||||
|
||||
+2
-2
@@ -48,7 +48,7 @@ func newAndTerm(tk *Token) (inst *term) {
|
||||
}
|
||||
|
||||
func evalAnd(ctx ExprContext, self *term) (v any, err error) {
|
||||
if isEnabled(ctx, control_bool_shortcut) {
|
||||
if isEnabled(ctx, ControlBoolShortcut) {
|
||||
v, err = evalAndWithShortcut(ctx, self)
|
||||
} else {
|
||||
v, err = evalAndWithoutShortcut(ctx, self)
|
||||
@@ -117,7 +117,7 @@ func newOrTerm(tk *Token) (inst *term) {
|
||||
}
|
||||
|
||||
func evalOr(ctx ExprContext, self *term) (v any, err error) {
|
||||
if isEnabled(ctx, control_bool_shortcut) {
|
||||
if isEnabled(ctx, ControlBoolShortcut) {
|
||||
v, err = evalOrWithShortcut(ctx, self)
|
||||
} else {
|
||||
v, err = evalOrWithoutShortcut(ctx, self)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-length.go
|
||||
package expr
|
||||
|
||||
//-------- builtin term
|
||||
|
||||
func newBuiltinTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 1),
|
||||
position: posPrefix,
|
||||
priority: priSign,
|
||||
evalFunc: evalBuiltin,
|
||||
}
|
||||
}
|
||||
|
||||
func evalBuiltin(ctx ExprContext, self *term) (v any, err error) {
|
||||
var rightValue any
|
||||
|
||||
if rightValue, err = self.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
count := 0
|
||||
if isList(rightValue) {
|
||||
list, _ := rightValue.([]any)
|
||||
for i, moduleSpec := range list {
|
||||
if module, ok := moduleSpec.(string); ok {
|
||||
if ImportInContext(ctx, module) {
|
||||
count++
|
||||
} else {
|
||||
err = self.Errorf("unknown module %q", module)
|
||||
break
|
||||
}
|
||||
} else {
|
||||
err = self.Errorf("expected string at item nr %d, got %T", i+1, moduleSpec)
|
||||
break
|
||||
}
|
||||
}
|
||||
} else if isString(rightValue) {
|
||||
module, _ := rightValue.(string)
|
||||
count, err = ImportInContextByGlobPattern(ctx, module)
|
||||
} else {
|
||||
err = self.errIncompatibleType(rightValue)
|
||||
}
|
||||
if err == nil {
|
||||
v = count
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymKwBuiltin, newBuiltinTerm)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-dot.go
|
||||
package expr
|
||||
|
||||
// -------- dot term
|
||||
func newDotTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priDot,
|
||||
evalFunc: evalDot,
|
||||
}
|
||||
}
|
||||
|
||||
func evalDot(ctx ExprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
indexTerm := self.children[1]
|
||||
if !isInteger(rightValue) {
|
||||
err = indexTerm.Errorf("index expression must be integer, got %T", rightValue)
|
||||
return
|
||||
}
|
||||
|
||||
index64, _ := rightValue.(int64)
|
||||
index := int(index64)
|
||||
|
||||
if isList(leftValue) {
|
||||
list, _ := leftValue.([]any)
|
||||
if index >= 0 && index < len(list) {
|
||||
v = list[index]
|
||||
} else if index >= -len(list) {
|
||||
v = list[len(list)+index]
|
||||
} else {
|
||||
err = indexTerm.Errorf("index %v out of bounds", index)
|
||||
}
|
||||
} else if isString(leftValue) {
|
||||
s, _ := leftValue.(string)
|
||||
if index >= 0 && index < len(s) {
|
||||
v = string(s[index])
|
||||
} else if index >= -len(s) {
|
||||
v = string(s[len(s)+index])
|
||||
} else {
|
||||
err = indexTerm.Errorf("index %v out of bounds", index)
|
||||
}
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymDot, newDotTerm)
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-insert.go
|
||||
package expr
|
||||
|
||||
//-------- insert term
|
||||
|
||||
func newInsertTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priAssign,
|
||||
evalFunc: evalInsert,
|
||||
}
|
||||
}
|
||||
|
||||
func newAppendTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priAssign,
|
||||
evalFunc: evalAppend,
|
||||
}
|
||||
}
|
||||
|
||||
func evalInsert(ctx ExprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if isList(rightValue) {
|
||||
list, _ := rightValue.([]any)
|
||||
v = append([]any{leftValue}, list...)
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func evalAppend(ctx ExprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if isList(leftValue) {
|
||||
list, _ := leftValue.([]any)
|
||||
v = append(list, rightValue)
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymInsert, newInsertTerm)
|
||||
registerTermConstructor(SymAppend, newAppendTerm)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// operator-length.go
|
||||
package expr
|
||||
|
||||
//-------- length term
|
||||
|
||||
func newLengthTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 1),
|
||||
position: posPrefix,
|
||||
priority: priSign,
|
||||
evalFunc: evalLength,
|
||||
}
|
||||
}
|
||||
|
||||
func evalLength(ctx ExprContext, self *term) (v any, err error) {
|
||||
var rightValue any
|
||||
|
||||
if rightValue, err = self.evalPrefix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if isList(rightValue) {
|
||||
list, _ := rightValue.([]any)
|
||||
v = len(list)
|
||||
} else if isString(rightValue) {
|
||||
s, _ := rightValue.(string)
|
||||
v = len(s)
|
||||
// } else {
|
||||
// v = 1
|
||||
// }
|
||||
} else {
|
||||
err = self.errIncompatibleType(rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymHash, newLengthTerm)
|
||||
}
|
||||
+62
-4
@@ -4,13 +4,71 @@
|
||||
// operator-selector.go
|
||||
package expr
|
||||
|
||||
// //-------- export all term
|
||||
|
||||
// func newSelectorTerm(tk *Token) (inst *term) {
|
||||
// return &term{
|
||||
// tk: *tk,
|
||||
// children: make([]*term, 0, 3),
|
||||
// position: posMultifix,
|
||||
// priority: priSelector,
|
||||
// evalFunc: evalSelector,
|
||||
// }
|
||||
// }
|
||||
|
||||
// func isSelectorCase(ctx ExprContext, exprValue, caseSel any, caseIndex int) (selectedValue any, err error) {
|
||||
// caseData, _ := caseSel.(*selectorCase)
|
||||
// if caseData.filterList == nil {
|
||||
// selectedValue, err = caseData.caseExpr.eval(ctx, false)
|
||||
// } else {
|
||||
// filterList := caseData.filterList.children
|
||||
// if len(filterList) == 0 && exprValue == int64(caseIndex) {
|
||||
// selectedValue, err = caseData.caseExpr.eval(ctx, false)
|
||||
// } else {
|
||||
// var caseValue any
|
||||
// for _, caseTerm := range filterList {
|
||||
// if caseValue, err = caseTerm.compute(ctx); err != nil || caseValue == exprValue {
|
||||
// selectedValue, err = caseData.caseExpr.eval(ctx, false)
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// func evalSelector(ctx ExprContext, self *term) (v any, err error) {
|
||||
// var exprValue any
|
||||
// // var caseList []*term
|
||||
|
||||
// if err = self.checkOperands(); err != nil {
|
||||
// return
|
||||
// }
|
||||
// exprTerm := self.children[0]
|
||||
// if exprValue, err = exprTerm.compute(ctx); err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// caseList := self.children[1:]
|
||||
// for i, caseTerm := range caseList {
|
||||
// caseSel := caseTerm.value()
|
||||
// if v, err = isSelectorCase(ctx, exprValue, caseSel, i); err != nil || v != nil {
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// if err == nil && v == nil {
|
||||
// err = exprTerm.tk.Errorf("no case catches the value (%v) of the selection expression", exprValue)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
//-------- export all term
|
||||
|
||||
func newSelectorTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 3),
|
||||
position: posMultifix,
|
||||
position: posInfix,
|
||||
priority: priSelector,
|
||||
evalFunc: evalSelector,
|
||||
}
|
||||
@@ -20,8 +78,7 @@ func isSelectorCase(ctx ExprContext, exprValue, caseSel any, caseIndex int) (sel
|
||||
caseData, _ := caseSel.(*selectorCase)
|
||||
if caseData.filterList == nil {
|
||||
selectedValue, err = caseData.caseExpr.eval(ctx, false)
|
||||
} else {
|
||||
filterList := caseData.filterList.children
|
||||
} else if filterList, ok := caseData.filterList.value().([]*term); ok {
|
||||
if len(filterList) == 0 && exprValue == int64(caseIndex) {
|
||||
selectedValue, err = caseData.caseExpr.eval(ctx, false)
|
||||
} else {
|
||||
@@ -49,7 +106,8 @@ func evalSelector(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
caseList := self.children[1:]
|
||||
caseListTerm := self.children[1]
|
||||
caseList, _ := caseListTerm.value().([]*term)
|
||||
for i, caseTerm := range caseList {
|
||||
caseSel := caseTerm.value()
|
||||
if v, err = isSelectorCase(ctx, exprValue, caseSel, i); err != nil || v != nil {
|
||||
|
||||
@@ -66,8 +66,6 @@ func evalPlus(ctx ExprContext, self *term) (v any, err error) {
|
||||
func newMinusTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
// class: classOperator,
|
||||
// kind: kindUnknown,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priSum,
|
||||
|
||||
@@ -154,6 +154,19 @@ func (self *parser) parseSelectorCase(scanner *scanner, allowVarRef bool, defaul
|
||||
return
|
||||
}
|
||||
|
||||
func addSelectorCase(selectorTerm, caseTerm *term) {
|
||||
if len(selectorTerm.children) < 2 {
|
||||
caseListTerm := newListTermA(caseTerm)
|
||||
selectorTerm.children = append(selectorTerm.children, caseListTerm)
|
||||
} else {
|
||||
caseListTerm := selectorTerm.children[1]
|
||||
caseList, _ := caseListTerm.value().([]*term)
|
||||
caseList = append(caseList, caseTerm)
|
||||
caseListTerm.tk.Value = caseList
|
||||
}
|
||||
caseTerm.parent = selectorTerm
|
||||
}
|
||||
|
||||
func (self *parser) parseSelector(scanner *scanner, tree *ast, allowVarRef bool) (selectorTerm *term, err error) {
|
||||
var caseTerm *term
|
||||
tk := scanner.makeToken(SymSelector, '?')
|
||||
@@ -162,8 +175,7 @@ func (self *parser) parseSelector(scanner *scanner, tree *ast, allowVarRef bool)
|
||||
}
|
||||
|
||||
if caseTerm, err = self.parseSelectorCase(scanner, allowVarRef, false); err == nil {
|
||||
selectorTerm.children = append(selectorTerm.children, caseTerm)
|
||||
caseTerm.parent = selectorTerm
|
||||
addSelectorCase(selectorTerm, caseTerm)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -179,14 +191,14 @@ func (self *parser) Parse(scanner *scanner, termSymbols ...Symbol) (tree *ast, e
|
||||
func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef bool, termSymbols ...Symbol) (tree *ast, err error) {
|
||||
var selectorTerm *term = nil
|
||||
var currentTerm *term = nil
|
||||
var tk *Token
|
||||
tree = NewAst()
|
||||
firstToken := true
|
||||
lastSym := SymUnknown
|
||||
for tk := scanner.Next(); err == nil && tk != nil && !tk.IsTerm(termSymbols); tk = scanner.Next() {
|
||||
for tk = scanner.Next(); err == nil && tk != nil && !tk.IsTerm(termSymbols); tk = scanner.Next() {
|
||||
if tk.Sym == SymComment {
|
||||
continue
|
||||
}
|
||||
//resetSelector := true
|
||||
|
||||
if tk.Sym == SymSemiColon {
|
||||
if allowForest {
|
||||
@@ -246,9 +258,7 @@ func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef
|
||||
currentTerm, err = tree.addToken2(tk)
|
||||
}
|
||||
case SymQuestion:
|
||||
/*if selectorTerm != nil {
|
||||
err = tk.Errorf("nested selectors must be enclosed in parentheses")
|
||||
} else*/if selectorTerm, err = self.parseSelector(scanner, tree, allowVarRef); err == nil {
|
||||
if selectorTerm, err = self.parseSelector(scanner, tree, allowVarRef); err == nil {
|
||||
currentTerm = selectorTerm
|
||||
}
|
||||
case SymColon, SymDoubleColon:
|
||||
@@ -256,27 +266,25 @@ func (self *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarRef
|
||||
if selectorTerm == nil {
|
||||
err = tk.Errorf("selector-case outside of a selector context")
|
||||
} else if caseTerm, err = self.parseSelectorCase(scanner, allowVarRef, tk.Sym == SymDoubleColon); err == nil {
|
||||
selectorTerm.children = append(selectorTerm.children, caseTerm)
|
||||
caseTerm.parent = selectorTerm
|
||||
addSelectorCase(selectorTerm, caseTerm)
|
||||
currentTerm = caseTerm
|
||||
if tk.Sym == SymDoubleColon {
|
||||
selectorTerm = nil
|
||||
}
|
||||
}
|
||||
//resetSelector = tk.Sym == SymDoubleColon
|
||||
default:
|
||||
currentTerm, err = tree.addToken2(tk)
|
||||
}
|
||||
|
||||
if currentTerm != nil && currentTerm.parent != nil && currentTerm.parent.tk.Sym != SymSelector {
|
||||
if currentTerm != nil && currentTerm.tk.Sym != SymSelector && currentTerm.parent != nil && currentTerm.parent.tk.Sym != SymSelector {
|
||||
selectorTerm = nil
|
||||
|
||||
}
|
||||
// if resetSelector {
|
||||
// selectorTree = nil
|
||||
// }
|
||||
lastSym = tk.Sym
|
||||
}
|
||||
if err == nil {
|
||||
err = tk.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+6
-5
@@ -155,6 +155,7 @@ func TestParser(t *testing.T) {
|
||||
/* 134 */ {`10 ? {"a"} : {"b"}`, nil, errors.New(`[1:3] no case catches the value (10) of the selection expression`)},
|
||||
/* 135 */ {`10 ? {"a"} :: {"b"} : {"c"}`, nil, errors.New(`[1:22] selector-case outside of a selector context`)},
|
||||
/* 136 */ {`1 ? {"a"} : {"b"} ? ["a"] {"A"} :["b"] {"B"}`, "B", nil},
|
||||
/* 137 */ {`2 + 1 ? {"a"} : {"b"} * 3`, "2bbb", nil},
|
||||
}
|
||||
check_env_expr_path := 113
|
||||
|
||||
@@ -162,7 +163,7 @@ func TestParser(t *testing.T) {
|
||||
failed := 0
|
||||
|
||||
// inputs1 := []inputType{
|
||||
// {`1 ? {"a"} : {"b"} ? ["a"] {"A"} :["b"] {"B"}`, "B", nil},
|
||||
// {`10 ? {"a"} :[true, 2+8] {"b"} :: {"c"}`, "b", nil},
|
||||
// }
|
||||
|
||||
for i, input := range inputs {
|
||||
@@ -173,8 +174,8 @@ func TestParser(t *testing.T) {
|
||||
ctx := NewSimpleFuncStore()
|
||||
ctx.SetVar("var1", int64(123))
|
||||
ctx.SetVar("var2", "abc")
|
||||
importMathFuncs(ctx)
|
||||
importImportFunc(ctx)
|
||||
ImportMathFuncs(ctx)
|
||||
ImportImportFuncs(ctx)
|
||||
parser := NewParser(ctx)
|
||||
|
||||
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
|
||||
@@ -204,7 +205,7 @@ func TestParser(t *testing.T) {
|
||||
} else {
|
||||
failed++
|
||||
if i+1 == check_env_expr_path {
|
||||
t.Logf(`NOTICE: Test nr %d requires EXPR_PATH environment variable with value "."`, check_env_expr_path)
|
||||
t.Logf(`NOTICE: Test nr %d requires EXPR_PATH="."`, check_env_expr_path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -249,7 +250,7 @@ func TestListParser(t *testing.T) {
|
||||
ctx := NewSimpleFuncStore()
|
||||
ctx.SetVar("var1", int64(123))
|
||||
ctx.SetVar("var2", "abc")
|
||||
importMathFuncs(ctx)
|
||||
ImportMathFuncs(ctx)
|
||||
parser := NewParser(ctx)
|
||||
|
||||
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
|
||||
|
||||
+76
-17
@@ -152,9 +152,10 @@ func (self *scanner) fetchNextToken() (tk *Token) {
|
||||
case ';':
|
||||
tk = self.makeToken(SymSemiColon, ch)
|
||||
case '.':
|
||||
if next, _ := self.peek(); next >= '0' && next <= '9' {
|
||||
tk = self.parseNumber(ch)
|
||||
} else if next == '/' {
|
||||
//if next, _ := self.peek(); next >= '0' && next <= '9' {
|
||||
// tk = self.parseNumber(ch)
|
||||
//} else if next == '/' {
|
||||
if next, _ := self.peek(); next == '/' {
|
||||
tk = self.moveOn(SymDotSlash, ch, next)
|
||||
} else {
|
||||
tk = self.makeToken(SymDot, ch)
|
||||
@@ -219,6 +220,8 @@ func (self *scanner) fetchNextToken() (tk *Token) {
|
||||
case '<':
|
||||
if next, _ := self.peek(); next == '=' {
|
||||
tk = self.moveOn(SymLessOrEqual, ch, next)
|
||||
} else if next == '<' {
|
||||
tk = self.moveOn(SymAppend, ch, next)
|
||||
} else if next == '>' {
|
||||
tk = self.moveOn(SymLessGreater, ch, next)
|
||||
} else {
|
||||
@@ -227,6 +230,8 @@ func (self *scanner) fetchNextToken() (tk *Token) {
|
||||
case '>':
|
||||
if next, _ := self.peek(); next == '=' {
|
||||
tk = self.moveOn(SymGreaterOrEqual, ch, next)
|
||||
} else if next == '>' {
|
||||
tk = self.moveOn(SymInsert, ch, next)
|
||||
} else {
|
||||
tk = self.makeToken(SymGreater, ch)
|
||||
}
|
||||
@@ -276,17 +281,78 @@ func (self *scanner) sync(err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func isBinaryDigit(ch byte) bool {
|
||||
return ch == '0' || ch == '1'
|
||||
}
|
||||
|
||||
func isOctalDigit(ch byte) bool {
|
||||
return ch >= '0' && ch <= '7'
|
||||
}
|
||||
|
||||
func isDecimalDigit(ch byte) bool {
|
||||
return ch >= '0' && ch <= '9'
|
||||
}
|
||||
|
||||
func isHexDigit(ch byte) bool {
|
||||
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
|
||||
}
|
||||
|
||||
func (self *scanner) initBase(sb *strings.Builder, currentFirstCh byte) (firstCh byte, numBase int, digitFunc func(byte) bool, err error) {
|
||||
var ch byte
|
||||
var digitType string
|
||||
firstCh = currentFirstCh
|
||||
digitFunc = isDecimalDigit
|
||||
numBase = 10
|
||||
|
||||
if ch, err = self.peek(); err == nil {
|
||||
if ch == 'b' || ch == 'B' {
|
||||
numBase = 2
|
||||
digitType = "binary"
|
||||
self.readChar()
|
||||
digitFunc = isBinaryDigit
|
||||
firstCh, err = self.readChar()
|
||||
} else if ch == 'o' || ch == 'O' {
|
||||
numBase = 8
|
||||
digitType = "octal"
|
||||
self.readChar()
|
||||
digitFunc = isOctalDigit
|
||||
firstCh, err = self.readChar()
|
||||
} else if ch == 'x' || ch == 'X' {
|
||||
numBase = 16
|
||||
digitType = "hex"
|
||||
self.readChar()
|
||||
digitFunc = isHexDigit
|
||||
firstCh, err = self.readChar()
|
||||
}
|
||||
if err == nil && !digitFunc(firstCh) {
|
||||
if len(digitType) == 0 {
|
||||
digitType = "decimal"
|
||||
}
|
||||
err = fmt.Errorf("expected %s digit, got '%c'", digitType, firstCh)
|
||||
}
|
||||
} else if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (self *scanner) parseNumber(firstCh byte) (tk *Token) {
|
||||
var err error
|
||||
var ch byte
|
||||
var sym Symbol = SymInteger
|
||||
var value any
|
||||
var sb strings.Builder
|
||||
var isDigit func(byte) bool = isDecimalDigit
|
||||
var numBase = 10
|
||||
|
||||
for ch = firstCh; err == nil && (ch >= '0' && ch <= '9'); ch, err = self.readChar() {
|
||||
if firstCh == '0' {
|
||||
firstCh, numBase, isDigit, err = self.initBase(&sb, firstCh)
|
||||
}
|
||||
for ch = firstCh; err == nil && isDigit(ch); ch, err = self.readChar() {
|
||||
sb.WriteByte(ch)
|
||||
}
|
||||
if ch == '.' {
|
||||
|
||||
if numBase == 10 {
|
||||
if err == nil && ch == '.' {
|
||||
sym = SymFloat
|
||||
sb.WriteByte(ch)
|
||||
ch, err = self.readChar()
|
||||
@@ -296,7 +362,7 @@ func (self *scanner) parseNumber(firstCh byte) (tk *Token) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if ch == 'e' || ch == 'E' {
|
||||
if err == nil && (ch == 'e' || ch == 'E') {
|
||||
sym = SymFloat
|
||||
sb.WriteByte(ch)
|
||||
if ch, err = self.readChar(); err == nil {
|
||||
@@ -308,30 +374,23 @@ func (self *scanner) parseNumber(firstCh byte) (tk *Token) {
|
||||
for ; err == nil && (ch >= '0' && ch <= '9'); ch, err = self.readChar() {
|
||||
sb.WriteByte(ch)
|
||||
}
|
||||
//err = self.sync(err)
|
||||
} else {
|
||||
err = errors.New("expected integer exponent")
|
||||
}
|
||||
}
|
||||
// } else {
|
||||
// err = self.sync(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil && err != io.EOF {
|
||||
tk = self.makeErrorToken(err)
|
||||
} else {
|
||||
var value any
|
||||
err = self.sync(err)
|
||||
txt := sb.String()
|
||||
if sym == SymFloat {
|
||||
value, err = strconv.ParseFloat(txt, 64)
|
||||
} else if strings.HasPrefix(txt, "0x") {
|
||||
value, err = strconv.ParseInt(txt, 16, 64)
|
||||
} else if strings.HasPrefix(txt, "0o") {
|
||||
value, err = strconv.ParseInt(txt, 8, 64)
|
||||
} else if strings.HasPrefix(txt, "0b") {
|
||||
value, err = strconv.ParseInt(txt, 2, 64)
|
||||
} else {
|
||||
value, err = strconv.ParseInt(txt, 10, 64)
|
||||
value, err = strconv.ParseInt(txt, numBase, 64)
|
||||
}
|
||||
if err == nil {
|
||||
tk = self.makeValueToken(sym, txt, value)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// simple-func-store.go
|
||||
package expr
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// simple-var-store.go
|
||||
package expr
|
||||
|
||||
|
||||
@@ -59,6 +59,8 @@ const (
|
||||
SymQuestionEqual // 48: '?='
|
||||
SymDoubleAt // 49: '@@'
|
||||
SymDoubleColon // 50: '::'
|
||||
SymInsert // 51: '>>'
|
||||
SymAppend // 52: '<<'
|
||||
SymChangeSign
|
||||
SymUnchangeSign
|
||||
SymIdentifier
|
||||
@@ -87,6 +89,7 @@ const (
|
||||
SymKwOr
|
||||
SymKwBut
|
||||
SymKwFunc
|
||||
SymKwBuiltin
|
||||
)
|
||||
|
||||
var keywords map[string]Symbol
|
||||
@@ -95,6 +98,7 @@ func init() {
|
||||
//keywords = make(map[string]Symbol)
|
||||
keywords = map[string]Symbol{
|
||||
"AND": SymKwAnd,
|
||||
"BUILTIN": SymKwBuiltin,
|
||||
"BUT": SymKwBut,
|
||||
"FUNC": SymKwFunc,
|
||||
"NOT": SymKwNot,
|
||||
|
||||
@@ -24,6 +24,7 @@ const (
|
||||
priSign
|
||||
priFact
|
||||
priCoalesce
|
||||
priDot
|
||||
priValue
|
||||
)
|
||||
|
||||
@@ -166,7 +167,7 @@ func (self *term) checkOperands() (err error) {
|
||||
switch self.position {
|
||||
case posInfix:
|
||||
if self.children == nil || len(self.children) != 2 || self.anyChildrenNil() {
|
||||
err = self.tk.Errorf("infix operator %q requires two not nil operands, got %d", self.source(), self.getChildrenCount())
|
||||
err = self.tk.Errorf("infix operator %q requires two non-nil operands, got %d", self.source(), self.getChildrenCount())
|
||||
}
|
||||
case posPrefix:
|
||||
if self.children == nil || len(self.children) != 1 || self.children[0] == nil {
|
||||
|
||||
+3
-4
@@ -5,7 +5,6 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -16,7 +15,7 @@ func TestString(t *testing.T) {
|
||||
|
||||
tree := NewAst()
|
||||
if gotErr := tree.addTokens(tk1, tk2, tk3); gotErr == nil {
|
||||
fmt.Println("Tree:", tree)
|
||||
t.Log("Tree:", tree)
|
||||
} else {
|
||||
t.Errorf("err: got <%v>, want <nil>", gotErr)
|
||||
}
|
||||
@@ -27,7 +26,7 @@ func TestGetRoom(t *testing.T) {
|
||||
|
||||
tree := NewAst()
|
||||
if gotErr := tree.addTokens(tk1); gotErr == nil {
|
||||
fmt.Println("Tree-root room:", tree.root.getRoom())
|
||||
t.Log("Tree-root room:", tree.root.getRoom())
|
||||
} else {
|
||||
t.Errorf("err: got <%v>, want <nil>", gotErr)
|
||||
}
|
||||
@@ -37,7 +36,7 @@ func TestGetChildrenCount(t *testing.T) {
|
||||
|
||||
tree := NewAst()
|
||||
if gotErr := tree.addTokens(tk1); gotErr == nil {
|
||||
fmt.Println("Tree-root children count:", tree.root.getChildrenCount())
|
||||
t.Log("Tree-root children count:", tree.root.getChildrenCount())
|
||||
} else {
|
||||
t.Errorf("err: got <%v>, want <nil>", gotErr)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,11 @@ func (tk *Token) DevString() string {
|
||||
|
||||
func (tk *Token) String() string {
|
||||
if tk.Value != nil {
|
||||
return fmt.Sprintf("%#v", tk.Value)
|
||||
if s, ok := tk.Value.(string); ok {
|
||||
return fmt.Sprintf("%q", s)
|
||||
} else {
|
||||
return fmt.Sprintf("%v", tk.Value)
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s", tk.source)
|
||||
}
|
||||
@@ -63,3 +67,17 @@ func (self *Token) Errorf(template string, args ...any) (err error) {
|
||||
err = fmt.Errorf(fmt.Sprintf("[%d:%d] ", self.row, self.col)+template, args...)
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Token) Error() (err error) {
|
||||
if self.Sym == SymError {
|
||||
if msg, ok := self.Value.(error); ok {
|
||||
err = fmt.Errorf("[%d:%d] %v", self.row, self.col, msg)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (self *Token) Errors(msg string) (err error) {
|
||||
err = fmt.Errorf("[%d:%d] %v", self.row, self.col, msg)
|
||||
return
|
||||
}
|
||||
|
||||
+25
-5
@@ -5,14 +5,34 @@
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDevString(t *testing.T) {
|
||||
tk1 := NewValueToken(0, 0, SymInteger, "100", 100)
|
||||
tk2 := NewToken(0, 0, SymPlus, "+")
|
||||
type inputType struct {
|
||||
source string
|
||||
sym Symbol
|
||||
value any
|
||||
wantResult string
|
||||
}
|
||||
|
||||
fmt.Println("Token '100':", tk1.DevString())
|
||||
fmt.Println("Token '+':", tk2.DevString())
|
||||
inputs := []inputType{
|
||||
/* 1 */ {"100", SymInteger, 100, `[55]"100"{100}`},
|
||||
/* 2 */ {"+", SymPlus, nil, `[6]"+"{}`},
|
||||
}
|
||||
|
||||
for i, input := range inputs {
|
||||
var tk *Token
|
||||
if input.value == nil {
|
||||
tk = NewToken(0, 0, input.sym, input.source)
|
||||
} else {
|
||||
tk = NewValueToken(0, 0, input.sym, input.source, input.value)
|
||||
}
|
||||
|
||||
t.Logf("Test nr %2d: %q --> %q", i+1, input.source, input.wantResult)
|
||||
|
||||
if s := tk.DevString(); s != input.wantResult {
|
||||
t.Errorf("wrong token from symbol '+': expected %q, got %q", input.wantResult, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user