Skip to content

Commit 4ab0643

Browse files
committed
Fix removing parentheses when using deducing assignement
The issue is caused by `emit(expression_list_node)` that skips parentheses when node is inside initializer - that serves cases like: ```cpp v : std::vector<int> = (1,2,3); ``` Where it generates: ```cpp std::vector<int> v{1,2,3}; ``` When `:=` is used in the following cases: ```cpp d := (1 + 2) * (3 + 4) * (5 + 6); ``` It removes first parentheses and we end up with: ```cpp auto d = {1 + 2 * (3 + 4) * ( 5 + 6)}; ``` This change corrects this behaviour on the parsing side. After parsing expression list it checks if the next lexeme is `Semicolon`. If it is it means that we are on the initializer of the form: ```cpp d1 := ((2 + 1) * (4 - 1) * (8 - 3)); d3 := (move d2); v : std::vector<int> = (); ``` And we can suppres printing of parentheses - as there will be braces: ```cpp auto d1 {(2 + 1) * (4 - 1) * (8 - 3)}; auto d3 {std::move(d2)}; std::vector<int> v {}; ``` When next lexeme is not `Semicolon` it means that we are in initializer of the form: ```cpp d2 := (2 + 1) * (4 - 1) * (8 - 3); d4 : _ = (1 + 2) * (3 + 4) * (5 + 6); d5 : int = (1 + 2) * (3 + 4) * (5 + 6); ``` And we need to keep all the parentheses and it will be generates to: ```cpp auto d2 {(2 + 1) * (4 - 1) * (8 - 3)}; auto d4 {(1 + 2) * (3 + 4) * (5 + 6)}; int d5 {(1 + 2) * (3 + 4) * (5 + 6)}; ```
1 parent 0982b8e commit 4ab0643

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

source/parse.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2852,7 +2852,9 @@ class parser
28522852
// || curr().type() == lexeme::LeftBrace
28532853
)
28542854
{
2855-
bool inside_initializer = (peek(-1)->type() == lexeme::Assignment);
2855+
bool inside_initializer = (
2856+
peek(-1) && peek(-1)->type() == lexeme::Assignment
2857+
);
28562858
auto open_paren = &curr();
28572859
auto close = close_paren_type(open_paren->type());
28582860
auto close_text = [&] () -> std::string { if (close == lexeme::RightParen) { return ")"; } return "}"; }();
@@ -2870,6 +2872,9 @@ class parser
28702872
}
28712873
expr_list->close_paren = &curr();
28722874
next();
2875+
if (curr().type() != lexeme::Semicolon) {
2876+
expr_list->inside_initializer = false;
2877+
}
28732878
n->expr = std::move(expr_list);
28742879
return n;
28752880
}

0 commit comments

Comments
 (0)