aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp2
-rw-r--r--src/runtime.cpp29
-rw-r--r--src/runtime.h7
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 <iostream>
Runtime::Runtime() {
@@ -6,20 +7,34 @@ Runtime::Runtime() {
void Runtime::initValues(const std::vector<int>& 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<Token>& 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<int> m_values;
- bool runCommand(const Token& token);
+ std::vector<int> m_values;
+ void runCommand(const Token& token);
+
+ void doSum();
public:
Runtime();
void initValues(const std::vector<int>& values);
void run(std::vector<Token>& tokens);
+ void printValues() const;
};