compile-time regex redux

Hasan Aljudy hasan.aljudy at gmail.com
Wed Feb 7 16:03:33 PST 2007



Walter Bright wrote:
> String mixins, in order to be useful, need an ability to manipulate 
> strings at compile time. Currently, the core operations on strings that 
> can be done are:
> 
> 1) indexed access
> 2) slicing
> 3) comparison
> 4) getting the length
> 5) concatenation
> 
> Any other functionality can be built up from these using template 
> metaprogramming.
> 
> The problem is that parsing strings using templates generates a large 
> number of template instantiations, is (relatively) very slow, and 
> consumes a lot of memory (at compile time, not runtime). For example, 
> ParseInteger would need 4 template instantiations to parse 5678, and 
> each template instantiation would also include the rest of the input as 
> part of the template instantiation's mangled name.
> 
> At some point, this will prove a barrier to large scale use of this 
> feature.
> 
> Andrei suggested using compile time regular expressions to shoulder much 
> of the burden, reducing parsing of any particular token to one 
> instantiation.
> 
> The last time I introduced core regular expressions into D, it was 
> soundly rejected by the community and was withdrawn, and for good reasons.
> 
> But I think we now have good reasons to revisit this, at least for 
> compile time use only. For example:
> 
>     ("aa|b" ~~ "ababb") would evaluate to "ab"
> 
> I expect one would generally only see this kind of thing inside 
> templates, not user code.

How about a much simpler and a more general approach: allow compile-time 
evaluation of functions.

a function will have a specific attribute such that if you pass it 
constant parameters, it gets evaluated/unfolded at compile time.

This attriute can be called "meta" for example .. (since it will be 
mainly used for meta programming)

/// Definition
meta int add( int x, int y )
{
     return x+y;
}

/// Usage:
int y = add( 10, 12 );

/// and that would get unfolded/interpreted at compile time to:

int y = 22;

Basically you execute the "meta" function at compile time, you feed it 
the input and get the output out of it and replace the function call 
with its compile time output.



More information about the Digitalmars-d mailing list