Doc: more details on the syntax of the dictionaries, variables and multi-expressions

This commit is contained in:
Celestino Amoroso 2024-05-17 07:31:13 +02:00
parent f0a152a17a
commit ac3e690f87
2 changed files with 215 additions and 115 deletions

View File

@ -22,7 +22,7 @@ Expressions calculator
toc::[] toc::[]
#TODO: Work in progress (last update on 2024/05/16, 7:08 a.m.)# #TODO: Work in progress (last update on 2024/05/17, 7:30 a.m.)#
== Expr == Expr
_Expr_ is a GO package capable of analysing, interpreting and calculating expressions. _Expr_ is a GO package capable of analysing, interpreting and calculating expressions.
@ -44,7 +44,7 @@ Here are some examples of execution.
.Run `dev-expr` in REPL mode and ask for help .Run `dev-expr` in REPL mode and ask for help
[source,shell] [source,shell]
---- ----
# Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program. # Type 'exit' or Ctrl+D to quit the program.
[user]$ ./dev-expr [user]$ ./dev-expr
expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it) expr -- Expressions calculator v1.7.1(build 2),2024/05/16 (celestino.amoroso@portale-stac.it)
@ -235,6 +235,7 @@ _digit-seq_ = _see-integer-literal-syntax_ +
Fractions can be used together with integers and floats in expressions. Fractions can be used together with integers and floats in expressions.
.Examples
`>>>` [blue]`1|2 + 5` + `>>>` [blue]`1|2 + 5` +
[green]`11|2` + [green]`11|2` +
`>>>` [blue]`4 - 1|2` + `>>>` [blue]`4 - 1|2` +
@ -290,7 +291,7 @@ _item_ = _string-expr_ "**.**" _integer-expr_
`>>>` [blue]`#"abc"` [gray]_number of chars_ + `>>>` [blue]`#"abc"` [gray]_number of chars_ +
[green]`3` + [green]`3` +
=== Boolean === Booleans
Boolean data type has two values only: [blue]_true_ and [blue]_false_. Relational and boolean expressions result in boolean values. Boolean data type has two values only: [blue]_true_ and [blue]_false_. Relational and boolean expressions result in boolean values.
.Relational operators .Relational operators
@ -341,35 +342,42 @@ Currently, boolean operations are evaluated using _short cut evaluation_. This m
==== ====
=== Lists === Lists
_Expr_ supports list of mixed-type values, also specified by normal expressions. _Expr_ supports list of mixed-type values, also specified by normal expressions. Internally, _Expr_'s lists are Go arrays.
.List examples .List literal syntax
[source,go] ====
---- *_list_* = _empty-list_ | _non-empty-list_ +
[1, 2, 3] // List of integers _empty-list_ = "**[]**" +
["one", "two", "three"] // List of strings _non-empty-list_ = "**[**" _any-value_ {"**,**" _any-value} "**]**" +
["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 .Examples
---- `>>>` [blue]`[1,2,3]` [gray]_// List of integers_ +
[green]`[1, 2, 3]` +
`>>>` [blue]`["one", "two", "three"]` [gray]_// List of strings_ +
[green]`["one", "two", "three"]` +
`>>>` [blue]`["one", 2, false, 4.1]` [gray]_// List of mixed-types_ +
[green]`["one", 2, false, 4.1]` +
`>>>` [blue]`["one"+1, 2.0*(9-2)]` [gray]_// List of expressions_ +
[green]`["one1", 14]` +
`>>>` [blue]`[ [1,"one"], [2,"two"]]` [gray]_// List of lists_ +
[green]`[[1, "one"], [2, "two"]]`
.List operators .List operators
[cols="^2,^2,5,4"] [cols="^2,^2,5,4"]
|=== |===
| Symbol | Operation | Description | Examples | Symbol | Operation | Description | Examples
| [blue]`+` | _Join_ | Joins two lists | [blue]`[1,2] + [3]` _[ [1,2,3] ]_ | [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]_
| [blue]`-` | _Difference_ | Left list without elements in the right list | [blue]`[1,2,3] - [2]` _[ [1,3] ]_
|=== |===
The items of array can be accessed using the dot `.` operator. The items of array can be accessed using the dot `.` operator.
.Item access syntax .Item access syntax
[source,bnf] ====
---- _item_ = _list-expr_ "**.**" _list-expr_
<item> ::= <list-expr>"."<index-expr> ====
----
.Items of list .Items of list
`>>>` [blue]`[1,2,3].1` + `>>>` [blue]`[1,2,3].1` +
@ -389,44 +397,73 @@ The items of array can be accessed using the dot `.` operator.
== Dictionaries === 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.
.Dictionary 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. WARNING: Support for dictionaries is still ongoing.
== Variables The _dictionary_, or _dict_, 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.
A variable is an identifier with an assigned value. Variables are stored in the object that implements the Go _ExprContext_ interface, e.g. _SimpleVarStore_ or _SimpleFuncStore_.
.Dict literal syntax
====
*_dict_* = _empty-dict_ | _non-empty-dict_ +
_empty-dict_ = "**{}**" +
_non-empty-dict_ = "**{**" _key-scalar_ "**:**" _any-value_ {"**,**" _key-scalar_ "**:**" _any-value} "**}**" +
====
.Examples .Examples
[source,go] `>>>` [blue]`{1:"one", 2:"two"}` +
---- `>>>` [blue]`{"one":1, "two": 2}` +
a=1 `>>>` [blue]`{"sum":1+2+3, "prod":1*2*3}`
x = 5.2 * (9-3)
x = 1; y = 2*x == Variables
---- _Expr_ supports variables like most programming languages. A variable is an identifier with an assigned value. Variables are stored in _contexts_.
.Variable literal syntax
====
*_variable_* = _identifier_ "*=*" _any-value_ +
_identifier_ = _alpha_ {(_alpha_)|_dec-digit_|"*_*"} +
__alpha__ = "*a*"|"*b*"|..."*z*"|"*A*"|"*B*"|..."*Z*"
====
NOTE: The assign operator [blue]`=` returns the value assigned to the variable.
.Examples
`>>>` [blue]`a=1` +
[green]`1` +
`>>>` [blue]`a_b=1+2` +
[green]`1+2` +
`>>>` [blue]`a_b` +
[green]`3` +
`>>>` [blue]`x = 5.2 * (9-3)` [gray]_// The assigned value has the approximation error typical of the float data-type_ +
[green]`31.200000000000003` +
`>>>` [blue]`x = 1; y = 2*x` +
[green]`2` +
`>>>` [blue]`_a=2` +
[red]`Parse Error: [1:2] unexpected token "_"` +
`>>>` [blue]`1=2` +
[red]`Parse Error: assign operator ("=") must be preceded by a variable`
== Other operations == Other operations
=== [blue]`;` operator === [blue]`;` operator
The semicolon operator [blue]`;` is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The latter is the final result. The semicolon operator [blue]`;` is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The value of the latter is the final result.
An expression that contains [blue]`;` is called a _multi-expression_ and each component expressione is called a _sub-expression_.
IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions. 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. TIP: [blue]`;` can be used to set some variables before the final calculation.
.Example .Example
[source,go] `>>>` [blue]`a=1; b=2; c=3; a+b+c` +
---- [green]`6`
a=1; b=2; c=3; a+b+c // returns 6
---- The value of each sub-expression is stored in the automatica variable _last_.
.Example
`>>>` [blue]`2+3; b=last+10; last` +
[green]`15`
=== [blue]`but` operator === [blue]`but` operator
[blue]`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: [blue]`5 but 2` returns 2, [blue]`x=2*3 but x-1` returns 5. [blue]`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: [blue]`5 but 2` returns 2, [blue]`x=2*3 but x-1` returns 5.

View File

@ -549,31 +549,31 @@ pre.rouge .ss {
</ul> </ul>
</li> </li>
<li><a href="#_strings">2.2. Strings</a></li> <li><a href="#_strings">2.2. Strings</a></li>
<li><a href="#_boolean">2.3. Boolean</a></li> <li><a href="#_booleans">2.3. Booleans</a></li>
<li><a href="#_lists">2.4. Lists</a></li> <li><a href="#_lists">2.4. Lists</a></li>
<li><a href="#_dictionaries">2.5. Dictionaries</a></li>
</ul> </ul>
</li> </li>
<li><a href="#_dictionaries">3. Dictionaries</a></li> <li><a href="#_variables">3. Variables</a></li>
<li><a href="#_variables">4. Variables</a></li> <li><a href="#_other_operations">4. Other operations</a>
<li><a href="#_other_operations">5. Other operations</a>
<ul class="sectlevel2"> <ul class="sectlevel2">
<li><a href="#_operator">5.1. <code class="blue">;</code> operator</a></li> <li><a href="#_operator">4.1. <code class="blue">;</code> operator</a></li>
<li><a href="#_but_operator">5.2. <code class="blue">but</code> operator</a></li> <li><a href="#_but_operator">4.2. <code class="blue">but</code> operator</a></li>
<li><a href="#_assignment_operator">5.3. Assignment operator <code class="blue">=</code></a></li> <li><a href="#_assignment_operator">4.3. Assignment operator <code class="blue">=</code></a></li>
<li><a href="#_selector_operator">5.4. Selector operator <code class="blue">? : ::</code></a></li> <li><a href="#_selector_operator">4.4. Selector operator <code class="blue">? : ::</code></a></li>
</ul> </ul>
</li> </li>
<li><a href="#_priorities_of_operators">6. Priorities of operators</a></li> <li><a href="#_priorities_of_operators">5. Priorities of operators</a></li>
<li><a href="#_functions">7. Functions</a> <li><a href="#_functions">6. Functions</a>
<ul class="sectlevel2"> <ul class="sectlevel2">
<li><a href="#_function_calls">7.1. Function calls</a></li> <li><a href="#_function_calls">6.1. Function calls</a></li>
<li><a href="#_function_definitions">7.2. Function definitions</a></li> <li><a href="#_function_definitions">6.2. Function definitions</a></li>
</ul> </ul>
</li> </li>
<li><a href="#_builtins">8. Builtins</a> <li><a href="#_builtins">7. Builtins</a>
<ul class="sectlevel2"> <ul class="sectlevel2">
<li><a href="#_builtin_functions">8.1. Builtin functions</a></li> <li><a href="#_builtin_functions">7.1. Builtin functions</a></li>
<li><a href="#_import">8.2. <em class="blue">import()</em></a></li> <li><a href="#_import">7.2. <em class="blue">import()</em></a></li>
</ul> </ul>
</li> </li>
</ul> </ul>
@ -584,7 +584,7 @@ pre.rouge .ss {
<div class="sectionbody"> <div class="sectionbody">
<!-- toc disabled --> <!-- toc disabled -->
<div class="paragraph"> <div class="paragraph">
<p><mark>TODO: Work in progress (last update on 2024/05/16, 7:08 a.m.)</mark></p> <p><mark>TODO: Work in progress (last update on 2024/05/17, 7:30 a.m.)</mark></p>
</div> </div>
</div> </div>
</div> </div>
@ -622,7 +622,7 @@ pre.rouge .ss {
<div class="listingblock"> <div class="listingblock">
<div class="title">Run <code>dev-expr</code> in REPL mode and ask for help</div> <div class="title">Run <code>dev-expr</code> in REPL mode and ask for help</div>
<div class="content"> <div class="content">
<pre class="rouge highlight"><code data-lang="shell"><span class="c"># Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program.</span> <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 <span class="o">[</span>user]<span class="nv">$ </span>./dev-expr
<span class="nb">expr</span> <span class="nt">--</span> Expressions calculator v1.7.1<span class="o">(</span>build 2<span class="o">)</span>,2024/05/16 <span class="o">(</span>celestino.amoroso@portale-stac.it<span class="o">)</span> <span class="nb">expr</span> <span class="nt">--</span> Expressions calculator v1.7.1<span class="o">(</span>build 2<span class="o">)</span>,2024/05/16 <span class="o">(</span>celestino.amoroso@portale-stac.it<span class="o">)</span>
@ -947,6 +947,7 @@ pre.rouge .ss {
<p>Fractions can be used together with integers and floats in expressions.</p> <p>Fractions can be used together with integers and floats in expressions.</p>
</div> </div>
<div class="paragraph"> <div class="paragraph">
<div class="title">Examples</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">1|2 + 5</code><br> <p><code>&gt;&gt;&gt;</code> <code class="blue">1|2 + 5</code><br>
<code class="green">11|2</code><br> <code class="green">11|2</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">4 - 1|2</code><br> <code>&gt;&gt;&gt;</code> <code class="blue">4 - 1|2</code><br>
@ -1034,7 +1035,7 @@ pre.rouge .ss {
</div> </div>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_boolean"><a class="anchor" href="#_boolean"></a><a class="link" href="#_boolean">2.3. Boolean</a></h3> <h3 id="_booleans"><a class="anchor" href="#_booleans"></a><a class="link" href="#_booleans">2.3. Booleans</a></h3>
<div class="paragraph"> <div class="paragraph">
<p>Boolean data type has two values only: <em class="blue">true</em> and <em class="blue">false</em>. Relational and boolean expressions result in boolean values.</p> <p>Boolean data type has two values only: <em class="blue">true</em> and <em class="blue">false</em>. Relational and boolean expressions result in boolean values.</p>
</div> </div>
@ -1171,18 +1172,31 @@ pre.rouge .ss {
<div class="sect2"> <div class="sect2">
<h3 id="_lists"><a class="anchor" href="#_lists"></a><a class="link" href="#_lists">2.4. Lists</a></h3> <h3 id="_lists"><a class="anchor" href="#_lists"></a><a class="link" href="#_lists">2.4. Lists</a></h3>
<div class="paragraph"> <div class="paragraph">
<p><em>Expr</em> supports list of mixed-type values, also specified by normal expressions.</p> <p><em>Expr</em> supports list of mixed-type values, also specified by normal expressions. Internally, <em>Expr</em>'s lists are Go arrays.</p>
</div> </div>
<div class="listingblock"> <div class="exampleblock">
<div class="title">List examples</div> <div class="title">Example 5. List literal syntax</div>
<div class="content"> <div class="content">
<pre class="rouge highlight"><code data-lang="go"><span class="p">[</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">]</span> <span class="c">// List of integers</span> <div class="paragraph">
<span class="p">[</span><span class="s">"one"</span><span class="p">,</span> <span class="s">"two"</span><span class="p">,</span> <span class="s">"three"</span><span class="p">]</span> <span class="c">// List of strings</span> <p><strong><em>list</em></strong> = <em>empty-list</em> | <em>non-empty-list</em><br>
<span class="p">[</span><span class="s">"one"</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="no">false</span><span class="p">,</span> <span class="m">4.1</span><span class="p">]</span> <span class="c">// List of mixed-types</span> <em>empty-list</em> = "<strong>[]</strong>"<br>
<span class="p">[</span><span class="s">"one"</span><span class="o">+</span><span class="m">1</span><span class="p">,</span> <span class="m">2.0</span><span class="o">*</span><span class="p">(</span><span class="m">9</span><span class="o">-</span><span class="m">2</span><span class="p">)]</span> <span class="c">// List of expressions</span> <em>non-empty-list</em> = "<strong>[</strong>" <em>any-value</em> {"<strong>,</strong>" _any-value} "<strong>]</strong>"<br></p>
<span class="p">[</span> <span class="p">[</span><span class="m">1</span><span class="p">,</span><span class="s">"one"</span><span class="p">],</span> <span class="p">[</span><span class="m">2</span><span class="p">,</span><span class="s">"two"</span><span class="p">]]</span> <span class="c">// List of lists</span></code></pre>
</div> </div>
</div> </div>
</div>
<div class="paragraph">
<div class="title">Examples</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">[1,2,3]</code> <em class="gray">// List of integers</em><br>
<code class="green">[1, 2, 3]</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">["one", "two", "three"]</code> <em class="gray">// List of strings</em><br>
<code class="green">["one", "two", "three"]</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">["one", 2, false, 4.1]</code> <em class="gray">// List of mixed-types</em><br>
<code class="green">["one", 2, false, 4.1]</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">["one"+1, 2.0*(9-2)]</code> <em class="gray">// List of expressions</em><br>
<code class="green">["one1", 14]</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">[ [1,"one"], [2,"two"]]</code> <em class="gray">// List of lists</em><br>
<code class="green">[[1, "one"], [2, "two"]]</code></p>
</div>
<table class="tableblock frame-all grid-all stretch"> <table class="tableblock frame-all grid-all stretch">
<caption class="title">Table 6. List operators</caption> <caption class="title">Table 6. List operators</caption>
<colgroup> <colgroup>
@ -1204,23 +1218,25 @@ pre.rouge .ss {
<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"><code class="blue">+</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Join</em></p></td> <td class="tableblock halign-center valign-top"><p class="tableblock"><em>Join</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Joins two lists</p></td> <td class="tableblock halign-left valign-top"><p class="tableblock">Joins two lists</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">[1,2] + [3]</code> <em>[ [1,2,3] ]</em></p></td> <td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">[1,2] + [3]</code> &#8594; <em>[1,2,3]</em></p></td>
</tr> </tr>
<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"><code class="blue">-</code></p></td>
<td class="tableblock halign-center valign-top"><p class="tableblock"><em>Difference</em></p></td> <td class="tableblock halign-center valign-top"><p class="tableblock"><em>Difference</em></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Left list without elements in the right list</p></td> <td class="tableblock halign-left valign-top"><p class="tableblock">Left list without elements in the right list</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">[1,2,3] - [2]</code> <em>[ [1,3] ]</em></p></td> <td class="tableblock halign-left valign-top"><p class="tableblock"><code class="blue">[1,2,3] - [2]</code> &#8594; <em>[1,3]</em></p></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
<div class="paragraph"> <div class="paragraph">
<p>The items of array can be accessed using the dot <code>.</code> operator.</p> <p>The items of array can be accessed using the dot <code>.</code> operator.</p>
</div> </div>
<div class="listingblock"> <div class="exampleblock">
<div class="title">Item access syntax</div> <div class="title">Example 6. Item access syntax</div>
<div class="content"> <div class="content">
<pre class="rouge highlight"><code data-lang="bnf">&lt;item&gt; ::= &lt;list-expr&gt;"."&lt;index-expr&gt;</code></pre> <div class="paragraph">
<p><em>item</em> = <em>list-expr</em> "<strong>.</strong>" <em>list-expr</em></p>
</div>
</div> </div>
</div> </div>
<div class="paragraph"> <div class="paragraph">
@ -1241,22 +1257,8 @@ pre.rouge .ss {
<code class="green">3</code></p> <code class="green">3</code></p>
</div> </div>
</div> </div>
</div> <div class="sect2">
</div> <h3 id="_dictionaries"><a class="anchor" href="#_dictionaries"></a><a class="link" href="#_dictionaries">2.5. Dictionaries</a></h3>
<div class="sect1">
<h2 id="_dictionaries"><a class="anchor" href="#_dictionaries"></a><a class="link" href="#_dictionaries">3. Dictionaries</a></h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <em>dictionary</em> data-type is set of pairs <em>key/value</em>. It is also known as <em>map</em> or <em>associative array</em>. Dictionary literals are sequences of pairs separated by comma <code>,</code>; sequences are enclosed between brace brackets.</p>
</div>
<div class="listingblock">
<div class="title">Dictionary examples</div>
<div class="content">
<pre class="rouge highlight"><code data-lang="go"><span class="p">{</span><span class="m">1</span><span class="o">:</span><span class="s">"one"</span><span class="p">,</span> <span class="m">2</span><span class="o">:</span><span class="s">"two"</span><span class="p">}</span>
<span class="p">{</span><span class="s">"one"</span><span class="o">:</span><span class="m">1</span><span class="p">,</span> <span class="s">"two"</span><span class="o">:</span> <span class="m">2</span><span class="p">}</span>
<span class="p">{</span><span class="s">"sum"</span><span class="o">:</span><span class="m">1</span><span class="o">+</span><span class="m">2</span><span class="o">+</span><span class="m">3</span><span class="p">,</span> <span class="s">"prod"</span><span class="o">:</span><span class="m">1</span><span class="o">*</span><span class="m">2</span><span class="o">*</span><span class="m">3</span><span class="p">}</span></code></pre>
</div>
</div>
<div class="admonitionblock warning"> <div class="admonitionblock warning">
<table> <table>
<tr> <tr>
@ -1269,31 +1271,85 @@ Support for dictionaries is still ongoing.
</tr> </tr>
</table> </table>
</div> </div>
<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>. Dictionary literals are sequences of pairs separated by comma <code>,</code>; sequences are enclosed between brace brackets.</p>
</div>
<div class="exampleblock">
<div class="title">Example 7. Dict literal syntax</div>
<div class="content">
<div class="paragraph">
<p><strong><em>dict</em></strong> = <em>empty-dict</em> | <em>non-empty-dict</em><br>
<em>empty-dict</em> = "<strong>{}</strong>"<br>
<em>non-empty-dict</em> = "<strong>{</strong>" <em>key-scalar</em> "<strong>:</strong>" <em>any-value</em> {"<strong>,</strong>" <em>key-scalar</em> "<strong>:</strong>" _any-value} "<strong>}</strong>"<br></p>
</div>
</div>
</div>
<div class="paragraph">
<div class="title">Examples</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">{1:"one", 2:"two"}</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">{"one":1, "two": 2}</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">{"sum":1+2+3, "prod":1*2*3}</code></p>
</div>
</div>
</div> </div>
</div> </div>
<div class="sect1"> <div class="sect1">
<h2 id="_variables"><a class="anchor" href="#_variables"></a><a class="link" href="#_variables">4. Variables</a></h2> <h2 id="_variables"><a class="anchor" href="#_variables"></a><a class="link" href="#_variables">3. Variables</a></h2>
<div class="sectionbody"> <div class="sectionbody">
<div class="paragraph"> <div class="paragraph">
<p>A variable is an identifier with an assigned value. Variables are stored in the object that implements the Go <em>ExprContext</em> interface, e.g. <em>SimpleVarStore</em> or <em>SimpleFuncStore</em>.</p> <p><em>Expr</em> supports variables like most programming languages. A variable is an identifier with an assigned value. Variables are stored in <em>contexts</em>.</p>
</div> </div>
<div class="listingblock"> <div class="exampleblock">
<div class="title">Examples</div> <div class="title">Example 8. Variable literal syntax</div>
<div class="content"> <div class="content">
<pre class="rouge highlight"><code data-lang="go"><span class="n">a</span><span class="o">=</span><span class="m">1</span> <div class="paragraph">
<span class="n">x</span> <span class="o">=</span> <span class="m">5.2</span> <span class="o">*</span> <span class="p">(</span><span class="m">9</span><span class="o">-</span><span class="m">3</span><span class="p">)</span> <p><strong><em>variable</em></strong> = <em>identifier</em> "<strong>=</strong>" <em>any-value</em><br>
<span class="n">x</span> <span class="o">=</span> <span class="m">1</span><span class="p">;</span> <span class="n">y</span> <span class="o">=</span> <span class="m">2</span><span class="o">*</span><span class="n">x</span></code></pre> <em>identifier</em> = <em>alpha</em> {(<em>alpha</em>)|<em>dec-digit</em>|"<strong>_</strong>"}<br>
<em>alpha</em> = "<strong>a</strong>"|"<strong>b</strong>"|&#8230;&#8203;"<strong>z</strong>"|"<strong>A</strong>"|"<strong>B</strong>"|&#8230;&#8203;"<strong>Z</strong>"</p>
</div> </div>
</div> </div>
</div> </div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The assign operator <code class="blue">=</code> returns the value assigned to the variable.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<div class="title">Examples</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">a=1</code><br>
<code class="green">1</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">a_b=1+2</code><br>
<code class="green">1+2</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">a_b</code><br>
<code class="green">3</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">x = 5.2 * (9-3)</code> <em class="gray">// The assigned value has the approximation error typical of the float data-type</em><br>
<code class="green">31.200000000000003</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">x = 1; y = 2*x</code><br>
<code class="green">2</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue"><em>a=2</code><br>
<code class="red">Parse Error: [1:2] unexpected token "</em>"</code><br>
<code>&gt;&gt;&gt;</code> <code class="blue">1=2</code><br>
<code class="red">Parse Error: assign operator ("=") must be preceded by a variable</code></p>
</div>
</div>
</div> </div>
<div class="sect1"> <div class="sect1">
<h2 id="_other_operations"><a class="anchor" href="#_other_operations"></a><a class="link" href="#_other_operations">5. Other operations</a></h2> <h2 id="_other_operations"><a class="anchor" href="#_other_operations"></a><a class="link" href="#_other_operations">4. Other operations</a></h2>
<div class="sectionbody"> <div class="sectionbody">
<div class="sect2"> <div class="sect2">
<h3 id="_operator"><a class="anchor" href="#_operator"></a><a class="link" href="#_operator">5.1. <code class="blue">;</code> operator</a></h3> <h3 id="_operator"><a class="anchor" href="#_operator"></a><a class="link" href="#_operator">4.1. <code class="blue">;</code> operator</a></h3>
<div class="paragraph"> <div class="paragraph">
<p>The semicolon operator <code class="blue">;</code> is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The latter is the final result.</p> <p>The semicolon operator <code class="blue">;</code> is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The value of the latter is the final result.</p>
</div>
<div class="paragraph">
<p>An expression that contains <code class="blue">;</code> is called a <em>multi-expression</em> and each component expressione is called a <em>sub-expression</em>.</p>
</div> </div>
<div class="admonitionblock important"> <div class="admonitionblock important">
<table> <table>
@ -1319,15 +1375,22 @@ Technically <code class="blue">;</code> is not treated as a real operator. It ac
</tr> </tr>
</table> </table>
</div> </div>
<div class="listingblock"> <div class="paragraph">
<div class="title">Example</div> <div class="title">Example</div>
<div class="content"> <p><code>&gt;&gt;&gt;</code> <code class="blue">a=1; b=2; c=3; a+b+c</code><br>
<pre class="rouge highlight"><code data-lang="go"><span class="n">a</span><span class="o">=</span><span class="m">1</span><span class="p">;</span> <span class="n">b</span><span class="o">=</span><span class="m">2</span><span class="p">;</span> <span class="n">c</span><span class="o">=</span><span class="m">3</span><span class="p">;</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">+</span><span class="n">c</span> <span class="c">// returns 6</span></code></pre> <code class="green">6</code></p>
</div> </div>
<div class="paragraph">
<p>The value of each sub-expression is stored in the automatica variable <em>last</em>.</p>
</div>
<div class="paragraph">
<div class="title">Example</div>
<p><code>&gt;&gt;&gt;</code> <code class="blue">2+3; b=last+10; last</code><br>
<code class="green">15</code></p>
</div> </div>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_but_operator"><a class="anchor" href="#_but_operator"></a><a class="link" href="#_but_operator">5.2. <code class="blue">but</code> operator</a></h3> <h3 id="_but_operator"><a class="anchor" href="#_but_operator"></a><a class="link" href="#_but_operator">4.2. <code class="blue">but</code> operator</a></h3>
<div class="paragraph"> <div class="paragraph">
<p><code class="blue">but</code> 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: <code class="blue">5 but 2</code> returns 2, <code class="blue">x=2*3 but x-1</code> returns 5.</p> <p><code class="blue">but</code> 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: <code class="blue">5 but 2</code> returns 2, <code class="blue">x=2*3 but x-1</code> returns 5.</p>
</div> </div>
@ -1336,7 +1399,7 @@ Technically <code class="blue">;</code> is not treated as a real operator. It ac
</div> </div>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_assignment_operator"><a class="anchor" href="#_assignment_operator"></a><a class="link" href="#_assignment_operator">5.3. Assignment operator <code class="blue">=</code></a></h3> <h3 id="_assignment_operator"><a class="anchor" href="#_assignment_operator"></a><a class="link" href="#_assignment_operator">4.3. Assignment operator <code class="blue">=</code></a></h3>
<div class="paragraph"> <div class="paragraph">
<p>The assignment operator <code class="blue">=</code> is used to define variables in the evaluation context or to change their value (see <em>ExprContext</em>). <p>The assignment operator <code class="blue">=</code> is used to define variables in the evaluation context or to change their value (see <em>ExprContext</em>).
The value on the left side of <code class="blue">=</code> must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation.</p> The value on the left side of <code class="blue">=</code> must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation.</p>
@ -1349,7 +1412,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
</div> </div>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_selector_operator"><a class="anchor" href="#_selector_operator"></a><a class="link" href="#_selector_operator">5.4. Selector operator <code class="blue">? : ::</code></a></h3> <h3 id="_selector_operator"><a class="anchor" href="#_selector_operator"></a><a class="link" href="#_selector_operator">4.4. Selector operator <code class="blue">? : ::</code></a></h3>
<div class="paragraph"> <div class="paragraph">
<p>The <em>selector operator</em> is very similar to the <em>switch/case/default</em> statement available in many programming languages.</p> <p>The <em>selector operator</em> is very similar to the <em>switch/case/default</em> statement available in many programming languages.</p>
</div> </div>
@ -1397,7 +1460,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
</div> </div>
</div> </div>
<div class="sect1"> <div class="sect1">
<h2 id="_priorities_of_operators"><a class="anchor" href="#_priorities_of_operators"></a><a class="link" href="#_priorities_of_operators">6. Priorities of operators</a></h2> <h2 id="_priorities_of_operators"><a class="anchor" href="#_priorities_of_operators"></a><a class="link" href="#_priorities_of_operators">5. Priorities of operators</a></h2>
<div class="sectionbody"> <div class="sectionbody">
<div class="paragraph"> <div class="paragraph">
<p>The table below shows all supported operators by decreasing priorities.</p> <p>The table below shows all supported operators by decreasing priorities.</p>
@ -1618,7 +1681,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
</div> </div>
</div> </div>
<div class="sect1"> <div class="sect1">
<h2 id="_functions"><a class="anchor" href="#_functions"></a><a class="link" href="#_functions">7. Functions</a></h2> <h2 id="_functions"><a class="anchor" href="#_functions"></a><a class="link" href="#_functions">6. Functions</a></h2>
<div class="sectionbody"> <div class="sectionbody">
<div class="paragraph"> <div class="paragraph">
<p>Functions in <em>Expr</em> are very similar to functions in many programming languages.</p> <p>Functions in <em>Expr</em> are very similar to functions in many programming languages.</p>
@ -1627,13 +1690,13 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
<p>In <em>Expr</em> 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 <code class="blue">@</code> it is possibile to export local definition to the calling context.</p> <p>In <em>Expr</em> 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 <code class="blue">@</code> it is possibile to export local definition to the calling context.</p>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_function_calls"><a class="anchor" href="#_function_calls"></a><a class="link" href="#_function_calls">7.1. Function calls</a></h3> <h3 id="_function_calls"><a class="anchor" href="#_function_calls"></a><a class="link" href="#_function_calls">6.1. Function calls</a></h3>
<div class="paragraph"> <div class="paragraph">
<p><mark>TODO: function calls operations</mark></p> <p><mark>TODO: function calls operations</mark></p>
</div> </div>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_function_definitions"><a class="anchor" href="#_function_definitions"></a><a class="link" href="#_function_definitions">7.2. Function definitions</a></h3> <h3 id="_function_definitions"><a class="anchor" href="#_function_definitions"></a><a class="link" href="#_function_definitions">6.2. Function definitions</a></h3>
<div class="paragraph"> <div class="paragraph">
<p><mark>TODO: function definitions operations</mark></p> <p><mark>TODO: function definitions operations</mark></p>
</div> </div>
@ -1641,17 +1704,17 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
</div> </div>
</div> </div>
<div class="sect1"> <div class="sect1">
<h2 id="_builtins"><a class="anchor" href="#_builtins"></a><a class="link" href="#_builtins">8. Builtins</a></h2> <h2 id="_builtins"><a class="anchor" href="#_builtins"></a><a class="link" href="#_builtins">7. Builtins</a></h2>
<div class="sectionbody"> <div class="sectionbody">
<div class="paragraph"> <div class="paragraph">
<p><mark>TODO: builtins</mark></p> <p><mark>TODO: builtins</mark></p>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_builtin_functions"><a class="anchor" href="#_builtin_functions"></a><a class="link" href="#_builtin_functions">8.1. Builtin functions</a></h3> <h3 id="_builtin_functions"><a class="anchor" href="#_builtin_functions"></a><a class="link" href="#_builtin_functions">7.1. Builtin functions</a></h3>
</div> </div>
<div class="sect2"> <div class="sect2">
<h3 id="_import"><a class="anchor" href="#_import"></a><a class="link" href="#_import">8.2. <em class="blue">import()</em></a></h3> <h3 id="_import"><a class="anchor" href="#_import"></a><a class="link" href="#_import">7.2. <em class="blue">import()</em></a></h3>
<div class="paragraph"> <div class="paragraph">
<p><em class="blue">import(<span class="grey">&lt;source-file&gt;</span>)</em> loads the multi-expression contained in the specified source and returns its value.</p> <p><em class="blue">import(<span class="grey">&lt;source-file&gt;</span>)</em> loads the multi-expression contained in the specified source and returns its value.</p>
</div> </div>
@ -1661,7 +1724,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
</div> </div>
<div id="footer"> <div id="footer">
<div id="footer-text"> <div id="footer-text">
Last updated 2024-05-16 07:10:03 +0200 Last updated 2024-05-17 07:30:14 +0200
</div> </div>
</div> </div>
</body> </body>