diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /tiger-compiler/src/type/pretty-printer.cc | |
Diffstat (limited to 'tiger-compiler/src/type/pretty-printer.cc')
| -rw-r--r-- | tiger-compiler/src/type/pretty-printer.cc | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/tiger-compiler/src/type/pretty-printer.cc b/tiger-compiler/src/type/pretty-printer.cc new file mode 100644 index 0000000..a6f4a99 --- /dev/null +++ b/tiger-compiler/src/type/pretty-printer.cc @@ -0,0 +1,127 @@ +/** + ** \file type/pretty-printer.cc + ** \brief Implementation for type/pretty-printer.hh. + */ + +#include <type/libtype.hh> +#include <type/pretty-printer.hh> +#include <type/type.hh> +#include <type/types.hh> + +namespace type +{ + namespace + { + template <typename Type> + std::ostream& print_type(std::ostream& ostr, const Type& type) + { + PrettyPrinter printer{ostr}; + printer(type); + return ostr; + } + + /// How many times did we go through operator()(const Named&)? + inline long int& indent(std::ostream& o) + { + // The slot to store the current indentation level. + static const int indent_index = std::ios::xalloc(); + return o.iword(indent_index); + } + + } // namespace + + std::ostream& operator<<(std::ostream& ostr, const Attribute& e) + { + return print_type(ostr, e); + } + + std::ostream& operator<<(std::ostream& ostr, const Field& e) + { + return print_type(ostr, e); + } + + std::ostream& operator<<(std::ostream& ostr, const Type& e) + { + return print_type(ostr, e); + } + + PrettyPrinter::PrettyPrinter(std::ostream& ostr) + : ostr_{ostr} + {} + + void PrettyPrinter::operator()(const Nil& e) + { + ostr_ << "nil = "; + if (auto record_type = e.record_type_get()) + ostr_ << *record_type; + else + ostr_ << "(null)"; + } + + void PrettyPrinter::operator()(const Void&) { ostr_ << "void"; } + + void PrettyPrinter::operator()(const Int&) + { + // FIXME DONE: Some code was deleted here. + ostr_ << "int"; + } + + void PrettyPrinter::operator()(const String&) + { + // FIXME DONE: Some code was deleted here. + ostr_ << "string"; + } + + void PrettyPrinter::operator()(const Named& e) + { + // FIXME DONE: Some code was deleted here. + if (const misc::symbol& name = e.name_get(); + name != "int" && name != "string") + ostr_ << name << ": "; + + super_type::operator()(e); + } + + void PrettyPrinter::operator()(const Array& e) + { + // FIXME DONE: Some code was deleted here. + ostr_ << "array of "; + super_type::operator()(e); + } + + void PrettyPrinter::operator()(const Record& e) + { + // FIXME DONE: Some code was deleted here. + ostr_ << "record {" << misc::incendl; + super_type::operator()(e); + ostr_ << misc::decendl << "}"; + } + + void PrettyPrinter::operator()(const Class& e) + { + // FIXME DONE: Some code was deleted here. + ostr_ << "class {" << misc::incendl; + super_type::operator()(e); + ostr_ << misc::decendl << "}"; + } + + void PrettyPrinter::operator()(const Function& e) + { + // FIXME DONE: Some code was deleted here. + ostr_ << "function ("; + e.formals_get().accept(*this); + ostr_ << ") -> "; + e.result_get().accept(*this); + } + + void PrettyPrinter::operator()(const Attribute& e) + { + ostr_ << e.name_get() << " : " << e.type_get(); + } + + void PrettyPrinter::operator()(const Field& e) + { + ostr_ << e.name_get() << " : " << e.type_get(); + } + +} // namespace type |
