From be734b47c34f54392e3932a4aa8ced68d3da0f00 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 21 Sep 2022 09:37:50 +0300 Subject: [PATCH] refactor: switched from type const & to const type & syntax --- include/compiler/treeifier.hh | 4 ++-- include/compiler/treeifier/lexer.hh | 4 ++-- include/compiler/treeifier/tokenizer.hh | 14 ++++++------- include/lang/common.hh | 6 +++--- include/lang/namespace.hh | 10 +++++----- include/utils/message.hh | 2 +- include/utils/strings.hh | 6 +++--- include/utils/threading.hh | 2 +- src/compiler/treeifier/lexer.cc | 6 +++--- src/compiler/treeifier/operators.cc | 6 +++--- src/lsproj.cc | 6 +++--- src/main/main.cc | 26 ++++++++++++------------- src/main/opions.hh | 6 +++--- src/main/options.cc | 10 +++++----- src/utils/data.cc | 21 +++++++------------- src/utils/message.cc | 4 ++-- src/utils/strings.cc | 2 +- 17 files changed, 64 insertions(+), 71 deletions(-) diff --git a/include/compiler/treeifier.hh b/include/compiler/treeifier.hh index f9f6f45..c4b4d44 100644 --- a/include/compiler/treeifier.hh +++ b/include/compiler/treeifier.hh @@ -4,8 +4,8 @@ #include "utils/message.hh" namespace ppc::compiler { - // bool treeify(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::string const &source, namespace_t *pout); - // bool treeify_file(std::string const &filename, ppc::messages::msg_stack_t &pmsg_stack, namespace_t *pout); + // bool treeify(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, const std::string &source, namespace_t *pout); + // bool treeify_file(const std::string &filename, ppc::messages::msg_stack_t &pmsg_stack, namespace_t *pout); } #endif \ No newline at end of file diff --git a/include/compiler/treeifier/lexer.hh b/include/compiler/treeifier/lexer.hh index 46ca516..b36d9f8 100644 --- a/include/compiler/treeifier/lexer.hh +++ b/include/compiler/treeifier/lexer.hh @@ -21,7 +21,7 @@ namespace ppc::comp::tree::lex { std::string data; ppc::location_t location; - static std::vector parse_file(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::istream &f); - static std::vector parse_many(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::string const &src); + static std::vector parse_file(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, std::istream &f); + static std::vector parse_many(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, const std::string &src); }; } diff --git a/include/compiler/treeifier/tokenizer.hh b/include/compiler/treeifier/tokenizer.hh index 9e2a0e4..f889d05 100644 --- a/include/compiler/treeifier/tokenizer.hh +++ b/include/compiler/treeifier/tokenizer.hh @@ -99,7 +99,7 @@ namespace ppc::comp::tree::tok { bool is_char_lit() { return kind == CHAR; } bool is_string_lit() { return kind == STRING; } - auto const &identifier() { + const auto &identifier() { if (!is_identifier()) throw std::string { "Token is not an identifier." }; else return *data.identifier; } @@ -119,7 +119,7 @@ namespace ppc::comp::tree::tok { if (!is_char_lit()) throw std::string { "Token is not a char literal." }; else return data.char_literal; } - auto const &string_lit() { + const auto &string_lit() { if (!is_string_lit()) throw std::string { "Token is not a string literal." }; else return *data.string_literal; } @@ -128,7 +128,7 @@ namespace ppc::comp::tree::tok { bool is_identifier(std::string &&val) { return is_identifier() && identifier() == val; } token_t() { kind = NONE; } - token_t(std::string const &identifier, location_t loc = NO_LOCATION) { + token_t(const std::string &identifier, location_t loc = NO_LOCATION) { kind = IDENTIFIER; data.identifier = new std::string { identifier }; location = loc; @@ -153,12 +153,12 @@ namespace ppc::comp::tree::tok { data.char_literal = c; location = loc; } - token_t(std::vector const &val, location_t loc = NO_LOCATION) { + token_t(const std::vector &val, location_t loc = NO_LOCATION) { kind = STRING; data.string_literal = new std::vector { val }; location = loc; } - token_t(token_t const &tok) { + token_t(const token_t &tok) { kind = tok.kind; switch (kind) { case NONE: break; @@ -184,6 +184,6 @@ namespace ppc::comp::tree::tok { static std::vector parse_many(messages::msg_stack_t &msg_stack, std::vector tokens); }; - operator_t operator_find(std::string const &text); - std::string const &operator_stringify(operator_t kw); + operator_t operator_find(const std::string &text); + const std::string &operator_stringify(operator_t kw); } diff --git a/include/lang/common.hh b/include/lang/common.hh index 71a4f19..5e2fba2 100644 --- a/include/lang/common.hh +++ b/include/lang/common.hh @@ -8,11 +8,11 @@ namespace ppc::lang { std::vector segments; ppc::location_t location; - bool operator ==(namespace_name_t const &other); + bool operator ==(const namespace_name_t &other); }; - bool is_identifier_valid(messages::msg_stack_t &msg_stack, ppc::location_t location, std::string const &name); - inline bool is_identifier_valid(std::string const &name) { + bool is_identifier_valid(messages::msg_stack_t &msg_stack, ppc::location_t location, const std::string &name); + inline bool is_identifier_valid(const std::string &name) { messages::msg_stack_t ms { }; return is_identifier_valid(ms, { }, name); } diff --git a/include/lang/namespace.hh b/include/lang/namespace.hh index a4f80dd..5e293a6 100644 --- a/include/lang/namespace.hh +++ b/include/lang/namespace.hh @@ -16,21 +16,21 @@ namespace ppc::lang { // A list of all the defined fields inside the namespace std::vector fields; - bool contains_def(std::string const &name, location_t &ploc) const; - inline bool contains_def(std::string const &name) const { + bool contains_def(const std::string &name, location_t &ploc) const; + inline bool contains_def(const std::string &name) const { location_t ploc; return contains_def(name, ploc); } template - static bool contains_def(T namespaces, namespace_name_t const &def_nmsp, std::string const &name, location_t &ploc) { - for (namespace_t const &nmsp : namespaces) { + static bool contains_def(T namespaces, const namespace_name_t &def_nmsp, const std::string &name, location_t &ploc) { + for (const namespace_t &nmsp : namespaces) { if (nmsp.name == def_nmsp && nmsp.contains_def(name)) return true; } return false; } template - static inline bool contains_def(T namespaces, namespace_name_t const &def_nmsp, std::string const &name) { + static inline bool contains_def(T namespaces, const namespace_name_t &def_nmsp, const std::string &name) { location_t ploc; return contains_def(namespaces, def_nmsp, name, ploc); } diff --git a/include/utils/message.hh b/include/utils/message.hh index 85ba3b7..90439bd 100644 --- a/include/utils/message.hh +++ b/include/utils/message.hh @@ -28,7 +28,7 @@ namespace ppc::messages { inline auto begin() { return messages.begin(); } inline auto end() { return messages.end(); } - void push(message_t const &msg) { messages.push_back(msg); } + void push(const message_t &msg) { messages.push_back(msg); } void clear() { messages.clear(); } bool is_failed() const; diff --git a/include/utils/strings.hh b/include/utils/strings.hh index 95aab3d..d813ec1 100644 --- a/include/utils/strings.hh +++ b/include/utils/strings.hh @@ -4,12 +4,12 @@ #include namespace ppc::str { - std::vector split(std::string const &splittable, std::initializer_list splitters, bool remove_empty_entries = false); + std::vector split(const std::string &splittable, std::initializer_list splitters, bool remove_empty_entries = false); std::string trim(std::string splittable, std::initializer_list splitters = { ' ', '\n', '\t', '\r' }); - inline bool begins_with(std::string const &str, std::string const &other) { + inline bool begins_with(const std::string &str, const std::string &other) { return !str.find(other); } - inline bool ends_with(std::string const &str, std::string const &other) { + inline bool ends_with(const std::string &str, const std::string &other) { return str.find_last_of(other) == (str.length() - other.length()); } } \ No newline at end of file diff --git a/include/utils/threading.hh b/include/utils/threading.hh index f3dbf37..51a7a5a 100644 --- a/include/utils/threading.hh +++ b/include/utils/threading.hh @@ -21,7 +21,7 @@ namespace ppc::threading { thread_t(void *handle) { this->handle = handle; } template - inline static thread_t start(thread_func_t func, T const &args) { + inline static thread_t start(thread_func_t func, const T &args) { T _args = args; return start_impl((void*)func, &_args); } diff --git a/src/compiler/treeifier/lexer.cc b/src/compiler/treeifier/lexer.cc index 367e662..9783e90 100644 --- a/src/compiler/treeifier/lexer.cc +++ b/src/compiler/treeifier/lexer.cc @@ -330,7 +330,7 @@ const lexlet_t LEXLET_MULTICOMMENT = { .type = token_t::NONE, }; -std::vector token_t::parse_many(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::string const &src) { +std::vector token_t::parse_many(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, const std::string &src) { std::vector tokens; std::vector curr_token; lexlet_t curr = LEXLET_DEFAULT; @@ -377,7 +377,7 @@ std::vector token_t::parse_many(ppc::messages::msg_stack_t &msg_stack, i++; } } - catch (messages::message_t const &msg) { + catch (const messages::message_t &msg) { throw messages::message_t { msg.level, { filename, line, start, i - length, length }, msg.content }; } } @@ -392,7 +392,7 @@ std::vector token_t::parse_many(ppc::messages::msg_stack_t &msg_stack, return tokens; } -std::vector token_t::parse_file(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::istream &f) { +std::vector token_t::parse_file(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, std::istream &f) { std::vector contents; int c; diff --git a/src/compiler/treeifier/operators.cc b/src/compiler/treeifier/operators.cc index ed89ce2..4d560c4 100644 --- a/src/compiler/treeifier/operators.cc +++ b/src/compiler/treeifier/operators.cc @@ -12,19 +12,19 @@ std::vector operators = { "+", "-", "/", "*", "%", "?", "??", "=", "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", "^=", "&=", "|=", "&&=", "||=", "??=", - "->", ".", ",", ";", ":", + "->", ".", ",", ";", ":", "::", "=>", "[", "]", "{", "}", "(", ")" }; -std::string const &tok::operator_stringify(tok::operator_t kw) { +const std::string &tok::operator_stringify(tok::operator_t kw) { if (kw < 0 || kw >= operators.size()) throw "Invalid operator ID given."s; return operators[kw]; } tok::operator_t tok::operator_find(const std::string &raw) { std::size_t i = 0; - for (auto const &op : operators) { + for (const auto &op : operators) { if (op == raw) return (tok::operator_t)i; i++; } diff --git a/src/lsproj.cc b/src/lsproj.cc index 44dd592..8df6e4e 100644 --- a/src/lsproj.cc +++ b/src/lsproj.cc @@ -8,7 +8,7 @@ struct project_t { std::vector deps; }; -std::string read_str(std::istream &f, std::string const &skip_chars, std::string const &end_chars, int &end_char) { +std::string read_str(std::istream &f, const std::string &skip_chars, const std::string &end_chars, int &end_char) { std::vector res { }; int c; @@ -41,7 +41,7 @@ std::string read_str(std::istream &f, std::string const &skip_chars, std::string return std::string { res.begin(), res.end() }; } -std::string read_str(std::istream &f, std::string const &skip_chars, std::string const &end_chars) { +std::string read_str(std::istream &f, const std::string &skip_chars, const std::string &end_chars) { int end_char; return read_str(f, skip_chars, end_chars, end_char); } @@ -87,7 +87,7 @@ project_t read_project(std::istream &f) { }; } -void print_err(std::string const &error, std::string const &context) { +void print_err(const std::string &error, const std::string &context) { std::cerr << context << ": " << error; } diff --git a/src/main/main.cc b/src/main/main.cc index 4b53424..5494818 100644 --- a/src/main/main.cc +++ b/src/main/main.cc @@ -33,7 +33,7 @@ void add_flags(options::parser_t &parser) { .name = "version", .shorthands = "v", .description = "Displays version and license agreement of this binary", - .execute = [](options::parser_t &parser, std::string const &option, ppc::messages::msg_stack_t &global_stack) { + .execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) { cout << "++C compiler\n" << " Version: v" << PPC_VERSION_MAJOR << '.' << PPC_VERSION_MINOR << '.' << PPC_VERSION_BUILD #if WINDOWS @@ -50,13 +50,13 @@ void add_flags(options::parser_t &parser) { .name = "help", .shorthands = "h", .description = "Displays a list of all flags and their meaning", - .execute = [](options::parser_t &parser, std::string const &option, ppc::messages::msg_stack_t &global_stack) { + .execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) { cout << "Usage: ...flags ...files\n\n" << "Flags and file names can be interlaced\n" << "Flags will execute in the order they're written, then compilation begins\n\n" << "Flags:\n"; - for (auto const &flag : parser) { + for (const auto &flag : parser) { std::stringstream buff; buff << " --" << flag.name; @@ -116,7 +116,7 @@ void add_flags(options::parser_t &parser) { .name = "print-what", .description = "Prints a 'what?' type of message (you'll see)", .match_type = options::MATCH_PREFIX, - .execute = [](options::parser_t &parser, std::string const &option, ppc::messages::msg_stack_t &global_stack) { + .execute = [](options::parser_t &parser, const std::string &option, ppc::messages::msg_stack_t &global_stack) { global_stack.push({ (messages::message_t::level_t)69, NO_LOCATION, "IDK LOL." }); } }); @@ -140,28 +140,28 @@ int main(int argc, const char *argv[]) { data::map_t conf; add_flags(parser); - for (auto const &arg : args) { + for (const auto &arg : args) { if (!parser.parse(arg, msg_stack, conf)) { files.push_back(arg); } } - for (auto const &file : files) { + for (const auto &file : files) { std::ifstream f { file, std::ios_base::in }; try { auto res = tok::token_t::parse_many(msg_stack, lex::token_t::parse_file(msg_stack, file, f)); for (auto tok : res) { - if (tok.is_identifier()) std::cout << "Identifier: " << tok.identifier(); - if (tok.is_operator()) std::cout << "Operator: " << tok::operator_stringify(tok._operator()); - if (tok.is_float_lit()) std::cout << "Float: " << tok.float_lit(); - if (tok.is_int_lit()) std::cout << "Int: " << tok.int_lit(); - if (tok.is_char_lit()) std::cout << "Char: " << tok.char_lit(); - if (tok.is_string_lit()) std::cout << "String: " << std::string { tok.string_lit().begin(), tok.string_lit().end() }; + if (tok.is_identifier()) std::cout << "Identifier: \t" << tok.identifier(); + if (tok.is_operator()) std::cout << "Operator: \t" << tok::operator_stringify(tok._operator()); + if (tok.is_float_lit()) std::cout << "Float: \t" << tok.float_lit(); + if (tok.is_int_lit()) std::cout << "Int: \t" << tok.int_lit(); + if (tok.is_char_lit()) std::cout << "Char: \t" << tok.char_lit(); + if (tok.is_string_lit()) std::cout << "String: \t" << std::string { tok.string_lit().begin(), tok.string_lit().end() }; std::cout << std::endl; } } - catch (messages::message_t const &msg) { + catch (const messages::message_t &msg) { msg_stack.push(msg); } } diff --git a/src/main/opions.hh b/src/main/opions.hh index 639f2f6..3075712 100644 --- a/src/main/opions.hh +++ b/src/main/opions.hh @@ -17,19 +17,19 @@ namespace ppc::options { std::string shorthands; std::string description; flag_match_type_t match_type; - void (*execute)(parser_t &parser, std::string const &option, messages::msg_stack_t &global_stack); + void (*execute)(parser_t &parser, const std::string &option, messages::msg_stack_t &global_stack); }; struct parser_t { private: std::vector flags; public: - void add_flag(flag_t const &flag); + void add_flag(const flag_t &flag); void clear_flags(); auto begin() { return flags.begin(); } auto end() { return flags.end(); } - bool parse(std::string const &option, messages::msg_stack_t &msg_stack, data::map_t &conf); + bool parse(const std::string &option, messages::msg_stack_t &msg_stack, data::map_t &conf); }; } diff --git a/src/main/options.cc b/src/main/options.cc index 8c613d3..d5b46c2 100644 --- a/src/main/options.cc +++ b/src/main/options.cc @@ -3,7 +3,7 @@ using namespace ppc; -bool check_shorthand(std::string &option, options::flag_t const &flag) { +bool check_shorthand(std::string &option, const options::flag_t &flag) { if (option.size() < 2 || option[0] != '-') return false; if (option.size() == 2 && std::string { flag.shorthands }.find(option[1]) != -1u) { @@ -17,7 +17,7 @@ bool check_shorthand(std::string &option, options::flag_t const &flag) { } return false; } -bool check_name(std::string &option, options::flag_t const &flag) { +bool check_name(std::string &option, const options::flag_t &flag) { if (option.size() > 2 && option[0] == '-' && option[1] == '-') { std::string candidate = option.substr(2); if (flag.match_type == options::MATCH_WHOLE) { @@ -41,19 +41,19 @@ bool check_name(std::string &option, options::flag_t const &flag) { return false; } -void ppc::options::parser_t::add_flag(flag_t const &flag) { +void ppc::options::parser_t::add_flag(const flag_t &flag) { flags.push_back(flag); } void ppc::options::parser_t::clear_flags() { flags.clear(); } -bool ppc::options::parser_t::parse(std::string const &option, messages::msg_stack_t &msg_stack, data::map_t &conf) { +bool ppc::options::parser_t::parse(const std::string &option, messages::msg_stack_t &msg_stack, data::map_t &conf) { if (option.empty()) return false; std::string opt = option; - for (auto const &flag : flags) { + for (const auto &flag : flags) { if (check_name(opt, flag) || check_shorthand(opt, flag)) { flag.execute(*this, opt, msg_stack); return true; diff --git a/src/utils/data.cc b/src/utils/data.cc index f3c0fe1..f54daf9 100644 --- a/src/utils/data.cc +++ b/src/utils/data.cc @@ -1,9 +1,5 @@ #include "data.hh" -template -struct overload : Ts ... { using Ts::operator() ...; }; -template overload(Ts...) -> overload; - bool ppc::data::value_t::is_null() const { return type == type_t::Null; } @@ -59,11 +55,11 @@ bool ppc::data::value_t::boolean(ppc::data::bool_t &out) const { return false; } -ppc::data::array_t const &ppc::data::value_t::array() const { +const ppc::data::array_t &ppc::data::value_t::array() const { if (is_array()) return *val.arr; else throw (std::string)"The value isn't an array."; } -ppc::data::map_t const &ppc::data::value_t::map() const { +const ppc::data::map_t &ppc::data::value_t::map() const { if (is_map()) return *val.map; else throw (std::string)"The value isn't a map."; } @@ -71,7 +67,7 @@ ppc::data::number_t ppc::data::value_t::number() const { if (is_number()) return val.num; else throw (std::string)"The value isn't a number."; } -ppc::data::string_t const &ppc::data::value_t::string() const { +const ppc::data::string_t &ppc::data::value_t::string() const { if (is_string()) return *val.str; else throw (std::string)"The value isn't a string."; } @@ -83,15 +79,15 @@ ppc::data::bool_t ppc::data::value_t::boolean() const { ppc::data::value_t::value_t() { this->type = type_t::Null; } -ppc::data::value_t::value_t(ppc::data::array_t const &val) { +ppc::data::value_t::value_t(const ppc::data::array_t &val) { this->type = type_t::Arr; this->val.arr = new array_t { val }; } -ppc::data::value_t::value_t(ppc::data::map_t const &val) { +ppc::data::value_t::value_t(const ppc::data::map_t &val) { this->type = type_t::Map; this->val.map = new map_t { val }; } -ppc::data::value_t::value_t(ppc::data::string_t const &val) { +ppc::data::value_t::value_t(const ppc::data::string_t &val) { this->type = type_t::Str; this->val.str = new string_t { val }; } @@ -103,7 +99,7 @@ ppc::data::value_t::value_t(ppc::data::number_t val) { this->type = type_t::Num; this->val.num = val; } -ppc::data::value_t::value_t(ppc::data::value_t const &other) { +ppc::data::value_t::value_t(const ppc::data::value_t &other) { type = other.type; switch (other.type) { case type_t::Map: @@ -137,6 +133,3 @@ ppc::data::value_t::~value_t() { } } -// ppc::data::value_t &ppc::data::value_t::operator=(ppc::data::value_t const &other) { -// return *this; -// } diff --git a/src/utils/message.cc b/src/utils/message.cc index cd68c42..8b49f57 100644 --- a/src/utils/message.cc +++ b/src/utils/message.cc @@ -29,7 +29,7 @@ bool messages::message_t::is_severe() const { } bool messages::msg_stack_t::is_failed() const { - for (auto const &msg : messages) { + for (const auto &msg : messages) { if (msg.is_severe()) return true; } return false; @@ -37,7 +37,7 @@ bool messages::msg_stack_t::is_failed() const { void messages::msg_stack_t::print(std::ostream &output, messages::message_t::level_t threshold, bool color_output) const { if (!messages.size()) return; - for (auto const &msg : messages) { + for (const auto &msg : messages) { if (msg.level < threshold) continue; std::string loc_readable = msg.location.to_string(); diff --git a/src/utils/strings.cc b/src/utils/strings.cc index 928608f..4982718 100644 --- a/src/utils/strings.cc +++ b/src/utils/strings.cc @@ -3,7 +3,7 @@ using namespace ppc; -std::vector str::split(std::string const &splittable, std::initializer_list splitters, bool remove_empty_entries) { +std::vector str::split(const std::string &splittable, std::initializer_list splitters, bool remove_empty_entries) { std::stringstream buff; std::vector res;