From d1c4106bba1fa02bd4cb5bc8f777919565cc7aa1 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Tue, 22 Sep 2020 13:13:50 +0200 Subject: [PATCH] renamed _stack to _values, to make a clearer distinction between value and call stacks --- interpreter.cpp | 234 ++++++++++++++++++++++++------------------------ interpreter.hpp | 2 +- 2 files changed, 118 insertions(+), 118 deletions(-) diff --git a/interpreter.cpp b/interpreter.cpp index aa91e31..5599ccb 100644 --- a/interpreter.cpp +++ b/interpreter.cpp @@ -20,7 +20,7 @@ int to_int(const std::string& val) { void interpreter::reset() { _prog.clear(); - _stack.clear(); + _values.clear(); _labels.clear(); _vars.clear(); _calls.clear(); @@ -52,36 +52,36 @@ std::string interpreter::eval(std::istream& in, bool& done) { // check literal int if (std::isdigit(code[0])) { - _stack.push_back(code); + _values.push_back(code); _pc++; continue; } // check literal string if (code[0] == '\\') { - _stack.push_back(code.substr(1)); + _values.push_back(code.substr(1)); _pc++; continue; } // check label ref if (code[0] == '>') { - _stack.push_back(code.substr(1)); + _values.push_back(code.substr(1)); _pc++; continue; } // check var assignment if (code[0] == '=') { - _vars[code.substr(1)] = _stack.back(); - _stack.pop_back(); + _vars[code.substr(1)] = _values.back(); + _values.pop_back(); _pc++; continue; } // check var ref if (code[0] == '$') { - _stack.push_back(_vars[code.substr(1)]); + _values.push_back(_vars[code.substr(1)]); _pc++; continue; } @@ -90,8 +90,8 @@ std::string interpreter::eval(std::istream& in, bool& done) { exec_instruction(code, done); } - if (_stack.size()) { - result = _stack.back(); + if (_values.size()) { + result = _values.back(); } return result; } @@ -137,136 +137,136 @@ void interpreter::exec_instruction(const std::string& code, bool& done) { // integer operations ----------------------------------------------------- void interpreter::add() { - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(val1 + val2)); + int val2 {to_int(_values.back())}; + _values.pop_back(); + int val1 {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(val1 + val2)); _pc++; } void interpreter::sub() { - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(val1 - val2)); + int val2 {to_int(_values.back())}; + _values.pop_back(); + int val1 {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(val1 - val2)); _pc++; } void interpreter::mul() { - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(val1 * val2)); + int val2 {to_int(_values.back())}; + _values.pop_back(); + int val1 {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(val1 * val2)); _pc++; } void interpreter::div() { - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(val1 / val2)); + int val2 {to_int(_values.back())}; + _values.pop_back(); + int val1 {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(val1 / val2)); _pc++; } void interpreter::mod() { - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(val1 % val2)); + int val2 {to_int(_values.back())}; + _values.pop_back(); + int val1 {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(val1 % val2)); _pc++; } void interpreter::abs() { - int val {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(val < 0 ? -val : val)); + int val {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(val < 0 ? -val : val)); _pc++; } void interpreter::neg() { - int val {to_int(_stack.back())}; - _stack.pop_back(); - _stack.push_back(std::to_string(-val)); + int val {to_int(_values.back())}; + _values.pop_back(); + _values.push_back(std::to_string(-val)); _pc++; } void interpreter::inc() { - int val {to_int(_stack.back()) + 1}; - _stack.pop_back(); - _stack.push_back(std::to_string(val)); + int val {to_int(_values.back()) + 1}; + _values.pop_back(); + _values.push_back(std::to_string(val)); _pc++; } void interpreter::dec() { - int val {to_int(_stack.back()) - 1}; - _stack.pop_back(); - _stack.push_back(std::to_string(val)); + int val {to_int(_values.back()) - 1}; + _values.pop_back(); + _values.push_back(std::to_string(val)); _pc++; } // string operations ------------------------------------------------------ void interpreter::dup() { - _stack.push_back(_stack.back()); + _values.push_back(_values.back()); _pc++; } void interpreter::rev() { - std::string val {_stack.back()}; - _stack.pop_back(); + std::string val {_values.back()}; + _values.pop_back(); std::string result; for (auto it = val.rbegin(); it != val.rend(); ++it) { result += *it; } - _stack.push_back(result); + _values.push_back(result); _pc++; } void interpreter::slc() { - int to {to_int(_stack.back())}; - _stack.pop_back(); - int from {to_int(_stack.back())}; - _stack.pop_back(); - std::string val {_stack.back()}; - _stack.pop_back(); - _stack.push_back(val.substr(from, to - from)); + 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(); + _values.push_back(val.substr(from, to - from)); _pc++; } void interpreter::idx() { - int idx {to_int(_stack.back())}; - _stack.pop_back(); - std::string val {_stack.back()}; - _stack.pop_back(); + int idx {to_int(_values.back())}; + _values.pop_back(); + std::string val {_values.back()}; + _values.pop_back(); val = val[idx]; - _stack.emplace_back(val); + _values.emplace_back(val); _pc++; } void interpreter::cat() { - std::string val2 {_stack.back()}; - _stack.pop_back(); - std::string val1 {_stack.back()}; - _stack.pop_back(); - _stack.emplace_back(val1 + val2); + std::string val2 {_values.back()}; + _values.pop_back(); + std::string val1 {_values.back()}; + _values.pop_back(); + _values.emplace_back(val1 + val2); _pc++; } void interpreter::len() { - std::string val {_stack.back()}; - _stack.pop_back(); - _stack.push_back(std::to_string(val.size())); + std::string val {_values.back()}; + _values.pop_back(); + _values.push_back(std::to_string(val.size())); _pc++; } void interpreter::rot() { - std::string val {_stack.back()}; - _stack.pop_back(); + std::string val {_values.back()}; + _values.pop_back(); std::transform(val.begin(), val.end(), val.begin(), [](char ch) -> char { if (ch >= 'a' && ch <= 'm') { return ch + 13; @@ -279,32 +279,32 @@ void interpreter::rot() { } return ch; }); - _stack.push_back(val); + _values.push_back(val); _pc++; } void interpreter::enl() { - std::string val {_stack.back()}; - _stack.pop_back(); + std::string val {_values.back()}; + _values.pop_back(); val += '\n'; - _stack.push_back(val); + _values.push_back(val); _pc++; } // tests & jumps ---------------------------------------------------------- void interpreter::gto() { - _pc = _labels[_stack.back()]; - _stack.pop_back(); + _pc = _labels[_values.back()]; + _values.pop_back(); } void interpreter::geq() { - std::string label {_stack.back()}; - _stack.pop_back(); - std::string val2 {_stack.back()}; - _stack.pop_back(); - std::string val1 {_stack.back()}; - _stack.pop_back(); + std::string label {_values.back()}; + _values.pop_back(); + std::string val2 {_values.back()}; + _values.pop_back(); + std::string val1 {_values.back()}; + _values.pop_back(); if (val1 == val2) { _pc = _labels[label]; } else { @@ -313,12 +313,12 @@ void interpreter::geq() { } void interpreter::gne() { - std::string label {_stack.back()}; - _stack.pop_back(); - std::string val2 {_stack.back()}; - _stack.pop_back(); - std::string val1 {_stack.back()}; - _stack.pop_back(); + std::string label {_values.back()}; + _values.pop_back(); + std::string val2 {_values.back()}; + _values.pop_back(); + std::string val1 {_values.back()}; + _values.pop_back(); if (val1 != val2) { _pc = _labels[label]; } else { @@ -327,12 +327,12 @@ void interpreter::gne() { } void interpreter::glt() { - std::string label {_stack.back()}; - _stack.pop_back(); - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); + 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(); if (val1 < val2) { _pc = _labels[label]; } else { @@ -341,12 +341,12 @@ void interpreter::glt() { } void interpreter::gle() { - std::string label {_stack.back()}; - _stack.pop_back(); - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); + 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(); if (val1 <= val2) { _pc = _labels[label]; } else { @@ -355,12 +355,12 @@ void interpreter::gle() { } void interpreter::ggt() { - std::string label {_stack.back()}; - _stack.pop_back(); - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); + 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(); if (val1 > val2) { _pc = _labels[label]; } else { @@ -369,12 +369,12 @@ void interpreter::ggt() { } void interpreter::gge() { - std::string label {_stack.back()}; - _stack.pop_back(); - int val2 {to_int(_stack.back())}; - _stack.pop_back(); - int val1 {to_int(_stack.back())}; - _stack.pop_back(); + 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(); if (val1 >= val2) { _pc = _labels[label]; } else { @@ -399,16 +399,16 @@ void interpreter::ret() { void interpreter::inp() { std::string val; std::cin >> val; - _stack.push_back(val); + _values.push_back(val); _pc++; } void interpreter::out() { - std::cout << _stack.back() << '\n'; + std::cout << _values.back() << '\n'; _pc++; } void interpreter::err() { - std::cerr << _stack.back() << '\n'; + std::cerr << _values.back() << '\n'; _pc++; } diff --git a/interpreter.hpp b/interpreter.hpp index ad0049a..3415dfb 100644 --- a/interpreter.hpp +++ b/interpreter.hpp @@ -33,7 +33,7 @@ class interpreter { private: std::vector _prog; // program lines without label defs or comments - std::vector _stack; // value stack + std::vector _values; // value stack std::map _labels; // label name => prog line index std::map _vars; // var name => string value std::vector _calls; // call stack