diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-09-01 11:14:10 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-09-01 11:14:10 +0300 |
| commit | be011366f249b8ebed3905030ce3b7021597ee34 (patch) | |
| tree | 3b357df175ba1f484d852d73d4cbf3da9c1910df /parser.cpp | |
| parent | d6b44fb2323b4f6f5e0bd3de33a40d6fd8283c1a (diff) | |
Add value parsing from stdin, remove value class
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; } |
