1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity high)
4 module dil.lexer.IDs;
5 
6 import dil.lexer.Identifier,
7        dil.lexer.IDsList,
8        dil.lexer.IDsEnum,
9        dil.lexer.TokensEnum;
10 
11 /// The namespace for all Identifiers.
12 struct IDs
13 {
14   alias T = TOK;
15   alias I = Identifier;
16 static:
17   /// The pre-compiled list of Identifier declarations.
18   enum identifiers = (){
19     char[] code = "I ".dup;
20     foreach (xy; keywordIDs)
21     {
22       auto name = xy[0], id = xy[1];
23       code ~= name~` = I("`~id~`", T.`~name~", IDK."~name~"),\n";
24     }
25     foreach (xy; specialIDs)
26     {
27       auto name = xy[0], id = xy[1];
28       code ~= name~` = I("`~id~`", T.SpecialID, IDK.`~name~"),\n";
29     }
30     foreach (xy; predefinedIDs)
31     {
32       auto name = xy[0], id = xy[1];
33       code ~= name~` = I("`~id~`", T.Identifier, IDK.`~name~"),\n";
34     }
35     code[$-2] = ';';
36     return code;
37   }();
38 
39   mixin(identifiers);
40 
41   /// Returns an array of Identifiers.
42   Identifier[] getIDs(alias List, .size_t Len = List.length)()
43   { // Take the address of the first ID and return a slice.
44     return (&mixin(List[0][0]))[0 .. Len];
45   }
46 
47   alias getKeywordIDs = getIDs!(keywordIDs);
48   alias getSpecialIDs = getIDs!(specialIDs);
49   alias getPredefinedIDs = getIDs!(predefinedIDs);
50   alias getAllIDs = getIDs!(keywordIDs,
51     keywordIDs.length + specialIDs.length + predefinedIDs.length);
52 
53   /// Returns true for assembler jump opcode identifiers.
54   bool isJumpOpcode(IDK kind)
55   {
56     return IDK.ja <= kind && kind <= IDK.jz;
57   }
58 }
59 
60 /// E.g.: enum Identifier* Abstract = &IDs.Abstract
61 char[] generatePointersToIDs(string[2][] list)
62 {
63   char[] code = "enum Identifier* ".dup;
64   foreach (name; list)
65     code ~= name[0] ~ " = &IDs." ~ name[0] ~ ",\n";
66   code[$-2] = ';';
67   return code;
68 }
69 
70 /// A namespace for keyword identifiers.
71 struct Keyword
72 {
73 static:
74   mixin(generatePointersToIDs(keywordIDs));
75 }
76 
77 /// A namespace for special identifiers.
78 struct SpecialID
79 {
80 static:
81   mixin(generatePointersToIDs(specialIDs));
82 }
83 
84 /// A namespace for the predefined identifiers.
85 struct Ident
86 {
87 static:
88   mixin(generatePointersToIDs(predefinedIDs));
89   alias isJumpOpcode = IDs.isJumpOpcode;
90 }