From ecd18a51c3e9ee01673e8c7320d39c566e4ad3cb Mon Sep 17 00:00:00 2001 From: JaDogg Date: Fri, 3 May 2024 22:46:14 +0100 Subject: [PATCH] refactor(ykdatatype): change recursive stringify to non recursive --- compiler/src/utilities/ykdatatype.cpp | 43 +++++++++++++++++++-------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/compiler/src/utilities/ykdatatype.cpp b/compiler/src/utilities/ykdatatype.cpp index 1af08acb..7127b85a 100644 --- a/compiler/src/utilities/ykdatatype.cpp +++ b/compiler/src/utilities/ykdatatype.cpp @@ -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> 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(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(nullptr, ", ")); + } } - arg->write_to_str(s, write_mod); + stack.emplace_back( + std::make_pair(nullptr, "[")); } - s << "]"; } } std::string ykdatatype::as_string() const {