It's a bit strange that some language cling to macros, while others
(C#, Java, etc) never seem to need them. To make a really good macro
system, I tried to write do the *purpose* of macros. What I came up
with:
- replacing common code
- inlining (instead of function). This one is actually not valid,
since a function call is superior if the compiler can inline it. - feature selection
the difference with a function is that a macro is able to 'access'
the 'calling' scope.
The Nim language is a bit in the same domain as C2. It has some pretty
nice ideas as well. For macros they stated (something like): "with macros
you can change the AST". Indeed the case if you think about it.
http://nim-lang.org/docs/tut2.html#macrosI agree with your design (1-5). My additions:
6. A macro can be public or not, just like other declarations.
7. There needs to be a distinction
(also described in the nim link): There are *expression* macros and
*statement/decl* macros. You cannot have a declaration if a place wher
an expression is expected. It might be possible to discover this, but
it might be needed for a developer to specify this as well..
8. The argument of a macro must be an identifier (only). This avoids
nasty issues like in C:
#define MAX(x, y) (x > y ? x : y)
int c = MAX(a++, b++)
as this expands to
int c = ((a++) > (b++) ? (a++) : (b++));