2012-09-21 06:43:30 +00:00
|
|
|
#ifndef SPROUT_BRAINFUCK_BRAINFUCK_HPP
|
|
|
|
#define SPROUT_BRAINFUCK_BRAINFUCK_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <sprout/config.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/array/array.hpp>
|
|
|
|
#include <sprout/pit/pit.hpp>
|
2012-09-21 06:43:30 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/iterator/value_iterator.hpp>
|
|
|
|
#include <sprout/container/traits.hpp>
|
|
|
|
#include <sprout/container/functions.hpp>
|
|
|
|
#include <sprout/algorithm/fixed/result_of.hpp>
|
|
|
|
#include <sprout/algorithm/fixed/copy.hpp>
|
|
|
|
#include <sprout/operation/fixed/set.hpp>
|
2012-11-30 18:00:46 +00:00
|
|
|
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
|
2012-09-21 06:43:30 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace brainfuck {
|
|
|
|
namespace detail {
|
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR InputIterator
|
|
|
|
find_scope_end(InputIterator first, std::size_t count = 0) {
|
|
|
|
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
|
|
|
|
return *first == value_type('[') ? sprout::brainfuck::detail::find_scope_end(sprout::next(first), count + 1)
|
|
|
|
: *first == value_type(']') ? count == 0
|
|
|
|
? first
|
|
|
|
: sprout::brainfuck::detail::find_scope_end(sprout::next(first), count - 1)
|
|
|
|
: sprout::brainfuck::detail::find_scope_end(sprout::next(first), count)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename BidirectionalIterator>
|
|
|
|
inline SPROUT_CONSTEXPR BidirectionalIterator
|
|
|
|
find_scope_start(BidirectionalIterator first, std::size_t count = 0) {
|
|
|
|
typedef typename std::iterator_traits<BidirectionalIterator>::value_type value_type;
|
|
|
|
return *first == value_type(']') ? sprout::brainfuck::detail::find_scope_start(sprout::prev(first), count + 1)
|
|
|
|
: *first == value_type('[') ? count == 0
|
|
|
|
? first
|
|
|
|
: sprout::brainfuck::detail::find_scope_start(sprout::prev(first), count - 1)
|
|
|
|
: sprout::brainfuck::detail::find_scope_start(sprout::prev(first), count)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_well_formed(InputIterator first, InputIterator last, std::size_t count = 0) {
|
|
|
|
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
|
|
|
|
return first == last ? count == 0
|
|
|
|
: *first == value_type('[')
|
|
|
|
? sprout::brainfuck::detail::is_well_formed(sprout::next(first), last, count + 1)
|
|
|
|
: *first == value_type(']')
|
|
|
|
? count != 0 && sprout::brainfuck::detail::is_well_formed(sprout::next(first), last, count - 1)
|
|
|
|
: sprout::brainfuck::detail::is_well_formed(sprout::next(first), last, count)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<
|
2013-02-03 16:10:26 +00:00
|
|
|
typename BidirectionalIteratorSource, typename Output, typename InputIteratorInput,
|
2012-09-21 06:43:30 +00:00
|
|
|
typename Buffer, typename OutputBuffer
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Output>::type
|
|
|
|
exec_impl(
|
2013-02-03 16:10:26 +00:00
|
|
|
BidirectionalIteratorSource first, BidirectionalIteratorSource last,
|
|
|
|
Output const& output, InputIteratorInput in_first, InputIteratorInput in_last,
|
2012-09-21 06:43:30 +00:00
|
|
|
Buffer const& buffer, OutputBuffer const& out_buffer, std::size_t pos = 0, std::size_t out_pos = 0
|
|
|
|
)
|
|
|
|
{
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef typename std::iterator_traits<BidirectionalIteratorSource>::value_type value_type;
|
2012-09-21 06:43:30 +00:00
|
|
|
typedef typename sprout::container_traits<OutputBuffer>::value_type out_value_type;
|
2012-11-30 18:00:46 +00:00
|
|
|
return first == last
|
|
|
|
? sprout::fixed::copy(
|
|
|
|
sprout::begin(out_buffer),
|
|
|
|
sprout::next(sprout::begin(out_buffer), NS_SSCRISK_CEL_OR_SPROUT::min(out_pos, sprout::size(out_buffer))),
|
|
|
|
output
|
|
|
|
)
|
2012-09-21 06:43:30 +00:00
|
|
|
: *first == value_type('>')
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos + 1, out_pos
|
|
|
|
)
|
|
|
|
: *first == value_type('<')
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos - 1, out_pos
|
|
|
|
)
|
|
|
|
: *first == value_type('+')
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
sprout::fixed::set(buffer, pos, value_type(buffer.at(pos) + 1)), out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: *first == value_type('-')
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
sprout::fixed::set(buffer, pos, value_type(buffer.at(pos) - 1)), out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: *first == value_type('.') ? out_pos != out_buffer.size()
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
buffer, sprout::fixed::set(out_buffer, out_pos, out_value_type(buffer.at(pos))), pos, out_pos + 1
|
|
|
|
)
|
|
|
|
: throw std::out_of_range("output out of range")
|
|
|
|
: *first == value_type(',') ? in_first != in_last
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, sprout::next(in_first), in_last,
|
|
|
|
sprout::fixed::set(buffer, pos, value_type(*in_first)), out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: throw std::out_of_range("input out of range")
|
|
|
|
: *first == value_type('[') ? buffer.at(pos) == 0
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(sprout::brainfuck::detail::find_scope_end(sprout::next(first))), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: *first == value_type(']') ? buffer.at(pos) != 0
|
|
|
|
? sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(sprout::brainfuck::detail::find_scope_start(sprout::prev(first))), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
: sprout::brainfuck::detail::exec_impl(
|
|
|
|
sprout::next(first), last, output, in_first, in_last,
|
|
|
|
buffer, out_buffer, pos, out_pos
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// exec
|
|
|
|
//
|
2013-02-03 16:10:26 +00:00
|
|
|
template<std::size_t BufferSize = 32, typename BidirectionalIteratorSource, typename Output, typename InputIteratorInput>
|
2012-09-21 06:43:30 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Output>::type
|
|
|
|
exec(
|
2013-02-03 16:10:26 +00:00
|
|
|
BidirectionalIteratorSource first, BidirectionalIteratorSource last,
|
|
|
|
Output const& output, InputIteratorInput in_first, InputIteratorInput in_last
|
2012-09-21 06:43:30 +00:00
|
|
|
)
|
|
|
|
{
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef typename std::iterator_traits<BidirectionalIteratorSource>::value_type value_type;
|
2012-09-21 06:43:30 +00:00
|
|
|
typedef sprout::container_traits<Output> out_traits;
|
|
|
|
return sprout::brainfuck::detail::exec_impl(
|
|
|
|
first, last, output, in_first, in_last,
|
|
|
|
sprout::array<value_type, BufferSize>{{}}, sprout::array<typename out_traits::value_type, out_traits::static_size>{{}}
|
|
|
|
);
|
|
|
|
}
|
2013-02-03 16:10:26 +00:00
|
|
|
template<std::size_t BufferSize = 32, typename BidirectionalIteratorSource, typename Output>
|
2012-09-21 06:43:30 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Output>::type
|
|
|
|
exec(
|
2013-02-03 16:10:26 +00:00
|
|
|
BidirectionalIteratorSource first, BidirectionalIteratorSource last,
|
2012-09-21 06:43:30 +00:00
|
|
|
Output const& output
|
|
|
|
)
|
|
|
|
{
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef typename std::iterator_traits<BidirectionalIteratorSource>::value_type value_type;
|
2012-09-21 06:43:30 +00:00
|
|
|
return sprout::brainfuck::exec<BufferSize>(
|
|
|
|
first, last, output, sprout::value_iterator<value_type>(value_type()), sprout::value_iterator<value_type>()
|
|
|
|
);
|
|
|
|
}
|
2013-02-03 16:10:26 +00:00
|
|
|
template<std::size_t BufferSize = 32, typename BidirectionalIteratorSource>
|
2012-09-21 06:43:30 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<
|
2013-02-03 16:10:26 +00:00
|
|
|
sprout::array<typename std::iterator_traits<BidirectionalIteratorSource>::value_type, BufferSize>
|
2012-09-21 06:43:30 +00:00
|
|
|
>::type
|
|
|
|
exec(
|
2013-02-03 16:10:26 +00:00
|
|
|
BidirectionalIteratorSource first, BidirectionalIteratorSource last
|
2012-09-21 06:43:30 +00:00
|
|
|
)
|
|
|
|
{
|
2013-02-03 16:10:26 +00:00
|
|
|
typedef typename std::iterator_traits<BidirectionalIteratorSource>::value_type value_type;
|
2012-09-21 06:43:30 +00:00
|
|
|
return sprout::brainfuck::exec<BufferSize>(
|
|
|
|
first, last, sprout::pit<sprout::array<value_type, BufferSize> >()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// exec_range
|
|
|
|
//
|
2013-02-03 16:10:26 +00:00
|
|
|
template<std::size_t BufferSize = 32, typename BidirectionalRangeSource, typename Output, typename InputRangeInput>
|
2012-09-21 06:43:30 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Output>::type
|
2013-02-03 16:10:26 +00:00
|
|
|
exec_range(BidirectionalRangeSource const& source, Output const& output, InputRangeInput const& input) {
|
2012-09-21 06:43:30 +00:00
|
|
|
return sprout::brainfuck::exec<BufferSize>(
|
|
|
|
sprout::begin(source), sprout::end(source), output, sprout::begin(input), sprout::end(input)
|
|
|
|
);
|
|
|
|
}
|
2013-02-03 16:10:26 +00:00
|
|
|
template<std::size_t BufferSize = 32, typename BidirectionalRangeSource, typename Output>
|
2012-09-21 06:43:30 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Output>::type
|
2013-02-03 16:10:26 +00:00
|
|
|
exec_range(BidirectionalRangeSource const& source, Output const& output) {
|
2012-09-21 06:43:30 +00:00
|
|
|
return sprout::brainfuck::exec<BufferSize>(
|
|
|
|
sprout::begin(source), sprout::end(source), output
|
|
|
|
);
|
|
|
|
}
|
2013-02-03 16:10:26 +00:00
|
|
|
template<std::size_t BufferSize = 32, typename BidirectionalRangeSource>
|
2012-09-21 06:43:30 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<
|
2013-02-03 16:10:26 +00:00
|
|
|
sprout::array<typename sprout::container_traits<BidirectionalRangeSource>::value_type, BufferSize>
|
2012-09-21 06:43:30 +00:00
|
|
|
>::type
|
2013-02-03 16:10:26 +00:00
|
|
|
exec_range(BidirectionalRangeSource const& source) {
|
2012-09-21 06:43:30 +00:00
|
|
|
return sprout::brainfuck::exec<BufferSize>(
|
|
|
|
sprout::begin(source), sprout::end(source)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// is_well_formed
|
|
|
|
//
|
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_well_formed(InputIterator first, InputIterator last) {
|
|
|
|
return sprout::brainfuck::detail::is_well_formed(first, last);
|
|
|
|
}
|
|
|
|
} // namespace brainfuck
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_BRAINFUCK_BRAINFUCK_HPP
|