summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/type/pretty-printer.cc
blob: a6f4a999c0a1306d0c0c7ed021016d4066d22797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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