fix: replace namespace_name_t implementation with vector inheritence

This commit is contained in:
TopchetoEU 2022-10-04 20:03:46 +03:00
parent 3c08cd13db
commit 1c552c2c5c
2 changed files with 18 additions and 14 deletions

View File

@ -14,21 +14,16 @@ namespace ppc::lang {
located_t(Args ...args): T(args...), location(location_t::NONE) { } located_t(Args ...args): T(args...), location(location_t::NONE) { }
}; };
struct namespace_name_t { struct namespace_name_t : public std::vector<std::string> {
std::vector<std::string> segments; using base = std::vector<std::string>;
bool is_empty() const { return segments.empty(); }
auto begin() { return segments.begin(); }
auto end() { return segments.end(); }
bool operator ==(const namespace_name_t &other) const; bool operator ==(const namespace_name_t &other) const;
const std::string &operator[](size_t i) const { return segments[i]; } bool operator !=(const namespace_name_t &other) const;
std::string to_string() const; std::string to_string() const;
namespace_name_t() { } namespace_name_t() { }
namespace_name_t(std::initializer_list<std::string> segments): segments(segments.begin(), segments.end()) { } namespace_name_t(std::initializer_list<std::string> segments): base(segments.begin(), segments.end()) { }
}; };
bool is_identifier_valid(messages::msg_stack_t &msg_stack, ppc::location_t location, const std::string &name); bool is_identifier_valid(messages::msg_stack_t &msg_stack, ppc::location_t location, const std::string &name);

View File

@ -6,19 +6,28 @@ namespace ppc::lang {
std::string namespace_name_t::to_string() const { std::string namespace_name_t::to_string() const {
std::stringstream res; std::stringstream res;
for (size_t i = 0; i < segments.size(); i++) { for (size_t i = 0; i < size(); i++) {
if (i != 0) res << "::"; if (i != 0) res << "::";
res << segments[i]; res << (*this)[i];
} }
return res.str(); return res.str();
} }
bool namespace_name_t::operator==(const namespace_name_t &other) const { bool namespace_name_t::operator==(const namespace_name_t &other) const {
if (other.segments.size() != segments.size()) return false; if (other.size() != size()) return false;
for (size_t i = 0; i < segments.size(); i++) { for (size_t i = 0; i < size(); i++) {
if (other[i] != segments[i]) return false; if (other[i] != (*this)[i]) return false;
}
return true;
}
bool namespace_name_t::operator!=(const namespace_name_t &other) const {
if (other.size() != size()) return true;
for (size_t i = 0; i < size(); i++) {
if (other[i] == (*this)[i]) return false;
} }
return true; return true;