summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/ast/pretty-printer.hh
blob: 753233107c4b4474ca749c6e474e59f7c5453b8c (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
/**
 ** \file ast/pretty-printer.hh
 ** \brief Declaration of ast::PrettyPrinter.
 */

#pragma once

#include <ast/assert-visitor.hh>
#include <ast/default-visitor.hh>
#include <ast/object-visitor.hh>

namespace ast
{
  /// Visit an Ast and print the content of each node.
  class PrettyPrinter
    : virtual public DefaultConstVisitor
    , virtual public ObjectConstVisitor
    , virtual public AssertConstVisitor
  {
  public:
    using super_type = DefaultConstVisitor;
    // Import overloaded virtual functions.
    using super_type::operator();

    /// Build to print on \a ostr.
    PrettyPrinter(std::ostream& ostr);

    /// Visit methods.
    /// \{
    void operator()(const SimpleVar& e) override;
    void operator()(const FieldVar& e) override;
    void operator()(const SubscriptVar& e) override;
    void operator()(const CastExp& e) override;
    // FIXME DONE: Some code was deleted here.
    void operator()(const NilExp& e) override;
    void operator()(const IntExp& e) override;
    void operator()(const StringExp& e) override;
    void operator()(const ObjectExp& e) override;
    void operator()(const CallExp& e) override;
    void operator()(const MethodCallExp& e) override;
    void operator()(const OpExp& e) override;
    void operator()(const RecordExp& e) override;
    void operator()(const SeqExp& e) override;
    void operator()(const AssignExp& e) override;
    void operator()(const IfExp& e) override;
    void operator()(const WhileExp& e) override;
    void operator()(const ForExp& e) override;
    void operator()(const BreakExp&) override;
    void operator()(const LetExp& e) override;
    void operator()(const ArrayExp& e) override;
    void operator()(const FieldInit& e) override;
    /// \}

    /// Visit Var declarations.
    void operator()(const VarChunk& e) override;
    void operator()(const VarDec& e) override;

    /// Visit Function declarations.
    void operator()(const FunctionChunk& e) override;
    void operator()(const FunctionDec& e) override;
    void operator()(const MethodChunk& e) override;
    void operator()(const MethodDec& e) override;

    /// Visit Type declarations.
    void operator()(const TypeChunk& e) override;
    void operator()(const TypeDec& e) override;

    /** \} */

    /** \name Visit Type related nodes.
     ** \{ */
    void operator()(const NameTy& e) override;
    void operator()(const RecordTy& e) override;
    void operator()(const ArrayTy& e) override;
    void operator()(const ClassTy& e) override;
    /** \} */

    /** \name Visit Field related nodes. */
    void operator()(const Field& e) override;

    /** \name Visit Assertion nodes. */
    void operator()(const AssertExp& e) override;

  private:
    // Factor pretty-printing of RecordExp and RecordTy.
    template <typename RecordClass> void print_record(const RecordClass& e);

    // Whether we are in a ast::ClassTy.
    bool within_classty_p_ = false;

  protected:
    /// The stream to print on.
    std::ostream& ostr_;
  };

} // namespace ast