2012-09-15
詰んでる〜
メモのかたまりにする対象をyutopp's blogにした.
こっちの日記は適当に何か書いていこう.放っておくのもなんだかアレだしね.
それにしても煮詰まってる感がすごい.うーどうしよう.
2011-12-13
brainf*cked
むしゃくしゃして書いた。
(12/22 ちょいと書き換え)
#include <iostream> #include <string> #include <vector> #include <iterator> namespace brainfuck { namespace detail { struct default_dictionary { typedef char char_type; static const int max_length = 1; static const char* increment_pointer() { return ">"; } static const char* decrement_pointer() { return "<"; } static const char* repeat() { return "["; } static const char* loop() { return "]"; } static const char* increment_value() { return "+"; } static const char* decrement_value() { return "-"; } static const char* input() { return ","; } static const char* output() { return "."; } }; } template<typename DictT> struct dictionary_traits { typedef typename DictT::char_type char_type; static const int max_length = DictT::max_length; static const char_type* increment_pointer() { return DictT::increment_pointer(); } static const char_type* decrement_pointer() { return DictT::decrement_pointer(); } static const char_type* repeat() { return DictT::repeat(); } static const char_type* loop() { return DictT::loop(); } static const char_type* increment_value() { return DictT::increment_value(); } static const char_type* decrement_value() { return DictT::decrement_value(); } static const char_type* input() { return DictT::input(); } static const char_type* output() { return DictT::output(); } }; template<typename IterT, typename DictT = detail::default_dictionary, typename ElemT = unsigned char, std::size_t MemLimit = 30000> class interpreter { typedef DictT dictinary_type; typedef ElemT value_type; typedef typename std::vector<value_type> container; typedef typename container::iterator iterator; struct order { typedef int type; enum { finish = 0, increment_pointer, decrement_pointer, repeat, loop, increment_value, decrement_value, input, output, nop }; }; public: interpreter( const IterT& begin, const IterT& end ) : memory_( MemLimit ) , ptr_( memory_.begin() ) , begin_( begin ) , end_( end ) {} bool run() { return eval( begin_, end_ ); } private: bool eval( IterT& it, const IterT& end ) { while( it != end ) { const auto order = get_token( it, end ); const auto& type = order.first; if ( type == order::finish ) break; if ( type == order::increment_pointer ) { ++ptr_; if ( ptr_ == memory_.cend() ) return false; } else if ( type == order::decrement_pointer ) { if ( ptr_ == memory_.cbegin() ) return false; --ptr_; } else if ( type == order::repeat ) { if ( *ptr_ ) { auto tmp = it; std::advance( tmp, order.second ); if ( !eval( tmp, end ) ) return false; } else { int depth = 0; for(;;) { const auto order = get_token( it, end ); if ( order.first == order::finish ) return false; std::advance( it, order.second ); if ( order.first == order::loop ) { --depth; if ( depth == 0) break; } else if ( order.first == order::repeat ) { ++depth; } } } continue; } else if ( type == order::loop ) { return true; } else if ( type == order::increment_value ) { ++(*ptr_); } else if ( type == order::decrement_value ) { --(*ptr_); } else if ( type == order::input ) { std::cout << *ptr_ ; } else if ( type == order::output ) { std::cin >> *ptr_; }/* else { // nop }*/ std::advance( it, order.second ); } return true; } std::pair<typename order::type, std::size_t> get_token( const IterT& begin, const IterT& end ) const { typedef dictionary_traits<dictinary_type> dic_type; std::basic_string<typename std::iterator_traits<IterT>::value_type> order; std::size_t retry = 0; for( auto it=begin; it!=end; ++it, ++retry ) { if ( retry >= dic_type::max_length ) return std::make_pair( order::nop, 1 ); order += *it; if ( order == dic_type::increment_pointer() ) { return std::make_pair( order::increment_pointer, order.size() ); } else if ( order == dic_type::decrement_pointer() ) { return std::make_pair( order::decrement_pointer, order.size() ); } else if ( order == dic_type::repeat() ) { return std::make_pair( order::repeat, order.size() ); } else if ( order == dic_type::loop() ) { return std::make_pair( order::loop, order.size() ); } else if ( order == dic_type::increment_value() ) { return std::make_pair( order::increment_value, order.size() ); } else if ( order == dic_type::decrement_value() ) { return std::make_pair( order::decrement_value, order.size() ); } else if ( order == dic_type::output() ) { return std::make_pair( order::input, order.size() ); } else if ( order == dic_type::input() ) { return std::make_pair( order::output, order.size() ); } } return std::make_pair( order::finish, 0 ); } container memory_; iterator ptr_; IterT begin_, end_; }; template<typename IterT> bool parse( IterT& begin, IterT end ) { auto bf = interpreter<IterT>( begin, end ); return bf.run(); } template<typename DictT, typename IterT> bool parse( IterT& begin, IterT end ) { auto bf = interpreter<IterT, DictT>( begin, end ); return bf.run(); } } struct honmono { typedef wchar_t char_type; static const int max_length = 6; static const char_type* increment_pointer() { return L"ピャ"; } static const char_type* decrement_pointer() { return L"<"; } static const char_type* repeat() { return L"(^q^)"; } static const char_type* loop() { return L"!"; } static const char_type* increment_value() { return L"ァ"; } static const char_type* decrement_value() { return L"-"; } static const char_type* input() { return L","; } static const char_type* output() { return L"ウンバボ"; } }; /*------ main ------*/ int main() { const std::wstring s = L"ピャァァァァァァァァァ(^q^)<ァァァァァァァァピャ-!<ウンバボ" \ L"ピャァァァァァァァ(^q^)<ァァァァピャ-!<ァウンバボァァァァァァァ" \ L"ウンバボウンバボァァァウンバボ(^q^)-!ピャァァァァァァァァ" \ L"(^q^)<ァァァァピャ-!<ウンバボピャァァァァァァァァァァァ" \ L"(^q^)<ァァァァァピャ-!<ウンバボピャァァァァァァァァ(^q^)<ァァァピャ-!" \ L"<ウンバボァァァウンバボ------ウンバボ--------ウンバボ" \ L"(^q^)-!ピャァァァァァァァァ(^q^)<ァァァァピャ-!<ァウンバボ" \ L"(^q^)-!ァァァァァァァァァァウンバボ"; const bool b = brainfuck::parse<honmono>( s.cbegin(), s.cend() ); if ( !b ) std::cout << " : error..." << std::endl; }
結果
Hello World!
2011-09-18
NativeClientのサンプルのコメント
NativeClientやっと試せた<1>
Chromeのバージョンが遂に14になり、NativeClientがデフォルトで使えるようになったということでチュートリアルを試してみました。
まだ暑いですしね、塩分の補給ということでメモ。
環境:Windows7 x86
後はなんだかんだで最初はNativeClientが無効になってたので、アドレスバーに "chrome://flags/" と打ち込み "ネイティブ クライアント" を "有効にする" にしてからChromeを再起動します。
1.SDKを入れます。
Download the Native Client SDK - Native Client — Google Developers
ここの一番上のWindowsの項の "Download the SDK." よりnaclsdk_win.exeを頂く。
そんでもってインストール。今回はC:直下に。
後はいきなりPython大活躍になるので、入れていない場合は2.x系列のものをインストールしてパスを通しておいたほうがいいです。
2.サンプルで遊んでみる。
C++ Tutorial: Getting Started - Native Client — Google Developers
ただ単にネイティブクライアントのファイルをアドレスに貼っつけても動かないから覚悟しろよな!って言ってるので、言われた通りにhttpd.pyを実行します。親切や・・・。
そしてlocalhostにアクセスすると・・・
\NativeClientすげぇ!/
3.書く。
さっき(2.)のチュートリアル通りsdk内のproject_templates内にあるinit_project.pyを実行し、生成されたフォルダ内のコードを書き足し、scons.batを実行すればネイティブクライアントの実行ファイルとマニフェストがぱぱっと生成されるので、localhostにアクセスして動かしてみるとテンションが上がります。
あとはコレを読んで書いていくだけ・・・なのか!
Application Structure - Native Client — Google Developers
ページが見つかりません
4.感想。
ワクワクが止まりません。






















