Commit c89587e6 authored by s_kleplj's avatar s_kleplj
Browse files

did some work on the first HW

parent e3494744
......@@ -12,7 +12,7 @@ bool macroprocessor_impl::process(
for (
auto [i, j] = std::tuple{s.begin(), s.end()};;
++i;
++i
) {
if (j != s.end()) {
if (i == s.end()) {
......@@ -32,37 +32,59 @@ bool macroprocessor_impl::process(
if (parsed.size() > 1 && parsed[1] == "=") {
if (parsed.size() != 2) {
auto tmp = std::vector<word*> {};
tmp.reserve(parsed - 2);
tmp.reserve(parsed.size() - 2);
for (auto i = parsed.begin() + 2; i != parse.end(); i++) {
auto it = word_.find(*i);
for (auto i = parsed.begin() + 2; i != parsed.end(); i++) {
auto it = words_.find(*i);
if (it != words_.end()) {
tmp.emplace_back(*i);
tmp.emplace_back(&it->second);
} else {
tmp.emplace_back(
std::string {parsed[0]},
words_.emplace(std::string {parsed[0]}, tmp).second);
auto w_it = words_.emplace(parsed[0], std::string{parsed[0]}).first;
const_cast<std::string_view&>(w_it->first) = w_it->second.name_;
tmp.emplace_back(&w_it->second);
}
}
words_.emplace(std::string {parsed[0]}, tmp);
auto it = words_.find(parsed[0]);
if (it == words_.end()) {
auto w_it = words_.emplace(
std::piecewise_construct,
std::forward_as_tuple(parsed[0]),
std::forward_as_tuple(std::string {parsed[0]}, std::move(tmp))).first;
const_cast<std::string_view&>(w_it->first) = w_it->second.name_;
} else {
it->second.value_ = std::move(tmp);
}
} else {
words_.emplace(std::string {parsed[0]}, std::vector<word*> {});
auto it = words_.find(parsed[0]);
if (it == words_.end()) {
auto w_it = words_.emplace(
std::piecewise_construct,
std::forward_as_tuple(parsed[0]),
std::forward_as_tuple(std::string {parsed[0]}, std::vector<word*>{})).first;
const_cast<std::string_view&>(w_it->first) = w_it->second.name_;
} else {
it->second.value_ = {};
}
}
return false;
} else {
for (auto&& p : parsed) {
auto it = words_.find(p);
if (it != words_.end()) {
output.reserve(output.size() + it->cached_length);
it->append_to(output);
output.reserve(output.size() + it->second.cached_length);
it->second.append_to(output);
} else {
it += *p;
output += p;
}
}
return true;
}
return true;
}
template class macroprocessor< policy_scalar>;
......
......@@ -38,54 +38,43 @@ class macroprocessor_impl {
bool process(const std::string& s, std::string& output);
private:
struct compare {
bool operator()(
const std::string& lhs,
const std::string& rhs) const {
return lhs < rhs;
}
bool operator()(
const std::string_view& lhs,
const std::string& rhs) const {
return lhs < rhs;
}
};
struct word {
word(std::string_view sv) {
value_ = sv;
cached_length = sv.size();
}
word(std::vector<word*>&& vector) {
value_ = vector;
cached_length = 0;
word(std::string&& name) noexcept :
name_{std::move(name)},
cached_length{name_.size()},
value_{}
{}
word(std::string&& name, std::vector<word*>&& vector) noexcept :
name_{std::move(name)},
value_{std::move(vector)},
cached_length{0}
{
for (const word* i : vector) {
cached_length += i->cached_length;
}
}
void append_to(std::string&& output) {
std::visit([](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::string_view>) {
output += arg;
} else if constexpr (std::is_same_v<T, std::vector<word*>) {
void append_to(std::string& output) {
if (cached_length != 0) {
if (value_.size() != 0) {
cached_length = 0;
for (word* w : arg) {
for (word* w : value_) {
w->append_to(output);
cached_length += w->cached_length;
}
} else {
output += name_;
}
}, value_);
}
}
std::variant<std::string_view, std::vector<word*>> value_;
std::string name_;
std::vector<word*> value_;
std::size_t cached_length;
};
std::map<std::string, word, compare> words_;
std::map<std::string_view, word> words_;
};
template< typename policy>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment