Skip to content

Commit

Permalink
Fix Id. (#12)
Browse files Browse the repository at this point in the history
Fix id

Co-authored-by: Bowen Fu <missing>
  • Loading branch information
BowenFu committed Jun 24, 2022
1 parent 7acd0e8 commit e33cfdd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --output-on-failure --parallel 4 -C ${{env.BUILD_TYPE}}
run: ctest --output-on-failure --parallel -C ${{env.BUILD_TYPE}}

2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ message(STATUS "CXX_STANDARD: ${CMAKE_CXX_STANDARD}")
list(APPEND
BASE_COMPILE_FLAGS
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall;-Wextra;-pedantic;-Werror;-Wno-parentheses;-Wno-shadow;-Wconversion;-Wsign-conversion>"
"$<$<CXX_COMPILER_ID:MSVC>:/W4>") # /WX for -Werror
"$<$<CXX_COMPILER_ID:MSVC>:/W4;/Zm20>") # /WX for -Werror

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
Expand Down
37 changes: 26 additions & 11 deletions include/hspp.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,37 @@ auto overload(Ts&&... ts)

namespace doN
{
// make sure Id is not a const obj.
template <typename T>
class Id
{
// should be std::shared_ptr<std::optional<T>>
std::shared_ptr<T> mT = std::make_shared<T>();
using OptT = std::optional<T>;
std::shared_ptr<OptT> mT = std::make_shared<OptT>();
public:
constexpr Id() = default;
constexpr auto const& value() const
{
if (!mT)
{
throw std::runtime_error{"Invalid id!"};
}
if (!mT->has_value())
{
throw std::runtime_error{"Id has no binding!"};
}
return *mT;
return mT->value();
}
constexpr void bind(T v)
constexpr void bind(T v) const
{
*mT = std::move(v);
// if (mT->has_value())
// {
// throw std::runtime_error{"Id already has a binding!"};
// }
const_cast<OptT&>(*mT) = std::move(v);
}
constexpr void reset() const
{
const_cast<OptT&>(*mT) = OptT{};
}
};

Expand Down Expand Up @@ -3251,7 +3264,7 @@ class Traversable<std::tuple, Args...> : public TraversableBase<std::tuple, Args
{
constexpr auto sizeMinusOne = std::tuple_size_v<std::decay_t<decltype(in)>> - 1;
auto last = std::get<sizeMinusOne>(in);
auto const func = toGFunc<1>([in=std::move(in)](auto&& lastElem)
auto const func = toGFunc<1>([in=std::forward<decltype(in)>(in)](auto&& lastElem)
{
constexpr auto sizeMinusOne = std::tuple_size_v<std::decay_t<decltype(in)>> - 1;
return std::tuple_cat(subtuple<0, sizeMinusOne>(std::move(in)), std::make_tuple(lastElem));
Expand Down Expand Up @@ -3417,14 +3430,14 @@ class DeMonad
{
return mM;
}
constexpr auto id() const -> std::reference_wrapper<Id<T>>
constexpr auto id() const -> Id<T>
{
return mId;
}

private:
std::reference_wrapper<M const> mM;
std::reference_wrapper<Id<T>> mId;
Id<T> mId;
};

template <typename... Ts>
Expand Down Expand Up @@ -3508,13 +3521,15 @@ BIN_OP_FOR_NULLARY(<)
BIN_OP_FOR_NULLARY(&&)

template <typename T, typename BodyBaker>
constexpr auto funcWithParams(std::reference_wrapper<Id<T>> const& param, BodyBaker const& bodyBaker)
constexpr auto funcWithParams(Id<T> const& param, BodyBaker const& bodyBaker)
{
return [=](T const& t)
{
// bind before baking body.
param.get().bind(t);
return evaluate_(bodyBaker());
const_cast<Id<T>&>(param).bind(t);
auto result = evaluate_(bodyBaker());
// const_cast<Id<T>&>(param).reset();
return result;
};
}

Expand Down
24 changes: 8 additions & 16 deletions test/hspp/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,24 +1421,16 @@ constexpr auto isSpace = toFunc<> | [](char c)

const auto space = many || sat | isSpace;

// // This will fail some tests.
// constexpr auto token = toGFunc<1> | [](auto p)
// {
// using A = DataType<decltype(p)>;
// doN::Id<A> a;
// return doN::do_(
// a <= p,
// space,
// return_ | a
// );
// };

// This will fail some tests.
constexpr auto token = toGFunc<1> | [](auto p)
{
return p >>= [](auto a) { return
space >>
(return_ | a);
};
using A = DataType<std::decay_t<decltype(p)>>;
doN::Id<A> a;
return doN::do_(
a <= p,
space,
return_ | a
);
};

constexpr auto symb = token <o> string;
Expand Down

0 comments on commit e33cfdd

Please sign in to comment.