From 50bafbdd68160d00606a4e83d9759c21223c11f8 Mon Sep 17 00:00:00 2001 From: jantuomi Date: Thu, 1 Sep 2016 11:20:59 +0300 Subject: Restructure project --- Makefile | 5 ++-- main.cpp | 40 -------------------------- parser.cpp | 88 --------------------------------------------------------- parser.h | 15 ---------- runtime.cpp | 19 ------------- runtime.h | 14 --------- src/main.cpp | 40 ++++++++++++++++++++++++++ src/parser.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.h | 15 ++++++++++ src/runtime.cpp | 19 +++++++++++++ src/runtime.h | 14 +++++++++ src/token.cpp | 14 +++++++++ src/token.h | 13 +++++++++ token.cpp | 14 --------- token.h | 13 --------- 15 files changed, 206 insertions(+), 205 deletions(-) delete mode 100644 main.cpp delete mode 100644 parser.cpp delete mode 100644 parser.h delete mode 100644 runtime.cpp delete mode 100644 runtime.h create mode 100644 src/main.cpp create mode 100644 src/parser.cpp create mode 100644 src/parser.h create mode 100644 src/runtime.cpp create mode 100644 src/runtime.h create mode 100644 src/token.cpp create mode 100644 src/token.h delete mode 100644 token.cpp delete mode 100644 token.h diff --git a/Makefile b/Makefile index 6cfc87a..846cc13 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ CPPFLAGS=-std=c++11 -Wall CC=g++ TARGET=jarkko +SRCDIR=src all: jarkko -jarkko: *.cpp - $(CC) *.cpp $(CPPFLAGS) -o $(TARGET) +jarkko: $(SRCDIR)/*.cpp + $(CC) $(SRCDIR)/*.cpp $(CPPFLAGS) -o $(TARGET) clean: rm $(TARGET) diff --git a/main.cpp b/main.cpp deleted file mode 100644 index c39e469..0000000 --- a/main.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include -#include -#include "parser.h" -#include "runtime.h" - -std::string readFile(std::string& filename) { - std::ifstream t(filename); - std::string str((std::istreambuf_iterator(t)), - (std::istreambuf_iterator())); - 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 tokens = parser.parseCode(code); - std::vector values = parser.parseValues(valueString); - parser.printTokens(tokens); - parser.printValues(values); - - Runtime runtime; - runtime.run(tokens); - - return 0; -} diff --git a/parser.cpp b/parser.cpp deleted file mode 100644 index 2472f6d..0000000 --- a/parser.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "parser.h" -#include -#include -#include -#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 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; - 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& tokens) { - for (auto& token : tokens) { - std::cout << "Token[" << token.getLexeme() << "] "; - } - std::cout << std::endl; -} - -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 deleted file mode 100644 index cd39732..0000000 --- a/parser.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include -#include "token.h" -#include - -class Parser { - public: - 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 deleted file mode 100644 index 4edb3e6..0000000 --- a/runtime.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#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& tokens) { - for (auto& token : tokens) { - if (!runCommand(token)) { - - } - } -} diff --git a/runtime.h b/runtime.h deleted file mode 100644 index 3572391..0000000 --- a/runtime.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include -#include -#include "token.h" - -class Runtime { - private: - std::stack m_values; - bool runCommand(const Token& token); - - public: - Runtime(); - void run(std::vector& tokens); -}; 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 +#include +#include +#include +#include "parser.h" +#include "runtime.h" + +std::string readFile(std::string& filename) { + std::ifstream t(filename); + std::string str((std::istreambuf_iterator(t)), + (std::istreambuf_iterator())); + 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 tokens = parser.parseCode(code); + std::vector 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 +#include +#include +#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 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; + 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& tokens) { + for (auto& token : tokens) { + std::cout << "Token[" << token.getLexeme() << "] "; + } + std::cout << std::endl; +} + +void Parser::printValues(const std::vector& 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 +#include "token.h" +#include + +class Parser { + public: + 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/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& 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 +#include +#include "token.h" + +class Runtime { + private: + std::stack m_values; + bool runCommand(const Token& token); + + public: + Runtime(); + void run(std::vector& 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 + +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; +}; diff --git a/token.cpp b/token.cpp deleted file mode 100644 index 6e9e5e2..0000000 --- a/token.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#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/token.h b/token.h deleted file mode 100644 index 8ccb975..0000000 --- a/token.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include - -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; -}; -- cgit v1.3