From 967be9e750221ab2ab783f95df79bb26d290a45e Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:07:58 +0200 Subject: add: added projects --- tiger-compiler/lib/misc/scoped-map.hxx | 86 ++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tiger-compiler/lib/misc/scoped-map.hxx (limited to 'tiger-compiler/lib/misc/scoped-map.hxx') diff --git a/tiger-compiler/lib/misc/scoped-map.hxx b/tiger-compiler/lib/misc/scoped-map.hxx new file mode 100644 index 0000000..14258b9 --- /dev/null +++ b/tiger-compiler/lib/misc/scoped-map.hxx @@ -0,0 +1,86 @@ +/** \file misc/scoped-map.hxx + ** \brief Implementation of misc::scoped_map. + */ + +#include + +#pragma once + +#include +#include +#include + +#include +#include +#include +#include + +namespace misc +{ + // FIXME DONE: Some code was deleted here. + + template + void scoped_map::put(const Key& key, const Data& value) + { + scopes_.back().insert_or_assign(key, value); + } + + template + Data scoped_map::get(const Key& key) const + requires(std::is_pointer_v) + { + if (auto data = scopes_.back().find(key); data != scopes_.back().end()) + return data->second; + return nullptr; + } + + template + Data scoped_map::get(const Key& key) const + requires(!std::is_pointer_v) + { + if (auto data = scopes_.back().find(key); data != scopes_.back().end()) + return data->second; + throw std::range_error("Key is not in scope."); + } + + template + std::ostream& scoped_map::dump(std::ostream& ostr) const + { + ostr << "Printing\n"; + const char* sep = "-------------------------\n"; + int depth = 0; + for (auto scope : scopes_) + { + ostr << sep << "Scope " << depth++ << incendl; + for (auto kvp : scope) + { + ostr << kvp.first << " -> " << kvp.second << iendl; + } + ostr << decendl << sep; + } + ostr << "endprint\n"; + return ostr; + } + template + void scoped_map::scope_begin() + { + std::map new_scope; + for (auto kvp : scopes_.back()) + { + new_scope.insert_or_assign(kvp.first, kvp.second); + } + scopes_.push_back(new_scope); + } + template void scoped_map::scope_end() + { + scopes_.pop_back(); + } + + template + inline std::ostream& operator<<(std::ostream& ostr, + const scoped_map& tbl) + { + return tbl.dump(ostr); + } + +} // namespace misc -- cgit v1.2.3