Last active
November 27, 2021 00:21
-
-
Save MrSmith33/d5544392c9c57fea3e6670af2e4c91ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
void main() { | |
foreach(kw; keyword_strings) { | |
TokenType t = identifier_to_keyword(kw); | |
writefln("%s %s", kw, t); | |
} | |
} | |
TokenType identifier_to_keyword(const(char[]) tok) { | |
if (tok.length > 8) return TokenType.IDENTIFIER; | |
ulong num; | |
char* buf = cast(char*)(&num); | |
buf[0..tok.length] = tok; | |
static immutable(ubyte[64]) sorted_vals = [ | |
27,31,12,9,35,29,33,36,30,34,28,32,1,19,24,10,41,26,25,22,21,18,38,42,39,40,7,17,6,14,20,15,37,16,13,3,4,8,5,2,11,23, | |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; | |
static immutable(ulong[64]) sorted_keys = [ | |
0x0000000000003869,0x0000000000003875,0x0000000000006669,0x0000000000006F64,0x0000000000323366,0x0000000000323369, | |
0x0000000000323375,0x0000000000343666,0x0000000000343669,0x0000000000343675,0x0000000000363169,0x0000000000363175, | |
0x0000000000666923,0x0000000000726F66,0x0000000064696F76,0x0000000065736C65,0x0000000065757274,0x000000006C6C756E, | |
0x000000006C6F6F62,0x000000006D756E65,0x0000000074736163,0x000000656C696877,0x0000006570797424,0x00000065736C6166, | |
0x000000657A697369,0x000000657A697375,0x0000006B61657262,0x0000006E6F696E75,0x0000007361696C61,0x0000656C75646F6D, | |
0x0000686374697773,0x00006E7275746572,0x00007361696C6124,0x0000746375727473,0x000074726F706D69,0x00656E696C6E6923, | |
0x0074726573736123,0x65756E69746E6F63,0x68636165726F6623,0x6E6F697372657623,0x6E6F6974636E7566,0x6E72757465726F6E, | |
0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF, | |
0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF, | |
0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF, | |
0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF,0xFFFFFFFFFFFFFFFF]; | |
uint i = 0; | |
i += (sorted_keys[i + 32] <= num) * 32; | |
i += (sorted_keys[i + 16] <= num) * 16; | |
i += (sorted_keys[i + 8] <= num) * 8; | |
i += (sorted_keys[i + 4] <= num) * 4; | |
i += (sorted_keys[i + 2] <= num) * 2; | |
i += (sorted_keys[i + 1] <= num) * 1; | |
ulong val = sorted_keys[i]; | |
return val == num ? cast(TokenType)sorted_vals[i] : TokenType.IDENTIFIER; | |
} | |
immutable string[] keyword_strings = ["i8", "u8", "if", "do", "f32", "i32", "u32", "f64", "i64", "u64", | |
"i16", "u16", "#if", "for", "void", "else", "true", "null", "bool", "enum", "cast", "while", | |
"$type", "false", "break", "union", "alias", "module", "switch", "return", "$alias", "struct", | |
"import", "#inline", "#assert", "continue", "#foreach", "#version", "function", "noreturn"]; | |
enum TokenType : ubyte { | |
IDENTIFIER, | |
HASH_IF, // #if | |
HASH_VERSION, // #version | |
HASH_INLINE, // #inline | |
HASH_ASSERT, // #assert | |
HASH_FOREACH, // #foreach | |
ALIAS_SYM, // alias | |
BREAK_SYM, // break | |
CONTINUE_SYM, // continue | |
DO_SYM, // do | |
ELSE_SYM, // else | |
FUNCTION_SYM, // function | |
IF_SYM, // if | |
IMPORT_SYM, // import | |
MODULE_SYM, // module | |
RETURN_SYM, // return | |
STRUCT_SYM, // struct | |
UNION_SYM, // union | |
WHILE_SYM, // while | |
FOR_SYM, // for | |
SWITCH_SYM, // switch | |
CAST, // cast | |
ENUM, // enum | |
TYPE_NORETURN, // noreturn | |
TYPE_VOID, // void | |
TYPE_BOOL, // bool | |
NULL, // null | |
TYPE_I8, // i8 | |
TYPE_I16, // i16 | |
TYPE_I32, // i32 | |
TYPE_I64, // i64 | |
TYPE_U8, // u8 | |
TYPE_U16, // u16 | |
TYPE_U32, // u32 | |
TYPE_U64, // u64 | |
TYPE_F32, // f32 | |
TYPE_F64, // f64 | |
TYPE_ALIAS, // $alias | |
TYPE_TYPE, // $type | |
TYPE_ISIZE, // isize | |
TYPE_USIZE, // usize | |
TRUE_LITERAL, // true | |
FALSE_LITERAL, // false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment