From 8df0f5a1c5275b9b690075e412cf23eb70f3e188 Mon Sep 17 00:00:00 2001 From: Bowen Fu Date: Sun, 19 Jun 2022 19:38:38 +0800 Subject: [PATCH 01/16] parser --- include/hspp.h | 780 +++++++++++++++++++++++++++++++++++---------- test/hspp/test.cpp | 384 +++++++++++++++++----- 2 files changed, 908 insertions(+), 256 deletions(-) diff --git a/include/hspp.h b/include/hspp.h index e21e6ec..6fe04a0 100644 --- a/include/hspp.h +++ b/include/hspp.h @@ -230,6 +230,12 @@ class CallViaPipe } }; +template +constexpr auto operator&(Arg&& arg, CallViaPipe const& func) +{ + return func | arg; +} + template class Function : public CallViaPipe> { @@ -265,7 +271,7 @@ template constexpr static auto isFunctionV = IsFunction>::value; template -constexpr auto toFunc(Func const& func) +constexpr auto toFuncImpl(Func const& func) { if constexpr(sizeof...(Args) == 0) { @@ -278,13 +284,13 @@ constexpr auto toFunc(Func const& func) } template -constexpr auto toTEFunc(Func const& func) +constexpr auto toTEFuncImpl(Func const& func) { return ToFunction::run(func); } template -constexpr auto toTEFunc(Func const& func) +constexpr auto toTEFuncImpl(Func const& func) { return TEFunction{func}; } @@ -316,11 +322,29 @@ class GenericFunction : public CallViaPipe> }; template -constexpr auto toGFunc(Func const& func) +constexpr auto toGFuncImpl(Func const& func) { return GenericFunction{func}; } +template +constexpr inline auto toGFunc = toGFuncImpl<1>([](auto data) +{ + return toGFuncImpl(std::move(data)); +}); + +template +constexpr inline auto toFunc = toGFunc<1>([](auto func) +{ + return toFuncImpl(std::move(func)); +}); + +template +constexpr inline auto toTEFunc = toGFunc<1>([](auto func) +{ + return toTEFuncImpl(std::move(func)); +}); + constexpr inline auto id = toGFunc<1>([](auto data) { return data; @@ -374,7 +398,14 @@ class Compose template constexpr auto operator()(Func&& f, Function const& g) const { - return toFunc([=](FirstArg x){ return f(g(x));}); + return toFunc<> | [=](FirstArg x){ return f(g(x));}; + } + template + constexpr auto operator()(F&& f, GenericFunction const& g) const + { + // return toGFunc([=](auto&&... args){ return f((g | ... | args));}); + // static_assert(I==1); + return toGFunc<1>([=](auto x){ return f(g(x));}); } template constexpr auto operator()(F&& f, G&&g) const @@ -419,7 +450,7 @@ auto ioData(Data data) return io([data=std::move(data)] { return data; }); } -constexpr auto putStrLn = toFunc([](std::string str) +constexpr auto putStrLn = toFunc<> | [](std::string str) { return io( [str=std::move(str)] @@ -428,7 +459,7 @@ constexpr auto putStrLn = toFunc([](std::string str) return _o_; } ); -}); +}; const inline auto getLine = io([] { @@ -1301,12 +1332,12 @@ namespace impl template constexpr auto flipImpl(Function f) { - return toFunc([f=std::move(f)](Arg1 x, Arg2 y){ return f | y | x; }); + return toFunc<>([f=std::move(f)](Arg1 x, Arg2 y){ return f | y | x; }); } template constexpr auto flipImpl(Function, Arg1> f) { - return toFunc([f=std::move(f)](Arg2 x, Arg1 y){ return f | y | x; }); + return toFunc<>([f=std::move(f)](Arg2 x, Arg1 y){ return f | y | x; }); } } // namespace impl @@ -1360,12 +1391,6 @@ constexpr auto foldl = toGFunc<3>([](auto func, auto init, auto const& list) constexpr auto equalTo = toGFunc<2>(std::equal_to<>{}); -// constexpr inline auto elem = any equalTo; -constexpr inline auto elem = toGFunc<2>([](auto t, auto const& c) -{ - return std::any_of(c.begin(), c.end(), [t=std::move(t)](auto const& e){return e == t;}); -}); - template constexpr auto onImpl(Function f, Function g) { @@ -1423,12 +1448,6 @@ constexpr inline auto enumFrom = toGFunc<1>([](auto start) return ownedRange(IotaView{start}); }); -constexpr inline auto length = toGFunc<1>([](auto r) -{ - constexpr auto inc = toGFunc<2>([](auto sum, auto){ return sum + 1; }); - return foldl | inc | 0 | r; -}); - constexpr inline auto take = toGFunc<2>([](auto r, size_t num) { return ownedRange(TakeView{r, num}); @@ -1468,6 +1487,112 @@ constexpr inline auto makeTuple = toGFunc([](auto e, auto... l) return std::make_tuple(std::move(e), std::move(l)...); }); +template +class DataHolder +{ + Data mData; +public: + constexpr DataHolder(Data data) + : mData{std::move(data)} + {} + auto const& get() const + { + return mData; + } +}; + +template +constexpr auto operator==(DataHolder const& l, DataHolder const& r) +{ + return l.get() == r.get(); +} + +template