On 25/09/2009 18:32, Robert Jacques wrote:
On Fri, 25 Sep 2009 03:29:21 -0400, Don <nos...@nospam.com> wrote:
[snip]
A bit of history: A macro system was planned for D2, and 'macro' was
made a reserved word. But in discussions at the end of the first D
conference, I demonstrated that the combination of CTFE + string
mixins, even in D1, was dramatically more powerful than the proposed
macros. It became clear that we didn't have a macro design that was
anywhere near powerful enough, and macros were postponed to D3. We
don't know how they should work.
By all means make a proposal.

About the only thing I find lacking today is the ability to do zero
weight expression templates. I think fleshing out the alias keyword
might do the trick (i.e. alias tuples + alias function parameters) but I
haven't though it all through.

My preferred design, which I can't imagine implemented in the near future, would be something in these lines:
(disclaimer: heavily influenced by Nemerle)

1. macro definitions are separate from regular code. Macros are compiled into libs that are loaded by the compiler:
something like:
dmd --load-macro=my_macro.dll mysource.d
(can be implemented as DDLs for portability)

2. macros contain regular D code and can use facilities from the stdlib to manipulate their input (examples will follow)

3. macros can either be called from regular code with function call syntax or by using an attribute system to specify hook points.
e.g.
int result = max_macro(50, 90); //  transformed to: "int result = 90;"

[macro(params)] // some imaginary attribute/annotations syntax
class Class { .... }

examples of macro definitions:
a) CTFE replacement:
macro max(int a, int b) {
  return (a > b) ? a : b;
}

b) operate on the current AST node:
/// stdlib provided utilities to work with the AST
import Compiler.AST;
import Compiler.parser;

macro (AST input) {
// input is set by compiler to the current AST sub-tree
  return some_ast_transform(input);
  // can call regular functions to implement macros
}

c) compile-time vs. run-time :

macro () {
   Stdout("Compile time").newline; //printed at compile-time
   return AST("Stdout(\"Run time\").newline;"); //printed at run-time
}

The Compiler package above provides facilities for the programmer to directly manipulate the AST in a standard way with API calls instead of string manipulations.

Reply via email to