DIP 50 - AST macros

Rikki Cattermole alphaglosined at gmail.com
Mon Nov 11 02:11:42 PST 2013


On Monday, 11 November 2013 at 10:04:31 UTC, Jacob Carlborg wrote:
> On 2013-11-11 10:38, Rikki Cattermole wrote:
>
>> An example of this might be:
>>
>> macro foo (Context context, Ast!(string) str)
>> if (lexer.isSymbol(0, SymbolTypes.Class))
>> {
>>     return str;
>> }
>>
>> class Bar {
>>  int i;
>> }
>>
>> foo {
>>  Bar(7);
>> }
>> The above code would succeed however the below code will give 
>> a compiler
>> error stating that the given macro is not found.
>>
>> struct Haz {
>>  int i;
>> }
>>
>> foo {
>>  Haz(9);
>> }
>>
>> The above code is based on the assumption of a D lexer in 
>> phobos.
>> This is simpler set of code which won't require a lexer.
>>
>> macro foo (Context context, Ast!(string) str)
>> if (str == "working")
>> {
>>     return "";
>> }
>>
>> foo {fails}
>> foo {working}
>> In these cases its a simple string test to determine if the 
>> text given
>> is specified value.
>>
>> In the original example I gave, I was using regex to make the 
>> point of
>> validation for a given line passed to the macro.
>
> My idea of handling errors in macros is more something like 
> triggering a real compile error. That could be done via the 
> context parameter, something like:
>
> macro foo (Context context, Ast!(string) str)
> {
>     if (str.eval() == "bar")
>         context.error("Illegal value 'bar'"); // this will 
> trigger the compile error
> }

For errors I was thinking a new pragma.
pragma(error, "You did x wrong");
This would fire an error and display the text given. Returning 
error code as expected. It is after all in the same league as msg.
It would also remove our current static asserts in our code.
So other words it could be replicated by:

pragma(msg, "You did x wrong");
static assert(0);

Just without the whole failed assert message.

Although what I was showing was the idea of overloading of macros 
like we have in templates.


More information about the Digitalmars-d mailing list