GSC Interpreter
A Turing-complete interpreter developed for a compiler course
Loading...
Searching...
No Matches
token.cpp
Go to the documentation of this file.
1#include "gsc/token.hpp"
2#include <utility>
3
4Token::Token(TokenType type, std::string lexeme, std::any literal, int line)
5 : type{type}, lexeme{std::move(lexeme)}, literal{std::move(literal)},
6 line{line} {}
7
8TokenType Token::getType() const { return type; }
9
10std::string Token::getLexeme() const { return lexeme; }
11
12std::any Token::getLiteral() const { return literal; }
13
14int Token::getLine() const { return line; }
15
16std::string Token::toString() const {
17 std::string literal_str;
18
19 switch (type) {
20 case (TRUE):
21 literal_str = "true";
22 break;
23 case (FALSE):
24 literal_str = "false";
25 break;
26 case (NUMBER):
27 literal_str = std::to_string(std::any_cast<int>(literal));
28 break;
29 case (STRING):
30 try {
31 literal_str = std::any_cast<std::string>(literal);
32 } catch (const std::bad_any_cast &e) {
33 literal_str =
34 static_cast<std::string>(std::any_cast<const char *>(literal));
35 }
36 break;
37 case (IDENTIFIER):
38 literal_str = lexeme;
39 break;
40 default:
41 literal_str = "nil";
42 break;
43 }
44
45 return ::toString(type) + " " + lexeme + " " + literal_str;
46}