Skip to content

Commit b035008

Browse files
committed
Add method chaining example to regression tests
1 parent 0f45579 commit b035008

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
@@ -1692,10 +1692,18 @@ class cppfront
16921692

16931693
// Otherwise, do the UFCS work...
16941694

1695+
auto find_id_of_a_last = [](auto& ops, auto op){
1696+
for (int i = std::ssize(ops)-1; i>0; --i) {
1697+
if (ops[i].op->type() == op)
1698+
return i;
1699+
}
1700+
return 0;
1701+
};
1702+
16951703
// If method are chained we need to go from the last to the first
16961704
// token.a(a-expr-list).b(b-expr-list).c(c-expr-list) will be tranformed to:
16971705
// CPP2_UFCS(c, CPP2_UFCS(b, CPP2_UFCS(a,token, a-expr-list), b-expr-list), c-expr-list )
1698-
for (auto i = std::ssize(n.ops)-1; i > 0; i -= 2)
1706+
for (auto i = find_id_of_a_last(n.ops, lexeme::LeftParen); i > 0; i -= 2)
16991707
{
17001708
// The . has its id_expr
17011709
assert (n.ops[i-1].id_expr);
@@ -1723,7 +1731,7 @@ class cppfront
17231731
}
17241732

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

0 commit comments

Comments
 (0)