blob: d4c9154384517f8ad48def6ebc957781500ba3b0 (
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
55
56
57
58
59
60
61
62
63
64
65
|
#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();
}
else if (lexeme == ":") {
doSplice();
}
}
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);
}
void Runtime::doSplice() {
int count = m_values.back();
m_values.pop_back();
std::vector<int> spliced(m_values.begin(), m_values.begin() + count);
m_values = spliced;
}
|