-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
69 lines (47 loc) · 1.45 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include"brainf_ck.h"
#include<boost/iterator/filter_iterator.hpp>
#include<boost/assign.hpp>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<iterator>
#include<set>
struct bf_filter_predicate {
bf_filter_predicate(const std::set<char>& allowed_chars)
: m_allowed(allowed_chars) {}
bool operator()(char c) const {
return m_allowed.count(c) > 0;
}
private:
const std::set<char>& m_allowed;
};
using namespace std;
using namespace boost::spirit::qi;
int main(int argc, char** argv) {
if(argc != 2)
return 1;
std::set<char> allowed_chars =
boost::assign::list_of('+')('-')('<')('>')('[')(']')('.');
ifstream program_file(argv[1]);
program_file >> skipws;
std::vector<char> program;
std::copy(boost::make_filter_iterator(bf_filter_predicate(allowed_chars)
, istream_iterator<char>(program_file)
, istream_iterator<char>())
, boost::make_filter_iterator(bf_filter_predicate(allowed_chars)
, istream_iterator<char>()
, istream_iterator<char>())
, back_inserter(program));
bf_grammar bfg;
std::vector<char>::iterator it_(program.begin());
std::vector<bf_expression> ast;
if(parse(it_, program.end(), bfg, ast)) {
turing_machine tm;
BOOST_FOREACH(bf_expression bf_e, ast) {
boost::apply_visitor(turing_machine_visitor(tm), bf_e);
}
} else {
std::cerr << "there was a problem parsing your program." << std::endl;
}
return 0;
}