Heron-Centric: Ruminations of a Language Designer

Heron 1.0 Alpha 3 : Compile-Time Reflection, the HeronEdit IDE, and more

by Christopher Diggins

April 21, 2010



Summary

The Alpha 3 release of Heron 1.0 is now available for download from Google code. The completion of a couple of features, such as compile-time code reflection, and the inclusion of an editor, makes this one of the most interesting releases of Heron yet.


Heron 1.0 Alpha 3

The Alpha 3 release of Heron 1.0 is now available for download. There are a number of minor bug fixes, and a couple of significant new additions: compile-time reflection, a mini IDE, and enhanced duck-typing.

This version is still an Alpha since I am still on the fence whether a couple of features may make it into the official 1.0 gold version.

Compile-Time Reflection (Metaprogramming)

In previous versions of Heron you could only examine the code model of a Heron program at compile-time (i.e. compile-time introspection). In this version you have full read and write access to the code model at compile-time. So for example you could iterate over the functions of a class and insert trace statements where you want.

Compile-time reflection in Heron is enabled by providing a second program entry point called Meta() (in addition to the run-time entry-point Main() ). The Meta() function accepts a ProgramDefn object as an argument. A ProgramDefn type represents the abstract syntax tree (AST) of a Heron program and can be modified as much as the programmer desires. When a Heron program is finally run (or interpreted) the Main() function of the transformed AST is run instead of the original. This means that a programmer can do virtually anything they want to a program at compile-time. The only caveat is that it won't work if the original program wasn't a syntactically valid Heron program to begin with.

The following code example demonstrates how compile-time reflection works in Heron, by adding trace statements to all of the functions in the program:

module TestMetaProgramming { imports { console = new Heron.Windows.Console(); meta = new Heron.Standard.Metaprogramming(); } methods { Meta(p : ProgramDefn) { var m : ModuleDefn = p.GetModule("TestMetaProgramming"); foreach (f in m.GetDeclaredMethods()) { if (f.name != "Meta") { var st = CodeModelBuilder .CreateStatement("WriteLine(\"At function " + f.name + "()\");"); f.body.statements.Prepend(st); } } } Main() { f(); } f() { WriteLine("Nothing to see here, carry on"); } } }

Running this program produces the following output:

At function Main() At function f() Nothing to see here, carry on

Compile-time code generation can be very useful in a number of ways:

code optimization

static code analysis tools (e.g. type checkers)

automatic test generation

documentation generation

The HeronEdit mini-IDE

With the Alpha 3 release I have introduced a simple IDE for editing and running Heron programs called HeronEdit. HeronEdit is effectively with the following enhancements for working with Heron files:

Syntax coloring

On-the-fly parse error reporting

Multi-level undo

Single key-press for running a file

The editor can be programmatically extended via macros (scripts) written in Heron.

So while HeronEdit is written in C#, with full source code provided and licened under the MIT License 1.0, it does provide the interesting ability to be extended using scripts written in Heron.

A sample macro script for HeronEdit can be seen at Macros.heron.

Duck-Typing

Previous versions of Heron supported only explicit duck-typing via the as operator. Using as an object (class instance or interface instance) could be cast to any interface instance as long as the required interfaces were supported. In the current version of Heron this has been modified so that the as operator is no longer required. Now simply casting to an interface instance (i.e. via assignment or by passing a value as an argument to a function) will cause the object to be coerced into the new type.

Heron also allows type annotations to be omitted. A value with no type annotation will have its type checked at run-time, and thus provides another form of duck-typing.

Candidate Features for Heron 1.0 Gold

I plan on moving Heron into the beta phase once I have definitively decided whether or not to add support for a couple of features. These are:

program piping - One feature that is a must-have in Heron (whether it is released in 1.0 or I push it back to 1.1) is ability to redirect the standard output of a module to the standard input of another module from within Heron.

annotations - This is another must-have feature, and will probably make it into 1.0. Annotations are expressions that are associated with various program elements (e.g. types, methods, fields) that can be accessed at compile-time when examining the code model.

Talk Back!

Have an opinion? Readers have already posted 2 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Christopher Diggins adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Christopher Diggins is a software developer and freelance writer. Christopher loves programming, but is eternally frustrated by the shortcomings of modern programming languages. As would any reasonable person in his shoes, he decided to quit his day job to write his own ( www.heron-language.com ). Christopher is the co-author of the C++ Cookbook from O'Reilly. Christopher can be reached through his home page at www.cdiggins.com.

This weblog entry is Copyright © 2010 Christopher Diggins. All rights reserved.