From be011366f249b8ebed3905030ce3b7021597ee34 Mon Sep 17 00:00:00 2001 From: jantuomi Date: Thu, 1 Sep 2016 11:14:10 +0300 Subject: Add value parsing from stdin, remove value class --- parser.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) (limited to 'parser.cpp') diff --git a/parser.cpp b/parser.cpp index 828b574..2472f6d 100644 --- a/parser.cpp +++ b/parser.cpp @@ -4,7 +4,40 @@ #include #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 Parser::parseValues(const std::string& input) { + std::vector 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(c) ); + } + } + + tmp = ""; + } + } + return res; +} + +std::vector Parser::parseCode(const std::string& input) { std::vector result; bool isString = false; @@ -37,16 +70,19 @@ Parser::Parser(std::string& input) { } } - m_tokens = result; + return result; } -std::vector& Parser::getTokens() { - return m_tokens; +void Parser::printTokens(const std::vector& 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& values) { + for (auto& value : values) { + std::cout << "Value[" << value << "] "; } std::cout << std::endl; } -- cgit v1.3