Skip to content

Commit

Permalink
refactor(ykdatatype): change recursive stringify to non recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed May 3, 2024
1 parent 83b835b commit ecd18a5
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions compiler/src/utilities/ykdatatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,39 @@ bool ykdatatype::is_none() const {
return primitive_type_ == ykprimitive::NONE;
}
void ykdatatype::write_to_str(std::stringstream &s, bool write_mod) const {
if (!module_.empty() && write_mod && !is_dimension()) { s << module_ << ":::"; }
s << token_->token_;
if (!args_.empty()) {
s << "[";
bool first = true;
for (auto arg : args_) {
if (first) {
first = false;
} else {
s << ", ";
std::vector<std::pair<const ykdatatype *, std::string>> stack{};
stack.emplace_back(this, "");
while (!stack.empty()) {
auto item = stack.back();
const ykdatatype *dt = item.first;
auto text = item.second;
stack.pop_back();
// text node, write it
if (dt == nullptr) {
s << text;
continue;
}
// actual datatype or datatype arg (also a datatype)
if (!dt->module_.empty() && write_mod && !dt->is_dimension()) {
s << dt->module_ << ":::";
}
s << dt->token_->token_;
// arguments
if (!dt->args_.empty()) {
stack.emplace_back(
std::make_pair<const ykdatatype *, std::string>(nullptr, "]"));
int length = (int) dt->args_.size();
for (int i = length; i > 0; --i) {
const ykdatatype *arg = dt->args_[i - 1];
stack.emplace_back(arg, "");
if (i > 1) {
stack.emplace_back(
std::make_pair<const ykdatatype *, std::string>(nullptr, ", "));
}
}
arg->write_to_str(s, write_mod);
stack.emplace_back(
std::make_pair<const ykdatatype *, std::string>(nullptr, "["));
}
s << "]";
}
}
std::string ykdatatype::as_string() const {
Expand Down

0 comments on commit ecd18a5

Please sign in to comment.