Skip to content

Instantly share code, notes, and snippets.

@usbharu
Created September 15, 2020 11:15
Show Gist options
  • Save usbharu/20892af3461f8ba5899987e15d8cb9df to your computer and use it in GitHub Desktop.
Save usbharu/20892af3461f8ba5899987e15d8cb9df to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <map>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/lambda/loops.hpp>
using namespace std;
using namespace boost;
using namespace boost::lambda;
int main(int, const char* argv[])
{
typedef vector<unsigned char> Memory;
typedef Memory::iterator Pointer;
cin.unsetf(ios_base::skipws);
// メモリ
Memory imem, dmem(65536);
// プログラム読み込み
ifstream fin(argv[1]);
if( fin )
copy( istream_iterator<char>(fin),
istream_iterator<char>(), back_inserter(imem) );
// ポインタ初期化
Pointer iptr_s=imem.begin(), dptr_s=dmem.begin();
int t_s;
// Bfの各命令に対応する関数の定義
map< char, std::function<void()> > action_for;
{
var_type<Pointer>::type iptr(var(iptr_s));
var_type<Pointer>::type dptr(var(dptr_s));
var_type<int>::type t(var(t_s));
action_for['h']= ++ dptr;
action_for['o']= -- dptr;
action_for['u']= ++*dptr;
action_for['s']= --*dptr;
action_for['y']= cout << *dptr;
action_for['k']= cin >> *dptr;
action_for['a']= if_( !*dptr )[
for_(t=1, t, ++iptr)[
if_( *iptr == 'a' )[ ++t ],
if_( *iptr == 'n' )[ --t ]
]];
action_for['n']= (iptr-=2, for_(t=-1, t, --iptr)[
if_( *iptr == 'a' )[ ++t ],
if_( *iptr == 'n' )[ --t ]
], ++iptr);
}
// 実行。無関係な文字はスキップする
while( iptr_s != imem.end() )
if( std::function<void()> f = action_for[ *iptr_s++ ] )
f();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment