refactor: switched from type const & to const type & syntax

This commit is contained in:
TopchetoEU 2022-09-21 09:37:50 +03:00
parent 7d8c97b235
commit be734b47c3
No known key found for this signature in database
GPG Key ID: 0F2543CA49C81E3A
17 changed files with 64 additions and 71 deletions

View File

@ -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

View File

@ -21,7 +21,7 @@ namespace ppc::comp::tree::lex {
std::string data;
ppc::location_t location;
static std::vector<token_t> parse_file(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::istream &f);
static std::vector<token_t> parse_many(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::string const &src);
static std::vector<token_t> parse_file(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, std::istream &f);
static std::vector<token_t> parse_many(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, const std::string &src);
};
}

View File

@ -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<char> const &val, location_t loc = NO_LOCATION) {
token_t(const std::vector<char> &val, location_t loc = NO_LOCATION) {
kind = STRING;
data.string_literal = new std::vector<char> { 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<token_t> parse_many(messages::msg_stack_t &msg_stack, std::vector<lex::token_t> 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);
}

View File

@ -8,11 +8,11 @@ namespace ppc::lang {
std::vector<std::string> 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);
}

View File

@ -16,21 +16,21 @@ namespace ppc::lang {
// A list of all the defined fields inside the namespace
std::vector<field_t> 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 <class T>
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 <class T>
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);
}

View File

@ -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;

View File

@ -4,12 +4,12 @@
#include <string>
namespace ppc::str {
std::vector<std::string> split(std::string const &splittable, std::initializer_list<char> splitters, bool remove_empty_entries = false);
std::vector<std::string> split(const std::string &splittable, std::initializer_list<char> splitters, bool remove_empty_entries = false);
std::string trim(std::string splittable, std::initializer_list<char> 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());
}
}

View File

@ -21,7 +21,7 @@ namespace ppc::threading {
thread_t(void *handle) { this->handle = handle; }
template <class T>
inline static thread_t start(thread_func_t<T> func, T const &args) {
inline static thread_t start(thread_func_t<T> func, const T &args) {
T _args = args;
return start_impl((void*)func, &_args);
}

View File

@ -330,7 +330,7 @@ const lexlet_t LEXLET_MULTICOMMENT = {
.type = token_t::NONE,
};
std::vector<token_t> token_t::parse_many(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::string const &src) {
std::vector<token_t> token_t::parse_many(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, const std::string &src) {
std::vector<token_t> tokens;
std::vector<char> curr_token;
lexlet_t curr = LEXLET_DEFAULT;
@ -377,7 +377,7 @@ std::vector<token_t> 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> token_t::parse_many(ppc::messages::msg_stack_t &msg_stack,
return tokens;
}
std::vector<token_t> token_t::parse_file(ppc::messages::msg_stack_t &msg_stack, std::string const &filename, std::istream &f) {
std::vector<token_t> token_t::parse_file(ppc::messages::msg_stack_t &msg_stack, const std::string &filename, std::istream &f) {
std::vector<char> contents;
int c;

View File

@ -12,19 +12,19 @@ std::vector<std::string> 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++;
}

View File

@ -8,7 +8,7 @@ struct project_t {
std::vector<std::string> 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<char> 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;
}

View File

@ -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);
}
}

View File

@ -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<flag_t> 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);
};
}

View File

@ -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;

View File

@ -1,9 +1,5 @@
#include "data.hh"
template<typename ... Ts>
struct overload : Ts ... { using Ts::operator() ...; };
template<class... Ts> overload(Ts...) -> overload<Ts...>;
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;
// }

View File

@ -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();

View File

@ -3,7 +3,7 @@
using namespace ppc;
std::vector<std::string> str::split(std::string const &splittable, std::initializer_list<char> splitters, bool remove_empty_entries) {
std::vector<std::string> str::split(const std::string &splittable, std::initializer_list<char> splitters, bool remove_empty_entries) {
std::stringstream buff;
std::vector<std::string> res;