diff options
Diffstat (limited to 'tiger-compiler/lib/misc/scoped-map.hh')
| -rw-r--r-- | tiger-compiler/lib/misc/scoped-map.hh | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tiger-compiler/lib/misc/scoped-map.hh b/tiger-compiler/lib/misc/scoped-map.hh new file mode 100644 index 0000000..a193e24 --- /dev/null +++ b/tiger-compiler/lib/misc/scoped-map.hh @@ -0,0 +1,51 @@ +/** + ** \file misc/scoped-map.hh + ** \brief Declaration of misc::scoped_map. + ** + ** This implements a stack of dictionnaries. Each time a scope is + ** opened, a new dictionnary is added on the top of the stack; the + ** dictionary is removed when the scope is closed. Lookup of keys + ** is done in the last added dictionnary first (LIFO). + ** + ** In particular this class is used to implement symbol tables. + **/ + +#pragma once + +#include <map> +#include <ostream> +#include <stack> +#include <vector> + +namespace misc +{ + template <typename Key, typename Data> class scoped_map + { + // FIXME DONE: Some code was deleted here. + public: + // initializes a new stack with an empty map on top as the first scope + scoped_map() + { + scopes_.push_back(std::map<Key,Data>()); + } + void put(const Key& key, const Data& value); + Data get(const Key& key) const requires(!std::is_pointer_v<Data>); + Data get(const Key& key) const requires(std::is_pointer_v<Data>); + std::ostream& dump(std::ostream& ostr) const; + void scope_begin(); + void scope_end(); + + private: + std::vector<std::map<Key, Data>> scopes_; + }; + + template <typename Key, typename Data> + std::ostream& operator<<(std::ostream& ostr, + const scoped_map<Key, Data>& tbl); + + // FIXME DONE: Some code was deleted here. + // heehee + +} // namespace misc + +#include <misc/scoped-map.hxx> |
