diff options
| -rw-r--r-- | main.cpp | 18 | ||||
| -rw-r--r-- | parser.cpp | 50 | ||||
| -rw-r--r-- | parser.h | 12 | ||||
| -rw-r--r-- | runtime.cpp | 3 | ||||
| -rw-r--r-- | runtime.h | 4 | ||||
| -rw-r--r-- | value.cpp | 25 | ||||
| -rw-r--r-- | value.h | 22 |
7 files changed, 66 insertions, 68 deletions
@@ -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<Token> tokens = parser.parseCode(code); + std::vector<int> values = parser.parseValues(valueString); + parser.printTokens(tokens); + parser.printValues(values); Runtime runtime; - runtime.run(parser.getTokens()); + runtime.run(tokens); return 0; } @@ -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; } @@ -4,10 +4,12 @@ #include <vector> class Parser { - private: - std::vector<Token> m_tokens; public: - Parser(std::string&); - std::vector<Token>& getTokens(); - void printTokens(); + Parser(); + + std::vector<int> parseValues(const std::string&); + std::vector<Token> parseCode(const std::string&); + + static void printTokens(const std::vector<Token>&); + static void printValues(const std::vector<int>&); }; 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<Token>& tokens) { for (auto& token : tokens) { if (!runCommand(token)) { - Value value(token); + } } } @@ -1,11 +1,11 @@ #pragma once -#include "value.h" #include <vector> +#include <stack> #include "token.h" class Runtime { private: - std::vector<Value> m_values; + std::stack<int> 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 <string> - -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 <string> -#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; -}; |
