all repos — h3rald @ ebd415d3ecd14483862eb49dde0569e14ffef979

The sources of https://h3rald.com

contents/glyph/book/text_editing/evaluation.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
-----
title: "Glyph – Simple Programming and Code Evaluation"
content-type: page
-----
<nav class="navigation"><a href="/glyph/book/text_editing/conditionals.html">← Conditional Macros</a> | <a href="/glyph/book/index.html">Contents</a> | <a href="/glyph/book/compiling/compiling.html">Compiling a project →</a></nav>
  <section class="section">
<header><h1 id="h_44" class="toc">Turing-completeness</h1></header>
<p>As of version 0.5.0, Glyph can be considered <em>Turing-complete</em>, as it satisfies the following <a href="http://c2.com/cgi/wiki?LanguageRequirementsForTuringCompleteness">requirements for Turing-completeness</a>:</p>
<ul>
	<li>A conditional construct, implemented via the <a href="/glyph/book/macros/macros_core.html#m_condition"><code>condition</code></a> macro.</li>
	<li>Variable assignment, by setting the value of snippets using the <a href="/glyph/book/macros/macros_core.html#m_snippet_"><code>snippet:</code></a> macro and of attributes using the <a href="/glyph/book/macros/macros_core.html#m_attribute_"><code>attribute:</code></a> macro.</li>
	<li>(infinite) iteration implemented through the <a href="/glyph/book/macros/macros_core.html#m_while"><code>while</code></a> macro or recursion, which is possible thanks to the <a href="/glyph/book/macros/macros_core.html#m_define_"><code>define:</code></a> macro.</li>
	<li>A memory model which emulates an infinite store: there are no enforced limits on attribute/snippets allocations and number of algorithms or parameters.</li>
</ul>

</section>

  <section class="section">
<header><h1 id="h_45" class="toc">Operations on integer values</h1></header>
<p>Glyph can be used to perform operation on integer values (additions, subtractions and multiplications). For example, <code>add[2|3|7]</code> will evaluate to @12@, and <code>multiply[add[3|7]|subtract[5|1|2]]</code> will return 20.</p>

<p>As a more complex example, consider the following @factorial@ macro, which is able to calculate the factorial of a number recursively:</p>

  <div class="CodeRay">
  <div class="code"><pre><span class="line-numbers"><a href="#n1" name="n1">1</a></span>def:[factorial|
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>  ?[
<span class="line-numbers"><a href="#n3" name="n3">3</a></span>    eq[{{0}}|0]|1|
<span class="line-numbers"><a href="#n4" name="n4">4</a></span>    multiply[
<span class="line-numbers"><a href="#n5" name="n5">5</a></span>      {{0}} | factorial[subtract[{{0}}|1]]
<span class="line-numbers"><a href="#n6" name="n6">6</a></span>    ]
<span class="line-numbers"><a href="#n7" name="n7">7</a></span>  ]
<span class="line-numbers"><a href="#n8" name="n8">8</a></span>]</pre></div>
</div>


<p>If you try executing <code>factorial[5]</code>, it will evaluate to @120@.</p>

</section>
  <section class="section">
<header><h1 id="h_46" class="toc">Lexically-scoped attribute assignment</h1></header>
<p><a href="/glyph/book/text_editing/inclusions.html#snippets">Snippets</a> can be used in a similar way as <em>variables</em> are used in programming languages. Or better, they can be used as <em>global variables</em>, as they are visible from anywhere in the Glyph document. If you need something more restricted to, say, a section and all its subsections, you can define your own attributes and use them in a very similar way.</p>
<p>Consider the following Glyph code:</p>

</section>
  <div class="CodeRay">
  <div class="code"><pre><span class="line-numbers"><a href="#n1" name="n1">1</a></span>let[
<span class="line-numbers"><a href="#n2" name="n2">2</a></span>  @:[a|bits]
<span class="line-numbers"><a href="#n3" name="n3">3</a></span>  @:[b|bobs]
<span class="line-numbers"><a href="#n4" name="n4">4</a></span>  section[
<span class="line-numbers"><a href="#n5" name="n5">5</a></span>    @title[Something more about attributes]
<span class="line-numbers"><a href="#n6" name="n6">6</a></span>Attributes are like lexically scoped variables. You can use them to store @[a] and @[b].
<span class="line-numbers"><a href="#n7" name="n7">7</a></span>  ]
<span class="line-numbers"><a href="#n8" name="n8">8</a></span>]</pre></div>
</div>

  <p>The <a href="/glyph/book/macros/macros_core.html#m_let"><code>let</code></a> macro here only acts as a dummy macro (it does nothing really) to bind attributes using the <a href="/glyph/book/macros/macros_core.html#m_attribute_"><code>attribute:</code></a> macro (aliased by <code>@:</code>). Attributes can then be used anywhere within the <code>let</code> macro, so the content of the section reads: &#8220;Attributes are like lexically-scoped variables. You can use them to store bits and bobs&#8221;.</p>
<p>Note that attributes defined through the <a href="/glyph/book/macros/macros_core.html#m_attribute_"><code>attribute:</code></a> macro are&#8230; well, attributes! Feel free to use the <a href="/glyph/book/macros/macros_core.html#m_attribute"><code>attribute</code></a> macro to access standard attributes like <code>title</code>, etc.</p>
  <section class="section">
<header><h1 id="h_47" class="toc">Evaluating Ruby code</h1></header>
<p>For anything more complex than what described in the previous sections you can also evaluate simple ruby code snippets using the <code>ruby</code> macro (aliased to <code>%</code>), like this:</p>
<ul>
	<li><code>%[2 + 2]</code> &rarr; 4</li>
	<li><code>%[Time.now]</code> &rarr; 2014-10-04 21:34:10 +0200</li>
	<li><code>%[Glyph::VERSION]</code> &rarr; 0.5.3.1</li>
</ul>
<p>The scope for the code evaluation is the Kernel module, (with all inclusions required by Glyph itself).</p>
<p>Although it is possible to retrieve Glyph configuration settings in this way (e.g. <code>%[cfg('document.author')]</code>), the <a href="/glyph/book/macros/macros_core.html#m_config"><code>config</code></a> macro (aliased to <code>$</code>) makes things slightly simpler (e.g. <code>$[document.author]</code>).</p>

</section>
<nav class="navigation"><a href="/glyph/book/text_editing/conditionals.html">← Conditional Macros</a> | <a href="/glyph/book/index.html">Contents</a> | <a href="/glyph/book/compiling/compiling.html">Compiling a project →</a></nav>