Hi all,

Im porting some C++ code, which has a mess of a section that implements prime number type id's. I've had to smother it to death with test cases to get it reliable, I think metaprogramming that D provides is the better solution - Id rather not reimplement that C++ mess ideally.

A simplified example,

enum token_type {
  endOfFile = 2,

  unknown = 3,
  newline = 5,
  identifier = 7,
    userDefined = 13 * identifier,
    // Keyword
    var = 17 * identifier,
    uses = 19 * identifier,
    constructor = 23 * identifier,
    do_ = 29 * identifier,
    end_ = 31 * identifier,

  operator = 11,
        copyAssignment = 13 * operator

  // LAST ID = 13
}

Its effectly a tree, with the starting child node being one more than the last child number of the parent.

Is it possible to produce this via metaprogramming? I'm thinking mixin? Do I need to make a function that returns D code that will produce the enum structure above? I'm unsure as to how I would be able to get the LAST ID of the siblings at each level, and I would describe this with some kind of tree/function call?

e.g. Pseudo-code

enum token_type = prime_ids(
  value("endOfFile"),
  value("unknown"),
  value("newline"),
  branch("identifier",
    value("userDefined"),
    value("var"),
    value("uses"),
    value("constructor"),
    value("do_"),
    value("end_")
  ),
  ...
;

What would be the direction I need to go in to achieve this?

Kind regards,
Mike

Reply via email to