#include "Token.h" #include "utils.h" const std::string token_strings[] = { "TT_IDENTIFIER", "TT_NUMBER", "TT_FLOAT", "TT_KEYWORD", "TT_STRING", "TT_PLUS", "TT_MINUS", "TT_MUL", "TT_DIV", "TT_AT", "TT_EQUAL", "TT_LESSTHAN", "TT_GREATERTHAN", "TT_LPAREN", "TT_RPAREN", "TT_LBRACKET", "TT_RBRACKET", "TT_SEMICOLON", "TT_LOADEDSTRING", "TT_EOF", "TT_NULL", "TT_EQUALS", "TT_GTE", "TT_LTE", "TT_PERIOD", "TT_COMMA", "TT_PATH", "TT_EXCLAMATION", "TT_NEQUAL", "TT_LSQB", "TT_RSQB", "TT_TYPE", "TT_IMPORT", "TT_SYSCALL0", "TT_SYSCALL1", "TT_SYSCALL2", "TT_SYSCALL3", "TT_SYSCALL4", "TT_SYSCALL5", "TT_COMPILERMACRO"}; Token::Token(const TokenType& type) : tk_type(type), loc(0, 0, "") { } Token::Token(const TokenType& type, const Location& location) : tk_type(type), loc(location) { } Token::Token(const TokenType& type, const std::string& val) : tk_type(type), loc(0, 0, ""), string_value(val) { } Token::Token(const TokenType& type, const int& val, const Location& location) : tk_type(type), int_value(val), loc(location) { } Token::Token(const TokenType& type, const std::string& val, const Location& location) : tk_type(type), string_value(val), loc(location) { } Token::Token(const TokenType& type, const float& val, const Location& location) : tk_type(type), float_value(val), loc(location) { } Token::~Token() { } Token Token::copy_with_new_type(const TokenType& type) { Token result(type, loc); result.int_value = int_value; result.float_value = float_value; result.string_value = string_value; result.line_text = line_text; return result; } std::string Token::to_string() const { std::string details = loc.paren_str(); if (tk_type == TT_Number) { return format_string("INT:%d %s", int_value, details); } else if (tk_type == TT_Float) { return format_string("FLOAT:%f %s", float_value, details); } else if (tk_type == TT_Identifier) { return format_string("ID:%s %s", string_value, details); } else if (tk_type == TT_Keyword) { return format_string("KEYWORD:%s %s", string_value, details); } else if (tk_type == TT_Type) { return format_string("TYPE:%s %s", string_value, details); } else if (tk_type == TT_String) { replace(const_cast(string_value), "\n", "\\n"); return format_string("STRING:'%s' %s", string_value, details); } switch (tk_type) { case TT_EOF: return "EOF " + details; case TT_Plus: return "PLUS " + details; case TT_Minus: return "MINUS " + details; case TT_Mul: return "MUL " + details; case TT_Div: return "DIV " + details; case TT_At: return "AT " + details; case TT_Equal: return "EQUAL " + details; case TT_LessThan: return "LESSTHAN " + details; case TT_GreaterThan: return "GREATERTHAN " + details; case TT_LParen: return "LPAREN " + details; case TT_RParen: return "RPAREN " + details; case TT_LBracket: return "LBRACKET " + details; case TT_RBracket: return "RBRACKET " + details; case TT_Semicolon: return "SEMICOLON " + details; case TT_LoadedString: return "LDSTRING " + details; case TT_Equals: return "EQUALS " + details; case TT_GTE: return "GTE " + details; case TT_LTE: return "LTE " + details; case TT_Period: return "PERIOD " + details; case TT_Comma: return "COMMA " + details; case TT_Path: return "PATH " + details; case TT_Exclamation: return "EXCLAMATION " + details; case TT_NEqual: return "NEQUAL " + details; case TT_LSQB: return "LEFTSQB " + details; case TT_RSQB: return "RIGHTSQB " + details; case TT_Import: return "IMPORT " + details; case TT_Syscall0: return "SYSCALL0 " + details; case TT_Syscall1: return "SYSCALL1 " + details; case TT_Syscall2: return "SYSCALL2 " + details; case TT_Syscall3: return "SYSCALL3 " + details; case TT_Syscall4: return "SYSCALL4 " + details; case TT_Syscall5: return "SYSCALL5 " + details; case TT_CompilerMacro: return "COMPMACRO " + details; } return ""; } std::string Token::line() const { return this->line_text; } Token Token::make_with_line(const Token& origin, const std::string& line_text) { Token result(origin.tk_type, origin.loc); result.int_value = origin.int_value; result.float_value = origin.float_value; result.string_value = origin.string_value; result.line_text = line_text; return result; } void Token::operator=(const Token& other) { tk_type = other.tk_type; int_value = other.int_value; string_value = other.string_value; float_value = other.float_value; line_text = other.line_text; } void Token::erase(Token& tk) { tk.tk_type = TT_Null; } bool Token::match_token_types(const std::vector& a, const std::vector& b, int count) { int size = [](int a, int b) { return a > b ? b : a; }(a.size() - count, b.size()); for (int i = 0; i < size; ++i) { if (a[i + count].tk_type != b[i].tk_type) { return false; } } return true; }