diff --git a/doc/Expr.adoc b/doc/Expr.adoc index 68f74f7..9f6e273 100644 --- a/doc/Expr.adoc +++ b/doc/Expr.adoc @@ -22,7 +22,7 @@ Expressions calculator toc::[] -#TODO: Work in progress (last update on 2024/05/15, 10:00 p.m.)# +#TODO: Work in progress (last update on 2024/05/16, 7:08 a.m.)# == Expr _Expr_ is a GO package capable of analysing, interpreting and calculating expressions. @@ -33,9 +33,9 @@ _Expr_ is a GO package capable of analysing, interpreting and calculating expres image::expression-diagram.png[] === `dev-expr` test tool -`dev-expr` is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, beyond in additon to the automatic test suite based on the Go test framework, `dev-expr` provides an important aid for quickly testing of new features during their development. +`dev-expr` is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, in additon to the automatic verification test suite based on the Go test framework, `dev-expr` provides an important aid for quickly testing of new features during their development. -It cat work as a _REPL_, _**R**ead-**E**xecute-**P**rint-**L**oop_, or it can process expression acquired from files or standard input. +`dev-expr` can work as a _REPL_, _**R**ead-**E**xecute-**P**rint-**L**oop_, or it can process expression acquired from files or standard input. The program can be downloaded from https://git.portale-stac.it/go-pkg/-/packages/generic/dev-expr/[dev-expr]. @@ -46,19 +46,21 @@ Here are some examples of execution. ---- # Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program. -[user]$ tools/expr -- Expressions calculator v1.7.0,2024/05/08 (celestino.amoroso@portale-stac.it) - Type help to get the list of command. +[user]$ ./dev-expr +expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it) + Based on the Expr package v0.10.0 + Type help to get the list of available commands See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc >>> help --- REPL commands: + source -- Load a file as input + tty -- Enable/Disable ansi output <1> base -- Set the integer output base: 2, 8, 10, or 16 exit -- Exit the program help -- Show command list ml -- Enable/Disable multi-line output mods -- List builtin modules - source -- Load a file as input - tty -- Enable/Disable ansi output <1> --- Command line options: -b Import builtin modules. @@ -80,9 +82,12 @@ Here are some examples of execution. .REPL examples [source,shell] ---- -[user]$ tools/expr -- Expressions calculator v1.6.1,2024/05/06 (celestino.amoroso@portale-stac.it) - Type help to get the list of command. - See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc +[user]$ ./dev-expr +expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it) + Based on the Expr package v0.10.0 + Type help to get the list of available commands + See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc + >>> 2+3 5 >>> 2+3*(4-1.5) @@ -166,6 +171,20 @@ _sign_ = "**+**" | "**-**" + _dec-seq_ = _see-integer-literal-syntax_ ==== +.Examples +`>>>` [blue]`1.0` + +[green]`1` + +`>>>` [blue]`0.123` + +[green]`0.123` + +`>>>` [blue]`4.5e+3` + +[green]`4500` + +`>>>` [blue]`4.5E-33` + +[green]`4.5e-33` + +`>>>` [blue]`4.5E-3` + +[green]`0.0045` + +`>>>` [blue]`4.5E10` + +[green]`4.5e+10` + + .Arithmetic operators [cols="^1,^2,6,4"] @@ -196,19 +215,19 @@ _digit-seq_ = _see-integer-literal-syntax_ + // ---- `>>>` [blue]`1 | 2` + [green]`1|2` + -`>>>` [blue]`4|6` + -[green]`2|3` [gray]_Fractions are always reduced to their lowest terms_ + +`>>>` [blue]`4|6` [gray]_// Fractions are always reduced to their lowest terms_ + +[green]`2|3` + `>>>` [blue]`1|2 + 2|3` + [green]`7|6` + `>>>` [blue]`1|2 * 2|3` + [green]`1|3` + `>>>` [blue]`1|2 / 1|3` + [green]`3|2` + -`>>>` [blue]`1|2 ./ 1|3` [gray]_Force decimal division_ + +`>>>` [blue]`1|2 ./ 1|3` [gray]_// Force decimal division_ + [green]`1.5` + `>>>` [blue]`-1|2` + [green]`-1|2` + -`>>>` [blue]`1|-2` [gray]_Wrong sign specification_ + +`>>>` [blue]`1|-2` [gray]_// Invalid sign specification_ + [red]_Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1_ + `>>>` [blue]`1|(-2)` + [green]`-1|2` @@ -226,7 +245,18 @@ Fractions can be used together with integers and floats in expressions. === Strings -Strings are character sequences enclosed between two double quote [blue]`"`. Example: [blue]`"I'm a string"`. +Strings are character sequences enclosed between two double quote [blue]`"`. + +.Examples +`>>>` [blue]`"I'm a string"` + +[green]`I'm a string` + +`>>>` [blue]`"123abc?!"` + +[green]`123abc?!` + +`>>>` [blue]`"123\nabc"` + +[green]`123` + +[green]`abc` + +`>>>` [blue]`"123\tabc"` + +[green]`123{nbsp}{nbsp}{nbsp}{nbsp}abc` Some arithmetic operators can also be used with strings. @@ -244,10 +274,9 @@ Some arithmetic operators can also be used with strings. The items of strings can be accessed using the dot `.` operator. .Item access syntax -[source,bnf] ----- - ::= "." ----- +==== +_item_ = _string-expr_ "**.**" _integer-expr_ +==== .String examples `>>>` [blue]`s="abc"` [gray]_assign the string to variable s_ + @@ -262,26 +291,25 @@ The items of strings can be accessed using the dot `.` operator. [green]`3` + === Boolean -Boolean data type has two values only: _true_ and _false_. Relational and Boolean expressions produce Boolean values. - +Boolean data type has two values only: [blue]_true_ and [blue]_false_. Relational and boolean expressions result in 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]_ +| [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_ |=== @@ -290,19 +318,19 @@ Boolean data type has two values only: _true_ and _false_. Relational and Boolea |=== | Symbol | Operation | Description | Examples -| [blue]`NOT` | _Not_ | True if the right value is false | [blue]`NOT true` _[false]_ + -[blue]`NOT (2 < 1)` _[true]_ +| [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]`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]_ +| [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. +Currently, boolean operations are evaluated using _short cut evaluation_. This means that, if the left expression of the [blue]`and` and [blue]`or` operators is sufficient to establish the result of the whole operation, the right expression would not evaluated at all. .Example [source,go] diff --git a/doc/Expr.html b/doc/Expr.html index 4801cd5..8e67358 100644 --- a/doc/Expr.html +++ b/doc/Expr.html @@ -584,7 +584,7 @@ pre.rouge .ss {
-

