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

View File

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