Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4aa0113c6a | |||
| d035fa0d5e | |||
| cf73b5c98d | |||
| 8346e28340 | |||
| 9c2eca40d7 | |||
| c3198e4c79 | |||
| 99e0190b9c | |||
| d4f63a3837 | |||
| 389d48b646 | |||
| 1f7b9131fc | |||
| dce49fd2b7 |
@@ -128,6 +128,7 @@ func (self *ast) eval(ctx ExprContext, preset bool) (result any, err error) {
|
||||
}
|
||||
if err == nil {
|
||||
result, err = self.root.compute(ctx)
|
||||
ctx.setVar(ControlLastResult, result)
|
||||
}
|
||||
// } else {
|
||||
// err = errors.New("empty expression")
|
||||
|
||||
+89
-11
@@ -22,17 +22,23 @@ 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/10, 06:52 a.m.)#
|
||||
|
||||
== Expr
|
||||
_Expr_ is a GO package capable of analysing, interpreting and calculating expressions.
|
||||
|
||||
=== Concepts and terminology
|
||||
#TODO#
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
The program can be downloaded from https://git.portale-stac.it/go-pkg/-/packages/generic/dev-expr/[dev-expr].
|
||||
|
||||
The program in located in the _tools_ directory.
|
||||
Here are some examples of execution.
|
||||
|
||||
.Run `dev-expr` in REPL mode and ask for help
|
||||
@@ -103,11 +109,6 @@ Here are some examples of execution.
|
||||
<4> But operator, see <<_but_operator>>.
|
||||
<5> Multi-expression: the same result of the previous single expression but this it is obtained with two separated calculations.
|
||||
|
||||
=== Concepts and terminology
|
||||
#TODO#
|
||||
|
||||
image::expression-diagram.png[]
|
||||
|
||||
== Data types
|
||||
_Expr_ supports numerical, string, relational, boolean expressions, and mixed-type lists.
|
||||
|
||||
@@ -140,7 +141,44 @@ Numbers can be integers (GO int64) or float (GO float64). In mixed operations in
|
||||
|
||||
|===
|
||||
|
||||
=== String
|
||||
=== Fractions
|
||||
_Expr_ also supports fractions. Fraction literals are made with two 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 their 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.
|
||||
@@ -208,7 +246,7 @@ Currently, boolean operations are evaluated using _short cut evaluation_. This m
|
||||
<1> This multi-expression returns _1_ because in the first expression the left value of [blue]`or` is _true_ and as a conseguence its right value is not computed. Therefore the _a_ variable only receives the integer _1_.
|
||||
====
|
||||
|
||||
=== List
|
||||
=== Lists
|
||||
_Expr_ supports list of mixed-type values, also specified by normal expressions.
|
||||
|
||||
.List examples
|
||||
@@ -221,7 +259,6 @@ _Expr_ supports list of mixed-type values, also specified by normal expressions.
|
||||
[ [1,"one"], [2,"two"]] // List of lists
|
||||
----
|
||||
|
||||
|
||||
.List operators
|
||||
[cols="^2,^2,5,4"]
|
||||
|===
|
||||
@@ -232,6 +269,47 @@ _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] ]_
|
||||
|===
|
||||
|
||||
The items of array can be accessed using the dot `.` operator.
|
||||
|
||||
.Item access syntax
|
||||
[source,bnf]
|
||||
----
|
||||
<item> ::= <list-expr>"."<index-expr>
|
||||
----
|
||||
|
||||
.Items of list
|
||||
[source,go]
|
||||
----
|
||||
`>>>` [blue]`[1,2,3].1`
|
||||
[green]`2`
|
||||
`>>>` [blue]`list=[1,2,3]; list.1`
|
||||
[green]`2`
|
||||
`>>>` [blue]`["one","two","three"].1`
|
||||
[green]`two`
|
||||
`>>>` [blue]`list=["one","two","three"]; list.(2-1)`
|
||||
[green]`two`
|
||||
`>>>` [blue]`list.(-1)`
|
||||
[green]`three`
|
||||
`>>>` [blue]`list.(-1)`
|
||||
[green]`three`
|
||||
`>>>` [blue]`list.(10)`
|
||||
----
|
||||
|
||||
|
||||
|
||||
== 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.
|
||||
|
||||
|
||||
+148
-51
@@ -535,38 +535,40 @@ pre.rouge .ss {
|
||||
<ul class="sectlevel1">
|
||||
<li><a href="#_expr">1. Expr</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#_dev_expr_test_tool">1.1. <code>dev-expr</code> test tool</a></li>
|
||||
<li><a href="#_concepts_and_terminology">1.2. Concepts and terminology</a></li>
|
||||
<li><a href="#_concepts_and_terminology">1.1. Concepts and terminology</a></li>
|
||||
<li><a href="#_dev_expr_test_tool">1.2. <code>dev-expr</code> test tool</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_data_types">2. Data types</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#_numbers">2.1. Numbers</a></li>
|
||||
<li><a href="#_string">2.2. String</a></li>
|
||||
<li><a href="#_boolean">2.3. Boolean</a></li>
|
||||
<li><a href="#_list">2.4. List</a></li>
|
||||
<li><a href="#_fractions">2.2. Fractions</a></li>
|
||||
<li><a href="#_strings">2.3. Strings</a></li>
|
||||
<li><a href="#_boolean">2.4. Boolean</a></li>
|
||||
<li><a href="#_lists">2.5. Lists</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_variables">3. Variables</a></li>
|
||||
<li><a href="#_other_operations">4. Other operations</a>
|
||||
<li><a href="#_dictionaries">3. Dictionaries</a></li>
|
||||
<li><a href="#_variables">4. Variables</a></li>
|
||||
<li><a href="#_other_operations">5. Other operations</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#_operator">4.1. <code class="blue">;</code> operator</a></li>
|
||||
<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="#_operator">5.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="#_assignment_operator">5.3. Assignment operator <code class="blue">=</code></a></li>
|
||||
<li><a href="#_selector_operator">5.4. Selector operator <code class="blue">? : ::</code></a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_priorities_of_operators">5. Priorities of operators</a></li>
|
||||
<li><a href="#_functions">6. Functions</a>
|
||||
<li><a href="#_priorities_of_operators">6. Priorities of operators</a></li>
|
||||
<li><a href="#_functions">7. Functions</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#_function_calls">6.1. Function calls</a></li>
|
||||
<li><a href="#_function_definitions">6.2. Function definitions</a></li>
|
||||
<li><a href="#_function_calls">7.1. Function calls</a></li>
|
||||
<li><a href="#_function_definitions">7.2. Function definitions</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_builtins">7. Builtins</a>
|
||||
<li><a href="#_builtins">8. Builtins</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#_builtin_functions">7.1. Builtin functions</a></li>
|
||||
<li><a href="#_import">7.2. <em class="blue">import()</em></a></li>
|
||||
<li><a href="#_builtin_functions">8.1. Builtin functions</a></li>
|
||||
<li><a href="#_import">8.2. <em class="blue">import()</em></a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -577,7 +579,7 @@ pre.rouge .ss {
|
||||
<div class="sectionbody">
|
||||
<!-- toc disabled -->
|
||||
<div class="paragraph">
|
||||
<p><mark>TODO: Work in progress (last update on 2024/05/07, 07:15 am)</mark></p>
|
||||
<p><mark>TODO: Work in progress (last update on 2024/05/10, 06:52 a.m.)</mark></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -588,16 +590,29 @@ pre.rouge .ss {
|
||||
<p><em>Expr</em> is a GO package capable of analysing, interpreting and calculating expressions.</p>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_dev_expr_test_tool"><a class="anchor" href="#_dev_expr_test_tool"></a><a class="link" href="#_dev_expr_test_tool">1.1. <code>dev-expr</code> test tool</a></h3>
|
||||
<h3 id="_concepts_and_terminology"><a class="anchor" href="#_concepts_and_terminology"></a><a class="link" href="#_concepts_and_terminology">1.1. Concepts and terminology</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><mark>TODO</mark></p>
|
||||
</div>
|
||||
<div class="imageblock">
|
||||
<div class="content">
|
||||
<img src="expression-diagram.png" alt="expression diagram">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_dev_expr_test_tool"><a class="anchor" href="#_dev_expr_test_tool"></a><a class="link" href="#_dev_expr_test_tool">1.2. <code>dev-expr</code> test tool</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><code>dev-expr</code> 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, <code>dev-expr</code> provides an important aid for quickly testing of new features during their development.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>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.</p>
|
||||
<p>It cat work as a <em>REPL</em>, <em><strong>R</strong>ead-<strong>E</strong>xecute-<strong>P</strong>rint-<strong>L</strong>oop</em>, or it can process expression acquired from files or standard input.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>The program in located in the <em>tools</em> directory.
|
||||
Here are some examples of execution.</p>
|
||||
<p>The program can be downloaded from <a href="https://git.portale-stac.it/go-pkg/-/packages/generic/dev-expr/">dev-expr</a>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Here are some examples of execution.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="title">Run <code>dev-expr</code> in REPL mode and ask for help</div>
|
||||
@@ -695,17 +710,6 @@ Here are some examples of execution.</p>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_concepts_and_terminology"><a class="anchor" href="#_concepts_and_terminology"></a><a class="link" href="#_concepts_and_terminology">1.2. Concepts and terminology</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><mark>TODO</mark></p>
|
||||
</div>
|
||||
<div class="imageblock">
|
||||
<div class="content">
|
||||
<img src="expression-diagram.png" alt="expression diagram">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
@@ -787,7 +791,45 @@ Here are some examples of execution.</p>
|
||||
</table>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_string"><a class="anchor" href="#_string"></a><a class="link" href="#_string">2.2. String</a></h3>
|
||||
<h3 id="_fractions"><a class="anchor" href="#_fractions"></a><a class="link" href="#_fractions">2.2. Fractions</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><em>Expr</em> also supports fractions. Fraction literals are made with two integers separated by a vertical bar <code>|</code>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<div class="title">Examples</div>
|
||||
<p><code>>>></code> <code class="blue">1 | 2</code><br>
|
||||
<code class="green">1|2</code><br>
|
||||
<code>>>></code> <code class="blue">4|6</code><br>
|
||||
<code class="green">2|3</code> <em class="gray">Fractions are always reduced to their lowest terms</em><br>
|
||||
<code>>>></code> <code class="blue">1|2 + 2|3</code><br>
|
||||
<code class="green">7|6</code><br>
|
||||
<code>>>></code> <code class="blue">1|2 * 2|3</code><br>
|
||||
<code class="green">1|3</code><br>
|
||||
<code>>>></code> <code class="blue">1|2 / 1|3</code><br>
|
||||
<code class="green">3|2</code><br>
|
||||
<code>>>></code> <code class="blue">1|2 ./ 1|3</code> <em class="gray">Force decimal division</em><br>
|
||||
<code class="green">1.5</code><br>
|
||||
<code>>>></code> <code class="blue">-1|2</code><br>
|
||||
<code class="green">-1|2</code><br>
|
||||
<code>>>></code> <code class="blue">1|-2</code> <em class="gray">Wrong sign specification</em><br>
|
||||
<em class="red">Eval Error: [1:3] infix operator "|" requires two non-nil operands, got 1</em><br>
|
||||
<code>>>></code> <code class="blue">1|(-2)</code><br>
|
||||
<code class="green">-1|2</code></p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Fractions can be used together with integers and floats in expressions.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p><code>>>></code> <code class="blue">1|2 + 5</code><br>
|
||||
<code class="green">11|2</code><br>
|
||||
<code>>>></code> <code class="blue">4 - 1|2</code><br>
|
||||
<code class="green">7|2</code><br>
|
||||
<code>>>></code> <code class="blue">1.0 + 1|2</code><br>
|
||||
<code class="green">1.5</code><br></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_strings"><a class="anchor" href="#_strings"></a><a class="link" href="#_strings">2.3. Strings</a></h3>
|
||||
<div class="paragraph">
|
||||
<p>Strings are character sequences enclosed between two double quote <code class="blue">"</code>. Example: <code class="blue">"I’m a string"</code>.</p>
|
||||
</div>
|
||||
@@ -828,7 +870,7 @@ Here are some examples of execution.</p>
|
||||
</table>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_boolean"><a class="anchor" href="#_boolean"></a><a class="link" href="#_boolean">2.3. Boolean</a></h3>
|
||||
<h3 id="_boolean"><a class="anchor" href="#_boolean"></a><a class="link" href="#_boolean">2.4. Boolean</a></h3>
|
||||
<div class="paragraph">
|
||||
<p>Boolean data type has two values only: <em>true</em> and <em>false</em>. Relational and Boolean expressions produce Boolean values.</p>
|
||||
</div>
|
||||
@@ -963,7 +1005,7 @@ Here are some examples of execution.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_list"><a class="anchor" href="#_list"></a><a class="link" href="#_list">2.4. List</a></h3>
|
||||
<h3 id="_lists"><a class="anchor" href="#_lists"></a><a class="link" href="#_lists">2.5. Lists</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><em>Expr</em> supports list of mixed-type values, also specified by normal expressions.</p>
|
||||
</div>
|
||||
@@ -1008,11 +1050,66 @@ Here are some examples of execution.</p>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="paragraph">
|
||||
<p>The items of array can be accessed using the dot <code>.</code> operator.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="title">Item access syntax</div>
|
||||
<div class="content">
|
||||
<pre class="rouge highlight"><code data-lang="bnf"><item> ::= <list-expr>"."<index-expr></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="title">Items of list</div>
|
||||
<div class="content">
|
||||
<pre class="rouge highlight"><code data-lang="go"><span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`[1,2,3].1`</span>
|
||||
<span class="p">[</span><span class="n">green</span><span class="p">]</span><span class="s">`2`</span>
|
||||
<span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`list=[1,2,3]; list.1`</span>
|
||||
<span class="p">[</span><span class="n">green</span><span class="p">]</span><span class="s">`2`</span>
|
||||
<span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`["one","two","three"].1`</span>
|
||||
<span class="p">[</span><span class="n">green</span><span class="p">]</span><span class="s">`two`</span>
|
||||
<span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`list=["one","two","three"]; list.(2-1)`</span>
|
||||
<span class="p">[</span><span class="n">green</span><span class="p">]</span><span class="s">`two`</span>
|
||||
<span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`list.(-1)`</span>
|
||||
<span class="p">[</span><span class="n">green</span><span class="p">]</span><span class="s">`three`</span>
|
||||
<span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`list.(-1)`</span>
|
||||
<span class="p">[</span><span class="n">green</span><span class="p">]</span><span class="s">`three`</span>
|
||||
<span class="s">`>>>`</span> <span class="p">[</span><span class="n">blue</span><span class="p">]</span><span class="s">`list.(10)`</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<h2 id="_variables"><a class="anchor" href="#_variables"></a><a class="link" href="#_variables">3. Variables</a></h2>
|
||||
<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">List 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">
|
||||
<table>
|
||||
<tr>
|
||||
<td class="icon">
|
||||
<i class="fa icon-warning" title="Warning"></i>
|
||||
</td>
|
||||
<td class="content">
|
||||
Support for dictionaries is still ongoing.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<h2 id="_variables"><a class="anchor" href="#_variables"></a><a class="link" href="#_variables">4. Variables</a></h2>
|
||||
<div class="sectionbody">
|
||||
<div class="paragraph">
|
||||
<p>A variable is an identifier with an assigned value. Variables are stored in the object that implements the <em>ExprContext</em> interface.</p>
|
||||
@@ -1028,10 +1125,10 @@ Here are some examples of execution.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<h2 id="_other_operations"><a class="anchor" href="#_other_operations"></a><a class="link" href="#_other_operations">4. Other operations</a></h2>
|
||||
<h2 id="_other_operations"><a class="anchor" href="#_other_operations"></a><a class="link" href="#_other_operations">5. Other operations</a></h2>
|
||||
<div class="sectionbody">
|
||||
<div class="sect2">
|
||||
<h3 id="_operator"><a class="anchor" href="#_operator"></a><a class="link" href="#_operator">4.1. <code class="blue">;</code> operator</a></h3>
|
||||
<h3 id="_operator"><a class="anchor" href="#_operator"></a><a class="link" href="#_operator">5.1. <code class="blue">;</code> operator</a></h3>
|
||||
<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>
|
||||
</div>
|
||||
@@ -1067,7 +1164,7 @@ Technically <code class="blue">;</code> is not treated as a real operator. It ac
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
</div>
|
||||
@@ -1076,7 +1173,7 @@ Technically <code class="blue">;</code> is not treated as a real operator. It ac
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<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>
|
||||
<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>
|
||||
<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>).
|
||||
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>
|
||||
@@ -1089,7 +1186,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<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>
|
||||
<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>
|
||||
<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>
|
||||
</div>
|
||||
@@ -1129,7 +1226,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<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>
|
||||
<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>
|
||||
<div class="sectionbody">
|
||||
<div class="paragraph">
|
||||
<p>The table below shows all supported operators by decreasing priorities.</p>
|
||||
@@ -1350,7 +1447,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<h2 id="_functions"><a class="anchor" href="#_functions"></a><a class="link" href="#_functions">6. Functions</a></h2>
|
||||
<h2 id="_functions"><a class="anchor" href="#_functions"></a><a class="link" href="#_functions">7. Functions</a></h2>
|
||||
<div class="sectionbody">
|
||||
<div class="paragraph">
|
||||
<p>Functions in <em>Expr</em> are very similar to functions in many programming languages.</p>
|
||||
@@ -1359,13 +1456,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>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_function_calls"><a class="anchor" href="#_function_calls"></a><a class="link" href="#_function_calls">6.1. Function calls</a></h3>
|
||||
<h3 id="_function_calls"><a class="anchor" href="#_function_calls"></a><a class="link" href="#_function_calls">7.1. Function calls</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><mark>TODO: function calls operations</mark></p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_function_definitions"><a class="anchor" href="#_function_definitions"></a><a class="link" href="#_function_definitions">6.2. Function definitions</a></h3>
|
||||
<h3 id="_function_definitions"><a class="anchor" href="#_function_definitions"></a><a class="link" href="#_function_definitions">7.2. Function definitions</a></h3>
|
||||
<div class="paragraph">
|
||||
<p><mark>TODO: function definitions operations</mark></p>
|
||||
</div>
|
||||
@@ -1373,17 +1470,17 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<h2 id="_builtins"><a class="anchor" href="#_builtins"></a><a class="link" href="#_builtins">7. Builtins</a></h2>
|
||||
<h2 id="_builtins"><a class="anchor" href="#_builtins"></a><a class="link" href="#_builtins">8. Builtins</a></h2>
|
||||
<div class="sectionbody">
|
||||
<div class="paragraph">
|
||||
<p><mark>TODO: builtins</mark></p>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_builtin_functions"><a class="anchor" href="#_builtin_functions"></a><a class="link" href="#_builtin_functions">7.1. Builtin functions</a></h3>
|
||||
<h3 id="_builtin_functions"><a class="anchor" href="#_builtin_functions"></a><a class="link" href="#_builtin_functions">8.1. Builtin functions</a></h3>
|
||||
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_import"><a class="anchor" href="#_import"></a><a class="link" href="#_import">7.2. <em class="blue">import()</em></a></h3>
|
||||
<h3 id="_import"><a class="anchor" href="#_import"></a><a class="link" href="#_import">8.2. <em class="blue">import()</em></a></h3>
|
||||
<div class="paragraph">
|
||||
<p><em class="blue">import(<span class="grey"><source-file></span>)</em> loads the multi-expression contained in the specified source and returns its value.</p>
|
||||
</div>
|
||||
@@ -1393,7 +1490,7 @@ The value on the left side of <code class="blue">=</code> must be an identifier.
|
||||
</div>
|
||||
<div id="footer">
|
||||
<div id="footer-text">
|
||||
Last updated 2024-05-08 07:50:05 +0200
|
||||
Last updated 2024-05-10 06:38:03 +0200
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
+47
-2
@@ -18,6 +18,42 @@ func isNilFunc(ctx ExprContext, name string, args []any) (result any, err error)
|
||||
return
|
||||
}
|
||||
|
||||
func isIntFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsInteger(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
func isFloatFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsFloat(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
func isBoolFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsBool(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
func isStringFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsString(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
func isFractionFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsFract(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
func isListFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsList(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
func isDictionaryFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
result = IsDict(args[0])
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
func intFunc(ctx ExprContext, name string, args []any) (result any, err error) {
|
||||
if len(args) == 1 {
|
||||
switch v := args[0].(type) {
|
||||
@@ -50,8 +86,17 @@ func iteratorFunc(ctx ExprContext, name string, args []any) (result any, err err
|
||||
}
|
||||
|
||||
func ImportBuiltinsFuncs(ctx ExprContext) {
|
||||
ctx.RegisterFunc("isNil", &simpleFunctor{f: isNilFunc}, 1, -1)
|
||||
ctx.RegisterFunc("int", &simpleFunctor{f: intFunc}, 1, -1)
|
||||
ctx.RegisterFunc("isNil", &simpleFunctor{f: isNilFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isInt", &simpleFunctor{f: isIntFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isFloat", &simpleFunctor{f: isFloatFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isBool", &simpleFunctor{f: isBoolFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isString", &simpleFunctor{f: isStringFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isFraction", &simpleFunctor{f: isFractionFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isFract", &simpleFunctor{f: isFractionFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isList", &simpleFunctor{f: isListFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isDictionary", &simpleFunctor{f: isDictionaryFunc}, 1, 1)
|
||||
ctx.RegisterFunc("isDict", &simpleFunctor{f: isDictionaryFunc}, 1, 1)
|
||||
ctx.RegisterFunc("int", &simpleFunctor{f: intFunc}, 1, 1)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
+10
-4
@@ -20,7 +20,7 @@ func TestFuncs(t *testing.T) {
|
||||
/* 7 */ {`int(3.9)`, int64(3), nil},
|
||||
/* 8 */ {`int("432")`, int64(432), nil},
|
||||
/* 9 */ {`int("1.5")`, nil, errors.New(`strconv.Atoi: parsing "1.5": invalid syntax`)},
|
||||
/* 10 */ {`int("432", 4)`, nil, errors.New(`int() requires exactly one param`)},
|
||||
/* 10 */ {`int("432", 4)`, nil, errors.New(`too much params -- expected 1, got 2`)},
|
||||
/* 11 */ {`int(nil)`, nil, errors.New(`int() can't convert <nil> to int`)},
|
||||
/* 12 */ {`two=func(){2}; two()`, int64(2), nil},
|
||||
/* 13 */ {`double=func(x) {2*x}; (double(3))`, int64(6), nil},
|
||||
@@ -54,9 +54,15 @@ func TestFuncs(t *testing.T) {
|
||||
/* 41 */ {`builtin "string"; endsWithStr("0123456789", "xyz", "789")`, true, nil},
|
||||
/* 42 */ {`builtin "string"; endsWithStr("0123456789", "xyz", "0125")`, false, nil},
|
||||
/* 43 */ {`builtin "string"; endsWithStr("0123456789")`, nil, errors.New(`too few params -- expected 2 or more, got 1`)},
|
||||
/* 44 */ {`builtin "string"; splitStr("one-two-three", "-", )`, newListA("one","two","three"), nil},
|
||||
/* 45 */ {`["a", "b", "c"]`, newListA("a","b","c"), nil},
|
||||
/* 46 */ {`["a", "b", "c"]`, newList([]any{"a","b","c"}), nil},
|
||||
/* 44 */ {`builtin "string"; splitStr("one-two-three", "-", )`, newListA("one", "two", "three"), nil},
|
||||
/* 45 */ {`isInt(2+1)`, true, nil},
|
||||
/* 46 */ {`isInt(3.1)`, false, nil},
|
||||
/* 46 */ {`isFloat(3.1)`, true, nil},
|
||||
/* 47 */ {`isString("3.1")`, true, nil},
|
||||
/* 48 */ {`isString("3" + 1)`, true, nil},
|
||||
/* 49 */ {`isList(["3", 1])`, true, nil},
|
||||
/* 50 */ {`isDict({"a":"3", "b":1})`, true, nil},
|
||||
/* 51 */ {`isFract(3|1)`, true, nil},
|
||||
}
|
||||
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
+19
-16
@@ -20,22 +20,25 @@ func TestListParser(t *testing.T) {
|
||||
}
|
||||
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`[]`, []any{}, nil},
|
||||
/* 2 */ {`[1,2,3]`, []any{int64(1), int64(2), int64(3)}, nil},
|
||||
/* 3 */ {`[1,2,"hello"]`, []any{int64(1), int64(2), "hello"}, nil},
|
||||
/* 4 */ {`[1+2, not true, "hello"]`, []any{int64(3), false, "hello"}, nil},
|
||||
/* 5 */ {`[1,2]+[3]`, []any{int64(1), int64(2), int64(3)}, nil},
|
||||
/* 6 */ {`[1,4,3,2]-[3]`, []any{int64(1), int64(4), int64(2)}, nil},
|
||||
/* 7 */ {`add([1,4,3,2])`, int64(10), nil},
|
||||
/* 8 */ {`add([1,[2,2],3,2])`, int64(10), nil},
|
||||
/* 9 */ {`mul([1,4,3.0,2])`, float64(24.0), nil},
|
||||
/* 10 */ {`add([1,"hello"])`, nil, errors.New(`add(): param nr 2 (2 in 1) has wrong type string, number expected`)},
|
||||
/* 11 */ {`[a=1,b=2,c=3] but a+b+c`, int64(6), nil},
|
||||
/* 12 */ {`[1,2,3] << 2+2`, []any{int64(1), int64(2), int64(3), int64(4)}, nil},
|
||||
/* 13 */ {`2-1 >> [2,3]`, []any{int64(1), int64(2), int64(3)}, nil},
|
||||
/* 14 */ {`[1,2,3].1`, int64(2), nil},
|
||||
/* 15 */ {`ls=[1,2,3] but ls.1`, int64(2), nil},
|
||||
/* 16 */ {`ls=[1,2,3] but ls.(-1)`, int64(3), nil},
|
||||
/* 1 */ {`[]`, []any{}, nil},
|
||||
/* 2 */ {`[1,2,3]`, []any{int64(1), int64(2), int64(3)}, nil},
|
||||
/* 3 */ {`[1,2,"hello"]`, []any{int64(1), int64(2), "hello"}, nil},
|
||||
/* 4 */ {`[1+2, not true, "hello"]`, []any{int64(3), false, "hello"}, nil},
|
||||
/* 5 */ {`[1,2]+[3]`, []any{int64(1), int64(2), int64(3)}, nil},
|
||||
/* 6 */ {`[1,4,3,2]-[3]`, []any{int64(1), int64(4), int64(2)}, nil},
|
||||
/* 7 */ {`add([1,4,3,2])`, int64(10), nil},
|
||||
/* 8 */ {`add([1,[2,2],3,2])`, int64(10), nil},
|
||||
/* 9 */ {`mul([1,4,3.0,2])`, float64(24.0), nil},
|
||||
/* 10 */ {`add([1,"hello"])`, nil, errors.New(`add(): param nr 2 (2 in 1) has wrong type string, number expected`)},
|
||||
/* 11 */ {`[a=1,b=2,c=3] but a+b+c`, int64(6), nil},
|
||||
/* 12 */ {`[1,2,3] << 2+2`, []any{int64(1), int64(2), int64(3), int64(4)}, nil},
|
||||
/* 13 */ {`2-1 >> [2,3]`, []any{int64(1), int64(2), int64(3)}, nil},
|
||||
/* 14 */ {`[1,2,3].1`, int64(2), nil},
|
||||
/* 15 */ {`ls=[1,2,3] but ls.1`, int64(2), nil},
|
||||
/* 16 */ {`ls=[1,2,3] but ls.(-1)`, int64(3), nil},
|
||||
/* 17 */ {`list=["one","two","three"]; list.10`, nil, errors.New(`[1:36] index 10 out of bounds`)},
|
||||
/* 18 */ {`["a", "b", "c"]`, newListA("a", "b", "c"), nil},
|
||||
/* 19 */ {`["a", "b", "c"]`, newList([]any{"a", "b", "c"}), nil},
|
||||
|
||||
// /* 8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil},
|
||||
// /* 9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil},
|
||||
|
||||
+4
-3
@@ -22,10 +22,11 @@ func verifyIndex(ctx ExprContext, indexTerm *term, maxValue int) (index int, err
|
||||
var indexValue any
|
||||
if indexValue, err = indexTerm.compute(ctx); err == nil {
|
||||
if v, err = indexTerm.toInt(indexValue, "index expression value must be integer"); err == nil {
|
||||
if v < 0 && v >= -maxValue {
|
||||
v = maxValue + v
|
||||
}
|
||||
if v >= 0 && v < maxValue {
|
||||
index = v
|
||||
} else if index >= -maxValue {
|
||||
index = maxValue + v
|
||||
} else {
|
||||
err = indexTerm.Errorf("index %d out of bounds", v)
|
||||
}
|
||||
@@ -56,7 +57,7 @@ func evalDot(ctx ExprContext, self *term) (v any, err error) {
|
||||
case string:
|
||||
var index int
|
||||
if index, err = verifyIndex(ctx, indexTerm, len(unboxedValue)); err == nil {
|
||||
v = unboxedValue[index]
|
||||
v = string(unboxedValue[index])
|
||||
}
|
||||
case map[any]any:
|
||||
var ok bool
|
||||
|
||||
+5
-5
@@ -35,10 +35,10 @@ func TestGeneralParser(t *testing.T) {
|
||||
/* 14 */ {`not true`, false, nil},
|
||||
/* 15 */ {`true and false`, false, nil},
|
||||
/* 16 */ {`true or false`, true, nil},
|
||||
/* 17 */ {`"uno" + "due"`, `unodue`, nil},
|
||||
/* 18 */ {`"uno" + 2`, `uno2`, nil},
|
||||
/* 19 */ {`"uno" + (2+1)`, `uno3`, nil},
|
||||
/* 20 */ {`"uno" * (2+1)`, `unounouno`, nil},
|
||||
/* *17 */ {`"uno" + "due"`, `unodue`, nil},
|
||||
/* *18 */ {`"uno" + 2`, `uno2`, nil},
|
||||
/* *19 */ {`"uno" + (2+1)`, `uno3`, nil},
|
||||
/* *20 */ {`"uno" * (2+1)`, `unounouno`, nil},
|
||||
/* 21 */ {"1", int64(1), nil},
|
||||
/* 22 */ {"1.5", float64(1.5), nil},
|
||||
/* 23 */ {"1.5*2", float64(3.0), nil},
|
||||
@@ -177,7 +177,7 @@ func parserTest(t *testing.T, section string, inputs []inputType) {
|
||||
ctx := NewSimpleFuncStore()
|
||||
parser := NewParser(ctx)
|
||||
|
||||
logTest(t, i+1, "Iterator", input.source, input.wantResult, input.wantErr)
|
||||
logTest(t, i+1, section, input.source, input.wantResult, input.wantErr)
|
||||
|
||||
r := strings.NewReader(input.source)
|
||||
scanner := NewScanner(r, DefaultTranslations())
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// strings_test.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStringsParser(t *testing.T) {
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`"uno" + "due"`, `unodue`, nil},
|
||||
/* 2 */ {`"uno" + 2`, `uno2`, nil},
|
||||
/* 3 */ {`"uno" + (2+1)`, `uno3`, nil},
|
||||
/* 4 */ {`"uno" * (2+1)`, `unounouno`, nil},
|
||||
/* 5 */ {`"abc".1`, `b`, nil},
|
||||
/* 5 */ {`#"abc"`, int64(3), nil},
|
||||
}
|
||||
parserTest(t, "String", inputs)
|
||||
}
|
||||
@@ -24,6 +24,11 @@ func IsFloat(v any) (ok bool) {
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsBool(v any) (ok bool) {
|
||||
_, ok = v.(bool)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsList(v any) (ok bool) {
|
||||
_, ok = v.(*ListType)
|
||||
return ok
|
||||
@@ -34,6 +39,11 @@ func IsDict(v any) (ok bool) {
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsFract(v any) (ok bool) {
|
||||
_, ok = v.(*fraction)
|
||||
return ok
|
||||
}
|
||||
|
||||
func IsNumber(v any) (ok bool) {
|
||||
return IsFloat(v) || IsInteger(v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user