diff options
| author | jantuomi <jans.tuomi@gmail.com> | 2016-09-01 11:20:59 +0300 |
|---|---|---|
| committer | jantuomi <jans.tuomi@gmail.com> | 2016-09-01 11:20:59 +0300 |
| commit | 50bafbdd68160d00606a4e83d9759c21223c11f8 (patch) | |
| tree | 66c58cfb012dc0b7d222a03e8c55507b75e9af1f /src | |
| parent | be011366f249b8ebed3905030ce3b7021597ee34 (diff) | |
Restructure project
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 40 | ||||
| -rw-r--r-- | src/parser.cpp | 88 | ||||
| -rw-r--r-- | src/parser.h | 15 | ||||
| -rw-r--r-- | src/runtime.cpp | 19 | ||||
| -rw-r--r-- | src/runtime.h | 14 | ||||
| -rw-r--r-- | src/token.cpp | 14 | ||||
| -rw-r--r-- | src/token.h | 13 |
7 files changed, 203 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c39e469 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,40 @@ +#include <iostream> +#include <fstream> +#include <streambuf> +#include <string> +#include "parser.h" +#include "runtime.h" + +std::string readFile(std::string& filename) { + std::ifstream t(filename); + std::string str((std::istreambuf_iterator<char>(t)), + (std::istreambuf_iterator<char>())); + return str; +} + +int main(int argc, char** argv) { + if (argc != 2) { + std::cout << "Please supply input file name as a parameter." << std::endl; + return 1; + } + + std::string filename = std::string(argv[1]); + std::string code = readFile(filename); + + 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(tokens); + + return 0; +} diff --git a/src/parser.cpp b/src/parser.cpp new file mode 100644 index 0000000..2472f6d --- /dev/null +++ b/src/parser.cpp @@ -0,0 +1,88 @@ +#include "parser.h" +#include <iostream> +#include <string> +#include <vector> +#include "token.h" + +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; + std::string tmp; + for (auto& c : input) { + if (std::isspace(c)) { + continue; + } + + if (c == '"' && !isString) { + isString = true; + continue; + } + + if (c == '"' && isString) { + Token token(tmp, true); + result.push_back(token); + tmp = ""; + isString = false; + continue; + } + + if (!isString) { + std::string parsed(1, c); + Token token(parsed, false); + result.push_back(token); + } + else { + tmp += c; + } + } + + return result; +} + +void Parser::printTokens(const std::vector<Token>& tokens) { + for (auto& token : tokens) { + std::cout << "Token[" << token.getLexeme() << "] "; + } + std::cout << std::endl; +} + +void Parser::printValues(const std::vector<int>& values) { + for (auto& value : values) { + std::cout << "Value[" << value << "] "; + } + std::cout << std::endl; +} diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 0000000..cd39732 --- /dev/null +++ b/src/parser.h @@ -0,0 +1,15 @@ +#pragma once +#include <string> +#include "token.h" +#include <vector> + +class Parser { + public: + 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/src/runtime.cpp b/src/runtime.cpp new file mode 100644 index 0000000..4edb3e6 --- /dev/null +++ b/src/runtime.cpp @@ -0,0 +1,19 @@ +#include "runtime.h" + +Runtime::Runtime() { + +} + +/* Returns false if token is not a command */ +bool Runtime::runCommand(const Token& token) { + // TODO + return false; +} + +void Runtime::run(std::vector<Token>& tokens) { + for (auto& token : tokens) { + if (!runCommand(token)) { + + } + } +} diff --git a/src/runtime.h b/src/runtime.h new file mode 100644 index 0000000..3572391 --- /dev/null +++ b/src/runtime.h @@ -0,0 +1,14 @@ +#pragma once +#include <vector> +#include <stack> +#include "token.h" + +class Runtime { + private: + std::stack<int> m_values; + bool runCommand(const Token& token); + + public: + Runtime(); + void run(std::vector<Token>& tokens); +}; diff --git a/src/token.cpp b/src/token.cpp new file mode 100644 index 0000000..6e9e5e2 --- /dev/null +++ b/src/token.cpp @@ -0,0 +1,14 @@ +#include "token.h" + +Token::Token(std::string& lexeme, bool isString) : + m_lexeme(lexeme), m_isString(isString) { + +} + +const std::string& Token::getLexeme() const { + return m_lexeme; +} + +bool Token::isString() const { + return m_isString; +} diff --git a/src/token.h b/src/token.h new file mode 100644 index 0000000..8ccb975 --- /dev/null +++ b/src/token.h @@ -0,0 +1,13 @@ +#pragma once +#include <string> + +class Token { + private: + std::string m_lexeme; + bool m_isString; + + public: + Token(std::string& lexeme, bool isString); + const std::string& getLexeme() const; + bool isString() const; +}; |
