As of version 0.5.0, Glyph can be considered Turing-complete, as it satisfies the following requirements for Turing-completeness:
- A conditional construct, implemented via the
- Variable assignment, by setting the value of snippets using the
snippet:macro and of attributes using the
- (infinite) iteration implemented through the
whilemacro or recursion, which is possible thanks to the
- A memory model which emulates an infinite store: there are no enforced limits on attribute/snippets allocations and number of algorithms or parameters.
Glyph can be used to perform operation on integer values (additions, subtractions and multiplications). For example,
add[2|3|7] will evaluate to @12@, and
multiply[add[3|7]|subtract[5|1|2]] will return 20.
As a more complex example, consider the following @factorial@ macro, which is able to calculate the factorial of a number recursively:
If you try executing
factorial, it will evaluate to @120@.
Snippets can be used in a similar way as variables are used in programming languages. Or better, they can be used as global variables, 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.
Consider the following Glyph code:
let macro here only acts as a dummy macro (it does nothing really) to bind attributes using the
attribute: macro (aliased by
@:). Attributes can then be used anywhere within the
let macro, so the content of the section reads: “Attributes are like lexically-scoped variables. You can use them to store bits and bobs”.
For anything more complex than what described in the previous sections you can also evaluate simple ruby code snippets using the
ruby macro (aliased to
%), like this:
%[2 + 2]→ 4
%[Time.now]→ 2014-10-04 21:34:10 +0200
The scope for the code evaluation is the Kernel module, (with all inclusions required by Glyph itself).
Although it is possible to retrieve Glyph configuration settings in this way (e.g.
config macro (aliased to
$) makes things slightly simpler (e.g.