Lexer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Standard includes
  2. #include <cctype>
  3. #include <cstdio>
  4. // Local includes
  5. #include "Lexer.h"
  6. /// gettok - Return the next token from standard input.
  7. namespace lexer{
  8. std::string LexerObjects::IdentifierStr; // Filled in if tok_identifier
  9. double LexerObjects::NumVal;
  10. std::string getTokName(int Tok) {
  11. switch (Tok) {
  12. case tok_eof:
  13. return "eof";
  14. case tok_def:
  15. return "def";
  16. case tok_extern:
  17. return "extern";
  18. case tok_identifier:
  19. return "identifier";
  20. case tok_number:
  21. return "number";
  22. case tok_if:
  23. return "if";
  24. case tok_then:
  25. return "then";
  26. case tok_else:
  27. return "else";
  28. case tok_for:
  29. return "for";
  30. case tok_in:
  31. return "in";
  32. case tok_binary:
  33. return "binary";
  34. case tok_unary:
  35. return "unary";
  36. case tok_var:
  37. return "var";
  38. }
  39. return std::string(1, (char)Tok);
  40. }
  41. int gettok() {
  42. static int LastChar = ' ';
  43. // Skip any whitespace.
  44. while (isspace(LastChar))
  45. LastChar = getchar();
  46. if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
  47. LexerObjects::IdentifierStr = LastChar;
  48. while (isalnum((LastChar = getchar())))
  49. LexerObjects::IdentifierStr += LastChar;
  50. if (LexerObjects::IdentifierStr == "def")
  51. return tok_def;
  52. if (LexerObjects::IdentifierStr == "extern")
  53. return tok_extern;
  54. if (LexerObjects::IdentifierStr == "if")
  55. return tok_if;
  56. if (LexerObjects::IdentifierStr == "then")
  57. return tok_then;
  58. if (LexerObjects::IdentifierStr == "else")
  59. return tok_else;
  60. if (LexerObjects::IdentifierStr == "for")
  61. return tok_for;
  62. if (LexerObjects::IdentifierStr == "in")
  63. return tok_in;
  64. if (LexerObjects::IdentifierStr == "binary")
  65. return tok_binary;
  66. if (LexerObjects::IdentifierStr == "unary")
  67. return tok_unary;
  68. if (LexerObjects::IdentifierStr == "var")
  69. return tok_var;
  70. return tok_identifier;
  71. }
  72. if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
  73. std::string NumStr;
  74. do {
  75. NumStr += LastChar;
  76. LastChar = getchar();
  77. } while (isdigit(LastChar) || LastChar == '.');
  78. LexerObjects::NumVal = strtod(NumStr.c_str(), nullptr);
  79. return tok_number;
  80. }
  81. if (LastChar == '#') {
  82. // Comment until end of line.
  83. do
  84. LastChar = getchar();
  85. while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
  86. if (LastChar != EOF)
  87. return gettok();
  88. }
  89. // Check for end of file. Don't eat the EOF.
  90. if (LastChar == EOF)
  91. return tok_eof;
  92. // Otherwise, just return the character as its ascii value.
  93. int ThisChar = LastChar;
  94. LastChar = getchar();
  95. return ThisChar;
  96. }
  97. }