Created
October 26, 2011 00:17
-
-
Save sehe/1314900 to your computer and use it in GitHub Desktop.
Execute Command Script (http://stackoverflow.com/questions/7893768/using-boostbind-with-boostfunction-retrieve-binded-variable-type)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/execute WriteLine "bogus" | |
/execute Write "here comes the answer: " | |
/execute Write 42 | |
/execute Write 31415e-4 | |
/execute Write "that is the inverse of" 24 "and answers nothing" | |
/execute Shutdown "Bye" 9 | |
/execute Shutdown "Test default value for retval" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/spirit/include/qi.hpp> | |
#include <boost/spirit/include/phoenix.hpp> | |
#include <fstream> | |
namespace qi = boost::spirit::qi; | |
namespace phx = boost::phoenix; | |
/////////////////////////////////// | |
// 'domain classes' (scriptables) | |
struct Echo | |
{ | |
void WriteLine(const std::string& s) { std::cout << "WriteLine('" << s << "');" << std::endl; } | |
void WriteStr (const std::string& s) { std::cout << "Write(string: '" << s << "');" << std::endl; } | |
void WriteInt (int i) { std::cout << "Write(int: " << i << ");" << std::endl; } | |
void WriteDbl (double d) { std::cout << "Write(double: " << d << ");" << std::endl; } | |
void NewLine () { std::cout << "NewLine();" << std::endl; } | |
} echoService; | |
struct Admin | |
{ | |
void Shutdown(const std::string& reason, int retval) | |
{ | |
std::cout << "Shutdown(reason: '" << reason << "', retval: " << retval << ")" << std::endl; | |
// exit(retval); | |
} | |
} adminService; | |
void execute(const std::string& command) | |
{ | |
typedef std::string::const_iterator It; | |
It f(command.begin()), l(command.end()); | |
using namespace qi; | |
using phx::bind; | |
using phx::ref; | |
rule<It, std::string(), space_type> stringlit = lexeme[ '"' >> *~char_('"') >> '"' ]; | |
try | |
{ | |
if (!phrase_parse(f,l, "/execute" > ( | |
(lit("WriteLine") | |
> stringlit [ bind(&Echo::WriteLine, ref(echoService), _1) ]) | |
| (lit("Write") >> +( | |
double_ [ bind(&Echo::WriteDbl, ref(echoService), _1) ] // the order matters | |
| int_ [ bind(&Echo::WriteInt, ref(echoService), _1) ] | |
| stringlit [ bind(&Echo::WriteStr, ref(echoService), _1) ] | |
)) | |
| (lit("NewLine") [ bind(&Echo::NewLine, ref(echoService)) ]) | |
| (lit("Shutdown") > (stringlit > (int_ | attr(0))) | |
[ bind(&Admin::Shutdown, ref(adminService), _1, _2) ]) | |
), space)) | |
{ | |
if (f!=l) // allow whitespace only lines | |
std::cerr << "** (error interpreting command: " << command << ")" << std::endl; | |
} | |
} | |
catch (const expectation_failure<It>& e) | |
{ | |
std::cerr << "** (unexpected input '" << std::string(e.first, std::min(e.first+10, e.last)) << "') " << std::endl; | |
} | |
if (f!=l) | |
std::cerr << "** (warning: skipping unhandled input '" << std::string(f,l) << "')" << std::endl; | |
} | |
int main() | |
{ | |
std::ifstream ifs("input.txt"); | |
std::string command; | |
while (std::getline(ifs/*std::cin*/, command)) | |
execute(command); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all:main | |
%:%.cpp | |
g++ -O0 -g $^ -o $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment