Skip to content

Commit 9f7bd9f

Browse files
committed
Pretty-print an ordinary "else if" together without more indenting
1 parent 981e21c commit 9f7bd9f

File tree

3 files changed

+58
-46
lines changed

3 files changed

+58
-46
lines changed

regression-tests/test-results/pure2-autodiff.cpp2.output

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,16 +1396,13 @@ ad_test_2:/* @autodiff<"order=2"> @print */ type =
13961396
{
13971397
y = x * x;
13981398
}
1399+
else if x < 3
1400+
{
1401+
y = x + sin(x) + 10;
1402+
}
13991403
else
14001404
{
1401-
if x < 3
1402-
{
1403-
y = x + sin(x) + 10;
1404-
}
1405-
else
1406-
{
1407-
y = sin(x) * x * x;
1408-
}
1405+
y = sin(x) * x * x;
14091406
}
14101407
return;
14111408
}
@@ -1423,24 +1420,21 @@ ad_test_2:/* @autodiff<"order=2"> @print */ type =
14231420
y_d = x_d..mul(x_d, x, x);
14241421
y = x * x;
14251422
}
1423+
else if x < 3
1424+
{
1425+
temp_1_d: cpp2::taylor<double, 2> = x_d.sin(x);
1426+
temp_1: double = sin(x);
1427+
y_d = x_d + temp_1_d;
1428+
y = x + temp_1 + 10;
1429+
}
14261430
else
14271431
{
1428-
if x < 3
1429-
{
1430-
temp_1_d: cpp2::taylor<double, 2> = x_d.sin(x);
1431-
temp_1: double = sin(x);
1432-
y_d = x_d + temp_1_d;
1433-
y = x + temp_1 + 10;
1434-
}
1435-
else
1436-
{
1437-
temp_3_d: cpp2::taylor<double, 2> = x_d.sin(x);
1438-
temp_3: double = sin(x);
1439-
temp_4_d: _ = temp_3_d..mul(x_d, temp_3, x);
1440-
temp_4: _ = temp_3 * x;
1441-
y_d = temp_4_d..mul(x_d, temp_4, x);
1442-
y = temp_4 * x;
1443-
}
1432+
temp_3_d: cpp2::taylor<double, 2> = x_d.sin(x);
1433+
temp_3: double = sin(x);
1434+
temp_4_d: _ = temp_3_d..mul(x_d, temp_3, x);
1435+
temp_4: _ = temp_3 * x;
1436+
y_d = temp_4_d..mul(x_d, temp_4, x);
1437+
y = temp_4 * x;
14441438
}
14451439
return;
14461440
}

regression-tests/test-results/pure2-print.cpp2.output

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,13 @@ outer:/* @print */ type =
6464
{
6565
a();
6666
}
67+
else if !m.empty()
68+
{
69+
b();
70+
}
6771
else
6872
{
69-
if !m.empty()
70-
{
71-
b();
72-
}
73-
else
74-
{
75-
c();
76-
}
73+
c();
7774
}
7875
assert( true );
7976
return :() -> move std::string = (s + m[0])$;();

source/parse.h

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5453,9 +5453,9 @@ auto pretty_print_visualize(is_as_expression_node const& n, int indent)
54535453
-> std::string;
54545454
auto pretty_print_visualize(id_expression_node const& n, int indent)
54555455
-> std::string;
5456-
auto pretty_print_visualize(compound_statement_node const& n, int indent)
5456+
auto pretty_print_visualize(compound_statement_node const& n, int indent, bool after_else = false)
54575457
-> std::string;
5458-
auto pretty_print_visualize(selection_statement_node const& n, int indent)
5458+
auto pretty_print_visualize(selection_statement_node const& n, int indent, bool no_indent = false)
54595459
-> std::string;
54605460
auto pretty_print_visualize(iteration_statement_node const& n, int indent)
54615461
-> std::string;
@@ -5471,7 +5471,7 @@ auto pretty_print_visualize(jump_statement_node const& n, int indent)
54715471
-> std::string;
54725472
auto pretty_print_visualize(using_statement_node const& n, int indent)
54735473
-> std::string;
5474-
auto pretty_print_visualize(statement_node const& n, int indent)
5474+
auto pretty_print_visualize(statement_node const& n, int indent, bool no_indent = false)
54755475
-> std::string;
54765476
auto pretty_print_visualize(parameter_declaration_node const& n, int indent, bool is_template_param = false)
54775477
-> std::string;
@@ -5837,30 +5837,51 @@ auto pretty_print_visualize(id_expression_node const& n, int indent)
58375837
}
58385838

