Spróbuj coś takiego:
#include <iostream>
#include <functional>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <unordered_map>
#include <iterator>
using namespace std;
using arguments = vector<string>;
using command = function<void(arguments const &)>;
using commands = unordered_map<string, command>;
vector<string> str_to_tokens(string const &src) {
istringstream stream(src);
return {
istream_iterator<string>{stream},
istream_iterator<string>{}
};
}
void print(arguments const &args) {
for(auto &&arg: args) {
cout << arg << " ";
}
cout << endl;
}
int main() {
commands cmds = {
{"print", print}
};
string input;
while(getline(cin, input)) {
if(input.empty()) {
continue;
}
auto tokens = str_to_tokens(input);
auto cmd = tokens[0];
cmds.at(cmd)(arguments{begin(tokens)+1, end(tokens)});
}
return 0;
}
Proste jak budowa cepa, a powinno wystarczyć na twoje potrzeby.
http://ideone.com/bOrLAL
**input: **
print Ala ma dziwnego kota.
**output: **
Ala ma dziwnego kota.
Z takim niewydajnym podejściem możesz szybko stać się dość kreatywny, patrz np.
#include <iostream>
#include <functional>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <unordered_map>
#include <iterator>
#include <cassert>
using namespace std;
//boring helper functions
inline std::string trim(const std::string &s) {
auto wsfront=std::find_if_not(s.begin(),s.end(),[](int c){return std::isspace(c);});
return std::string(wsfront,std::find_if_not(s.rbegin(),std::string::const_reverse_iterator(wsfront),[](int c){return std::isspace(c);}).base());
}
vector<string> str_to_tokens(string const &src) {
istringstream stream(src);
return {
istream_iterator<string>{stream},
istream_iterator<string>{}
};
}
string join(vector<string> const &vec, string const &delim) {
stringstream res;
copy(vec.begin(), vec.end(), ostream_iterator<string>(res, delim.c_str()));
return res.str();
}
//scaffolding
using arguments = vector<string>;
using command = function<void(arguments const &)>;
using commands = unordered_map<string, command>;
using middleware = function<void(arguments &)>;
using middlewares = vector<middleware>;
//bullshit that matters
void print(arguments const &args) {
for(auto &&arg: args) {
cout << arg << " ";
}
cout << endl;
}
struct data_store {
using vname = string;
using vval = string;
unordered_map<vname, vval> vars;
void store(vname const &name, vval const &val) {
vars[name] = val;
}
void store_cmd(arguments const &args) {
enum { VNAME, VEQSIGN, VVAL_BEG };
assert(args[VEQSIGN] == "=");
auto val = join({begin(args)+VVAL_BEG, end(args)}, " ");
store(args[VNAME], trim(val));
}
vval get(vname const &name) const {
return vars.at(name);
}
};
void replace_vars_with_values(data_store const &store, string const &var_prefix, arguments &args) {
for(auto &arg: args) {
if(arg.rfind(var_prefix, 0) == 0) {
arg = store.get(string{begin(arg)+var_prefix.size(), end(arg)});
}
}
}
void apply_middlewares(middlewares const &mdws, arguments &args) {
for(auto &&mdw: mdws) {
mdw(args);
}
}
int main() {
string var_prefix = "$";
data_store dstore;
middlewares mdws = {
[&](arguments &args) { return replace_vars_with_values(dstore, var_prefix, args); }
};
commands cmds = {
{"print", print},
{"var", [&dstore](arguments const &args) { dstore.store_cmd(args); }}
};
string input;
while(getline(cin, input)) {
if(input.empty()) {
continue;
}
auto tokens = str_to_tokens(input);
apply_middlewares(mdws, tokens);
enum { FUNC_POS, ARGS_START };
cmds.at(tokens[FUNC_POS])(arguments{begin(tokens)+ARGS_START, end(tokens)});
}
return 0;
}
https://ideone.com/kdaGqy
input:
var person = Ala
var state = ma dziwnego kota.
var f = print
$f $person $state
output:
Ala ma dziwnego kota.