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/runtime.cpp | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'src/runtime.cpp') 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); } -- cgit v1.3