TODO: Work in progress (last update on 2024/05/15, 10:00 p.m.)

+

TODO: Work in progress (last update on 2024/05/16, 7:08 a.m.)

@@ -608,10 +608,10 @@ pre.rouge .ss {

1.2. dev-expr test tool

-

dev-expr is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, beyond in additon to the automatic test suite based on the Go test framework, dev-expr provides an important aid for quickly testing of new features during their development.

+

dev-expr is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, in additon to the automatic verification test suite based on the Go test framework, dev-expr provides an important aid for quickly testing of new features during their development.

-

It cat work as a REPL, Read-Execute-Print-Loop, or it can process expression acquired from files or standard input.

+

dev-expr can work as a REPL, Read-Execute-Print-Loop, or it can process expression acquired from files or standard input.

The program can be downloaded from dev-expr.

@@ -624,19 +624,21 @@ pre.rouge .ss {
# Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program.
 
-[user]$ tools/expr -- Expressions calculator v1.7.0,2024/05/08 (celestino.amoroso@portale-stac.it)
-	Type help to get the list of command.
+[user]$ ./dev-expr
+expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it)
+	Based on the Expr package v0.10.0
+	Type help to get the list of available commands
 	See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
 
 >>> help
 --- REPL commands:
+      source -- Load a file as input
+         tty -- Enable/Disable ansi output (1)
         base -- Set the integer output base: 2, 8, 10, or 16
         exit -- Exit the program
         help -- Show command list
           ml -- Enable/Disable multi-line output
         mods -- List builtin modules
-      source -- Load a file as input
-         tty -- Enable/Disable ansi output (1)
 
 --- Command line options:
     -b <builtin>       Import builtin modules.
@@ -667,9 +669,12 @@ pre.rouge .ss {
 
REPL examples
-
[user]$ tools/expr -- Expressions calculator v1.6.1,2024/05/06 (celestino.amoroso@portale-stac.it)
-        Type help to get the list of command.
-        See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
+
[user]$ ./dev-expr
+expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it)
+	Based on the Expr package v0.10.0
+	Type help to get the list of available commands
+	See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
+
 >>> 2+3
 5
 >>> 2+3*(4-1.5)
@@ -836,6 +841,21 @@ pre.rouge .ss {
 
+
+
Examples
+

>>> 1.0
+1
+>>> 0.123
+0.123
+>>> 4.5e+3
+4500
+>>> 4.5E-33
+4.5e-33
+>>> 4.5E-3
+0.0045
+>>> 4.5E10
+4.5e+10

+
@@ -906,19 +926,19 @@ pre.rouge .ss {
Examples

>>> 1 | 2
1|2
->>> 4|6
-2|3 Fractions are always reduced to their lowest terms
+>>> 4|6 // Fractions are always reduced to their lowest terms
+2|3
>>> 1|2 + 2|3
7|6
>>> 1|2 * 2|3
1|3
>>> 1|2 / 1|3
3|2
->>> 1|2 ./ 1|3 Force decimal division
+>>> 1|2 ./ 1|3 // Force decimal division
1.5
>>> -1|2
-1|2
->>> 1|-2 Wrong sign specification
+>>> 1|-2 // Invalid sign specification
Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1
>>> 1|(-2)
-1|2

@@ -939,7 +959,19 @@ pre.rouge .ss {

2.2. Strings

-

Strings are character sequences enclosed between two double quote ". Example: "I’m a string".

+

Strings are character sequences enclosed between two double quote ".

+
+
+
Examples
+

>>> "I’m a string"
+I’m a string
+>>> "123abc?!"
+123abc?!
+>>> "123\nabc"
+123
+abc
+>>> "123\tabc"
+123    abc

Some arithmetic operators can also be used with strings.

@@ -979,10 +1011,12 @@ pre.rouge .ss {

The items of strings can be accessed using the dot . operator.

-
-
Item access syntax
+
+
Example 4. Item access syntax
-
<item> ::= <string-expr>"."<index-expr>
+
+

item = string-expr "." integer-expr

+
@@ -1002,7 +1036,7 @@ pre.rouge .ss {

2.3. Boolean

-

Boolean data type has two values only: true and false. Relational and Boolean expressions produce Boolean values.

+

Boolean data type has two values only: true and false. Relational and boolean expressions result in boolean values.

Table 2. Arithmetic operators
@@ -1025,43 +1059,43 @@ pre.rouge .ss { - + - + - + - + - + - +
Table 4. Relational operators

==

Equal

True if the left value is equal to the right one

5 == 2 [false]
-"a" == "a" [true]

5 == 2false
+"a" == "a"true

!=

Not Equal

True if the left value is NOT equal to the right one

5 != 2 [true]
-"a" != "a" [false]

5 != 2true
+"a" != "a"false

<

Less

True if the left value is less than the right one

5 < 2 [false]
-"a" < "b" [true]

5 < 2false
+"a" < "b"true

<=

Less or Equal

True if the left value is less than or equal to the right one

5 <= 2 [false]
-"b" <= "b" [true]

5 <= 2false
+"b" <= "b"true

>

Greater

True if the left value is greater than the right one

5 > 2 [true]
-"a" < "b" [false]

5 > 2true
+"a" < "b"false

>=

Greater or Equal

True if the left value is greater than or equal to the right one

5 >= 2 [true]
-"b" <= "b" [true]

5 >= 2true
+"b" <= "b"true

@@ -1086,22 +1120,22 @@ pre.rouge .ss {

NOT

Not

True if the right value is false

-

NOT true [false]
-NOT (2 < 1) [true]

+

NOT truefalse
+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]

+

false && truefalse
+"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]

+

false or truetrue
+"a" == "b" OR (2 == 1)false

@@ -1113,7 +1147,7 @@ pre.rouge .ss {
-

Currently, boolean operations are evaluated using short cut evaluation. This means that, if the left expression of operators and and or is sufficient to establish the result of the whole operation, the right expression would not evaluated at all.

+

Currently, boolean operations are evaluated using short cut evaluation. This means that, if the left expression of the and and or operators is sufficient to establish the result of the whole operation, the right expression would not evaluated at all.

Example
@@ -1627,7 +1661,7 @@ The value on the left side of = must be an identifier.