2026-08-07 13:10:07 -05:00
|
|
|
#include "CommandParser.h"
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
|
|
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<std::string> CommandParser::Verb(std::stringstream &sentences) {
|
|
|
|
|
std::string word;
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> verbs = {};
|
|
|
|
|
|
|
|
|
|
while (sentences >> word) {
|
|
|
|
|
if (std::find(Verbs.begin(), Verbs.end(), word) != Verbs.end()) {
|
|
|
|
|
verbs.push_back(word);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (verbs.empty()) {
|
|
|
|
|
CommandUnrecognized();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return verbs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> CommandParser::Noun(std::stringstream &sentences) {
|
|
|
|
|
|
|
|
|
|
std::string word;
|
|
|
|
|
std::vector<std::string> 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;
|
|
|
|
|
|
2026-08-21 15:52:12 -05:00
|
|
|
std::string noun = "";
|
2026-08-07 13:10:07 -05:00
|
|
|
|
|
|
|
|
std::vector<std::string> verbs;
|
|
|
|
|
std::vector<std::string> nouns;
|
2026-08-21 15:52:12 -05:00
|
|
|
FUNCTION toRun;
|
|
|
|
|
|
2026-08-07 13:10:07 -05:00
|
|
|
|
|
|
|
|
printf("Please Enter a command \n");
|
|
|
|
|
printf("> ");
|
|
|
|
|
getline(std::cin, cmd);
|
2026-08-21 15:52:12 -05:00
|
|
|
std::cin.clear();
|
2026-08-07 13:10:07 -05:00
|
|
|
cmd[0] = toupper(cmd[0]);
|
|
|
|
|
std::stringstream c(cmd);
|
|
|
|
|
|
|
|
|
|
verbs = Verb(c);
|
|
|
|
|
nouns = Noun(c);
|
|
|
|
|
|
|
|
|
|
for (const auto &verb: verbs) {
|
2026-08-21 08:02:22 -05:00
|
|
|
|
|
|
|
|
if (!commands[verb]) {
|
|
|
|
|
printf("Sorry kid, can't understand ya! \n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-08-21 15:52:12 -05:00
|
|
|
toRun = commands[verb];
|
2026-08-07 13:10:07 -05:00
|
|
|
}
|
2026-08-21 15:52:12 -05:00
|
|
|
|
|
|
|
|
if (toRun) {
|
|
|
|
|
toRun(noun);
|
|
|
|
|
}
|
|
|
|
|
cmd.clear();
|
|
|
|
|
word.clear();
|
|
|
|
|
verbs.clear();
|
|
|
|
|
nouns.clear();
|
|
|
|
|
c.clear();
|
|
|
|
|
|
|
|
|
|
toRun = nullptr;
|
|
|
|
|
|
2026-08-07 13:10:07 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|