From 85ab7de253768682a8ba7cfa818e09abcb7b4945 Mon Sep 17 00:00:00 2001 From: NZH Date: Fri, 27 Oct 2023 11:14:52 +0800 Subject: [PATCH] refactor: token Signed-off-by: NZH --- src/include/token.h | 288 +++++++++++++++++++++++++++++++----------- src/lexical/token.cpp | 65 +++++----- 2 files changed, 250 insertions(+), 103 deletions(-) diff --git a/src/include/token.h b/src/include/token.h index 66852b7..0f0af41 100644 --- a/src/include/token.h +++ b/src/include/token.h @@ -17,111 +17,257 @@ #ifndef SIMPLECOMPILER_TOKEN_H #define SIMPLECOMPILER_TOKEN_H +#include #include #include -enum Tag { - ERR = -2, // 错误 - END = EOF, // 结束标识 - // 关键字 keyword - KW_INT = 0, // int - KW_CHAR, // char - KW_VOID, // void - KW_CONST, // const - KW_IF, // if - KW_ELSE, // else - KW_WHILE, // while - KW_FOR, // for - KW_BREAK, // break - KW_CONTINUE, // continue - KW_RETURN, // return - // 类型标识 - ID, // 标识符 - NUM, // int - CHAR, // char - STR, // str - // 操作符 operator - ASSIGN, // = - ADD, // + - SUB, // - - MUL, // * - DIV, // / - MOD, // % - ORBIT, // | - ANDBIT, // & - EORBIT, // ^ - AND, // && - OR, // || - NOT, // ! - GT, // > - GE, // >= - LT, // < - LE, // <= - EQU, // == - NEQU, // != - // 分界符 separator - LPAREN, // ( - RPAREN, // ) - LBRACE, // { - RBRACE, // } - LBRACKET, // [ - RBRACKET, // ] - COMMA, // , - COLON, // : - SEMICON, // ; - // 字面值 literal - // 1, 2, 3, a, b, c, true, flase, test, 233, 666, ... +/** + * 支持的类型枚举 + */ +enum Tag : ssize_t { + // 错误 + ERR = -2, + // 结束标识 + END = EOF, + + /// @name 关键字 keyword + /// @{ + /// int + KW_INT = 0, + /// char + KW_CHAR, + /// void + KW_VOID, + /// const + KW_CONST, + /// if + KW_IF, + /// else + KW_ELSE, + /// while + KW_WHILE, + /// for + KW_FOR, + /// break + KW_BREAK, + /// continue + KW_CONTINUE, + /// return + KW_RETURN, + /// @} + + /// @name 类型标识 + /// 标识符 + ID, + /// int + NUM, + /// char + CHAR, + /// str + STR, + /// @} + + /// @name 操作符 operator + /// = + ASSIGN, + /// + + ADD, + /// - + SUB, + /// * + MUL, + /// / + DIV, + /// % + MOD, + /// | + ORBIT, + /// & + ANDBIT, + /// ^ + EORBIT, + /// && + AND, + /// || + OR, + /// ! + NOT, + /// > + GT, + /// >= + GE, + /// < + LT, + /// <= + LE, + /// == + EQU, + /// != + NEQU, + /// @} + + /// @name 分界符 separator + /// ( + LPAREN, + /// ) + RPAREN, + /// { + LBRACE, + /// } + RBRACE, + /// [ + LBRACKET, + /// ] + RBRACKET, + /// , + COMMA, + /// : + COLON, + /// ; + SEMICON, + /// @} }; -// 基类 +/** + * 基类 + */ class Token { public: + /// 保存的 tag Tag tag; - Token(Tag _t); - virtual std::string to_string(); - virtual ~Token(); + + /** + * 构造函数 + * @param _tag tag + */ + explicit Token(Tag _tag); + + /// @name 默认构造/析构函数 + /// @{ + Token() = default; + Token(const Token &_token) = default; + Token(Token &&_token) = default; + auto operator=(const Token &_token) -> Token & = default; + auto operator=(Token &&_token) -> Token & = default; + virtual ~Token() = default; + /// @} + + /** + * 转换为 uint32_t + * @return uint32_t 结果 + * @return const std::string 转换为字符串结果 + */ + [[nodiscard]] virtual auto to_string() -> const std::string; }; -// 标识符 +/** + * 标识符 + */ class Id : public Token { public: std::string name; - Id(std::string _n); - virtual std::string to_string(); + + explicit Id(std::string _n); + + /// @name 默认构造/析构函数 + /// @{ + Id() = default; + Id(const Id &_id) = default; + Id(Id &&_id) = default; + auto operator=(const Id &_id) -> Id & = default; + auto operator=(Id &&_id) -> Id & = default; + ~Id() = default; + /// @} + + [[nodiscard]] auto to_string() -> const std::string override; }; -// 数字 +/** + * 数字 + */ class Num : public Token { public: int val; - Num(int _v); - virtual std::string to_string(); + + explicit Num(int _val); + + /// @name 默认构造/析构函数 + /// @{ + Num() = default; + Num(const Num &_num) = default; + Num(Num &&_num) = default; + auto operator=(const Num &_num) -> Num & = default; + auto operator=(Num &&_num) -> Num & = default; + ~Num() = default; + /// @} + + [[nodiscard]] auto to_string() -> const std::string override; }; -// 字符 +/** + * 字符 + */ class Char : public Token { public: char ch; - Char(char _c); - virtual std::string to_string(); + + explicit Char(char _ch); + + /// @name 默认构造/析构函数 + /// @{ + Char() = default; + Char(const Char &_char) = default; + Char(Char &&_char) = default; + auto operator=(const Char &_char) -> Char & = default; + auto operator=(Char &&_char) -> Char & = default; + ~Char() = default; + /// @} + + [[nodiscard]] auto to_string() -> const std::string override; }; -// 字符串 +/** + * 字符串 + */ class Str : public Token { public: std::string str; - Str(std::string _s); - virtual std::string to_string(); + + explicit Str(const std::string &_string); + + /// @name 默认构造/析构函数 + /// @{ + Str() = default; + Str(const Str &_str) = default; + Str(Str &&_str) = default; + auto operator=(const Str &_str) -> Str & = default; + auto operator=(Str &&_str) -> Str & = default; + ~Str() = default; + /// @} + + [[nodiscard]] auto to_string() -> const std::string override; }; -// 关键字 +/** + * 关键字 + */ class Keywords { -private: - std::unordered_map> keywords; - public: Keywords(); + + /// @name 默认构造/析构函数 + /// @{ + Keywords(const Keywords &_keywords) = default; + Keywords(Keywords &&_keywords) = default; + auto operator=(const Keywords &_keywords) -> Keywords & = default; + auto operator=(Keywords &&_keywords) -> Keywords & = default; + ~Keywords() = default; + /// @} + Tag get_tag(std::string _name); + +private: + std::unordered_map> keywords; }; #endif /* SIMPLECOMPILER_TOKEN_H */ diff --git a/src/lexical/token.cpp b/src/lexical/token.cpp index 891b5e8..420a927 100644 --- a/src/lexical/token.cpp +++ b/src/lexical/token.cpp @@ -16,26 +16,7 @@ #include "token.h" -// 初始化 -Keywords::Keywords() { - keywords["int"] = KW_INT; - keywords["char"] = KW_CHAR; - keywords["void"] = KW_VOID; - keywords["const"] = KW_CONST; - keywords["if"] = KW_IF; - keywords["else"] = KW_ELSE; - keywords["while"] = KW_WHILE; - keywords["for"] = KW_FOR; - keywords["break"] = KW_BREAK; - keywords["continue"] = KW_CONTINUE; - keywords["return"] = KW_RETURN; -} - -Tag Keywords::get_tag(std::string _name) { - return keywords.find(_name) != keywords.end() ? keywords[_name] : ID; -} - -const char *tokenName[] = { +static const std::string tokenName[] = { "INT", "CHAR", "VOID", "CONST", "IF", "ELSE", "WHILE", "FOR", "BREAK", "CONTINUE", "RETURN", "ID", "NUM", "CH", "STR", "ASSIGN", "ADD", "SUB", "MUL", "DIV", "MOD", @@ -44,28 +25,48 @@ const char *tokenName[] = { "LBRACE", "RBRACE", "LBRACKET", "RBRACKET", "COMMA", "COLON", "SEMICON", }; -Token::Token(Tag _t) : tag(_t) { return; } +Token::Token(Tag _t) : tag(_t) { ; } -std::string Token::to_string() { return tokenName[tag]; } +auto Token::to_string() -> const std::string { return tokenName[tag]; } -Token::~Token() { return; } +Id::Id(std::string _n) : Token(ID), name(_n) {} -Id::Id(std::string _n) : Token(ID), name(_n) { return; } - -std::string Id::to_string() { return Token::to_string() + "(" + name + ")"; } +const std::string Id::to_string() { + return Token::to_string() + "(" + name + ")"; +} -Num::Num(int _v) : Token(NUM), val(_v) { return; } +Num::Num(int _v) : Token(NUM), val(_v) {} -std::string Num::to_string() { +const std::string Num::to_string() { return Token::to_string() + "(" + std::to_string(val) + ")"; } -Char::Char(char _c) : Token(CHAR), ch(_c) { return; } +Char::Char(char _c) : Token(CHAR), ch(_c) {} -std::string Char::to_string() { +const std::string Char::to_string() { return Token::to_string() + "(" + std::to_string(ch) + ")"; } -Str::Str(std::string _s) : Token(STR), str(_s) { return; } +Str::Str(const std::string &_string) : Token(STR), str(_string) {} -std::string Str::to_string() { return Token::to_string() + "(" + str + ")"; } +const std::string Str::to_string() { + return Token::to_string() + "(" + str + ")"; +} + +Keywords::Keywords() { + keywords["int"] = KW_INT; + keywords["char"] = KW_CHAR; + keywords["void"] = KW_VOID; + keywords["const"] = KW_CONST; + keywords["if"] = KW_IF; + keywords["else"] = KW_ELSE; + keywords["while"] = KW_WHILE; + keywords["for"] = KW_FOR; + keywords["break"] = KW_BREAK; + keywords["continue"] = KW_CONTINUE; + keywords["return"] = KW_RETURN; +} + +Tag Keywords::get_tag(std::string _name) { + return keywords.find(_name) != keywords.end() ? keywords[_name] : ID; +}