Lunar Programming Language

by David A. Moon
January 2017 - January 2018



Templates

In metaprogramming it is often necessary to construct a fragment of code. That can be done directly by calling constructors with the appropriate arguments, but it is often more concise and convenient to use a code template.

The ` prefix operator begins and ends a code template which evaluates to a sequence of tokens and source_locations. The sequence can be explicitly inserted into a token_stream's push back buffer or returned as the expansion of a macro. The expression parser will push any sequence returned by a macro into the push back buffer and then parse it.

Each token inside the template represents itself, except for special handling of \ and $. However, a name not preceded by \ has hygienic context added from the locally visible value of the name context.

\ denatures the following name. The \ itself does not appear in the result. Denaturing a name removes any hygienic context and also prevents the template from treating `, \, or $ differently from any other name.

$ indicates an interpolation directive:

$name inserts the value of name into the result. If the value is a single token, it is inserted. If the value is false, nothing is inserted. If the value is a sequence, each member of the sequence is processed in this same way. If the value is anything else, an error occurs.

$(expression) evaluates expression and inserts the result in that same way.

$[expression] is exactly the same as $(expression) but can be used to emphasize that if the result of expression is false nothing is inserted.

${items & more_items} is an iterating interpolation. Note that unlike in a pattern the closing } is not followed by * or +. If items contain at least one interpolation directive whose value is a sequence, this iterates as many times as the longest such sequence. On each iteration, it inserts items but for each interpolation directive whose value is a sequence, it uses the next member of the sequence, or inserts nothing if the sequence is exhausted. On each iteration but the last, if items inserts anything it inserts more_items after items, with the same treatment of any sequences being interpolated.

Otherwise this just inserts items in the ordinary way and ignores more_items.

The & more_items part can contain newlines, is optional, and can be omitted.

${ cannot be nested inside ${...}

Indentation in a multi-line code template is handled specially. The indentation of each line after the first is recorded in the token sequence relative to the column position where the first token in the template starts. When the token sequence produced by the template is parsed, each line's indentation equals the column position where the parsing started plus that relative indentation. This produces the intuitively expected results.

A template can end in a newline token, which precedes the closing ` punctuation. This allows code that follows a template to be nested inside code generated by the template. As a convenience, if there is a newline at the end of the template and its relative indentation would be negative, it is adjusted to 0. This allows the opening and closing ` to be vertically aligned.

See String Interpolation for a similar feature for strings.

Template Examples

examples TBD


Previous page   Table of Contents   Next page



Creative Commons License
Lunar by David A. Moon is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Please inform me if you find this useful, or use any of the ideas embedded in it.
Comments and criticisms to dave underscore moon atsign alum dot mit dot edu.