void test_foreach() { using phoenix::placeholders::_1; using phoenix::ref; using phoenix::lambda; int a[10][20]; int sum = 0; //for_each(arg1, for_each_tester())(array).value_; // Was: // std::for_each(a, a + 10, // bind(ll::for_each(), _1, _1 + 20, // protect((_1 = var(sum), ++var(sum))))); // var replaced with ref, protect(..) replaced with lambda[..], no need for bind // phoenix algorithms are range based std::for_each(a, a + 10, phoenix::for_each(_1, lambda[_1 = ref(sum), ++ref(sum)])); /*phoenix::bind(phoenix::for_each, _1, lambda[_1 = ref(sum), ++ref(sum)]));*/ sum = 0; // Was: // std::for_each(a, a + 10, // bind(ll::for_each(), _1, _1 + 20, // protect((sum += _1)))); // std::for_each(a, a + 10, phoenix::for_each( _1, lambda[(ref(sum) += _1)])); BOOST_CHECK(sum == (199 + 1)/ 2 * 199); }
bool Compiler::execute() { using phoenix::ref; using qi::lit; using qi::no_skip; // iterate over stream input typedef std::istreambuf_iterator<char> base_iterator_type; base_iterator_type in_begin(m_input); // convert input iterator to forward iterator, usable by spirit parser typedef spirit::multi_pass<base_iterator_type> forward_iterator_type; forward_iterator_type fwd_begin = spirit::make_default_multi_pass(in_begin); forward_iterator_type fwd_end; // Initialize global scope Scope globalScope; StdLib::Init(globalScope); // Parsers ExpParser<forward_iterator_type> exp_; CmdParser<forward_iterator_type> cmd_(exp_); CodeParser<forward_iterator_type> code_(exp_); typedef qi::rule<forward_iterator_type, std::string(), ascii::space_type> StringRule; typedef qi::rule<forward_iterator_type, ascii::space_type> VoidRule; StringRule cmdline_ = lit('[') > (cmd_(ref(globalScope)) | code_(ref(globalScope))) > lit(']') ; VoidRule program_ = +( cmdline_[ref(m_output) << qi::_1] > -no_skip[ lit("\n")[ref(m_output) << endl] ] ); //BOOST_SPIRIT_DEBUG_NODE(cmdline_); //BOOST_SPIRIT_DEBUG_NODE(program_); bool r = qi::phrase_parse( fwd_begin, fwd_end, program_, ascii::space ); if (!r || fwd_begin != fwd_end) { return false; } // Cleanup the global scope :) m_output << globalScope.bytecode() << endl; return r; }
int main() { using phoenix::ref; using phoenix::arg_names::arg1; using phoenix::arg_names::arg2; using std::map; using std::string; using std::vector; { int x = 123; BOOST_TEST((&arg1)(x) == &x); //BOOST_TEST((*&arg1)(x) == 123); int y = 968; (ref(x) = arg1)(y); BOOST_TEST(x == y); (arg1 = 456)(x); BOOST_TEST(x == 456); int& r = (arg1 = 456)(x); // must be an lvalue BOOST_TEST(&r == &x); int c[] = { 1, 2, 3, 4, 5 }; BOOST_TEST((arg1[3])(c) == 4); int& r2 = (arg1[3])(c); // must be an lvalue BOOST_TEST(&r2 == &c[3]); vector<string> v; v.push_back("a"); v.push_back("b"); v.push_back("c"); v.push_back("d"); BOOST_TEST((arg1[3])(v) == "d"); map<string, int> m; (arg1["Kimpo"] = arg2)(m, x); BOOST_TEST(m["Kimpo"] == x); } return boost::report_errors(); }
bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v) { using qi::double_; using qi::phrase_parse; using qi::_1; using ascii::space; using phoenix::push_back; using phoenix::ref; bool r = phrase_parse(first, last, // Begin grammar ( double_[push_back(ref(v), _1)] % ',' ) , // End grammar space); if (first != last) // fail if we did not get a full match return false; return r; }