Token.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <stdbool.h>
  2. #include <string.h>
  3. #include "tokenizer/Token.h"
  4. const char* tGetName(Token token) {
  5. switch(token) {
  6. case T_VOID: return "void";
  7. case T_INT32: return "int";
  8. case T_INT64: return "long";
  9. case T_BOOL: return "bool";
  10. case T_FLOAT: return "float";
  11. case T_CONST_INT32: return "const int";
  12. case T_CONST_INT64: return "const long";
  13. case T_CONST_FLOAT: return "const float";
  14. case T_TEXT: return "text";
  15. case T_NULLPTR: return "nullptr";
  16. case T_TRUE: return "true";
  17. case T_FALSE: return "false";
  18. case T_ADD: return "+";
  19. case T_SUB: return "-";
  20. case T_MUL: return "*";
  21. case T_DIV: return "/";
  22. case T_MOD: return "%";
  23. case T_LESS: return "<";
  24. case T_LESS_EQUAL: return "<=";
  25. case T_GREATER: return ">";
  26. case T_GREATER_EQUAL: return ">=";
  27. case T_EQUAL: return "==";
  28. case T_NOT_EQUAL: return "!=";
  29. case T_NOT: return "!";
  30. case T_AND: return "&&";
  31. case T_OR: return "||";
  32. case T_BIT_NOT: return "~";
  33. case T_BIT_AND: return "&";
  34. case T_BIT_OR: return "|";
  35. case T_BIT_XOR: return "^";
  36. case T_LEFT_SHIFT: return "<<";
  37. case T_RIGHT_SHIFT: return ">>";
  38. case T_SET: return "=";
  39. case T_ADD_SET: return "+=";
  40. case T_SUB_SET: return "-=";
  41. case T_MUL_SET: return "*=";
  42. case T_DIV_SET: return "/=";
  43. case T_MOD_SET: return "%=";
  44. case T_BIT_AND_SET: return "&=";
  45. case T_BIT_OR_SET: return "|=";
  46. case T_BIT_XOR_SET: return "^=";
  47. case T_LEFT_SHIFT_SET: return "<<=";
  48. case T_RIGHT_SHIFT_SET: return ">>=";
  49. case T_INCREMENT: return "++";
  50. case T_DECREMENT: return "--";
  51. case T_LITERAL: return "literal";
  52. case T_IF: return "if";
  53. case T_ELSE: return "else";
  54. case T_WHILE: return "while";
  55. case T_FOR: return "for";
  56. case T_BREAK: return "break";
  57. case T_CONTINUE: return "continue";
  58. case T_RETURN: return "return";
  59. case T_STRUCT: return "struct";
  60. case T_COMMA: return ",";
  61. case T_SEMICOLON: return ";";
  62. case T_OPEN_BRACKET: return "(";
  63. case T_CLOSE_BRACKET: return ")";
  64. case T_OPEN_CURVED_BRACKET: return "{";
  65. case T_CLOSE_CURVED_BRACKET: return "}";
  66. case T_POINT: return ".";
  67. case T_ARROW: return "->";
  68. case T_OPEN_SQUARE_BRACKET: return "[";
  69. case T_CLOSE_SQUARE_BRACKET: return "]";
  70. case T_NEW: return "new";
  71. case T_DELETE: return "delete";
  72. case T_LENGTH: return "length";
  73. case T_END: return "end";
  74. }
  75. return "unknown";
  76. }
  77. #define MATCH_TOKEN(tokenName, token) \
  78. if(strcmp(name, tokenName) == 0) { \
  79. return token; \
  80. }
  81. Token tFromName(const char* name) {
  82. MATCH_TOKEN("nullptr", T_NULLPTR);
  83. MATCH_TOKEN("true", T_TRUE);
  84. MATCH_TOKEN("false", T_FALSE);
  85. MATCH_TOKEN("return", T_RETURN);
  86. MATCH_TOKEN("if", T_IF);
  87. MATCH_TOKEN("else", T_ELSE);
  88. MATCH_TOKEN("while", T_WHILE);
  89. MATCH_TOKEN("for", T_FOR);
  90. MATCH_TOKEN("break", T_BREAK);
  91. MATCH_TOKEN("continue", T_CONTINUE);
  92. MATCH_TOKEN("int", T_INT32);
  93. MATCH_TOKEN("long", T_INT64);
  94. MATCH_TOKEN("void", T_VOID);
  95. MATCH_TOKEN("bool", T_BOOL);
  96. MATCH_TOKEN("float", T_FLOAT);
  97. MATCH_TOKEN("struct", T_STRUCT);
  98. MATCH_TOKEN("new", T_NEW);
  99. MATCH_TOKEN("delete", T_DELETE);
  100. MATCH_TOKEN("length", T_LENGTH);
  101. return T_END;
  102. }