Compare commits

...

2 Commits

3 changed files with 157 additions and 59 deletions

View File

@ -79,20 +79,20 @@ Here are some examples of execution.
# Type 'exit' or Ctrl+D to quit the program.
[user]$ ./dev-expr
dev-expr -- Expressions calculator v1.10.0(build 14),2024/06/17 (celestino.amoroso@portale-stac.it)
Based on the Expr package v0.19.0
dev-expr -- Expressions calculator v1.12.0(build 1),2024/09/14 (celestino.amoroso@portale-stac.it)
Based on the Expr package v0.26.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
output -- Enable/Disable printing expression results. Options 'on', 'off', 'status'
source -- Load a file as input
tty -- Enable/Disable ansi output <1>
--- Command line options:
-b <builtin> Import builtin modules.
@ -155,9 +155,9 @@ _Expr_ supports three type of numbers:
. [blue]#Integers#
. [blue]#Floats#
. [blue]#Factions#
. [blue]#Fractions#
In mixed operations involving integers, fractions and floats, automatic type promotion to the largest type take place.
In mixed operations involving integers, fractions and floats, automatic type promotion to the largest type is performed.
==== Integers
__Expr__'s integers are a subset of the integer set. Internally they are stored as Golang _int64_ values.
@ -183,11 +183,11 @@ Value range: *-9223372036854775808* to *9223372036854775807*
[cols="^1,^2,6,4"]
|===
| Symbol | Operation | Description | Examples
| [blue]`+` | _sum_ | Add two values | [blue]`-1 + 2` -> 1
| [blue]`-` | _subtraction_ | Subtract the right value from the left one | [blue]`3 - 1` -> 2
| [blue]`*` | _product_ | Multiply two values | [blue]`-1 * 2` -> -2
| [blue]`/` | _Division_ | Divide the left value by the right one^(*)^ | [blue]`-10 / 2` -> 5
| [blue]`%` | _Modulo_ | Remainder of the integer division | [blue]`5 % 2` -> 1
| [blue]`+` | _Sum_ | Add two values | [blue]`-1 + 2` -> _1_
| [blue]`-` | _Subtraction_ | Subtract the right value from the left one | [blue]`3 - 1` -> _2_
| [blue]`*` | _Product_ | Multiply two values | [blue]`-1 * 2` -> _-2_
| [blue]`/` | _Integer division_ | Divide the left value by the right one^(*)^ | [blue]`-11 / 2` -> _-5_
| [blue]`%` | _Modulo_ | Remainder of the integer division | [blue]`5 % 2` -> _1_
|===
^(*)^ See also the _float division_ [blue]`./` below.
@ -228,11 +228,11 @@ _dec-seq_ = _see-integer-literal-syntax_
[cols="^1,^2,6,4"]
|===
| Symbol | Operation | Description | Examples
| [blue]`+` | _sum_ | Add two values | [blue]`4 + 0.5` -> 4.5
| [blue]`-` | _subtraction_ | Subtract the right value from the left one | [blue]`4 - 0.5` -> 3.5
| [blue]`*` | _product_ | Multiply two values | [blue]`4 * 0.5` -> 2.0
| [blue]`/` | _Division_ | Divide the left value by the right one | [blue]`1.0 / 2` -> 0.5
| [blue]`./`| _Float division_ | Force float division | [blue]`-1 ./ 2` -> -0.5
| [blue]`+` | _Sum_ | Add two values | [blue]`4 + 0.5` -> 4.5
| [blue]`-` | _Subtraction_ | Subtract the right value from the left one | [blue]`4 - 0.5` -> 3.5
| [blue]`*` | _Product_ | Multiply two values | [blue]`4 * 0.5` -> 2.0
| [blue]`/` | _Float division_ | Divide the left value by the right one | [blue]`1.0 / 2` -> 0.5
| [blue]`./`| _Forced float division_ | Force float division | [blue]`-1 ./ 2` -> -0.5
|===
==== Fractions
@ -308,7 +308,7 @@ Strings are character sequences enclosed between two double quote [blue]`"`.
`>>>` [blue]`"123\tabc"` +
[green]`123{nbsp}{nbsp}{nbsp}{nbsp}abc`
Some arithmetic operators can also be used with strings.
Some arithmetic operators also apply to strings.
.String operators
[cols="^1,^2,6,4"]
@ -444,6 +444,7 @@ _non-empty-list_ = "**[**" _any-value_ {"**,**" _any-value_} "**]**" +
| [blue]`[]` | _Item at index_ | Item at given position | [blue]`[1,2,3][1]` -> _2_
| [blue]`in` | _Item in list_ | True if item is in list | [blue]`2 in [1,2,3]` -> _true_ +
[blue]`6 in [1,2,3]` -> _false_
| [blue]`#` | _Size_ | Number of items in a list | [blue]`#[1,2,3]` -> _3_
|===
Array's items can be accessed using the index `[]` operator.
@ -458,10 +459,16 @@ Array's items can be accessed using the index `[]` operator.
*_slice_* = _string-expr_ "**[**" _integer-expr_ "**:**" _integer-expr_ "**]**"
====
.Items of list
.Examples: Getting items from lists
`>>>` [blue]`[1,2,3][1]` +
[green]`2`
`>>>` [blue]`index=2; ["a", "b", "c", "d"][index]` +
[green]`c`
`>>>` [blue]`["a", "b", "c", "d"][2:]` +
[green]`["c", "d"]`
`>>>` [blue]`list=[1,2,3]; list[1]` +
[green]`2`
@ -471,25 +478,44 @@ Array's items can be accessed using the index `[]` operator.
`>>>` [blue]`list=["one","two","three"]; list[2-1]` +
[green]`two`
`>>>` [blue]`list[1]="six"; list` +
[green]`["one", "six", "three"]`
`>>>` [blue]`list[-1]` +
[green]`three`
`>>>` [blue]`list[10]` +
[red]`Eval Error: [1:9] index 10 out of bounds`
.Example: Number of elements in a list
`>>>` [blue]`#list` +
[green]`3`
`>>>` [blue]`index=2; ["a", "b", "c", "d"][index]` +
[green]`c`
.Examples: Element insertion
`>>>` [blue]`"first" >> list` +
[green]`["first", "one", "six", "three"]`
`>>>` [blue]`list << "last"` +
[green]`["first", "one", "six", "three", "last"]`
.Examples: Element in list
`>>>` [blue]`"six" in list` +
[green]`true`
`>>>` [blue]`"ten" in list` +
[green]`false`
.Examples: Concatenation and filtering
`>>>` [blue]`[1,2,3] + ["one", "two", "three"]` +
[green]`[1, 2, 3, "one", "two", "three"]`
`>>>` [blue]`[1,2,3,4] - [2,4]` +
[green]`[1, 3]`
`>>>` [blue]`["a", "b", "c", "d"][2:]` +
[green]`["c", "d"]`
=== Dictionaries
The _dictionary_, or _dict_, data-type is set of pairs _key/value_. It is also known as _map_ or _associative array_.
The _dictionary_, or _dict_, data-type represents sets of pairs _key/value_. It is also known as _map_ or _associative array_.
Dictionary literals are sequences of pairs separated by comma [blue]`,` enclosed between brace brackets.
@ -515,6 +541,7 @@ _non-empty-dict_ = "**{**" _key-scalar_ "**:**" _any-value_ {"**,**" _key-scalar
| [blue]`[]` | _Dict item value_ | Item value of given key | [blue]`{"one":1, "two":2}["two"]` -> _2_
| [blue]`in` | _Key in dict_ | True if key is in dict | [blue]`"one" in {"one":1, "two":2}` -> _true_ +
[blue]`"six" in {"one":1, "two":2}` -> _false_
| [blue]`#` | _Size_ | Number of items in a dict | [blue]`#{1:"a",2:"b",3:"c"}` -> _3_
|===
.Examples
@ -533,6 +560,9 @@ _non-empty-dict_ = "**{**" _key-scalar_ "**:**" _any-value_ {"**,**" _key-scalar
`>>>` [blue]`d={"one":1, "two":2}; d["six"]=6; d` +
[green]`{"two": 2, "one": 1, "six": 6}`
`>>>` [blue]`#d` +
[green]`3`
== Variables
_Expr_, like most programming languages, supports variables. A variable is an identifier with an assigned value. Variables are stored in _contexts_.
@ -556,7 +586,7 @@ NOTE: The assign operator [blue]`=` returns the value assigned to the variable.
`>>>` [blue]`a_b` +
[green]`3`
`>>>` [blue]`x = 5.2 * (9-3)` [gray]_// The assigned value has the typical approximation error of the float data-type_ +
`>>>` [blue]`x = 5.2 * (9-3)` [gray]_// The assigned value here has the typical approximation error of the float data-type_ +
[green]`31.200000000000003`
`>>>` [blue]`x = 1; y = 2*x` +
@ -664,8 +694,8 @@ The [blue]`:` symbol (colon) is the separator of the selector-cases. Note that i
[red]`Eval Error: [1:3] no case catches the value (10) of the selection expression`
=== Variable default value [blue]`??` and [blue]`?=`
The left operand of these two operators must be a variable. The right operator can be any expression. They return the value of the variable if this is defined; otherwise they return the value of the right expression.
=== Variable default value [blue]`??`, [blue]`?=`, and [blue]`?!`
The left operand of first two operators, [blue]`??` and [blue]`?=`, must be a variable. The right operator can be any expression. They return the value of the variable if this is defined; otherwise they return the value of the right expression.
IMPORTANT: If the left variable is defined, the right expression is not evaluated at all.
@ -673,6 +703,10 @@ The [blue]`??` operator do not change the status of the left variable.
The [blue]`?=` assigns the calculated value of the right expression to the left variable.
The third one, [blue]`?!`, is the alternate operator. If the variable on the left size is not defined, it returns [blue]_nil_. Otherwise it returns the result of the expressione on the right side.
IMPORTANT: If the left variable is NOT defined, the right expression is not evaluated at all.
.Examples
`>>>` [blue]`var ?? (1+2)` +
[green]`3`

View File

@ -567,7 +567,7 @@ pre.rouge .ss {
<li><a href="#_but_operator">4.2. <code class="blue">but</code> operator</a></li>
<li><a href="#_assignment_operator">4.3. Assignment operator <code class="blue">=</code></a></li>
<li><a href="#_selector_operator">4.4. Selector operator <code class="blue">? : ::</code></a></li>
<li><a href="#_variable_default_value_and">4.5. Variable default value <code class="blue">??</code> and <code class="blue">?=</code></a></li>
<li><a href="#_variable_default_value_and">4.5. Variable default value <code class="blue">??</code>, <code class="blue">?=</code>, and <code class="blue">?!</code></a></li>
</ul>
</li>
<li><a href="#_priorities_of_operators">5. Priorities of operators</a></li>
@ -687,20 +687,20 @@ pre.rouge .ss {
<pre class="rouge highlight"><code data-lang="shell"><span class="c"># Type 'exit' or Ctrl+D to quit the program.</span>
<span class="o">[</span>user]<span class="nv">$ </span>./dev-expr
dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o">(</span>build 14<span class="o">)</span>,2024/06/17 <span class="o">(</span>celestino.amoroso@portale-stac.it<span class="o">)</span>
Based on the Expr package v0.19.0
dev-expr <span class="nt">--</span> Expressions calculator v1.12.0<span class="o">(</span>build 1<span class="o">)</span>,2024/09/14 <span class="o">(</span>celestino.amoroso@portale-stac.it<span class="o">)</span>
Based on the Expr package v0.26.0
Type <span class="nb">help </span>to get the list of available commands
See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
<span class="o">&gt;&gt;&gt;</span> <span class="nb">help</span>
<span class="nt">---</span> REPL commands:
<span class="nb">source</span> <span class="nt">--</span> Load a file as input
<span class="nb">tty</span> <span class="nt">--</span> Enable/Disable ansi output <i class="conum" data-value="1"></i><b>(1)</b>
base <span class="nt">--</span> Set the integer output base: 2, 8, 10, or 16
<span class="nb">exit</span> <span class="nt">--</span> Exit the program
<span class="nb">help</span> <span class="nt">--</span> Show <span class="nb">command </span>list
ml <span class="nt">--</span> Enable/Disable multi-line output
mods <span class="nt">--</span> List <span class="nb">builtin </span>modules
output <span class="nt">--</span> Enable/Disable printing expression results. Options <span class="s1">'on'</span>, <span class="s1">'off'</span>, <span class="s1">'status'</span>
<span class="nb">source</span> <span class="nt">--</span> Load a file as input
<span class="nb">tty</span> <span class="nt">--</span> Enable/Disable ansi output <i class="conum" data-value="1"></i><b>(1)</b>
<span class="nt">---</span> Command line options:
<span class="nt">-b</span> &lt;<span class="nb">builtin</span><span class="o">&gt;</span> Import <span class="nb">builtin </span>modules.
@ -805,12 +805,12 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<p><span class="blue">Floats</span></p>
</li>
<li>
<p><span class="blue">Factions</span></p>
<p><span class="blue">Fractions</span></p>
</li>
</ol>
</div>
<div class="paragraph">
<p>In mixed operations involving integers, fractions and floats, automatic type promotion to the largest type take place.</p>
<p>In mixed operations involving integers, fractions and floats, automatic type promotion to the largest type is performed.</p>
</div>
<div class="sect3">
<h4 id="_integers"><a class="anchor" href="#_integers"></a><a class="link" href="#_integers">2.1.1. Integers</a></h4>
@ -855,33 +855,33 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">+</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>sum</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Sum</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Add two values</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-1 + 2</code> &#8594; 1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-1 + 2</code> &#8594; <em>1</em></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">-</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>subtraction</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Subtraction</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Subtract the right value from the left one</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">3 - 1</code> &#8594; 2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">3 - 1</code> &#8594; <em>2</em></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">*</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>product</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Product</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Multiply two values</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-1 * 2</code> &#8594; -2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-1 * 2</code> &#8594; <em>-2</em></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">/</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Division</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Integer division</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Divide the left value by the right one<sup>(*)</sup></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-10 / 2</code> &#8594; 5</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-11 / 2</code> &#8594; <em>-5</em></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">%</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Modulo</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Remainder of the integer division</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">5 % 2</code> &#8594; 1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">5 % 2</code> &#8594; <em>1</em></p></td>
</tr>
</tbody>
</table>
@ -946,31 +946,31 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">+</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>sum</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Sum</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Add two values</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">4 + 0.5</code> &#8594; 4.5</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">-</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>subtraction</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Subtraction</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Subtract the right value from the left one</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">4 - 0.5</code> &#8594; 3.5</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">*</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>product</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Product</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Multiply two values</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">4 * 0.5</code> &#8594; 2.0</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">/</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Division</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Float division</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Divide the left value by the right one</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">1.0 / 2</code> &#8594; 0.5</p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">./</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Float division</em></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Forced float division</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Force float division</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">-1 ./ 2</code> &#8594; -0.5</p></td>
</tr>
@ -1074,7 +1074,7 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<code class="green">123&#160;&#160;&#160;&#160;abc</code></p>
</div>
<div class="paragraph">
<p>Some arithmetic operators can also be used with strings.</p>
<p>Some arithmetic operators also apply to strings.</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<caption class="title">Table 3. String operators</caption>
@ -1393,6 +1393,12 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">2 in [1,2,3]</code> &#8594; <em>true</em><br>
<code class="blue">6 in [1,2,3]</code> &#8594; <em>false</em></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">#</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Size</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Number of items in a list</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">#[1,2,3]</code> &#8594; <em>3</em></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
@ -1415,11 +1421,19 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
</div>
</div>
<div class="paragraph">
<div class="title">Items of list</div>
<div class="title">Examples: Getting items from lists</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">[1,2,3][1]</code><br>
<code class="green">2</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">index=2; ["a", "b", "c", "d"][index]</code><br>
<code class="green">c</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">["a", "b", "c", "d"][2:]</code><br>
<code class="green">["c", "d"]</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">list=[1,2,3]; list[1]</code><br>
<code class="green">2</code></p>
</div>
@ -1432,6 +1446,10 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<code class="green">two</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">list[1]="six"; list</code><br>
<code class="green">["one", "six", "three"]</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">list[-1]</code><br>
<code class="green">three</code></p>
</div>
@ -1440,22 +1458,42 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<code class="red">Eval Error: [1:9] index 10 out of bounds</code></p>
</div>
<div class="paragraph">
<div class="title">Example: Number of elements in a list</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">#list</code><br>
<code class="green">3</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">index=2; ["a", "b", "c", "d"][index]</code><br>
<code class="green">c</code></p>
<div class="title">Examples: Element insertion</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">"first" &gt;&gt; list</code><br>
<code class="green">["first", "one", "six", "three"]</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">["a", "b", "c", "d"][2:]</code><br>
<code class="green">["c", "d"]</code></p>
<p><code>&gt;&gt;&gt;</code> <code class="blue">list &lt;&lt; "last"</code><br>
<code class="green">["first", "one", "six", "three", "last"]</code></p>
</div>
<div class="paragraph">
<div class="title">Examples: Element in list</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">"six" in list</code><br>
<code class="green">true</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">"ten" in list</code><br>
<code class="green">false</code></p>
</div>
<div class="paragraph">
<div class="title">Examples: Concatenation and filtering</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">[1,2,3] + ["one", "two", "three"]</code><br>
<code class="green">[1, 2, 3, "one", "two", "three"]</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">[1,2,3,4] - [2,4]</code><br>
<code class="green">[1, 3]</code></p>
</div>
</div>
<div class="sect2">
<h3 id="_dictionaries"><a class="anchor" href="#_dictionaries"></a><a class="link" href="#_dictionaries">2.5. Dictionaries</a></h3>
<div class="paragraph">
<p>The <em>dictionary</em>, or <em>dict</em>, data-type is set of pairs <em>key/value</em>. It is also known as <em>map</em> or <em>associative array</em>.</p>
<p>The <em>dictionary</em>, or <em>dict</em>, data-type represents sets of pairs <em>key/value</em>. It is also known as <em>map</em> or <em>associative array</em>.</p>
</div>
<div class="paragraph">
<p>Dictionary literals are sequences of pairs separated by comma <code class="blue">,</code> enclosed between brace brackets.</p>
@ -1514,6 +1552,12 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">"one" in {"one":1, "two":2}</code> &#8594; <em>true</em><br>
<code class="blue">"six" in {"one":1, "two":2}</code> &#8594; <em>false</em></p></td>
</tr>
<tr>
<td class="tableblock halign-center valign-top"><p class="tableblock"><code class="blue">#</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Size</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Number of items in a dict</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">#{1:"a",2:"b",3:"c"}</code> &#8594; <em>3</em></p></td>
</tr>
</tbody>
</table>
<div class="paragraph">
@ -1537,6 +1581,10 @@ dev-expr <span class="nt">--</span> Expressions calculator v1.10.0<span class="o
<p><code>&gt;&gt;&gt;</code> <code class="blue">d={"one":1, "two":2}; d["six"]=6; d</code><br>
<code class="green">{"two": 2, "one": 1, "six": 6}</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">#d</code><br>
<code class="green">3</code></p>
</div>
</div>
</div>
</div>
@ -1582,7 +1630,7 @@ The assign operator <code class="blue">=</code> returns the value assigned to th
<code class="green">3</code></p>
</div>
<div class="paragraph">
<p><code>&gt;&gt;&gt;</code> <code class="blue">x = 5.2 * (9-3)</code> <em class="gray">// The assigned value has the typical approximation error of the float data-type</em><br>
<p><code>&gt;&gt;&gt;</code> <code class="blue">x = 5.2 * (9-3)</code> <em class="gray">// The assigned value here has the typical approximation error of the float data-type</em><br>
<code class="green">31.200000000000003</code></p>
</div>
<div class="paragraph">
@ -1751,9 +1799,9 @@ Technically <code class="blue">;</code> is not treated as a real operator. It ac
</div>
</div>
<div class="sect2">
<h3 id="_variable_default_value_and"><a class="anchor" href="#_variable_default_value_and"></a><a class="link" href="#_variable_default_value_and">4.5. Variable default value <code class="blue">??</code> and <code class="blue">?=</code></a></h3>
<h3 id="_variable_default_value_and"><a class="anchor" href="#_variable_default_value_and"></a><a class="link" href="#_variable_default_value_and">4.5. Variable default value <code class="blue">??</code>, <code class="blue">?=</code>, and <code class="blue">?!</code></a></h3>
<div class="paragraph">
<p>The left operand of these two operators must be a variable. The right operator can be any expression. They return the value of the variable if this is defined; otherwise they return the value of the right expression.</p>
<p>The left operand of first two operators, <code class="blue">??</code> and <code class="blue">?=</code>, must be a variable. The right operator can be any expression. They return the value of the variable if this is defined; otherwise they return the value of the right expression.</p>
</div>
<div class="admonitionblock important">
<table>
@ -1774,6 +1822,21 @@ If the left variable is defined, the right expression is not evaluated at all.
<p>The <code class="blue">?=</code> assigns the calculated value of the right expression to the left variable.</p>
</div>
<div class="paragraph">
<p>The third one, <code class="blue">?!</code>, is the alternate operator. If the variable on the left size is not defined, it returns <em class="blue">nil</em>. Otherwise it returns the result of the expressione on the right side.</p>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
If the left variable is NOT defined, the right expression is not evaluated at all.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<div class="title">Examples</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">var ?? (1+2)</code><br>
<code class="green">3</code></p>
@ -2204,7 +2267,7 @@ These operators have a high priority, in particular higher than the operator <co
</div>
<div id="footer">
<div id="footer-text">
Last updated 2024-09-12 06:56:30 +0200
Last updated 2024-09-14 11:58:38 +0200
</div>
</div>
</body>

View File

@ -417,6 +417,7 @@ func (parser *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarR
case SymEqual:
// if err = checkPrevSymbol(lastSym, SymIdentifier, tk); err == nil {
currentTerm, err = tree.addToken(tk)
firstToken = true
// }
case SymFuncDef:
var funcDefTerm *term
@ -483,7 +484,7 @@ func (parser *parser) parseGeneral(scanner *scanner, allowForest bool, allowVarR
// return
// }
func (parser *parser) expandOpAssign(scanner * scanner, tree *ast, tk *Token, allowVarRef bool) (t *term, err error) {
func (parser *parser) expandOpAssign(scanner *scanner, tree *ast, tk *Token, allowVarRef bool) (t *term, err error) {
var opSym Symbol
var opString string