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 --- main.cpp | 18 +++++++++++++----- parser.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++------- parser.h | 12 +++++++----- runtime.cpp | 3 +-- runtime.h | 4 ++-- value.cpp | 25 ------------------------- value.h | 22 ---------------------- 7 files changed, 66 insertions(+), 68 deletions(-) delete mode 100644 value.cpp delete mode 100644 value.h diff --git a/main.cpp b/main.cpp index 9de9ce8..c39e469 100644 --- a/main.cpp +++ b/main.cpp @@ -19,14 +19,22 @@ int main(int argc, char** argv) { } std::string filename = std::string(argv[1]); - std::string input = readFile(filename); - std::cout << input; + std::string code = readFile(filename); - Parser parser(input); - parser.printTokens(); + std::string valueString; + std::string stdinLine; + while (std::cin >> stdinLine) { + valueString += stdinLine + " "; + } + + Parser parser; + std::vector tokens = parser.parseCode(code); + std::vector values = parser.parseValues(valueString); + parser.printTokens(tokens); + parser.printValues(values); Runtime runtime; - runtime.run(parser.getTokens()); + runtime.run(tokens); return 0; } 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; } diff --git a/parser.h b/parser.h index eab8354..cd39732 100644 --- a/parser.h +++ b/parser.h @@ -4,10 +4,12 @@ #include class Parser { - private: - std::vector m_tokens; public: - Parser(std::string&); - std::vector& getTokens(); - void printTokens(); + Parser(); + + std::vector parseValues(const std::string&); + std::vector parseCode(const std::string&); + + static void printTokens(const std::vector&); + static void printValues(const std::vector&); }; diff --git a/runtime.cpp b/runtime.cpp index b4db6a8..4edb3e6 100644 --- a/runtime.cpp +++ b/runtime.cpp @@ -1,5 +1,4 @@ #include "runtime.h" -#include "value.h" Runtime::Runtime() { @@ -14,7 +13,7 @@ bool Runtime::runCommand(const Token& token) { void Runtime::run(std::vector& tokens) { for (auto& token : tokens) { if (!runCommand(token)) { - Value value(token); + } } } diff --git a/runtime.h b/runtime.h index cdecd84..3572391 100644 --- a/runtime.h +++ b/runtime.h @@ -1,11 +1,11 @@ #pragma once -#include "value.h" #include +#include #include "token.h" class Runtime { private: - std::vector m_values; + std::stack m_values; bool runCommand(const Token& token); public: diff --git a/value.cpp b/value.cpp deleted file mode 100644 index a0f0acc..0000000 --- a/value.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "value.h" -#include - -Value::Value(Token& token) { - if (!token.isString()) { - m_type = Value::Type::Integer; - m_intData = std::stoi(token.getLexeme(), nullptr, 10); - } else { - m_type = Value::Type::String; - m_strData = token.getLexeme(); - } -} - - -const Value::Type Value::getType() const { - return m_type; -} - -const int Value::getIntData() const { - return m_intData; -} - -const std::string& Value::getStrData() const { - return m_strData; -} diff --git a/value.h b/value.h deleted file mode 100644 index 7d85d3c..0000000 --- a/value.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include -#include "token.h" - -class Value { - public: - enum Type { - Integer, - String - }; - - Value(Token& token); - const Value::Type getType() const; - const int getIntData() const; - const std::string& getStrData() const; - - private: - int m_intData; - std::string m_strData; - - Value::Type m_type; -}; -- cgit v1.3