Will macros just be syntactic sugar?
Davidl
Davidl at 126.com
Wed Apr 25 02:26:27 PDT 2007
macro macroname(Token... tokens)
{
static if (tokens[0].ID != Token.LParen)
pragma(error, `( expected`);
else static if (tokens[1].ID != Token.Identifier)
pragma(error, `identifer expected`);
else static if (tokens[2].ID != Token.RParen)
pragma(error, ) expected`);
else static if (tokens[3].toChars != `:=`)
pragma(error, `:= expected`);
static if (tokens[4].ID != Token.LBracket)
pragma(error, `( expected`);
else static if (tokens[5].ID != Token.Identifier)
pragma(error, `identifer expected`);
else static if (tokens[6].ID != Token.RBracket)
pragma(error, ) expected`);
else static if (tokens[7].ID != Token.Identifier)
pragma(error, `identifer expected`);
else static if (tokens[8].ID != Token.plus)
pragma(error, `+ operator expected`);
else static if (tokens[9].ID != Token.Identifier)
pragma(error, `identifer expected`);
alias token[1] arg0;
alias token[5] arg1;
alias token[7] arg2;
alias token[9] arg3;
arg0[arg1] = arg2 + arg3;
}
caller could call the macro with :
macroname(arg0)[arg1] := arg2 + arg3;
macroname here is used for simplify the parsing.
> macro (Token... tokens)
> {
> static if (tokens[0].ID != Token.LParen)
> pragma(error, `( expected`);
> else static if (tokens[1].ID != Token.Identifier)
> pragma(error, `identifer expected`);
> else static if (tokens[2].ID != Token.RParen)
> pragma(error, ) expected`);
> else static if (tokens[3].toChars != `:=`)
> pragma(error, `:= expected`);
> static if (tokens[4].ID != Token.LBracket)
> pragma(error, `( expected`);
> else static if (tokens[5].ID != Token.Identifier)
> pragma(error, `identifer expected`);
> else static if (tokens[6].ID != Token.RBracket)
> pragma(error, ) expected`);
> else static if (tokens[7].ID != Token.Identifier)
> pragma(error, `identifer expected`);
> else static if (tokens[8].ID != Token.plus)
> pragma(error, `+ operator expected`);
> else static if (tokens[9].ID != Token.Identifier)
> pragma(error, `identifer expected`);
> alias token[1] arg0;
> alias token[5] arg1;
> alias token[7] arg2;
> alias token[9] arg3;
> arg0[arg1] = arg2 + arg3;
> }
> seems the above is more valuable.
More information about the Digitalmars-d
mailing list