diff options
Diffstat (limited to 'parser.cpp')
| -rw-r--r-- | parser.cpp | 50 |
1 files changed, 43 insertions, 7 deletions
@@ -4,7 +4,40 @@ #include <vector> #include "token.h" -Parser::Parser(std::string& input) { +Parser::Parser() { } + +bool isNumber(const std::string& str) { + for (const char& c : str) { + if (!std::isdigit(c)) { + return false; + } + } + return true; +} + +std::vector<int> Parser::parseValues(const std::string& input) { + std::vector<int> res; + std::string tmp; + for (const char& c : input) { + if (!std::isspace(c)) { + tmp += c; + } + else { + if (isNumber(tmp)) { + res.push_back(std::stoi(tmp)); + } else { + for (const char& c : tmp) { + res.push_back( static_cast<int>(c) ); + } + } + + tmp = ""; + } + } + return res; +} + +std::vector<Token> Parser::parseCode(const std::string& input) { std::vector<Token> result; bool isString = false; @@ -37,16 +70,19 @@ Parser::Parser(std::string& input) { } } - m_tokens = result; + return result; } -std::vector<Token>& Parser::getTokens() { - return m_tokens; +void Parser::printTokens(const std::vector<Token>& tokens) { + for (auto& token : tokens) { + std::cout << "Token[" << token.getLexeme() << "] "; + } + std::cout << std::endl; } -void Parser::printTokens() { - for (auto& token : getTokens()) { - std::cout << "Token[" << token.getLexeme() << "] "; +void Parser::printValues(const std::vector<int>& values) { + for (auto& value : values) { + std::cout << "Value[" << value << "] "; } std::cout << std::endl; } |
