The Artima Developer Community
Sponsored Link

Heron-Centric: Ruminations of a Language Designer
Macros as S-Expression Transformations
by Christopher Diggins
October 6, 2005
Summary
I am exploring the possibility of defining macros as patterns and transformation applied to an s-expression representation of the syntax. Maybe this is the path to writing a fully extendible language?

Advertisement

I want Heron macros to be completely unrecognizable from other language constructs. For instance I want to be able to define a repeat (x) {...} construct within a library

So far the most powerful semantics I can come up with is to define macros as patterns which operate on a typed s-expression representation of the parse tree. Consider the following code:

  repeat (n) { writeln("hello"); }
The first-level s-expression representation would be:
  (repeat, (n), (writeln, ("hello"))));
The syntax for declaration of the repeat macro which I am considering would be:
  macro ("repeat", list n, code x) = {
    def f $x; // avoid potential name collision with i
    int i = $n;
    while (i > 0) {
      f();
      --i;
    }
  );
Now the syntax is a bit strange with "repeat" as part of the argument list. This is because the macro is defined as a pattern to be matched and a transformation to be applied to the s-expression when the pattern is found.

To understand why I did it that way consider the if/then/else macro, which I also want to define in terms of a primitive construct called cond.

  macro ("if", bool b, "then", object x, "else", object y) =
    (cond, b, x, true, y);
This leads naturally to the idea that maybe symbols could be treated as macros as well:
  macro (bool x, "&&", bool y) =
    (invoke, _amp_amp, x, (y));
Coming back to the repeat macro, I would also want to provide a specialization of the pattern so that it can be unrolled if the parameter is a small compile-time constant:
  macro ("repeat", 'int n, code x) =
    (cond,
      (n > 5), (repeat, $n, x), // too many iterations for unrolloing
      (n == 1), $x,
      (n == 0), {},
      true, { $x; (repeat, (n - 1), x; }
    );
If this idea has any merit, it may be conciveable to define very different syntactic structures for the same language. One possible application of this technology would be for defining domain specific languages.

Talk Back!

Have an opinion? Readers have already posted 4 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 © 2005 Christopher Diggins. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use