forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.cpp
140 lines (133 loc) · 3.79 KB
/
statement.cpp
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
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/ast/statement.h"
#include "common/check.h"
#include "explorer/common/arena.h"
#include "llvm/Support/Casting.h"
namespace Carbon {
using llvm::cast;
Statement::~Statement() = default;
void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
if (depth == 0) {
out << " ... ";
return;
}
switch (kind()) {
case StatementKind::Match: {
const auto& match = cast<Match>(*this);
out << "match (" << match.expression() << ") {";
if (depth < 0 || depth > 1) {
out << "\n";
for (auto& clause : match.clauses()) {
out << "case " << clause.pattern() << " =>\n";
clause.statement().PrintDepth(depth - 1, out);
out << "\n";
}
} else {
out << "...";
}
out << "}";
break;
}
case StatementKind::While: {
const auto& while_stmt = cast<While>(*this);
out << "while (" << while_stmt.condition() << ")\n";
while_stmt.body().PrintDepth(depth - 1, out);
break;
}
case StatementKind::For: {
const auto& for_stmt = cast<For>(*this);
out << "for (" << for_stmt.variable_declaration() << " in "
<< for_stmt.loop_target() << ")\n";
for_stmt.body().PrintDepth(depth - 1, out);
break;
}
case StatementKind::Break:
out << "break;";
break;
case StatementKind::Continue:
out << "continue;";
break;
case StatementKind::VariableDefinition: {
const auto& var = cast<VariableDefinition>(*this);
if (var.is_returned()) {
out << "returned ";
}
out << "var " << var.pattern();
if (var.has_init()) {
out << " = " << var.init();
}
out << ";";
break;
}
case StatementKind::ExpressionStatement:
out << cast<ExpressionStatement>(*this).expression() << ";";
break;
case StatementKind::Assign: {
const auto& assign = cast<Assign>(*this);
out << assign.lhs() << " = " << assign.rhs() << ";";
break;
}
case StatementKind::If: {
const auto& if_stmt = cast<If>(*this);
out << "if (" << if_stmt.condition() << ")\n";
if_stmt.then_block().PrintDepth(depth - 1, out);
if (if_stmt.else_block()) {
out << "\nelse\n";
(*if_stmt.else_block())->PrintDepth(depth - 1, out);
}
break;
}
case StatementKind::ReturnVar: {
out << "return var;";
break;
}
case StatementKind::ReturnExpression: {
const auto& ret = cast<ReturnExpression>(*this);
if (ret.is_omitted_expression()) {
out << "return;";
} else {
out << "return " << ret.expression() << ";";
}
break;
}
case StatementKind::Block: {
const auto& block = cast<Block>(*this);
out << "{";
if (depth < 0 || depth > 1) {
out << "\n";
}
for (const auto* statement : block.statements()) {
statement->PrintDepth(depth, out);
if (depth < 0 || depth > 1) {
out << "\n";
}
}
out << "}";
if (depth < 0 || depth > 1) {
out << "\n";
}
break;
}
case StatementKind::Continuation: {
const auto& cont = cast<Continuation>(*this);
out << "continuation " << cont.name() << " ";
if (depth < 0 || depth > 1) {
out << "\n";
}
cont.body().PrintDepth(depth - 1, out);
if (depth < 0 || depth > 1) {
out << "\n";
}
break;
}
case StatementKind::Run:
out << "run " << cast<Run>(*this).argument() << ";";
break;
case StatementKind::Await:
out << "await;";
break;
}
}
} // namespace Carbon