added convenience methods pop_int and pop_str, which saved a lot of code

This commit is contained in:
Bob Polis 2020-09-22 14:12:39 +02:00
parent 7a6f9df82f
commit a20f002341
2 changed files with 62 additions and 92 deletions

View File

@ -135,78 +135,76 @@ void interpreter::exec_instruction(const std::string& code, bool& done) {
}
}
inline int interpreter::pop_int() {
int val {to_int(_values.back())};
_values.pop_back();
return val;
}
inline std::string interpreter::pop_str() {
auto val = _values.back();
_values.pop_back();
return val;
}
// integer operations -----------------------------------------------------
void interpreter::add() {
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto val2 = pop_int();
auto val1 = pop_int();
_values.push_back(std::to_string(val1 + val2));
_pc++;
}
void interpreter::sub() {
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto val2 = pop_int();
auto val1 = pop_int();
_values.push_back(std::to_string(val1 - val2));
_pc++;
}
void interpreter::mul() {
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto val2 = pop_int();
auto val1 = pop_int();
_values.push_back(std::to_string(val1 * val2));
_pc++;
}
void interpreter::div() {
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto val2 = pop_int();
auto val1 = pop_int();
_values.push_back(std::to_string(val1 / val2));
_pc++;
}
void interpreter::mod() {
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto val2 = pop_int();
auto val1 = pop_int();
_values.push_back(std::to_string(val1 % val2));
_pc++;
}
void interpreter::abs() {
int val {to_int(_values.back())};
_values.pop_back();
auto val = pop_int();
_values.push_back(std::to_string(val < 0 ? -val : val));
_pc++;
}
void interpreter::neg() {
int val {to_int(_values.back())};
_values.pop_back();
auto val = pop_int();
_values.push_back(std::to_string(-val));
_pc++;
}
void interpreter::inc() {
int val {to_int(_values.back()) + 1};
_values.pop_back();
_values.push_back(std::to_string(val));
auto val = pop_int();
_values.push_back(std::to_string(val + 1));
_pc++;
}
void interpreter::dec() {
int val {to_int(_values.back()) - 1};
_values.pop_back();
_values.push_back(std::to_string(val));
auto val = pop_int();
_values.push_back(std::to_string(val - 1));
_pc++;
}
@ -218,8 +216,7 @@ void interpreter::dup() {
}
void interpreter::rev() {
std::string val {_values.back()};
_values.pop_back();
auto val = pop_str();
std::string result;
for (auto it = val.rbegin(); it != val.rend(); ++it) {
result += *it;
@ -229,45 +226,36 @@ void interpreter::rev() {
}
void interpreter::slc() {
int to {to_int(_values.back())};
_values.pop_back();
int from {to_int(_values.back())};
_values.pop_back();
std::string val {_values.back()};
_values.pop_back();
auto to = pop_int();
auto from = pop_int();
auto val = pop_str();
_values.push_back(val.substr(from, to - from));
_pc++;
}
void interpreter::idx() {
int idx {to_int(_values.back())};
_values.pop_back();
std::string val {_values.back()};
_values.pop_back();
auto idx = pop_int();
auto val = pop_str();
val = val[idx];
_values.emplace_back(val);
_pc++;
}
void interpreter::cat() {
std::string val2 {_values.back()};
_values.pop_back();
std::string val1 {_values.back()};
_values.pop_back();
auto val2 = pop_str();
auto val1 = pop_str();
_values.emplace_back(val1 + val2);
_pc++;
}
void interpreter::len() {
std::string val {_values.back()};
_values.pop_back();
auto val = pop_str();
_values.push_back(std::to_string(val.size()));
_pc++;
}
void interpreter::rot() {
std::string val {_values.back()};
_values.pop_back();
auto val = pop_str();
std::transform(val.begin(), val.end(), val.begin(), [](char ch) -> char {
if (ch >= 'a' && ch <= 'm') {
return ch + 13;
@ -285,27 +273,21 @@ void interpreter::rot() {
}
void interpreter::enl() {
std::string val {_values.back()};
_values.pop_back();
val += '\n';
_values.push_back(val);
auto val = pop_str();
_values.emplace_back(val + '\n');
_pc++;
}
// tests & jumps ----------------------------------------------------------
void interpreter::gto() {
_pc = _labels[_values.back()];
_values.pop_back();
_pc = _labels[pop_str()];
}
void interpreter::geq() {
std::string label {_values.back()};
_values.pop_back();
std::string val2 {_values.back()};
_values.pop_back();
std::string val1 {_values.back()};
_values.pop_back();
auto label = pop_str();
auto val2 = pop_str();
auto val1 = pop_str();
if (val1 == val2) {
_pc = _labels[label];
} else {
@ -314,12 +296,9 @@ void interpreter::geq() {
}
void interpreter::gne() {
std::string label {_values.back()};
_values.pop_back();
std::string val2 {_values.back()};
_values.pop_back();
std::string val1 {_values.back()};
_values.pop_back();
auto label = pop_str();
auto val2 = pop_str();
auto val1 = pop_str();
if (val1 != val2) {
_pc = _labels[label];
} else {
@ -328,12 +307,9 @@ void interpreter::gne() {
}
void interpreter::glt() {
std::string label {_values.back()};
_values.pop_back();
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto label = pop_str();
auto val2 = pop_int();
auto val1 = pop_int();
if (val1 < val2) {
_pc = _labels[label];
} else {
@ -342,12 +318,9 @@ void interpreter::glt() {
}
void interpreter::gle() {
std::string label {_values.back()};
_values.pop_back();
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto label = pop_str();
auto val2 = pop_int();
auto val1 = pop_int();
if (val1 <= val2) {
_pc = _labels[label];
} else {
@ -356,12 +329,9 @@ void interpreter::gle() {
}
void interpreter::ggt() {
std::string label {_values.back()};
_values.pop_back();
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto label = pop_str();
auto val2 = pop_int();
auto val1 = pop_int();
if (val1 > val2) {
_pc = _labels[label];
} else {
@ -370,12 +340,9 @@ void interpreter::ggt() {
}
void interpreter::gge() {
std::string label {_values.back()};
_values.pop_back();
int val2 {to_int(_values.back())};
_values.pop_back();
int val1 {to_int(_values.back())};
_values.pop_back();
auto label = pop_str();
auto val2 = pop_int();
auto val1 = pop_int();
if (val1 >= val2) {
_pc = _labels[label];
} else {

View File

@ -41,6 +41,9 @@ class interpreter {
void reset();
void exec_instruction(const std::string& code, bool& done);
int pop_int();
std::string pop_str();
// integer operations
void add();