summaryrefslogtreecommitdiffstats
path: root/articles/xerl-0.3-atomic-expressions/index.html
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-06-13 09:54:12 +0200
committerLoïc Hoguin <[email protected]>2018-06-13 09:54:12 +0200
commit92b54aacc0de5446dd5497c39897b0bbff72e626 (patch)
treec3a98cfec636d1271f5804e5c19b35b208bba00d /articles/xerl-0.3-atomic-expressions/index.html
parent8b5c3dc972b99f174750123c9e4abc96259c34a9 (diff)
downloadninenines.eu-92b54aacc0de5446dd5497c39897b0bbff72e626.tar.gz
ninenines.eu-92b54aacc0de5446dd5497c39897b0bbff72e626.tar.bz2
ninenines.eu-92b54aacc0de5446dd5497c39897b0bbff72e626.zip
Rebuild using Asciideck
Diffstat (limited to 'articles/xerl-0.3-atomic-expressions/index.html')
-rw-r--r--articles/xerl-0.3-atomic-expressions/index.html171
1 files changed, 55 insertions, 116 deletions
diff --git a/articles/xerl-0.3-atomic-expressions/index.html b/articles/xerl-0.3-atomic-expressions/index.html
index 5ed954e2..861caedb 100644
--- a/articles/xerl-0.3-atomic-expressions/index.html
+++ b/articles/xerl-0.3-atomic-expressions/index.html
@@ -69,153 +69,92 @@
</p>
</header>
-<div class="paragraph"><p>We will be adding atomic integer expressions to our language.
-These look as follow in Erlang:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<p>We will be adding atomic integer expressions to our language. These look as follow in Erlang:</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #993399">42</span><span style="color: #990000">.</span></tt></pre></div></div>
-<div class="paragraph"><p>And the result of this expression is of course 42.</p></div>
-<div class="paragraph"><p>We will be running this expression at compile time, since we
-don&#8217;t have the means to run code at runtime yet. This will of
-course result in no module being compiled, but that&#8217;s OK, it will
-allow us to discuss a few important things we&#8217;ll have to plan for
-later on.</p></div>
-<div class="paragraph"><p>First, we must of course accept integers in the tokenizer.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt><font color="#993399">42</font><font color="#990000">.</font></tt></pre>
+</div></div>
+<p>And the result of this expression is of course 42.</p>
+<p>We will be running this expression at compile time, since we don&apos;t have the means to run code at runtime yet. This will of course result in no module being compiled, but that&apos;s OK, it will allow us to discuss a few important things we&apos;ll have to plan for later on.</p>
+<p>First, we must of course accept integers in the tokenizer.</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>{<span style="color: #009900">D</span>}<span style="color: #990000">+</span> <span style="color: #990000">:</span> {<span style="color: #FF6600">token</span>, {<span style="font-weight: bold"><span style="color: #000080">integer</span></span>, <span style="color: #009900">TokenLine</span>, <span style="font-weight: bold"><span style="color: #000080">list_to_integer</span></span>(<span style="color: #009900">TokenChars</span>)}}<span style="color: #990000">.</span></tt></pre></div></div>
-<div class="paragraph"><p>We must then accept atomic integer expressions in the parser.
-This is a simple change. The integer token is terminal so we need
-to add it to the list of terminals, and then we only need to add
-it as a possible expression.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt>{<font color="#009900">D</font>}<font color="#990000">+</font> <font color="#990000">:</font> {<font color="#FF6600">token</font>, {<b><font color="#000080">integer</font></b>, <font color="#009900">TokenLine</font>, <b><font color="#000080">list_to_integer</font></b>(<font color="#009900">TokenChars</font>)}}<font color="#990000">.</font></tt></pre>
+</div></div>
+<p>We must then accept atomic integer expressions in the parser. This is a simple change. The integer token is terminal so we need to add it to the list of terminals, and then we only need to add it as a possible expression.</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF6600">expr</span> <span style="color: #990000">-&gt;</span> <span style="color: #FF6600">integer</span> <span style="color: #990000">:</span> <span style="color: #FF6600">'$1'</span><span style="color: #990000">.</span></tt></pre></div></div>
-<div class="paragraph"><p>A file containing only the number 42 (with no terminating dot)
-will give the following result when parsing it. This is incidentally
-the same result as when tokenizing.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt><font color="#FF6600">expr</font> <font color="#990000">-&gt;</font> <font color="#FF6600">integer</font> <font color="#990000">:</font> <font color="#FF6600">'$1'</font><font color="#990000">.</font></tt></pre>
+</div></div>
+<p>A file containing only the number 42 (with no terminating dot) will give the following result when parsing it. This is incidentally the same result as when tokenizing.</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>[{<span style="font-weight: bold"><span style="color: #000080">integer</span></span>,<span style="color: #993399">1</span>,<span style="color: #993399">42</span>}]</tt></pre></div></div>
-<div class="paragraph"><p>We must then evaluate it. We&#8217;re going to interpret it for now.
-Since the result of this expression is not stored in a variable,
-we are going to simply print it on the screen and discard it.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt>[{<b><font color="#000080">integer</font></b>,<font color="#993399">1</font>,<font color="#993399">42</font>}]</tt></pre>
+</div></div>
+<p>We must then evaluate it. We&apos;re going to interpret it for now. Since the result of this expression is not stored in a variable, we are going to simply print it on the screen and discard it.</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000000">execute</span></span>(<span style="color: #009900">Filename</span>, [{<span style="font-weight: bold"><span style="color: #000080">integer</span></span>, <span style="color: #990000">_</span>, <span style="color: #009900">Int</span>}|<span style="color: #009900">Tail</span>], <span style="color: #009900">Modules</span>) <span style="color: #990000">-&gt;</span>
- <span style="font-weight: bold"><span style="color: #000000">io:format</span></span>(<span style="color: #FF0000">"integer ~p~n"</span>, [<span style="color: #009900">Int</span>]),
- <span style="font-weight: bold"><span style="color: #000000">execute</span></span>(<span style="color: #009900">Filename</span>, <span style="color: #009900">Tail</span>, <span style="color: #009900">Modules</span>)<span style="color: #990000">.</span></tt></pre></div></div>
-<div class="paragraph"><p>You might think by now that what we&#8217;ve done so far this time
-is useless. It brings up many interesting questions though.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-What happens if a file contains two integers?
-</p>
+<pre><tt><b><font color="#000000">execute</font></b>(<font color="#009900">Filename</font>, [{<b><font color="#000080">integer</font></b>, <font color="#990000">_</font>, <font color="#009900">Int</font>}|<font color="#009900">Tail</font>], <font color="#009900">Modules</font>) <font color="#990000">-&gt;</font>
+ <b><font color="#000000">io:format</font></b>(<font color="#FF0000">"integer ~p~n"</font>, [<font color="#009900">Int</font>]),
+ <b><font color="#000000">execute</font></b>(<font color="#009900">Filename</font>, <font color="#009900">Tail</font>, <font color="#009900">Modules</font>)<font color="#990000">.</font></tt></pre>
+</div></div>
+<p>You might think by now that what we&apos;ve done so far this time is useless. It brings up many interesting questions though.</p>
+<ul><li>What happens if a file contains two integers?
</li>
-<li>
-<p>
-Can we live without expression separators?
-</p>
+<li>Can we live without expression separators?
</li>
-<li>
-<p>
-Do we need an interpreter for the compile step?
-</p>
+<li>Do we need an interpreter for the compile step?
</li>
-</ul></div>
-<div class="paragraph"><p>This is what happens when we create a file that contains two
-integers on two separate lines:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+</ul>
+<p>This is what happens when we create a file that contains two integers on two separate lines:</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>[{<span style="font-weight: bold"><span style="color: #000080">integer</span></span>,<span style="color: #993399">1</span>,<span style="color: #993399">42</span>},{<span style="font-weight: bold"><span style="color: #000080">integer</span></span>,<span style="color: #993399">2</span>,<span style="color: #993399">43</span>}]</tt></pre></div></div>
-<div class="paragraph"><p>And on the same lines:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt>[{<b><font color="#000080">integer</font></b>,<font color="#993399">1</font>,<font color="#993399">42</font>},{<b><font color="#000080">integer</font></b>,<font color="#993399">2</font>,<font color="#993399">43</font>}]</tt></pre>
+</div></div>
+<p>And on the same lines:</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>[{<span style="font-weight: bold"><span style="color: #000080">integer</span></span>,<span style="color: #993399">1</span>,<span style="color: #993399">42</span>},{<span style="font-weight: bold"><span style="color: #000080">integer</span></span>,<span style="color: #993399">1</span>,<span style="color: #993399">43</span>}]</tt></pre></div></div>
-<div class="paragraph"><p>Does this mean we do not need separators between expressions?
-Not quite. The <code>+</code> and <code>-</code> operators are an
-example of why we can&#8217;t have nice things. They are ambiguous. They
-have two different meanings: make an atomic integer positive or
-negative, or perform an addition or a substraction between two
-integers. Without a separator you won&#8217;t be able to know if the
-following snippet is one or two expressions:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt>[{<b><font color="#000080">integer</font></b>,<font color="#993399">1</font>,<font color="#993399">42</font>},{<b><font color="#000080">integer</font></b>,<font color="#993399">1</font>,<font color="#993399">43</font>}]</tt></pre>
+</div></div>
+<p>Does this mean we do not need separators between expressions? Not quite. The <code>+</code> and <code>-</code> operators are an example of why we can&apos;t have nice things. They are ambiguous. They have two different meanings: make an atomic integer positive or negative, or perform an addition or a substraction between two integers. Without a separator you won&apos;t be able to know if the following snippet is one or two expressions:</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #993399">42</span> <span style="color: #990000">-</span> <span style="color: #993399">12</span></tt></pre></div></div>
-<div class="paragraph"><p>Can we use the line ending as an expression separator then?
-Some languages make whitespace important, often the line
-separator becomes the expression separator. I do not think this
-is the best idea, it can lead to errors. For example the following
-snippet would be two expressions:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight
+<pre><tt><font color="#993399">42</font> <font color="#990000">-</font> <font color="#993399">12</font></tt></pre>
+</div></div>
+<p>Can we use the line ending as an expression separator then? Some languages make whitespace important, often the line separator becomes the expression separator. I do not think this is the best idea, it can lead to errors. For example the following snippet would be two expressions:</p>
+<div class="listingblock"><div class="content"><!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">Var</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #000000">some_module:some_function</span></span>() <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">some_module:other_function</span></span>()
- <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">another_module:another_function</span></span>()</tt></pre></div></div>
-<div class="paragraph"><p>It is not obvious what would happen unless you are a veteran
-of the language, and so we will not go down that road. We will use
-an expression separator just like in Erlang: the comma. We will
-however allow a trailing comma to make copy pasting code easier,
-even if this means some old academics guy will go nuts about it
-later on. This trailing comma will be optional and simply discarded
-by the parser when encountered. We will implement this next.</p></div>
-<div class="paragraph"><p>The question as to how we will handle running expressions
-remains. We have two choices here: we can write an interpreter,
-or we can compile the code and run it. Writing an interpreter
-would require us to do twice the work, and we are lazy, so we will
-not do that.</p></div>
-<div class="paragraph"><p>You might already know that Erlang does not use the same code
-for compiling and for evaluating commands in the shell. The main
-reason for this is that in Erlang everything isn&#8217;t an expression.
-Indeed, the compiler compiles forms which contain expressions,
-but you can&#8217;t have forms in the shell.</p></div>
-<div class="paragraph"><p>How are we going to compile the code that isn&#8217;t part of a module
-then? What do we need to run at compile-time, anyway? The body of
-the file itself, of course. The body of module declarations. That&#8217;s
-about it.</p></div>
-<div class="paragraph"><p>For the file itself, we can simply compile it as a big function
-that will be executed. Then, everytime we encounter a module
-declaration, we will run the compiler on its body, making its body
-essentially a big function that will be executed. The same mechanism
-will be applied when we encounter a module declaration at runtime.</p></div>
-<div class="paragraph"><p>At runtime there&#8217;s nothing else for us to do, the result of this
-operation will load all the compiled modules. At compile time we
-will also want to save them to a file. We&#8217;ll see later how we can
-do that.</p></div>
-<div class="ulist"><ul>
-<li>
-<p>
-<a href="https://github.com/extend/xerl/blob/0.3/">View the source</a>
-</p>
+<pre><tt><font color="#009900">Var</font> <font color="#990000">=</font> <b><font color="#000000">some_module:some_function</font></b>() <font color="#990000">+</font> <b><font color="#000000">some_module:other_function</font></b>()
+ <font color="#990000">+</font> <b><font color="#000000">another_module:another_function</font></b>()</tt></pre>
+</div></div>
+<p>It is not obvious what would happen unless you are a veteran of the language, and so we will not go down that road. We will use an expression separator just like in Erlang: the comma. We will however allow a trailing comma to make copy pasting code easier, even if this means some old academics guy will go nuts about it later on. This trailing comma will be optional and simply discarded by the parser when encountered. We will implement this next.</p>
+<p>The question as to how we will handle running expressions remains. We have two choices here: we can write an interpreter, or we can compile the code and run it. Writing an interpreter would require us to do twice the work, and we are lazy, so we will not do that.</p>
+<p>You might already know that Erlang does not use the same code for compiling and for evaluating commands in the shell. The main reason for this is that in Erlang everything isn&apos;t an expression. Indeed, the compiler compiles forms which contain expressions, but you can&apos;t have forms in the shell.</p>
+<p>How are we going to compile the code that isn&apos;t part of a module then? What do we need to run at compile-time, anyway? The body of the file itself, of course. The body of module declarations. That&apos;s about it.</p>
+<p>For the file itself, we can simply compile it as a big function that will be executed. Then, everytime we encounter a module declaration, we will run the compiler on its body, making its body essentially a big function that will be executed. The same mechanism will be applied when we encounter a module declaration at runtime.</p>
+<p>At runtime there&apos;s nothing else for us to do, the result of this operation will load all the compiled modules. At compile time we will also want to save them to a file. We&apos;ll see later how we can do that.</p>
+<ul><li><a href="https://github.com/extend/xerl/blob/0.3/">View the source</a>
</li>
-</ul></div>
+</ul>
+
</article>
</div>