Token.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_ARRAY: return "array";
  65. case T_POINT: return ".";
  66. case T_OPEN_SQUARE_BRACKET: return "[";
  67. case T_CLOSE_SQUARE_BRACKET: return "]";
  68. case T_END: return "end";
  69. }
  70. return "unknown";
  71. }
  72. #define MATCH_TOKEN(tokenName, token) \
  73. if(strcmp(name, tokenName) == 0) { \
  74. return token; \
  75. }
  76. Token tFromName(const char* name) {
  77. MATCH_TOKEN("print", T_PRINT);
  78. MATCH_TOKEN("null", T_NULL);
  79. MATCH_TOKEN("true", T_TRUE);
  80. MATCH_TOKEN("false", T_FALSE);
  81. MATCH_TOKEN("return", T_RETURN);
  82. MATCH_TOKEN("if", T_IF);
  83. MATCH_TOKEN("else", T_ELSE);
  84. MATCH_TOKEN("while", T_WHILE);
  85. MATCH_TOKEN("for", T_FOR);
  86. MATCH_TOKEN("break", T_BREAK);
  87. MATCH_TOKEN("continue", T_CONTINUE);
  88. MATCH_TOKEN("array", T_ARRAY);
  89. MATCH_TOKEN("int", T_INT);
  90. MATCH_TOKEN("void", T_VOID);
  91. MATCH_TOKEN("bool", T_BOOL);
  92. MATCH_TOKEN("float", T_FLOAT);
  93. return T_END;
  94. }