Joos1W Compiler Framework
All Classes Functions Typedefs Pages
Stmt.cc
1 #include <ostream>
2 #include <string>
3 
4 #include "ast/AST.h"
5 namespace ast {
6 
7 using std::ostream;
8 using std::string;
9 
10 std::pair<int, int> printStmtSubgraph(utils::DotPrinter& dp, ast::Stmt* stmt) {
11  int firstId = -1;
12  int subgraphId = -1;
13  if(auto block = dyn_cast_or_null<ast::BlockStatement*>(stmt)) {
14  subgraphId = dp.id();
15  dp.startSubgraph(subgraphId);
16  dp.print("label=\"Statement body\"");
17  dp.print("color=lightblue");
18  firstId = printDotNodeList(dp, block->stmts());
19  if(firstId == -1) {
20  firstId = dp.id();
21  dp.printLabel(firstId, "Empty Body");
22  }
24  } else {
25  firstId = stmt->printDotNode(dp);
26  }
27  return {firstId, subgraphId};
28 }
29 
30 // BlockStatement //////////////////////////////////////////////////////////////
31 
32 std::ostream& BlockStatement::print(std::ostream& os, int indentation) const {
33  auto i1 = indent(indentation);
34  if(stmts().empty()) {
35  return os << i1 << "BlockStatement {}\n";
36  }
37  os << i1 << "BlockStatement {\n";
38  for(auto stmt : stmts()) {
39  stmt->print(os, indentation + 1) << "\n";
40  }
41  os << i1 << "}\n";
42  return os;
43 }
44 
45 int BlockStatement::printDotNode(DotPrinter& dp) const {
46  int id = dp.id();
47  dp.printLabel(id, "BlockStatement");
48  for(auto stmt : stmts_) {
49  dp.printConnection(id, stmt->printDotNode(dp));
50  }
51  return id;
52 }
53 
54 // DeclStmt ////////////////////////////////////////////////////////////////////
55 
56 std::ostream& DeclStmt::print(std::ostream& os, int indentation) const {
57  (void)indentation;
58  return os;
59 }
60 
61 int DeclStmt::printDotNode(DotPrinter& dp) const {
62  return decl_->printDotNode(dp);
63 }
64 
65 // ExprStmt ////////////////////////////////////////////////////////////////////
66 
67 std::ostream& ExprStmt::print(std::ostream& os, int indentation) const {
68  expr_->print(os, indentation);
69  return os;
70 }
71 
72 int ExprStmt::printDotNode(DotPrinter& dp) const {
73  auto id = dp.id();
74  std::ostringstream expr;
75  expr_->print(expr, -1);
76  dp.startTLabel(id);
77  dp.printTableSingleRow("ExprStmt", {"bgcolor", "lightblue"});
78  dp.printTableSingleRow(expr.str(), {"balign", "left"});
79  dp.endTLabel();
80  return id;
81 }
82 
83 // IfStmt //////////////////////////////////////////////////////////////////////
84 
85 std::ostream& IfStmt::print(std::ostream& os, int indentation) const {
86  // Implementation for IfStmt print
87  (void)indentation;
88  return os;
89 }
90 
91 int IfStmt::printDotNode(DotPrinter& dp) const {
92  auto id = dp.id();
93  std::ostringstream expr;
94  condition_->print(expr, -1);
95  dp.startTLabel(id);
96  dp.printTableSingleRow("IfStmt", {"bgcolor", "lightblue"});
98  "condition", expr.str(), {"port", "condition"}, {"balign", "left"});
99  dp.printTableDoubleRow("then", "else", {"port", "then"}, {"port", "else"});
100  dp.endTLabel();
101  // True branch
102  auto ids = printStmtSubgraph(dp, thenStmt_);
103  if(ids.second != -1)
104  dp.printConnection(id, ":then:c", ids.first, ids.second);
105  else
106  dp.printConnection(id, ":then:c", ids.first);
107  // False branch
108  if(elseStmt_) {
109  ids = printStmtSubgraph(dp, elseStmt_);
110  if(ids.second != -1)
111  dp.printConnection(id, ":else:c", ids.first, ids.second);
112  else
113  dp.printConnection(id, ":else:c", ids.first);
114  }
115  return id;
116 }
117 
118 // WhileStmt ///////////////////////////////////////////////////////////////////
119 
120 std::ostream& WhileStmt::print(std::ostream& os, int indentation) const {
121  // Implementation for WhileStmt print
122  (void)indentation;
123  return os;
124 }
125 
126 int WhileStmt::printDotNode(DotPrinter& dp) const {
127  auto id = dp.id();
128  dp.startTLabel(id);
129  dp.printTableSingleRow("WhileStmt", {"bgcolor", "lightblue"});
131  "condition", "body", {"port", "condition"}, {"port", "body"});
132  dp.endTLabel();
133  // dp.printConnection(id, ":condition", condition_->printDotNode(dp));
134  auto ids = printStmtSubgraph(dp, body_);
135  if(ids.second != -1)
136  dp.printConnection(id, ":body", ids.first, ids.second);
137  else
138  dp.printConnection(id, ":body", ids.first);
139  return id;
140 }
141 
142 // ForStmt /////////////////////////////////////////////////////////////////////
143 
144 std::ostream& ForStmt::print(std::ostream& os, int indentation) const {
145  // Implementation for ForStmt print
146  (void)indentation;
147  return os;
148 }
149 
150 int ForStmt::printDotNode(DotPrinter& dp) const {
151  auto id = dp.id();
152  std::ostringstream expr;
153  if(condition_) condition_->print(expr, -1);
154  dp.startTLabel(id);
155  dp.printTableSingleRow("ForStmt", {"bgcolor", "lightblue"});
157  "condition", expr.str(), {"port", "condition"}, {"balign", "left"});
158  dp.printTableTripleRow("init",
159  "update",
160  "body",
161  {"port", "init"},
162  {"port", "update"},
163  {"port", "body"});
164  dp.endTLabel();
165  if(init_) dp.printConnection(id, ":init", init_->printDotNode(dp));
166  if(update_) dp.printConnection(id, ":update", update_->printDotNode(dp));
167  auto ids = printStmtSubgraph(dp, body_);
168  if(ids.second != -1)
169  dp.printConnection(id, ":body", ids.first, ids.second);
170  else
171  dp.printConnection(id, ":body", ids.first);
172  return id;
173 }
174 
175 // ReturnStmt //////////////////////////////////////////////////////////////////
176 
177 std::ostream& ReturnStmt::print(std::ostream& os, int indentation) const {
178  (void)indentation;
179  return os;
180 }
181 
182 int ReturnStmt::printDotNode(DotPrinter& dp) const {
183  auto id = dp.id();
184  dp.startTLabel(id);
185  dp.printTableSingleRow("ReturnStmt", {"bgcolor", "lightblue"});
186  if(expr_) {
187  std::ostringstream expr;
188  expr_->print(expr, -1);
190  "expr", expr.str(), {"port", "condition"}, {"balign", "left"});
191  }
192  dp.endTLabel();
193  return id;
194 }
195 
196 // NullStmt ////////////////////////////////////////////////////////////////////
197 
198 std::ostream& NullStmt::print(std::ostream& os, int indentation) const {
199  (void)indentation;
200  return os;
201 }
202 
203 int NullStmt::printDotNode(DotPrinter& dp) const {
204  auto id = dp.id();
205  dp.printLabel(id, "NullStmt");
206  return id;
207 }
208 
209 } // namespace ast