/** ** \file type/pretty-printer.cc ** \brief Implementation for type/pretty-printer.hh. */ #include #include #include #include namespace type { namespace { template 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