Skip to content

Commit 783312e

Browse files
committed
Add method chaining example to regression tests
1 parent 79120b3 commit 783312e

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <vector>
2+
3+
add: (in v : _, in a : int) -> auto = {
4+
cv := v;
5+
for cv do :(inout e : _) = {
6+
e += a;
7+
}
8+
return cv;
9+
}
10+
11+
dbl: (in v : _) -> auto = {
12+
cv := v;
13+
for cv do :(inout e : _) = {
14+
e *= 2;
15+
}
16+
return cv;
17+
}
18+
19+
main: () -> int = {
20+
21+
v : std::vector<int> = (1,2,3,4,5,6,7,8,9,10);
22+
23+
for v.dbl().add(1).dbl() do :(in e : _) = {
24+
std::cout << e << ", ";
25+
}
26+
27+
std::cout << std::endl;
28+
}

source/cppfront.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,10 +1659,18 @@ class cppfront
16591659

16601660
// Otherwise, do the UFCS work...
16611661

1662+
auto find_id_of_a_last = [](auto& ops, auto op){
1663+
for (int i = std::ssize(ops)-1; i>0; --i) {
1664+
if (ops[i].op->type() == op)
1665+
return i;
1666+
}
1667+
return 0;
1668+
};
1669+
16621670
// If method are chained we need to go from the last to the first
16631671
// token.a(a-expr-list).b(b-expr-list).c(c-expr-list) will be tranformed to:
16641672
// CPP2_UFCS(c, CPP2_UFCS(b, CPP2_UFCS(a,token, a-expr-list), b-expr-list), c-expr-list )
1665-
for (auto i = std::ssize(n.ops)-1; i > 0; i -= 2)
1673+
for (auto i = find_id_of_a_last(n.ops, lexeme::LeftParen); i > 0; i -= 2)
16661674
{
16671675
// The . has its id_expr
16681676
assert (n.ops[i-1].id_expr);
@@ -1690,7 +1698,7 @@ class cppfront
16901698
}
16911699

16921700
// expr-list need to be added in reversed order then CPP2_UFCS macros
1693-
for (auto i = 0; i < std::ssize(n.ops); i += 2) {
1701+
for (auto i = 0; i < find_id_of_a_last(n.ops, lexeme::LeftParen); i += 2) {
16941702
// Then make the base expression the second argument - only needed on the most nested call
16951703
if (i == 0) {
16961704
emit(*n.expr);

0 commit comments

Comments
 (0)