Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Christopher Diggins

Dr. Dobb's Bloggers

Compile-Time Reflection: Metaprogramming in Heron

December 11, 2009

I have just released version 0.8 of Heron . This is a pretty significant version of Heron for me, because it implements a feature that I have wanted for a long-time: the ability to execute arbitrary code at compile-time and to modify the abstract syntax tree.

Version 0.8 of Heron has a number of small improvements and bug fixes, but the coolest new feature is the ability to execute arbitrary code at compile-time and to traverse the code object model. The code object model is a typed representation of the abstract syntax tree (AST). The Heron code object model is outlined inthis file.

In Heron any code in the program can be executed at compile-time or at run-time. There is no special syntax needed to identify code executable at compile-time.

This is made possible in Heron because the compiler has a built in interpreter which executes immediately after the input code is parsed into an abstract syntax tree. The only difference between compile-time and run-time execution is the entry point: where program execution starts.

In Heron the run-time entry point to a program (other than scripts) is by convention the constructor of a class named "Main". Program termination occurs once this is class is finished construction. At compile-time the entry point is a different class, this one named "Meta". (note: these conventions will probably change before the official version 1.0, because I should probably use static functions like C# and Java). The other difference is that the "Meta" class constructor accepts an argument of type "Program" which represents the root object in the code object hierarchy.

Another way to think of the Heron metaprogramming system is that it is a reflection system, but available only at compile-time. Unlike the reflection APIs available for C# (and I think Java) you can examine and modify code right down to the level of statements and expressions.

While the current version of Heron the metaprogramming is mostly limited to introspection (other functionality simply hasn't yet been tested, but may work), it already has a wide variety of potential uses. The first program I wrote to test it is a pretty printer, which outputs its own source code as text by walking the object model. You can see the source code here.

To test this program you canrun the interpreter with the test file from the command-line:

HeronEngine.exe tests/prettyprinter.heron

However, I see no reason not to write more interesting programs like source to source translators (e.g. from Heron to C), or even native-code compilers. I am especially interested in writing a Heron to JavaScript translator.

I may even write a static type checker using the metaprogramming system itself. I think though that once I have a Heron interpreter written in Heron itself is when things are really going to get interesting.

I'm not an expert in meta-programming or macro systems in other languages, so I'd be really interested to hear how this compares with other languages.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.