-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPrinter.cpp
More file actions
145 lines (123 loc) · 3.42 KB
/
Printer.cpp
File metadata and controls
145 lines (123 loc) · 3.42 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Printer.h"
#include "Exp.h"
#include "FuncDef.h"
#include "Program.h"
#include "Stmt.h"
#include "Visitor.h"
class ExpPrinter : public ExpVisitor
{
public:
ExpPrinter( std::ostream& out )
: m_out( out )
{
}
void Print( const Exp& exp ) { const_cast<Exp&>( exp ).Dispatch( *this ); }
void* Visit( BoolExp& exp ) override
{
m_out << (exp.GetValue() ? "true" : "false");
return nullptr;
}
void* Visit( IntExp& exp ) override
{
m_out << exp.GetValue();
return nullptr;
}
void* Visit( VarExp& exp ) override
{
m_out << exp.GetName();
return nullptr;
}
void* Visit( CallExp& exp ) override
{
m_out << exp.GetFuncName() << '(';
for( size_t i = 0; i < exp.GetArgs().size(); ++i )
{
if( i > 0 )
m_out << ", ";
exp.GetArgs()[i]->Dispatch( *this );
}
m_out << ')';
return nullptr;
}
private:
std::ostream& m_out;
};
class StmtPrinter : public StmtVisitor
{
public:
StmtPrinter( std::ostream& out )
: m_out( out )
{
}
void Print( const Stmt& stmt ) { const_cast<Stmt&>( stmt ).Dispatch( *this ); }
void Visit( CallStmt& stmt ) override { m_out << stmt.GetCallExp() << ';'; }
void Visit( AssignStmt& stmt ) override { m_out << stmt.GetVarName() << " = " << stmt.GetRvalue() << ';'; }
void Visit( DeclStmt& stmt ) override
{
m_out << *stmt.GetVarDecl();
if( stmt.HasInitExp() )
m_out << " = " << stmt.GetInitExp();
m_out << ';';
}
void Visit( ReturnStmt& stmt ) override { m_out << "return " << stmt.GetExp() << ';'; }
void Visit( SeqStmt& seq ) override
{
m_out << "{" << std::endl;
for( const StmtPtr& stmt : seq.Get() )
{
Print( *stmt );
m_out << std::endl;
}
m_out << "}";
}
void Visit( IfStmt& stmt ) override
{
m_out << "if (" << stmt.GetCondExp() << ")" << std::endl;
Print( stmt.GetThenStmt() );
if( stmt.HasElseStmt() )
{
m_out << std::endl << "else" << std::endl;
Print( stmt.GetElseStmt() );
}
}
void Visit( WhileStmt& stmt ) override
{
m_out << "while (" << stmt.GetCondExp() << ")" << std::endl;
Print( stmt.GetBodyStmt() );
}
private:
std::ostream& m_out;
};
std::ostream& operator<<( std::ostream& out, const Exp& exp )
{
ExpPrinter( out ).Print( exp );
return out;
}
std::ostream& operator<<( std::ostream& out, const Stmt& stmt )
{
StmtPrinter( out ).Print( stmt );
return out;
}
std::ostream& operator<<( std::ostream& out, const FuncDef& def )
{
out << ToString( def.GetReturnType() ) << ' ' << def.GetName() << '(';
for( size_t i = 0; i < def.GetParams().size(); ++i )
{
if( i > 0 )
out << ", ";
out << *def.GetParams()[i];
}
out << ')' << std::endl;
if( def.HasBody() )
out << def.GetBody();
return out;
}
std::ostream& operator<<( std::ostream& out, const Program& program )
{
for( const FuncDefPtr& funcDef : program.GetFunctions() )
{
if( funcDef->HasBody() )
out << *funcDef << std::endl;
}
return out;
}