Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sample. #27

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ Sample 1 for monadic do notation
Sample 2 for monad comprehension


[godbolt2]: https://godbolt.org/z/5oG8zGhTf
[godbolt2]: https://godbolt.org/z/M8Ynjvr3x

[![Try it on godbolt][badge.godbolt]][godbolt2]

```c++
using namespace hspp::doN;
using namespace hspp::data;
Id<int> i;
Id<int> j;
Id<int> k;
auto result = _(
Id<int> i, j, k;
auto const rng = _(
makeTuple<3> | i | j | k,
i <= (iota | 1 | 20),
j <= (iota | i | 20),
k <= (iota | j | 20),
k <= (enumFrom | 1),
i <= (iota | 1 | k),
j <= (iota | i | k),
if_ || (i*i + j*j == k*k)
);
```
Expand Down Expand Up @@ -88,7 +86,7 @@ extern TEParser<int> const expr;

Id<int> n;
auto const factor =
digit <triPlus>
digit <alt>
do_(
symb | "("s,
n <= expr,
Expand Down
10 changes: 5 additions & 5 deletions develop/include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using data::runParser;
using data::TEParser;
using data::toTEParser;

constexpr auto triPlus = toGFunc<2> | [](auto p, auto q)
constexpr auto alt = toGFunc<2> | [](auto p, auto q)
{
return data::toParser <o> data::toFunc<> | [=](std::string cs)
{
Expand Down Expand Up @@ -135,7 +135,7 @@ template <typename A, typename Repr>
constexpr auto manyImpl(Parser<A, Repr> p)
-> TEParser<std::vector<A>>
{
return toTEParser || triPlus | (many1 | p) | (Monad<Parser>::return_ | std::vector<A>{});
return toTEParser || alt | (many1 | p) | (Monad<Parser>::return_ | std::vector<A>{});
}

constexpr auto many = toGFunc<1> | [](auto p)
Expand All @@ -157,7 +157,7 @@ constexpr auto sepBy1 = toGFunc<2> | [](auto p, auto sep)
template <typename A, typename Repr, typename B, typename Repr1>
constexpr auto sepByImpl(Parser<A, Repr> p, Parser<B, Repr1> sep)
{
return (triPlus | (p <sepBy1> sep) | (Monad<Parser>::return_ | std::vector<A>{}));
return (alt | (p <sepBy1> sep) | (Monad<Parser>::return_ | std::vector<A>{}));
}

constexpr auto sepBy = toGFunc<2> | [](auto p, auto sep)
Expand All @@ -177,7 +177,7 @@ constexpr auto chainl1Impl(Parser<A, Repr> p, Op op)
};
};
auto const rhs = Monad<Parser>::return_ | a;
return toTEParser || (lhs <triPlus> rhs);
return toTEParser || (lhs <alt> rhs);
};
return (p >>= rest);
}
Expand All @@ -189,7 +189,7 @@ constexpr auto chainl1 = toGFunc<2> | [](auto p, auto op)

constexpr auto chainl = toGFunc<3> | [](auto p, auto op, auto a)
{
return (p <chainl1> op) <triPlus> (Monad<Parser>::return_ | a);
return (p <chainl1> op) <alt> (Monad<Parser>::return_ | a);
};

constexpr auto isSpace = toFunc<> | [](char c)
Expand Down
10 changes: 5 additions & 5 deletions include/hspp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3716,7 +3716,7 @@ using data::runParser;
using data::TEParser;
using data::toTEParser;

constexpr auto triPlus = toGFunc<2> | [](auto p, auto q)
constexpr auto alt = toGFunc<2> | [](auto p, auto q)
{
return data::toParser <o> data::toFunc<> | [=](std::string cs)
{
Expand Down Expand Up @@ -3834,7 +3834,7 @@ template <typename A, typename Repr>
constexpr auto manyImpl(Parser<A, Repr> p)
-> TEParser<std::vector<A>>
{
return toTEParser || triPlus | (many1 | p) | (Monad<Parser>::return_ | std::vector<A>{});
return toTEParser || alt | (many1 | p) | (Monad<Parser>::return_ | std::vector<A>{});
}

constexpr auto many = toGFunc<1> | [](auto p)
Expand All @@ -3856,7 +3856,7 @@ constexpr auto sepBy1 = toGFunc<2> | [](auto p, auto sep)
template <typename A, typename Repr, typename B, typename Repr1>
constexpr auto sepByImpl(Parser<A, Repr> p, Parser<B, Repr1> sep)
{
return (triPlus | (p <sepBy1> sep) | (Monad<Parser>::return_ | std::vector<A>{}));
return (alt | (p <sepBy1> sep) | (Monad<Parser>::return_ | std::vector<A>{}));
}

constexpr auto sepBy = toGFunc<2> | [](auto p, auto sep)
Expand All @@ -3876,7 +3876,7 @@ constexpr auto chainl1Impl(Parser<A, Repr> p, Op op)
};
};
auto const rhs = Monad<Parser>::return_ | a;
return toTEParser || (lhs <triPlus> rhs);
return toTEParser || (lhs <alt> rhs);
};
return (p >>= rest);
}
Expand All @@ -3888,7 +3888,7 @@ constexpr auto chainl1 = toGFunc<2> | [](auto p, auto op)

constexpr auto chainl = toGFunc<3> | [](auto p, auto op, auto a)
{
return (p <chainl1> op) <triPlus> (Monad<Parser>::return_ | a);
return (p <chainl1> op) <alt> (Monad<Parser>::return_ | a);
};

constexpr auto isSpace = toFunc<> | [](char c)
Expand Down
6 changes: 3 additions & 3 deletions sample/parse_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ static_assert((sub | 1 | 2) == -1);
static_assert((mul | 1 | 2) == 2);
static_assert((div | 4 | 2) == 2);

auto const addOp = do_(symb | "+", return_ | add) <triPlus> do_(symb | "-", return_ | sub);
auto const mulOp = do_(symb | "*", return_ | mul) <triPlus> do_(symb | "/", return_ | div);
auto const addOp = do_(symb | "+", return_ | add) <alt> do_(symb | "-", return_ | sub);
auto const mulOp = do_(symb | "*", return_ | mul) <alt> do_(symb | "/", return_ | div);
} // namespace op

using op::addOp;
Expand All @@ -89,7 +89,7 @@ extern TEParser<int> const expr;

Id<int> n;
auto const factor =
digit <triPlus>
digit <alt>
do_(
symb | "("s,
n <= expr,
Expand Down
16 changes: 7 additions & 9 deletions test/hspp/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,17 +1462,15 @@ TEST(do_, comprehension2)
TEST(do_, comprehension3)
{
using namespace hspp::doN;
Id<int> i;
Id<int> j;
Id<int> k;
auto const result = toVector |
_(
Id<int> i, j, k;
auto const rng = _(
makeTuple<3> | i | j | k,
i <= (iota | 1 | 20),
j <= (iota | i | 20),
k <= (iota | j | 20),
k <= (enumFrom | 1),
i <= (iota | 1 | k),
j <= (iota | i | k),
if_ || (i*i + j*j == k*k)
);
auto const expected = std::vector<std::tuple<int, int, int>>{ { 3, 4, 5 }, { 5, 12, 13 }, { 6, 8, 10 }, { 8, 15, 17 }, { 9, 12, 15 } };
auto const result = toVector || take | rng | 5U;
auto const expected = std::vector<std::tuple<int, int, int>>{ { 3, 4, 5 }, { 6, 8, 10 }, { 5, 12, 13 }, { 9, 12, 15 }, { 8, 15, 17 } };
EXPECT_EQ(result, expected);
}