AST macros

eao197 eao197 at intervale.ru
Sun Mar 18 06:48:29 PDT 2007


On Sun, 18 Mar 2007 05:00:34 +0300, Walter Bright  
<newshound at digitalmars.com> wrote:

> Right now, I see AST macros as manipulating expressions and statements,  
> not declarations.

If I have understood you correctly the D macros will be used like  
class_eval/module_eval in Ruby -- for generation new content of the  
programm. For example, in Ruby I can write:

class ProjectDescription
   # Methods which accept Array as argument.
   def add_files( files ); ...; end
   def add_resources( resouces ); ...; end

   # Helper class instance method for defining singular argument methods
   # on top on plural form method.
   def self.define_singular_form_method( method )
     class_eval %Q{
       # Construct 'method[0...-1]' removes ending 's' from method name.
       def #{method[0...-1]}(a); #{method}( [ a ] ); end
     }
   end
   ...
end

The same trick will be available in D via macro:

macro defineSingularFormMethod(methodName, paramType) {
   void methodName[0..$-1]( paramType a ) { methodName( [ a ] ); }
}

class ProjectDescription {
   // Methods which accept Array as argument.
   void addFiles( char[][] files ) { ... }
   void addResources( char[][] resources ) { ... }

   // Addition of singular form methods via macros.
   defineSingularFormMethod( addFiles, char[] );
   defineSingularFormMethod( addResources, char[] );
}

The main difference between Ruby's class_eval and D's macro is in the time  
of execution: in Ruby class_eval is executed in run-time, and in D macro  
is executed in compile-time (at syntax analyzing stage).

Am I right?

-- 
Regards,
Yauheni Akhotnikau



More information about the Digitalmars-d mailing list