diff --git a/src/parser.cpp b/src/parser.cpp new file mode 100644 index 0000000..eb713ff --- /dev/null +++ b/src/parser.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "parser.hpp" + +int parseCommand(LinkedList *commands, std::string line) { + std::smatch matched; + + if (regex_search(line, matched, std::regex("^(.*) (.*) (.*)"))) { + Command cmd; + int err; + + cmd.line = line; + cmd.type = commandTypes.at(matched[0]); + cmd.segment = segmentTypes.at(matched[1]); + cmd.index = std::stoi(matched[2]); + + if ((err = insertNode(commands, cmd)) != 0) { + std::cerr << "Unexpected error parsing vm command:" << line << std::endl; + return 1; + } + } + + if (regex_search(line, matched, std::regex("^(.*)"))) { + Command cmd; + int err; + + cmd.line = line; + cmd.type = matched[0]; + + if ((err = insertNode(commands, cmd)) != 0) { + std::cerr << "Unexpected error parsing vm command:" << line << std::endl; + return 1; + } + } + + return 0; +} diff --git a/src/parser.hpp b/src/parser.hpp new file mode 100644 index 0000000..ab00850 --- /dev/null +++ b/src/parser.hpp @@ -0,0 +1,6 @@ +#include + +#include "linked-list.hpp" +#include "types.hpp" + +int parseCommand(LinkedList *, std::string);