From dce49fd2b7f369c884e4689b8f6d70096fea8207 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Thu, 9 May 2024 07:20:22 +0200 Subject: [PATCH] Doc: fractions and dictionaries --- doc/Expr.adoc | 54 +++++++++++++++++++- doc/Expr.html | 136 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 154 insertions(+), 36 deletions(-) diff --git a/doc/Expr.adoc b/doc/Expr.adoc index fb3fa9e..ff6a06b 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/07, 07:15 am)# +#TODO: Work in progress (last update on 2024/05/09, 07:17 am)# == Expr _Expr_ is a GO package capable of analysing, interpreting and calculating expressions. @@ -140,7 +140,44 @@ Numbers can be integers (GO int64) or float (GO float64). In mixed operations in |=== -=== String +=== Fractions +_Expr_ also suports fractions. Fraction literals are made with tow integers separated by a vertical bar `|`. + +.Examples +// [source,go] +// ---- +`>>>` [blue]`1 | 2` + +[green]`1|2` + +`>>>` [blue]`4|6` + +[green]`2|3` [gray]_Fractions are always reduced to theri lowest terms_ + +`>>>` [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_ + +[green]`1.5` + +`>>>` [blue]`-1|2` + +[green]`-1|2` + +`>>>` [blue]`1|-2` [gray]_Wrong sign specification_ + +[red]_Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1_ + +`>>>` [blue]`1|(-2)` + +[green]`-1|2` +// ---- + +Fractions can be used together with integers and floats in expressions. + +`>>>` [blue]`1|2 + 5` + +[green]`11|2` + +`>>>` [blue]`4 - 1|2` + +[green]`7|2` + +`>>>` [blue]`1.0 + 1|2` + +[green]`1.5` + + + + +=== Strings 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. @@ -232,6 +269,19 @@ _Expr_ supports list of mixed-type values, also specified by normal expressions. | [blue]`-` | _Difference_ | Left list without elements in the right list | [blue]`[1,2,3] - [2]` _[ [1,3] ]_ |=== +== Dictionaries +The _dictionary_ data-type is set of pairs _key/value_. It is also known as _map_ or _associative array_. Dictionary literals are sequences of pairs separated by comma `,`; sequences are enclosed between brace brackets. + +.List examples +[source,go] +---- +{1:"one", 2:"two"} +{"one":1, "two": 2} +{"sum":1+2+3, "prod":1*2*3} +---- + +WARNING: Support for dictionaries is still ongoing. + == Variables A variable is an identifier with an assigned value. Variables are stored in the object that implements the _ExprContext_ interface. diff --git a/doc/Expr.html b/doc/Expr.html index d770864..d0f9ed4 100644 --- a/doc/Expr.html +++ b/doc/Expr.html @@ -542,31 +542,33 @@ pre.rouge .ss {
  • 2. Data types
  • -
  • 3. Variables
  • -
  • 4. Other operations +
  • 3. Dictionaries
  • +
  • 4. Variables
  • +
  • 5. Other operations
  • -
  • 5. Priorities of operators
  • -
  • 6. Functions +
  • 6. Priorities of operators
  • +
  • 7. Functions
  • -
  • 7. Builtins +
  • 8. Builtins
  • @@ -577,7 +579,7 @@ pre.rouge .ss {
    -

    TODO: Work in progress (last update on 2024/05/07, 07:15 am)

    +

    TODO: Work in progress (last update on 2024/05/09, 07:17 am)

    @@ -787,7 +789,45 @@ Here are some examples of execution.

    -

    2.2. String

    +

    2.2. Fractions

    +
    +

    Expr also suports fractions. Fraction literals are made with tow integers separated by a vertical bar |.

    +
    +
    +
    Examples
    +

    >>> 1 | 2
    +1|2
    +>>> 4|6
    +2|3 Fractions are always reduced to theri lowest terms
    +>>> 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.5
    +>>> -1|2
    +-1|2
    +>>> 1|-2 Wrong sign specification
    +Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1
    +>>> 1|(-2)
    +-1|2

    +
    +
    +

    Fractions can be used together with integers and floats in expressions.

    +
    +
    +

    >>> 1|2 + 5
    +11|2
    +>>> 4 - 1|2
    +7|2
    +>>> 1.0 + 1|2
    +1.5

    +
    +
    +
    +

    2.3. Strings

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

    @@ -828,7 +868,7 @@ Here are some examples of execution.

    -

    2.3. Boolean

    +

    2.4. Boolean

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

    @@ -963,7 +1003,7 @@ Here are some examples of execution.

    -

    2.4. List

    +

    2.5. List

    Expr supports list of mixed-type values, also specified by normal expressions.

    @@ -1012,7 +1052,35 @@ Here are some examples of execution.

    -

    3. Variables

    +

    3. Dictionaries

    +
    +
    +

    The dictionary data-type is set of pairs key/value. It is also known as map or associative array. Dictionary literals are sequences of pairs separated by comma ,; sequences are enclosed between brace brackets.

    +
    +
    +
    List examples
    +
    +
    {1:"one", 2:"two"}
    +{"one":1, "two": 2}
    +{"sum":1+2+3, "prod":1*2*3}
    +
    +
    +
    + + + + + +
    + + +Support for dictionaries is still ongoing. +
    +
    +
    +
    +
    +

    4. Variables

    A variable is an identifier with an assigned value. Variables are stored in the object that implements the ExprContext interface.

    @@ -1028,10 +1096,10 @@ Here are some examples of execution.

    -

    4. Other operations

    +

    5. Other operations

    -

    4.1. ; operator

    +

    5.1. ; operator

    The semicolon operator ; is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The latter is the final result.

    @@ -1067,7 +1135,7 @@ Technically ; is not treated as a real operator. It ac
    -

    4.2. but operator

    +

    5.2. but operator

    but is an infixed operator. Its operands can be expressions of any type. It evaluates the left expression first, then the right expression. The value of the right expression is the final result. Examples: 5 but 2 returns 2, x=2*3 but x-1 returns 5.

    @@ -1076,7 +1144,7 @@ Technically ; is not treated as a real operator. It ac
    -

    4.3. Assignment operator =

    +

    5.3. Assignment operator =

    The assignment operator = is used to define variables in the evaluation context or to change their value (see ExprContext). The value on the left side of = must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation.

    @@ -1089,7 +1157,7 @@ The value on the left side of = must be an identifier.
    -

    4.4. Selector operator ? : ::

    +

    5.4. Selector operator ? : ::

    The selector operator is very similar to the switch/case/default statement available in many programming languages.

    @@ -1129,7 +1197,7 @@ The value on the left side of = must be an identifier.
    -

    5. Priorities of operators

    +

    6. Priorities of operators

    The table below shows all supported operators by decreasing priorities.

    @@ -1350,7 +1418,7 @@ The value on the left side of = must be an identifier.
    -

    6. Functions

    +

    7. Functions

    Functions in Expr are very similar to functions in many programming languages.

    @@ -1359,13 +1427,13 @@ The value on the left side of = must be an identifier.

    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 @ it is possibile to export local definition to the calling context.

    -

    6.1. Function calls

    +

    7.1. Function calls

    TODO: function calls operations

    -

    6.2. Function definitions

    +

    7.2. Function definitions

    TODO: function definitions operations

    @@ -1373,17 +1441,17 @@ The value on the left side of = must be an identifier.
    -

    7. Builtins

    +

    8. Builtins

    TODO: builtins

    -

    7.2. import()

    +

    8.2. import()

    import(<source-file>) loads the multi-expression contained in the specified source and returns its value.

    @@ -1393,7 +1461,7 @@ The value on the left side of = must be an identifier.