Compile time getTimes

Jonathan M Davis jmdavisProg at gmx.com
Fri Dec 27 17:35:18 PST 2013


On Saturday, December 28, 2013 01:08:37 Casper Færgemand" 
<shorttail at hotmail.com>@puremagic.com wrote:
> I'm writing a compiler that uses Pegged for parsing D code.
> Pegged is fed a string with a special grammar syntax and changes
> it into templated D code, which is in turn mixed in. Even with
> just a small subset of D, Pegged currently produces 4000 lines of
> dense code, and it consumes enough time for it to be bothersome
> on every change, especially changes to the compiler that don't
> touch the grammar.
> 
> I wrote some simple code that uses datetime's getTimes to compare
> modification time. If grammar.d was modified more recently than
> precompiled_grammar.d, grammar.d will be churned through the
> machine once more and the result overwrite the
> precompiled_grammar.d.
> 
> However, it works only on runtime. I moved stuff around and used
> an enum for the mixin, forcing compile time function execution,
> but it whines about getTimes being unusable: "Error:
> GetFileAttributesExW cannot be interpreted at compile time,
> because it has no available source code"
> 
> Is there some hack available? Currently I run the compiler twice
> to use the past precompiled stuff or run it once and accept that
> it's slow. And this is D, so I really feel I shouldn't have to. =3

CTFE requires that all of the source code be available (which is not the case 
with C functions) and isn't able to interact with the file system or any kind 
of I/O (beyond importing a file as a string). So, I believe that it's 
impossible to do something like check file times at compile time. You'll have 
to restrict yourself to stuff that you can do in code without any kind of I/O.

http://dlang.org/function.html#interpretation

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list