tools.ctfe for D1

FeepingCreature default_357-line at yahoo.de
Thu Apr 22 10:36:31 PDT 2010


On 21.04.2010 16:43, Ellery Newcomer wrote:
> On 04/21/2010 05:43 AM, FeepingCreature wrote:
>> On 20.04.2010 01:49, Ellery Newcomer wrote:
>>> Are there any good libraries for ctfe/code generation?
>>>
>>> I don't know, things like parsing support for compile time strings,
>>> string formatting, type<->  string
>>>
>>> My project seems to be growing ctfe, and it's all horribly hacky and
>>> ugly code.
>>
>> This might be useful if you're on D1:
>> http://dsource.org/projects/scrapple/browser/trunk/tools/tools/ctfe.d
>>
> 
> Looks like itś exactly what I want, thanks!
> 
> A couple notes:
> 
> Is there any particular license associated with it? (whatever tangoś is
> would be fine)
> 

Feel free to use it as you wish.

> When I try to compile, I get forward referencing errors and had to take
> out the import to tools.compat
> 

Yeah, nevermind that, it just defines string as char[] .. or used to, I think that's in tools.text nowadays. tools.ctfe is pretty independent from the rest of tools.

> Also, are there any examples for usage of the table parsing functions?

Here's one from my IF language, for converting dynamic into native types:

  union {
    bool b;
    int i;
    string s;
    float f;
    Scope sr;
  }
  T to(T)() {
    const string Table = `
                 | bool          | int         | string               | float   | Scope
      -----------+---------------+-------------+----------------------+---------+----------
      Boolean    | b             | b           | b?q{true}p:q{false}p | ø       | ø
      Integer    | i != 0        | i           | Format(i)            | i       | ø
      String     | s == q{true}p | atoi(s)     | s                    | atof(s) | ø
      Float      | ø             | cast(int) f | Format(f)            | f       | ø
      ScopeRef   | !!sr          | ø           | (sr?sr.fqn:q{(null:r)}p) | ø   | sr
      ScopeValue | sr.value().to!(T) | sr.value().to!(T) | sr.value().to!(T) | sr.value().to!(T) | sr`;
    mixin(ctTableUnrollColMajor(Table,
      /* First parameter is the outer template, $COL substituting with the column name, and $BODY substituting with the inner template*/
      `static if (is(T == $COL))
        switch (flatType) {
          $BODY
          default: throw new Exception(Format("Invalid type: ", flatType));
        }
      else `,
      /* Second parameter is the inner template, $ROW substituting with the row name and $CELL with the cell's content.
      `case FlatType.$ROW:
        static if (q{$CELL}p == "ø")
          throw new Exception(q{Cannot convert $ROW to $COL: }p~to!(string)~q{! }p);
        else return $CELL;
      `
    ).litstring_expand() /* expand q{}p into ""s with correct nesting */ ~ `static assert(false, "Unsupported type: "~T.stringof); `);
  }


> And to whom do I give attribution?

Attribution is not necessary. I'm glad someone finds it useful.


More information about the Digitalmars-d-learn mailing list