#include "CommandParser.h" #include #include #include CommandParser::CommandParser() {} bool CommandParser::AddCommand(std::string c, FUNCTION f) { commands.emplace(c, f); return true; } void CommandParser::RemoveCommand(std::string c) { commands.erase(c); } std::vector CommandParser::Verb(std::stringstream &sentences) { std::string word; std::vector verbs = {}; while (sentences >> word) { if (std::find(Verbs.begin(), Verbs.end(), word) != Verbs.end()) { verbs.push_back(word); } } return verbs; } std::vector CommandParser::Noun(std::stringstream &sentences) { std::string word; std::vector nouns = {}; while (sentences >> word) { if (std::find(Nouns.begin(), Nouns.end(), word) != Nouns.end()) { nouns.push_back(word); } } return nouns; } int CommandParser::Parse() { std::string cmd; std::string word; std::string noun = ""; std::vector verbs; std::vector nouns; FUNCTION toRun; printf("Please Enter a command \n"); printf("> "); getline(std::cin, cmd); std::cin.clear(); cmd[0] = toupper(cmd[0]); std::stringstream c(cmd); verbs = Verb(c); nouns = Noun(c); for (const auto &verb: verbs) { if (!commands[verb]) { CommandUnrecognized(); break; } toRun = commands[verb]; } if (toRun) { toRun(noun); } cmd.clear(); word.clear(); verbs.clear(); nouns.clear(); c.clear(); toRun = nullptr; return 0; }