From 53e30cdd779f12ba5b19a59e9e18abf69c795ad5 Mon Sep 17 00:00:00 2001 From: jantuomi Date: Thu, 1 Sep 2016 13:47:24 +0300 Subject: Add sum function --- src/main.cpp | 2 ++ src/runtime.cpp | 29 ++++++++++++++++++++++------- src/runtime.h | 7 +++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 53a6540..8a00d55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,5 +37,7 @@ int main(int argc, char** argv) { runtime.initValues(values); runtime.run(tokens); + runtime.printValues(); + return 0; } diff --git a/src/runtime.cpp b/src/runtime.cpp index 26c5885..3f61060 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -1,4 +1,5 @@ #include "runtime.h" +#include Runtime::Runtime() { @@ -6,20 +7,34 @@ Runtime::Runtime() { void Runtime::initValues(const std::vector& values) { for (const int i : values) { - m_values.push(i); + m_values.push_back(i); } } -/* Returns false if token is not a command */ -bool Runtime::runCommand(const Token& token) { - // TODO - return false; +void Runtime::runCommand(const Token& token) { + if (token.getLexeme() == "S") { + doSum(); + } } void Runtime::run(std::vector& tokens) { for (auto& token : tokens) { - if (!runCommand(token)) { + runCommand(token); + } +} - } +void Runtime::printValues() const { + for (const int value : m_values) { + std::cout << value << " "; } + std::cout << std::endl; +} + +void Runtime::doSum() { + int sum = 0; + for (const int i : m_values) { + sum += i; + } + + m_values.push_back(sum); } diff --git a/src/runtime.h b/src/runtime.h index af8351f..16c1c28 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -5,11 +5,14 @@ class Runtime { private: - std::stack m_values; - bool runCommand(const Token& token); + std::vector m_values; + void runCommand(const Token& token); + + void doSum(); public: Runtime(); void initValues(const std::vector& values); void run(std::vector& tokens); + void printValues() const; }; -- cgit v1.3