[Mono-devel-list] Extension to mcs

Kamil Skalski nazgul at omega.pl
Fri Apr 23 10:29:16 EDT 2004

Friday 23 of April 2004 02:06, Rodrigo B. de Oliveira wrote:

> Very interesting! Amazing how a lot of different projects around the world
> have got to similar ideas on language/compiler extensibility, my

Recently I saw two Java compile-time systems for trasforming programs: 
OpenJava and Java Syntax Extensions (JSE). First one introduces features 
similar to those you are mentioning - enriching object environment and 
changing classes by means of special objects operating of AST (their 
definitions are also given by classes using API to ASTs). JSE is somewhat 
closer to Nemerle, because it uses special quotation syntax and gives 
possibility to do any transformation of any expressions in program. 

> The getter attribute would look something like the following:
> <snip>
> import Boo.Lang.Compiler
> import Boo.Lang.Compiler.Ast
> class GetterAttribute(AbstractAstAttribute):
>     _name as ReferenceExpression
>     def constructor([required] name as ReferenceExpression):
>          _name = name
>     override def Apply([required] node as Node):
>          field as Field = node
>          property = Property(_name.Name)
>          property.Getter = Method("get")
>          property.Getter.Body.Add(
>                   ReturnStatement(
>                            ReferenceExpression(field.Name)))
>          field.DeclaringType.Members.Add(property)
> </snip>
> My hope is that it should be somewhat easy to see what's going on:
>     * ReferenceExpression, Node, Property, Method and ReturnStatement are
> all AST node classes;
>     * the attribute's constructor defines exactly how the attribute should
> be used syntax-wise, in this
> case, the attribute must be used with a reference, [getter("Disposed")]
> would be signaled as an error
> by the compiler with an apropriate message;
>     * the Apply method receives the node to which the attribute was
> attached to and can do anything with it; yes,
> that's kind of dangerous but I'd rather put the programmers in charge than
> trying to protect them from themselves;

Our macros are also compiled as special classes operating on AST, but we give 
them some additional syntax sugar. 

macro getter (c : Class, refer) {
   c.DefineMember (<[ method: public get () { $refer } ]>)

So it takes class it operates on, adds a method, which returns given 

> I think the main thing is to have a clear and easy API for AST
> manipulation, this should give
> programmers a enough power.

IMHO if you want to use macros for some non-trivial tasks, you should consider 
adding quotation system to your language. For example

<[ foo (4 + x) ]>

is equivalent to something like this (Nemerle ASTs)

E_call (E_ref ("foo"), E_call (E_ref ("+"), E_literal (4), E_ref ("x")))

It's obvius, that no matter what API for AST you can create, it won't be so 
elegant and readable as quotations. You can also inline some values to 
def x = <[ foo (4) ]>;
<[ bar (76) + $x ]>   == <[ bar (76) + foo (4) ]>
(and x can be just any external variable containing some expression)

> I'd love to hear your thoughts.
> BTW, there's one more extensibility mechanism in boo: custom compilation
> pipelines but I'll leave
> that for the language docs ;-)

It would be nice to continue our discussion and exchange ideas on more related 
mailing list. For example at Nemerle development list

Do you have some web page of your language?

Kamil Skalski
Nemerle developer

More information about the Mono-devel-list mailing list