blob: 7519ea9c065e642ba962544fad420cef9cb51e45 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#include "runtime.h"
#include <iostream>
Runtime::Runtime() {
}
void Runtime::initValues(const std::vector<int>& values) {
for (const int i : values) {
m_values.push_back(i);
}
}
void Runtime::runCommand(const Token& token) {
const std::string lexeme = token.getLexeme();
if (lexeme == "S") {
doSum();
}
else if (lexeme == "o") {
doPrint();
}
else if (lexeme == "O") {
doPrintAscii();
}
}
void Runtime::run(std::vector<Token>& tokens) {
for (auto& token : tokens) {
runCommand(token);
}
}
void Runtime::doPrint() const {
for (const int value : m_values) {
std::cout << value << " ";
}
std::cout << std::endl;
}
void Runtime::doPrintAscii() const {
for (const int value : m_values) {
std::cout << static_cast<char>(value);
}
std::cout << std::endl;
}
void Runtime::doSum() {
int sum = 0;
for (const int i : m_values) {
sum += i;
}
m_values.push_back(sum);
}
|