Sprout/sprout/brainfuck/misa.hpp
2014-01-08 16:48:12 +09:00

99 lines
4.6 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef SPROUT_BRAINFUCK_MISA_HPP
#define SPROUT_BRAINFUCK_MISA_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/utility/pair/pair.hpp>
#include <sprout/container/traits.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/algorithm/fixed/results.hpp>
#include <sprout/weed/parse.hpp>
#include <sprout/weed/parser/lim.hpp>
#include <sprout/weed/parser/lit.hpp>
#include <sprout/weed/parser/char/char.hpp>
#include <sprout/weed/parser/char/char_class.hpp>
#include <sprout/weed/parser/string/string.hpp>
#include <sprout/weed/parser/directive/replace.hpp>
#include <sprout/weed/operator.hpp>
#include <sprout/brainfuck/detail/convert.hpp>
namespace sprout {
namespace brainfuck {
namespace misa {
//
// to_brainfuck
//
template<typename InputIterator, typename Result>
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::results::algorithm<Result>::type, bool>
to_brainfuck(InputIterator first, InputIterator last, Result const& result) {
return sprout::brainfuck::detail::parsed_to_brainfuck(
sprout::weed::parse(
first, last,
*sprout::weed::lim<sprout::container_traits<Result>::static_size>(
sprout::weed::replace('>')
[sprout::weed::lit('>') | "\x81\xA8"/*"<22>¨"*/ | "\x81\x60"/*"<22>`"*/ | "\x81\5B"/*"<22>["*/]
| sprout::weed::replace('<')
[sprout::weed::lit('<') | "\x81\xA9"/*"<22>©"*/ | "\x81\x9A"/*"<22>š"*/ | "\x81\x99"/*"<22>™"*/]
| sprout::weed::replace('+')
[sprout::weed::lit('+') | "\x82\xA0"/*" "*/ | "\x82\x9F"/*"‚Ÿ"*/ | "\x82\xA8"/*"‚¨"*/ | "\x82\xA7"/*"‚§"*/]
| sprout::weed::replace('-')
[sprout::weed::lit('-') | "\x82\xC1"/*"‚Á"*/ | "\x83\x62"/*"ƒb"*/]
| sprout::weed::replace('.')
[sprout::weed::lit('.') | "\x81\x49"/*"<22>I"*/]
| sprout::weed::replace(',')
[sprout::weed::lit(',') | "\x81\x48"/*"<22>H"*/]
| sprout::weed::replace('[')
[sprout::weed::lit('[') | "\x81\x75"/*"<22>u"*/ | "\x81\x77"/*"<22>w"*/]
| sprout::weed::replace(']')
[sprout::weed::lit(']') | "\x81\x76"/*"<22>v"*/ | "\x81\x78"/*"<22>x"*/]
| sprout::weed::replace(' ')
[sprout::weed::char_]
)
),
result
);
}
//
// exec_range
//
template<std::size_t BufferSize = 32, typename BidirectionalRangeSource, typename Output, typename InputRangeInput>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Output>::type
exec_range(BidirectionalRangeSource const& source, Output const& output, InputRangeInput const& input) {
typedef typename sprout::container_construct_traits<BidirectionalRangeSource>::copied_type copied_type;
return sprout::brainfuck::exec_range<BufferSize>(
sprout::brainfuck::misa::to_brainfuck(sprout::begin(source), sprout::end(source), sprout::pit<copied_type>()).first,
output, input
);
}
template<std::size_t BufferSize = 32, typename BidirectionalRangeSource, typename Output>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Output>::type
exec_range(BidirectionalRangeSource const& source, Output const& output) {
typedef typename sprout::container_construct_traits<BidirectionalRangeSource>::copied_type copied_type;
return sprout::brainfuck::exec_range<BufferSize>(
sprout::brainfuck::misa::to_brainfuck(sprout::begin(source), sprout::end(source), sprout::pit<copied_type>()).first,
output
);
}
template<std::size_t BufferSize = 32, typename BidirectionalRangeSource>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<
sprout::array<typename sprout::container_traits<BidirectionalRangeSource>::value_type, BufferSize>
>::type
exec_range(BidirectionalRangeSource const& source) {
typedef typename sprout::container_construct_traits<BidirectionalRangeSource>::copied_type copied_type;
return sprout::brainfuck::exec_range<BufferSize>(
sprout::brainfuck::misa::to_brainfuck(sprout::begin(source), sprout::end(source), sprout::pit<copied_type>()).first
);
}
} // namespace misa
} // namespace brainfuck
} // namespace sprout
#endif // #ifndef SPROUT_BRAINFUCK_MISA_HPP