Token.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_INT: return "int";
  8. case T_BOOL: return "bool";
  9. case T_FLOAT: return "float";
  10. case T_CONST_INT: return "const int";
  11. case T_CONST_FLOAT: return "const float";
  12. case T_TEXT: return "text";
  13. case T_NULL: return "null";
  14. case T_TRUE: return "true";
  15. case T_FALSE: return "false";
  16. case T_ADD: return "+";
  17. case T_SUB: return "-";
  18. case T_MUL: return "*";
  19. case T_DIV: return "/";
  20. case T_MOD: return "%";
  21. case T_LESS: return "<";
  22. case T_LESS_EQUAL: return "<=";
  23. case T_GREATER: return ">";
  24. case T_GREATER_EQUAL: return ">=";
  25. case T_EQUAL: return "==";
  26. case T_NOT_EQUAL: return "!=";
  27. case T_NOT: return "!";
  28. case T_AND: return "&&";
  29. case T_OR: return "||";
  30. case T_BIT_NOT: return "~";
  31. case T_BIT_AND: return "&";
  32. case T_BIT_OR: return "|";
  33. case T_BIT_XOR: return "^";
  34. case T_LEFT_SHIFT: return "<<";
  35. case T_RIGHT_SHIFT: return ">>";
  36. case T_SET: return "=";
  37. case T_ADD_SET: return "+=";
  38. case T_SUB_SET: return "-=";
  39. case T_MUL_SET: return "*=";
  40. case T_DIV_SET: return "/=";
  41. case T_MOD_SET: return "%=";
  42. case T_BIT_AND_SET: return "&=";
  43. case T_BIT_OR_SET: return "|=";
  44. case T_BIT_XOR_SET: return "^=";
  45. case T_LEFT_SHIFT_SET: return "<<=";
  46. case T_RIGHT_SHIFT_SET: return ">>=";
  47. case T_INCREMENT: return "++";
  48. case T_DECREMENT: return "--";
  49. case T_LITERAL: return "literal";
  50. case T_PRINT: return "print";
  51. case T_IF: return "if";
  52. case T_ELSE: return "else";
  53. case T_WHILE: return "while";
  54. case T_FOR: return "for";
  55. case T_BREAK: return "break";
  56. case T_CONTINUE: return "continue";
  57. case T_RETURN: return "return";
  58. case T_COMMA: return ",";
  59. case T_SEMICOLON: return ";";
  60. case T_OPEN_BRACKET: return "(";
  61. case T_CLOSE_BRACKET: return ")";
  62. case T_OPEN_CURVED_BRACKET: return "{";
  63. case T_CLOSE_CURVED_BRACKET: return "}";
  64. case T_POINT: return ".";
  65. case T_OPEN_SQUARE_BRACKET: return "[";
  66. case T_CLOSE_SQUARE_BRACKET: return "]";
  67. case T_END: return "end";
  68. }
  69. return "unknown";
  70. }
  71. #define MATCH_TOKEN(tokenName, token) \
  72. if(strcmp(name, tokenName) == 0) { \
  73. return token; \
  74. }
  75. Token tFromName(const char* name) {
  76. MATCH_TOKEN("print", T_PRINT);
  77. MATCH_TOKEN("null", T_NULL);
  78. MATCH_TOKEN("true", T_TRUE);
  79. MATCH_TOKEN("false", T_FALSE);
  80. MATCH_TOKEN("return", T_RETURN);
  81. MATCH_TOKEN("if", T_IF);
  82. MATCH_TOKEN("else", T_ELSE);
  83. MATCH_TOKEN("while", T_WHILE);
  84. MATCH_TOKEN("for", T_FOR);
  85. MATCH_TOKEN("break", T_BREAK);
  86. MATCH_TOKEN("continue", T_CONTINUE);
  87. MATCH_TOKEN("int", T_INT);
  88. MATCH_TOKEN("void", T_VOID);
  89. MATCH_TOKEN("bool", T_BOOL);
  90. MATCH_TOKEN("float", T_FLOAT);
  91. return T_END;
  92. }