README.adoc: examples, relational operators table completed

This commit is contained in:
Celestino Amoroso 2024-04-02 08:18:34 +02:00
parent 4683a08da2
commit 4fc8ac64e7

View File

@ -25,6 +25,32 @@ toc::[]
== Expr
_Expr_ is a GO package capable of analysing, interpreting and calculating expressions.
.Examples taken from parser_test.go
[source,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> [blue]`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_.
=== Usage
@ -82,7 +108,7 @@ func main() {
----
=== Data types
_Expr_ supports numerical, string, relational, and boolean expressions.
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
==== Numbers
Numbers can be integers (GO int64) and float (GO float64). In mixed operations involving integers and floats, integers are automatically promoted to floats.
@ -92,24 +118,27 @@ Numbers can be integers (GO int64) and float (GO float64). In mixed operations i
|===
| Symbol | Operation | Description | Examples
| `+` / `-` | _change sign_ | Change the sign of values | `-1` _[-1]_, `-(+2)` _[-2]_
| [blue]`+` / [blue]`-` | _change sign_ | Change the sign of values | [blue]`-1` _[-1]_, [blue]`-(+2)` _[-2]_
| `+` | _sum_ | Add two values | `-1 + 2` _[1]_, `4 + 0.5` _[4.5]_
| [blue]`+` | _sum_ | Add two values | [blue]`-1 + 2` _[1]_, [blue]`4 + 0.5` _[4.5]_
| `-` | _subtracion_ | Subtract the right value from the left one | `3 - 1` _[2]_, `4 - 0.5` _[3.5]_
| [blue]`-` | _subtracion_ | Subtract the right value from the left one | [blue]`3 - 1` _[2]_, [blue]`4 - 0.5` _[3.5]_
| `*` | _product_ | Multiply two values | `-1 * 2` _[-2]_, `4 * 0.5` _[2.0]_
| [blue]`*` | _product_ | Multiply two values | `-1 * 2` _[-2]_, [blue]`4 * 0.5` _[2.0]_
| `/` | _Division_ | Divide the left value by the right one | `-1 / 2` _[0]_, `1.0 / 2` _[0.5]_
| [blue]`/` | _Division_ | Divide the left value by the right one | [blue]`-1 / 2` _[0]_, [blue]`1.0 / 2` _[0.5]_
| `./` | _Float division_ | Force float division | `-1 ./ 2` _[-0.5]_
| [blue]`./` | _Float division_ | Force float division | [blue]`-1 ./ 2` _[-0.5]_
| `%` | _Modulo_ | Remainder of the integer division | `5 % 2` _[1]_
| [blue]`%` | _Modulo_ | Remainder of the integer division | [blue]`5 % 2` _[1]_
|===
==== String
Strings are character sequences enclosed between two double quote `"`.
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.
==== Boolean
Boolean data type has two values only: _true_ and _false_. Relational and Boolean expressions produce a Boolean values.
@ -120,19 +149,23 @@ Boolean data type has two values only: _true_ and _false_. Relational and Boolea
|===
| Symbol | Operation | Description | Examples
| `<` | _Less_ | True if the left value is less than the right one | `5 < 2` _[false]_, `"a" < "b"` [true]
| `\<=` | _Less or Greater_ | True if the left value is less or greater than the right one | `5 <= 2` _[false]_, `"b" <= "b"` [true]
| [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="^1,^2,6,4"]
[cols="^2,^2,6,4"]
|===
| Symbol | Operation | Description | Examples
| `NOT` | _Not_ | True if the right value is false | `NOT true` _[false]_, `NOT (2 < 1)` _[true]_
| [blue]`NOT` | _Not_ | True if the right value is false | [blue]`NOT true` _[false]_, [blue]`NOT (2 < 1)` _[true]_
| `AND` / `&&` | _And_ | True if both left and right values are true | `false && true` _[false]_, `"a" < "b" AND 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]_
| `OR` / `\|\|` | _Or_ | True if at least one of the left and right values integers true| `false or true` _[true]_, `"a" == "b" OR (2 == 1)` _[false]_
| [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]_
|===