58395839

5840-
auto pretty_print_visualize(compound_statement_node const& n, int indent)
5840+
auto pretty_print_visualize(compound_statement_node const& n, int indent, bool after_else /* = false */ )
58415841
-> std::string
58425842
{
5843-
auto ret = std::string{"\n"} + pre(indent) + "{";
5843+
auto ret = std::string{};
58445844

5845-
for (auto& stmt : n.statements) {
5846-
assert (stmt);
5847-
ret += pretty_print_visualize(*stmt, indent+1);
5845+
// If this is just a plain "if" right after an "else", pull them together visually
5846+
if (
5847+
after_else
5848+
&& std::ssize(n.statements) == 1
5849+
&& n.statements[0]
5850+
&& n.statements[0]->is_selection()
5851+
&& n.statements[0]->get_parameters().empty()
5852+
)
5853+
{
5854+
ret += pretty_print_visualize(*n.statements[0], indent, true);
58485855
}
58495856

5850-
ret += std::string{"\n"} + pre(indent) + "}";
5857+
else
5858+
{
5859+
ret += std::string{"\n"} + pre(indent) + "{";
5860+
5861+
for (auto& stmt : n.statements) {
5862+
assert (stmt);
5863+
ret += pretty_print_visualize(*stmt, indent+1);
5864+
}
5865+
5866+
ret += std::string{"\n"} + pre(indent) + "}";
5867+
}
58515868

58525869
return ret;
58535870
}
58545871

58555872

5856-
auto pretty_print_visualize(selection_statement_node const& n, int indent)
5873+
auto pretty_print_visualize(selection_statement_node const& n, int indent, bool no_indent /* = false */)
58575874
-> std::string
58585875
{
58595876
assert (n.identifier && n.expression && n.true_branch && n.false_branch);
58605877

58615878
auto ret = std::string{};
58625879

5863-
ret += std::string{"\n"} + pre(indent) + n.identifier->as_string_view() + " ";
5880+
if (!no_indent) {
5881+
ret += std::string{"\n"} + pre(indent);
5882+
}
5883+
ret += n.identifier->as_string_view();
5884+
ret += " ";
58645885

58655886
if (n.is_constexpr) {
58665887
ret += "constexpr ";
@@ -5871,7 +5892,7 @@ auto pretty_print_visualize(selection_statement_node const& n, int indent)
58715892

58725893
if (n.has_source_false_branch) {
58735894
ret += std::string{"\n"} + pre(indent) + "else "
5874-
+ pretty_print_visualize(*n.false_branch, indent);
5895+
+ pretty_print_visualize(*n.false_branch, indent, true);
58755896
}
58765897

58775898
return ret;
@@ -6076,7 +6097,7 @@ auto pretty_print_visualize(using_statement_node const& n, int indent)
60766097
}
60776098

60786099

6079-
auto pretty_print_visualize(statement_node const& n, int indent)
6100+
auto pretty_print_visualize(statement_node const& n, int indent, bool no_indent /* = false */ )
60806101
-> std::string
60816102
{
60826103
auto ret = std::string{};
@@ -6097,7 +6118,7 @@ auto pretty_print_visualize(statement_node const& n, int indent)
60976118
}
60986119

60996120
ret += try_pretty_print_visualize<statement_node::compound >(n.statement, indent);
6100-
ret += try_pretty_print_visualize<statement_node::selection >(n.statement, indent);
6121+
ret += try_pretty_print_visualize<statement_node::selection >(n.statement, indent, no_indent);
61016122
ret += try_pretty_print_visualize<statement_node::declaration>(n.statement, indent);
61026123
ret += try_pretty_print_visualize<statement_node::return_ >(n.statement, indent);
61036124
ret += try_pretty_print_visualize<statement_node::iteration >(n.statement, indent);

0 commit comments

Comments
 (0)