mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:11:04 +01:00
Explorer: Remove PrintDepth and implement PrintIndent. (#3113)
This PR removes `PrintDepth` from statement and declaration. Implements `PrintIndent` for better indented formatting along with various changes to make printing of statements and declarations better. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
05e061fe96
commit
289d05813e
+90
-32
@@ -15,39 +15,102 @@ using llvm::cast;
|
||||
|
||||
Statement::~Statement() = default;
|
||||
|
||||
void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
if (depth == 0) {
|
||||
out << " ... ";
|
||||
return;
|
||||
void Statement::PrintID(llvm::raw_ostream& out) const {
|
||||
switch (kind()) {
|
||||
case StatementKind::Match:
|
||||
out << "match (...) { ... }";
|
||||
break;
|
||||
case StatementKind::While:
|
||||
out << "while (...) { ... }";
|
||||
break;
|
||||
case StatementKind::For:
|
||||
out << "for (...) { ... }";
|
||||
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 ...";
|
||||
if (var.has_init()) {
|
||||
out << " = ...";
|
||||
}
|
||||
out << ";";
|
||||
break;
|
||||
}
|
||||
case StatementKind::ExpressionStatement:
|
||||
out << "<expression>;";
|
||||
break;
|
||||
case StatementKind::Assign: {
|
||||
const auto& assign = cast<Assign>(*this);
|
||||
out << "... " << AssignOperatorToString(assign.op()) << " ...;";
|
||||
break;
|
||||
}
|
||||
case StatementKind::IncrementDecrement: {
|
||||
const auto& inc_dec = cast<IncrementDecrement>(*this);
|
||||
out << (inc_dec.is_increment() ? "++" : "--") << "...;";
|
||||
break;
|
||||
}
|
||||
case StatementKind::If: {
|
||||
const auto& if_stmt = cast<If>(*this);
|
||||
out << "if (...) { ... }";
|
||||
if (if_stmt.else_block()) {
|
||||
out << " else { ... }";
|
||||
}
|
||||
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 ...;";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case StatementKind::Block:
|
||||
out << "{ ... }";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Statement::PrintIndent(int indent_num_spaces,
|
||||
llvm::raw_ostream& out) const {
|
||||
out.indent(indent_num_spaces);
|
||||
|
||||
switch (kind()) {
|
||||
case StatementKind::Match: {
|
||||
const auto& match = cast<Match>(*this);
|
||||
out << "match (" << match.expression() << ") {";
|
||||
if (depth < 0 || depth > 1) {
|
||||
out << "match (" << match.expression() << ") {\n";
|
||||
for (const auto& clause : match.clauses()) {
|
||||
out.indent(indent_num_spaces + 2)
|
||||
<< "case " << clause.pattern() << " =>\n";
|
||||
clause.statement().PrintIndent(indent_num_spaces + 2, out);
|
||||
out << "\n";
|
||||
for (const auto& clause : match.clauses()) {
|
||||
out << "case " << clause.pattern() << " =>\n";
|
||||
clause.statement().PrintDepth(depth - 1, out);
|
||||
out << "\n";
|
||||
}
|
||||
} else {
|
||||
out << "...";
|
||||
}
|
||||
out << "}";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
case StatementKind::While: {
|
||||
const auto& while_stmt = cast<While>(*this);
|
||||
out << "while (" << while_stmt.condition() << ")\n";
|
||||
while_stmt.body().PrintDepth(depth - 1, out);
|
||||
while_stmt.body().PrintIndent(indent_num_spaces, 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);
|
||||
for_stmt.body().PrintIndent(indent_num_spaces, out);
|
||||
break;
|
||||
}
|
||||
case StatementKind::Break:
|
||||
@@ -79,16 +142,18 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
}
|
||||
case StatementKind::IncrementDecrement: {
|
||||
const auto& inc_dec = cast<IncrementDecrement>(*this);
|
||||
out << (inc_dec.is_increment() ? "++" : "--") << inc_dec.argument();
|
||||
out << (inc_dec.is_increment() ? "++" : "--") << inc_dec.argument()
|
||||
<< ";";
|
||||
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_stmt.then_block().PrintIndent(indent_num_spaces, out);
|
||||
if (if_stmt.else_block()) {
|
||||
out << "\nelse\n";
|
||||
(*if_stmt.else_block())->PrintDepth(depth - 1, out);
|
||||
out << "\n";
|
||||
out.indent(indent_num_spaces) << "else\n";
|
||||
(*if_stmt.else_block())->PrintIndent(indent_num_spaces, out);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -107,20 +172,13 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
}
|
||||
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) {
|
||||
const auto statements = block.statements();
|
||||
out << "{\n";
|
||||
for (const auto* statement : statements) {
|
||||
statement->PrintIndent(indent_num_spaces + 2, out);
|
||||
out << "\n";
|
||||
}
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user