From e0665fc94ff6add2054618d5cf155630f815bd5e Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 25 Feb 2022 11:15:15 +0100 Subject: [PATCH 1/6] Removed some cpplint warnings --- Changelog.md | 1 + src/CPPLINT.cfg | 8 ++++-- src/input.hpp | 3 ++- src/lexer.hpp | 6 +++-- src/main.cpp | 2 +- src/mpreal-out.hpp | 5 ++-- src/object.hpp | 14 ++++++---- src/program.hpp | 2 +- src/rpn-general.cpp | 52 ++++++++++++++++++-------------------- src/rpn-logs.cpp | 10 ++++---- src/rpn-test-framework.cpp | 14 +++++----- src/rpn-trig.cpp | 6 ++--- 12 files changed, 66 insertions(+), 57 deletions(-) diff --git a/Changelog.md b/Changelog.md index 9b89648..db3e782 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,7 @@ Changelog - static and global variables - common patterns (ex: no static maps or vectors) - classes (explicit) + - naming: file names (.cc .h), types (PascalCase), variables (snake_case), members (trailing _), static const (camelCase begining with k), enum (enum class, values like static const) - cpplint used with a CPPLINT.cfg removing some warnings - Test files are now markdown (.md) files, tests result are slightly changed - Delivery as flatpak and snap diff --git a/src/CPPLINT.cfg b/src/CPPLINT.cfg index d771828..1910e8d 100644 --- a/src/CPPLINT.cfg +++ b/src/CPPLINT.cfg @@ -2,6 +2,10 @@ set noparent filter=-whitespace/line_length -// avoid "Is this a non-const reference? If so, make const or use a pointer" -// as it seems to be allowed by google now , cf https://github.com/cpplint/cpplint/issues/148 +# avoid "Is this a non-const reference? If so, make const or use a pointer" +# as it seems to be allowed by google now , cf https://github.com/cpplint/cpplint/issues/148 filter=-runtime/references + +# avoid "Include the directory when naming header files" +# for files not being in a subdirectory +filter=-build/include_subdir diff --git a/src/input.hpp b/src/input.hpp index cec0079..bd56db0 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -6,7 +6,8 @@ #include #include #include -using namespace std; +using std::vector; +using std::string; #include "linenoise.h" diff --git a/src/lexer.hpp b/src/lexer.hpp index 08fc468..18455f2 100644 --- a/src/lexer.hpp +++ b/src/lexer.hpp @@ -4,12 +4,14 @@ #define SRC_LEXER_HPP_ #include -using namespace mpfr; +using mpfr::mpreal; #include #include #include -using namespace std; +using std::map; +using std::string; +using std::vector; #include "object.hpp" diff --git a/src/main.cpp b/src/main.cpp index a88ee26..10a6722 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include #include #include -using namespace std; +using std::cerr; // internal includes #include "input.hpp" diff --git a/src/mpreal-out.hpp b/src/mpreal-out.hpp index cac8f10..c5f5e89 100644 --- a/src/mpreal-out.hpp +++ b/src/mpreal-out.hpp @@ -4,11 +4,12 @@ #define SRC_MPREAL_OUT_HPP_ #include -using namespace mpfr; +using mpfr::mpreal; #include #include -using namespace std; +using std::string; +using std::ostream; ostream& mpreal_output10base(ostream& out, const string& fmt, const mpreal& value); ostream& mpreal_outputNbase(ostream& out, int base, const mpreal& value); diff --git a/src/object.hpp b/src/object.hpp index 0c52665..77a7af8 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -4,14 +4,18 @@ #define SRC_OBJECT_HPP_ #include - -#include "mpreal-out.hpp" -using namespace mpfr; +using mpfr::mpreal; #include -#include #include -using namespace std; +#include +#include +using std::ostream; +using std::string; +using std::stringstream; +using std::complex; + +#include "mpreal-out.hpp" // definitions for objects /// diff --git a/src/program.hpp b/src/program.hpp index 3571bbf..a5d6d84 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -11,7 +11,7 @@ using namespace std; #include -using namespace mpfr; +using mpfr::mpreal; // internal includes #include "lexer.hpp" diff --git a/src/rpn-general.cpp b/src/rpn-general.cpp index cd04390..a493550 100644 --- a/src/rpn-general.cpp +++ b/src/rpn-general.cpp @@ -1,39 +1,36 @@ // Copyright (c) 2014-2022 Louis Rubet -#include +#include #include +#include +using std::cout; +using std::string; +using std::pair; #include "linenoise.h" -using namespace std; - #include "program.hpp" #include "version.h" // description -static const string ATTR_BOLD = "\33[1m"; -static const string ATTR_OFF = "\33[0m"; +#define ATTR_BOLD "\33[1m" +#define ATTR_OFF "\33[0m" #define XSTR(a) STR(a) #define STR(a) #a -static const string _description{ATTR_BOLD + "R" + ATTR_OFF + "everse " + ATTR_BOLD + "P" + ATTR_OFF + "olish " + - ATTR_BOLD + "N" + ATTR_OFF + "otation language\n\nusing " + ATTR_BOLD + "GMP" + - ATTR_OFF + - " v" XSTR(__GNU_MP_VERSION) "." XSTR(__GNU_MP_VERSION_MINOR) "." XSTR( - __GNU_MP_VERSION_PATCHLEVEL) " under GNU LGPL\n" + - ATTR_BOLD + "MPFR" + ATTR_OFF + " v" MPFR_VERSION_STRING " under GNU LGPL\nand " + - ATTR_BOLD + "linenoise-ng" + ATTR_OFF + " v" LINENOISE_VERSION " under BSD\n"}; +static const char _description[] = ATTR_BOLD + "R" ATTR_OFF "everse " ATTR_BOLD "P" ATTR_OFF "olish " ATTR_BOLD "N" ATTR_OFF "otation language\n\nusing " ATTR_BOLD + "GMP" ATTR_OFF " v" XSTR(__GNU_MP_VERSION) "." XSTR(__GNU_MP_VERSION_MINOR) "." XSTR( + __GNU_MP_VERSION_PATCHLEVEL) " under GNU LGPL\n" ATTR_BOLD "MPFR" ATTR_OFF " v" MPFR_VERSION_STRING + " under GNU LGPL\nand " ATTR_BOLD "linenoise-ng" ATTR_OFF " v" LINENOISE_VERSION + " under BSD\n"; -// syntax -static const string _syntax{ATTR_BOLD + "Syntax" + ATTR_OFF + - ": rpn [command]\nwith optional command = list of commands"}; +static const char _syntax[] = ATTR_BOLD "Syntax" ATTR_OFF ": rpn [command]\nwith optional command = list of commands"; -static const map _mpfr_round{{"nearest (even)", MPFR_RNDN}, - {"toward zero", MPFR_RNDZ}, - {"toward +inf", MPFR_RNDU}, - {"toward -inf", MPFR_RNDD}, - {"away from zero", MPFR_RNDA}, - {"faithful rounding", MPFR_RNDF}, - {"nearest (away from zero)", MPFR_RNDNA}}; +#define MPFR_ROUND \ + {"nearest (even)", MPFR_RNDN}, {"toward zero", MPFR_RNDZ}, {"toward +inf", MPFR_RNDU}, {"toward -inf", MPFR_RNDD}, \ + {"away from zero", MPFR_RNDA}, {"faithful rounding", MPFR_RNDF}, { \ + "nearest (away from zero)", MPFR_RNDNA \ + } /// @brief nop keyword implementation /// @@ -90,7 +87,8 @@ void program::rpn_help() { // bits precision, decimal digits and rounding mode cout << " with " << number::s_digits << " digits after the decimal point" << endl; cout << "Current floating point precision is " << static_cast(mpreal::get_default_prec()) << " bits" << endl; - for (auto& rn : _mpfr_round) + vector> rnd{MPFR_ROUND}; + for (auto& rn : rnd) if (rn.second == mpreal::get_default_rnd()) { cout << "Current rounding mode is '" << rn.first << '\'' << endl; break; @@ -104,9 +102,7 @@ void program::rpn_help() { /// @return true the precision is good /// @return false the precision is not good /// -static bool check_decimal_digits(int precision) { - return precision >= 0; -} +static bool check_decimal_digits(int precision) { return precision >= 0; } /// @brief std keyword implementation /// @@ -209,7 +205,7 @@ void program::rpn_precision() { // modify digits seen by user if std mode if (number::s_mode == number::std) { // calc max nb of digits user can see with the current bit precision - number::s_digits = bits2digits(mpreal::get_default_prec()); + number::s_digits = mpfr::bits2digits(mpreal::get_default_prec()); } _stack.pop(); } else { @@ -223,7 +219,7 @@ void program::rpn_round() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_string); - map matchRound{_mpfr_round}; + map matchRound{MPFR_ROUND}; auto found = matchRound.find(_stack.value(0)); if (found != matchRound.end()) diff --git a/src/rpn-logs.cpp b/src/rpn-logs.cpp index 0ee199b..ffbad53 100644 --- a/src/rpn-logs.cpp +++ b/src/rpn-logs.cpp @@ -4,7 +4,7 @@ /// @brief e keyword implementation /// -void program::rpn_e(void) { _stack.push(new number(const_euler())); } +void program::rpn_e(void) { _stack.push(new number(mpfr::const_euler())); } /// @brief log10 keyword implementation /// @@ -35,9 +35,9 @@ void program::rpn_alog10() { void program::rpn_log2() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = log(_stack.value(0)) / const_log2(); + _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = log(_stack.value(0)) / const_log2(); + _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); else setErrorContext(ret_bad_operand_type); } @@ -47,9 +47,9 @@ void program::rpn_log2() { void program::rpn_alog2() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = exp(const_log2() * _stack.value(0)); + _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = exp(const_log2() * _stack.value(0)); + _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); else setErrorContext(ret_bad_operand_type); } diff --git a/src/rpn-test-framework.cpp b/src/rpn-test-framework.cpp index 60d26b1..0685e8b 100644 --- a/src/rpn-test-framework.cpp +++ b/src/rpn-test-framework.cpp @@ -1,21 +1,21 @@ // Copyright (c) 2014-2022 Louis Rubet #include -using namespace std; +using std::string; #include "program.hpp" #include "version.h" // foreground colors -static const string FG_RED = "\33[31m"; -static const string FG_GREEN = "\33[32m"; -static const string COLOR_OFF = "\33[m"; +static const char FG_RED[] = "\33[31m"; +static const char FG_GREEN[] = "\33[32m"; +static const char COLOR_OFF[] = "\33[m"; -static void _findAndReplaceAll(std::string& data, std::string toSearch, std::string replaceStr) { +static void findAndReplaceAll(string& data, string toSearch, string replaceStr) { // Get the first occurrence size_t pos = data.find(toSearch); // Repeat till end is reached - while (pos != std::string::npos) { + while (pos != string::npos) { // Replace this occurrence of Sub String data.replace(pos, toSearch.size(), replaceStr); // Get the next occurrence from the current position @@ -242,7 +242,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail failed = true; } else { // parse entry and run line - _findAndReplaceAll(entry, "`", ""); + findAndReplaceAll(entry, "`", ""); if (!entry.empty()) { program prog(stk, hp); ret = prog.parse(entry); diff --git a/src/rpn-trig.cpp b/src/rpn-trig.cpp index 678c42d..ebce9d6 100644 --- a/src/rpn-trig.cpp +++ b/src/rpn-trig.cpp @@ -5,7 +5,7 @@ /// @brief pi keyword implementation /// void program::rpn_pi(void) { - _stack.push_front(new number(const_pi())); + _stack.push_front(new number(mpfr::const_pi())); } /// @brief d->r keyword implementation @@ -13,7 +13,7 @@ void program::rpn_pi(void) { void program::rpn_d2r(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) *= const_pi(); + _stack.value(0) *= mpfr::const_pi(); _stack.value(0) /= 180; } @@ -22,7 +22,7 @@ void program::rpn_d2r(void) { void program::rpn_r2d(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) /= const_pi(); + _stack.value(0) /= mpfr::const_pi(); _stack.value(0) *= 180; } From d0c7304ffdf3e5dd8c7e41c1c67ae41f5a68044e Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 25 Feb 2022 12:19:25 +0100 Subject: [PATCH 2/6] PascalCase classes, snake_case variables, (some) PAscalCase functions --- Changelog.md | 4 +- src/input.cpp | 15 ++-- src/input.hpp | 6 +- src/lexer.cpp | 72 ++++++++++---------- src/lexer.hpp | 18 ++--- src/main.cpp | 18 ++--- src/mpreal-out.cpp | 4 +- src/mpreal-out.hpp | 4 +- src/object.cpp | 4 +- src/object.hpp | 122 +++++++++++++++------------------ src/program.cpp | 83 +++++++++++----------- src/program.hpp | 32 ++++----- src/rpn-branch.cpp | 88 ++++++++++++------------ src/rpn-complex.cpp | 24 +++---- src/rpn-general.cpp | 42 ++++++------ src/rpn-logs.cpp | 58 ++++++++-------- src/rpn-program.cpp | 34 +++++----- src/rpn-real.cpp | 136 ++++++++++++++++++------------------- src/rpn-stack.cpp | 20 +++--- src/rpn-store.cpp | 28 ++++---- src/rpn-string.cpp | 28 ++++---- src/rpn-test-framework.cpp | 2 +- src/rpn-test.cpp | 52 +++++++------- src/rpn-time.cpp | 12 ++-- src/rpn-trig.cpp | 34 +++++----- src/stack.hpp | 12 ++-- 26 files changed, 472 insertions(+), 480 deletions(-) diff --git a/Changelog.md b/Changelog.md index db3e782..30360a0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,7 +15,9 @@ Changelog - static and global variables - common patterns (ex: no static maps or vectors) - classes (explicit) - - naming: file names (.cc .h), types (PascalCase), variables (snake_case), members (trailing _), static const (camelCase begining with k), enum (enum class, values like static const) + - naming: + - file names (.cc .h), types (PascalCase), variables (snake_case), members (trailing _), static const (camelCase begining with k), enum (enum class, values like static const + - consistent comments (//), class comments, functions comments - cpplint used with a CPPLINT.cfg removing some warnings - Test files are now markdown (.md) files, tests result are slightly changed - Delivery as flatpak and snap diff --git a/src/input.cpp b/src/input.cpp index c41f1bf..6a0fa67 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -4,21 +4,22 @@ #include -vector* Input::_acWords = nullptr; +vector* Input::_ac_list = nullptr; -Input::Input(string& entry, vector& autocompletionWords, string prompt, string mlPrompt) : status(cont) { +Input::Input(string& entry, vector& autocompletion_list, string prompt, string multiline_prompt) + : status(cont) { char* c_entry = nullptr; bool multiline = false; int entry_len; - _acWords = &autocompletionWords; + _ac_list = &autocompletion_list; // linenoise for entry linenoiseSetCompletionCallback(entry_completion_generator); while (status == cont) { // get user entry if (multiline) - c_entry = linenoise(mlPrompt.c_str(), &entry_len); + c_entry = linenoise(multiline_prompt.c_str(), &entry_len); else c_entry = linenoise(prompt.c_str(), &entry_len); @@ -53,16 +54,16 @@ Input::Input(string& entry, vector& autocompletionWords, string prompt, /// @param lc the completion object to add strings with linenoiseAddCompletion() /// void Input::entry_completion_generator(const char* text, linenoiseCompletions* lc) { - if (Input::_acWords == nullptr || text == nullptr) return; + if (Input::_ac_list == nullptr || text == nullptr) return; int text_len = strnlen(text, 6); if (text_len == 0) // propose all keywords - for (string& ac : *Input::_acWords) linenoiseAddCompletion(lc, ac.c_str()); + for (string& ac : *Input::_ac_list) linenoiseAddCompletion(lc, ac.c_str()); else // propose only keywords matching to text begining - for (string& ac : *Input::_acWords) + for (string& ac : *Input::_ac_list) // compare list entry with text, return if match if (ac.compare(0, text_len, text) == 0) linenoiseAddCompletion(lc, ac.c_str()); } diff --git a/src/input.hpp b/src/input.hpp index bd56db0..f1d9e19 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -6,21 +6,21 @@ #include #include #include -using std::vector; using std::string; +using std::vector; #include "linenoise.h" class Input { public: - Input(string& entry, vector& autocompletionWords, string prompt = "rpn> ", string mlPrompt = ">"); + Input(string& entry, vector& autocompletion_list, string prompt = "rpn> ", string multiline_prompt = ">"); enum { ok, cont, abort, ctrlc, error } status; static void preload(const char* preloadText); private: static void entry_completion_generator(const char* text, linenoiseCompletions* lc); - static vector* _acWords; + static vector* _ac_list; }; #endif // SRC_INPUT_HPP_ diff --git a/src/lexer.cpp b/src/lexer.cpp index e4428f3..747a5e2 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -49,39 +49,39 @@ void Lexer::trim(string& s) { s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end()); } -bool Lexer::parseString(string& entry, size_t idx, size_t& nextIdx, vector& errors, +bool Lexer::parseString(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { // here we are sure that entry[0] is at least '"' for (size_t i = idx + 1; i < entry.size(); i++) { if (entry[i] == '"') { if (entry[i] - 1 != '\\') { elements.push_back({cmd_string, .value = entry.substr(idx + 1, i - idx - 1)}); - nextIdx = i + 1; + next_idx = i + 1; return true; } } } elements.push_back({cmd_string, .value = entry.substr(idx + 1, entry.size() - idx - 1)}); - nextIdx = entry.size(); + next_idx = entry.size(); return true; } -bool Lexer::parseSymbol(string& entry, size_t idx, size_t& nextIdx, vector& errors, +bool Lexer::parseSymbol(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { // here we are sure that entry[0] is at least '\'' for (size_t i = idx + 1; i < entry.size(); i++) { if (entry[i] == '\'') { elements.push_back({cmd_symbol, .value = entry.substr(idx + 1, i - idx - 1), .autoEval = false}); - nextIdx = i + 1; + next_idx = i + 1; return true; } } elements.push_back({cmd_symbol, .value = entry.substr(idx + 1, entry.size() - idx - 1)}); - nextIdx = entry.size(); + next_idx = entry.size(); return true; } -bool Lexer::parseProgram(string& entry, size_t idx, size_t& nextIdx, vector& errors, +bool Lexer::parseProgram(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { // here we are sure that entry is at least "<<" // find last ">>" or "»" @@ -94,7 +94,7 @@ bool Lexer::parseProgram(string& entry, size_t idx, size_t& nextIdx, vector= entry.size()) return 10; if (entry[scan] == '+') { scan++; - nextIdx = scan; + next_idx = scan; } else if (entry[scan] == '-') { scan++; - nextIdx = scan; + next_idx = scan; positive = false; } if (scan + 2 >= entry.size()) return 10; @@ -131,38 +131,38 @@ int Lexer::getBaseAt(string& entry, size_t& nextIdx, bool& positive) { if (scan + 2 < entry.size()) c = entry[scan + 2]; if (a == '0') { if (b == 'x' || b == 'X') { - nextIdx = scan + 2; + next_idx = scan + 2; return 16; } if (b == 'b' || b == 'B') { - nextIdx = scan + 2; + next_idx = scan + 2; return 2; } } else if (isdigit(a)) { if (b == 'b' || b == 'B') { - nextIdx = scan + 2; + next_idx = scan + 2; return static_cast(a - '0'); } if (isdigit(b) && (c == 'b' || c == 'B')) { - nextIdx = scan + 3; + next_idx = scan + 3; return 10 * static_cast(a - '0') + static_cast(b - '0'); } } return 10; } -bool Lexer::getNumberAt(string& entry, size_t idx, size_t& nextIdx, int& base, mpreal** r, char delim) { +bool Lexer::getNumberAt(string& entry, size_t idx, size_t& next_idx, int& base, mpreal** r, char delim) { stringstream ss; int idxNumber = 0; string token; bool positive = true; - nextIdx = idx; + next_idx = idx; ss.str(entry.substr(idx)); if (getline(ss, token, delim)) { size_t numberIdx; - nextIdx = token.size() + idx + 1; + next_idx = token.size() + idx + 1; trim(token); base = getBaseAt(token, numberIdx, positive); if (base < 2 || base > 62) return false; @@ -176,15 +176,15 @@ bool Lexer::getNumberAt(string& entry, size_t idx, size_t& nextIdx, int& base, m return false; } } - nextIdx = token.size() + idx + 1; + next_idx = token.size() + idx + 1; return false; } -bool Lexer::parseNumber(string& entry, size_t idx, size_t& nextIdx, vector& errors, +bool Lexer::parseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { mpreal* r = nullptr; int base = 10; - if (getNumberAt(entry, idx, nextIdx, base, &r)) { + if (getNumberAt(entry, idx, next_idx, base, &r)) { elements.push_back({cmd_number, .re = r, .reBase = base}); return true; } else { @@ -193,44 +193,44 @@ bool Lexer::parseNumber(string& entry, size_t idx, size_t& nextIdx, vector& errors, +bool Lexer::parseComplex(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { mpreal* re = nullptr; mpreal* im = nullptr; int reBase, imBase = 10; if (idx + 1 == entry.size()) { elements.push_back({cmd_symbol, .value = entry.substr(idx, entry.size() - idx)}); - nextIdx = entry.size(); + next_idx = entry.size(); return true; // complex format error, return a symbol } - if (!getNumberAt(entry, idx + 1, nextIdx, reBase, &re, ',')) { + if (!getNumberAt(entry, idx + 1, next_idx, reBase, &re, ',')) { elements.push_back({cmd_symbol, .value = entry.substr(idx, entry.size() - idx)}); - nextIdx = entry.size(); + next_idx = entry.size(); return true; // complex format error, return a symbol } - size_t i = nextIdx; + size_t i = next_idx; if (i >= entry.size()) { elements.push_back({cmd_symbol, .value = entry.substr(idx, entry.size() - idx)}); - nextIdx = entry.size(); + next_idx = entry.size(); if (re != nullptr) delete re; if (im != nullptr) delete im; return true; // complex format error, return a symbol } - if (!getNumberAt(entry, i, nextIdx, imBase, &im, ')')) { + if (!getNumberAt(entry, i, next_idx, imBase, &im, ')')) { elements.push_back({cmd_symbol, .value = entry.substr(idx, entry.size() - idx)}); - nextIdx = entry.size(); + next_idx = entry.size(); if (re != nullptr) delete re; if (im != nullptr) delete im; return true; // complex format error, return a symbol } elements.push_back({cmd_complex, .re = re, .im = im, .reBase = reBase, .imBase = imBase}); - nextIdx++; + next_idx++; return true; } -bool Lexer::parseReserved(string& entry, size_t idx, size_t& nextIdx, vector& elements, +bool Lexer::parseReserved(string& entry, size_t idx, size_t& next_idx, vector& elements, map& keywords) { stringstream ss(entry.substr(idx)); string token; @@ -239,17 +239,17 @@ bool Lexer::parseReserved(string& entry, size_t idx, size_t& nextIdx, vectorsecond.type, .value = token, .fn = resa->second.fn}); - nextIdx = token.size() + idx; + next_idx = token.size() + idx; return true; } return false; } -bool Lexer::parseUnknown(string& entry, size_t idx, size_t& nextIdx, vector& elements) { +bool Lexer::parseUnknown(string& entry, size_t idx, size_t& next_idx, vector& elements) { stringstream ss(entry.substr(idx)); string token; ss >> token; elements.push_back({cmd_symbol, .value = token, .autoEval = true}); - nextIdx = token.size() + idx; + next_idx = token.size() + idx; return true; } diff --git a/src/lexer.hpp b/src/lexer.hpp index 18455f2..217ded8 100644 --- a/src/lexer.hpp +++ b/src/lexer.hpp @@ -53,23 +53,23 @@ class Lexer { vector& errors); private: - bool parseString(string& entry, size_t idx, size_t& nextIdx, vector& errors, + bool parseString(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseSymbol(string& entry, size_t idx, size_t& nextIdx, vector& errors, + bool parseSymbol(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseProgram(string& entry, size_t idx, size_t& nextIdx, vector& errors, + bool parseProgram(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseNumber(string& entry, size_t idx, size_t& nextIdx, vector& errors, + bool parseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseComplex(string& entry, size_t idx, size_t& nextIdx, vector& errors, + bool parseComplex(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseReserved(string& entry, size_t idx, size_t& nextIdx, vector& elements, + bool parseReserved(string& entry, size_t idx, size_t& next_idx, vector& elements, map& keywords); - bool parseUnknown(string& entry, size_t idx, size_t& nextIdx, vector& elements); + bool parseUnknown(string& entry, size_t idx, size_t& next_idx, vector& elements); void trim(string& s); - int getBaseAt(string& entry, size_t& nextIdx, bool& positive); - bool getNumberAt(string& entry, size_t idx, size_t& nextIdx, int& base, mpreal** r, char delim = ' '); + int getBaseAt(string& entry, size_t& next_idx, bool& positive); + bool getNumberAt(string& entry, size_t idx, size_t& next_idx, int& base, mpreal** r, char delim = ' '); }; #endif // SRC_LEXER_HPP_ diff --git a/src/main.cpp b/src/main.cpp index 10a6722..bed85d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ using std::cerr; /// @brief actions to be done at rpn exit /// -static void exit_interactive_rpn() { +static void ExitInteractive() { struct passwd* pw = getpwuid(getuid()); if (pw != nullptr) { stringstream history_path; @@ -34,7 +34,7 @@ static void exit_interactive_rpn() { /// @brief actions to be done at rpn exit /// -static void init_interactive_rpn() { +static void EnterInteractive() { struct passwd* pw = getpwuid(getuid()); if (pw != nullptr) { stringstream history_path; @@ -52,16 +52,16 @@ static void init_interactive_rpn() { /// @param siginfo signal info, see POSIX sigaction /// @param context see POSIX sigaction /// -static void ctrlc_handler(int sig, siginfo_t* siginfo, void* context) { exit_interactive_rpn(); } +static void CtrlHandler(int sig, siginfo_t* siginfo, void* context) { ExitInteractive(); } /// @brief setup signals handlers to stop with honours /// /// @param prog the prog to catch the signals to, must be checked not nullptr by user /// -static void catch_signals(program* prog) { +static void CatchSignals(program* prog) { struct sigaction act = {0}; - act.sa_sigaction = &ctrlc_handler; + act.sa_sigaction = &CtrlHandler; act.sa_flags = SA_SIGINFO; if (sigaction(SIGINT, &act, nullptr) < 0) cerr << "Warning, Ctrl-C cannot be caught [errno=" << errno << ' ' << strerror(errno) << "']" << endl; @@ -83,7 +83,7 @@ int main(int argc, char* argv[]) { // run with interactive prompt if (argc == 1) { // init history - init_interactive_rpn(); + EnterInteractive(); // entry loop heap heap; @@ -96,7 +96,7 @@ int main(int argc, char* argv[]) { switch (Input(entry, program::getAutocompletionWords()).status) { case Input::ok: // user could stop prog with CtrlC - catch_signals(&prog); + CatchSignals(&prog); // run it if (prog.parse(entry) == ret_ok && prog.run() == ret_good_bye) go_on = false; @@ -112,7 +112,7 @@ int main(int argc, char* argv[]) { } // manage history and exit - exit_interactive_rpn(); + ExitInteractive(); } else { // run with cmd line arguments heap heap; rpnstack stack; @@ -127,7 +127,7 @@ int main(int argc, char* argv[]) { ret = prog.parse(entry); if (ret == ret_ok) { // user could stop prog with CtrlC - catch_signals(&prog); + CatchSignals(&prog); // run it ret = prog.run(); diff --git a/src/mpreal-out.cpp b/src/mpreal-out.cpp index 276852d..6b71028 100644 --- a/src/mpreal-out.cpp +++ b/src/mpreal-out.cpp @@ -33,7 +33,7 @@ #define MPFR_IS_POS(x) (MPFR_SIGN(x) > 0) #define MPFR_PREC(x) ((x)->_mpfr_prec) -ostream& mpreal_output10base(ostream& out, const string& fmt, const mpreal& value) { +ostream& MprealOutput10Base(ostream& out, const string& fmt, const mpreal& value) { // cf std::ostream& mpreal::output(std::ostream& os) const char* s = NULL; if (!(mpfr_asprintf(&s, fmt.c_str(), mpreal::get_default_rnd(), value.mpfr_srcptr()) < 0)) { @@ -195,7 +195,7 @@ ostream& _out_number(ostream& out, int base, const mpreal& value) { return out; } -ostream& mpreal_outputNbase(ostream& out, int base, const mpreal& value) { +ostream& MprealOutputNBase(ostream& out, int base, const mpreal& value) { // see mpfr_vasprintf code int digits = 0; // forced 0 digits after separator diff --git a/src/mpreal-out.hpp b/src/mpreal-out.hpp index c5f5e89..ddab132 100644 --- a/src/mpreal-out.hpp +++ b/src/mpreal-out.hpp @@ -11,7 +11,7 @@ using mpfr::mpreal; using std::string; using std::ostream; -ostream& mpreal_output10base(ostream& out, const string& fmt, const mpreal& value); -ostream& mpreal_outputNbase(ostream& out, int base, const mpreal& value); +ostream& MprealOutput10Base(ostream& out, const string& fmt, const mpreal& value); +ostream& MprealOutputNBase(ostream& out, int base, const mpreal& value); #endif // SRC_MPREAL_OUT_HPP_ diff --git a/src/object.cpp b/src/object.cpp index 43b8d49..3ab1baa 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -3,5 +3,5 @@ #include "object.hpp" // number statics -number::mode_enum number::s_mode = number::DEFAULT_MODE; -int number::s_digits = DEFAULT_DECIMAL_DIGITS; +Number::mode_enum Number::s_mode = Number::DEFAULT_MODE; +int Number::s_digits = DEFAULT_DECIMAL_DIGITS; diff --git a/src/object.hpp b/src/object.hpp index 77a7af8..c491dd7 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -44,60 +44,60 @@ typedef enum { cmd_undef, cmd_number, // floating point number cmd_complex, // complex, couple of floating point numbers - cmd_string, // string like "string" - cmd_symbol, // symbol like 'symbol' - cmd_program, // program like << instructions >> - cmd_keyword, // langage keyword - cmd_branch, // langage branch keyword + cmd_string, // "string" + cmd_symbol, // 'symbol' + cmd_program, // << instructions >> «instructions» + cmd_keyword, // langage (reserved) keyword + cmd_branch, // langage (reserved) branch keyword cmd_max } cmd_type_t; class program; -class branch; +class Branch; typedef void (program::*program_fn_t)(void); -typedef size_t (program::*branch_fn_t)(branch&); +typedef size_t (program::*branch_fn_t)(Branch&); -/// @brief object - a generic stack object +/// @brief Object - a generic stack object /// -struct object { - explicit object(cmd_type_t type = cmd_undef) : _type(type) {} - virtual ~object() {} +struct Object { + explicit Object(cmd_type_t type = cmd_undef) : _type(type) {} + virtual ~Object() {} cmd_type_t _type; - virtual object* clone() { - object* o = new object(); + virtual Object* clone() { + Object* o = new Object(); if (o != nullptr) *o = *this; return o; } - virtual string name() { return string("object"); } + virtual string name() { return string("Object"); } virtual ostream& show(ostream& out) { out << "(" << name() << " - unknown representation)"; return out; } - friend ostream& operator<<(ostream& os, object* o) { return o->show(os); } + friend ostream& operator<<(ostream& os, Object* o) { return o->show(os); } unsigned int size() { return sizeof(*this); } }; -/// @brief stack objects derived from object +/// @brief stack objects derived from Object /// -struct number : object { - number() : object(cmd_number), base(10) {} - explicit number(const mpreal& value_, int base_ = 10) : object(cmd_number), base(base_), value(value_) {} - explicit number(int value_, int base_ = 10) : object(cmd_number), base(base_), value(value_) {} +struct Number : Object { + Number() : Object(cmd_number), base(10) {} + explicit Number(const mpreal& value_, int base_ = 10) : Object(cmd_number), base(base_), value(value_) {} + explicit Number(int value_, int base_ = 10) : Object(cmd_number), base(base_), value(value_) {} int base; mpreal value; - virtual object* clone() { return new number(value, base); } + virtual Object* clone() { return new Number(value, base); } virtual string name() { return string("number"); } virtual ostream& show(ostream& out) { return showValue(out, value, s_mode, s_digits, base); } // representation mode typedef enum { std, fix, sci } mode_enum; static mode_enum s_mode; - static constexpr mode_enum DEFAULT_MODE = number::std; + static constexpr mode_enum DEFAULT_MODE = Number::std; // precision static constexpr mpfr_prec_t MPFR_DEFAULT_PREC_BITS = 128; @@ -119,22 +119,22 @@ struct number : object { static ostream& showValue(ostream& out, const mpreal& value, mode_enum mode, int digits, int base) { if (base == 10) - return mpreal_output10base(out, _makeNumberFormat(s_mode, s_digits), value); + return MprealOutput10Base(out, _makeNumberFormat(s_mode, s_digits), value); else - return mpreal_outputNbase(out, base, value); + return MprealOutputNBase(out, base, value); } }; -/// @brief stack objects derived from object +/// @brief stack objects inheriting Object /// -struct ocomplex : object { - ocomplex() : object(cmd_complex), reBase(10), imBase(10) {} - explicit ocomplex(complex& value_, int reb = 10, int imb = 10) - : object(cmd_complex), reBase(reb), imBase(imb) { +struct Complex : Object { + Complex() : Object(cmd_complex), reBase(10), imBase(10) {} + explicit Complex(complex& value_, int reb = 10, int imb = 10) + : Object(cmd_complex), reBase(reb), imBase(imb) { value = value_; } - explicit ocomplex(mpreal& re_, mpreal& im_, int reb = 10, int imb = 10) - : object(cmd_complex), reBase(reb), imBase(imb) { + explicit Complex(mpreal& re_, mpreal& im_, int reb = 10, int imb = 10) + : Object(cmd_complex), reBase(reb), imBase(imb) { value.real(re_); value.imag(im_); } @@ -142,68 +142,58 @@ struct ocomplex : object { int reBase, imBase; complex value; - virtual object* clone() { return new ocomplex(value, reBase, imBase); } + virtual Object* clone() { return new Complex(value, reBase, imBase); } virtual string name() { return string("complex"); } virtual ostream& show(ostream& out) { out << '('; - number::showValue(out, value.real(), number::s_mode, number::s_digits, reBase); + Number::showValue(out, value.real(), Number::s_mode, Number::s_digits, reBase); out << ','; - number::showValue(out, value.imag(), number::s_mode, number::s_digits, imBase); + Number::showValue(out, value.imag(), Number::s_mode, Number::s_digits, imBase); return out << ')'; } }; -/// @brief object string -/// -struct ostring : object { - ostring() : object(cmd_string) {} - explicit ostring(const string& value_) : object(cmd_string), value(value_) {} - virtual object* clone() { return new ostring(value); } +struct String : Object { + String() : Object(cmd_string) {} + explicit String(const string& value_) : Object(cmd_string), value(value_) {} + virtual Object* clone() { return new String(value); } virtual string name() { return string("string"); } virtual ostream& show(ostream& out) { return out << "\"" << value << "\""; } string value; }; -/// @brief object program -/// -struct oprogram : object { - oprogram() : object(cmd_program) {} - explicit oprogram(const string& value_) : object(cmd_program), value(value_) {} - virtual object* clone() { return new oprogram(value); } +struct Program : Object { + Program() : Object(cmd_program) {} + explicit Program(const string& value_) : Object(cmd_program), value(value_) {} + virtual Object* clone() { return new Program(value); } virtual string name() { return string("program"); } virtual ostream& show(ostream& out) { return out << "«" << value << "»"; } string value; }; -/// @brief object symbol -/// -struct symbol : object { - explicit symbol(bool autoEval_ = true) : object(cmd_symbol), autoEval(autoEval_) {} - explicit symbol(const string& value_, bool autoEval_ = true) - : object(cmd_symbol), value(value_), autoEval(autoEval_) {} - virtual object* clone() { return new symbol(value, autoEval); } +struct Symbol : Object { + explicit Symbol(bool autoEval_ = true) : Object(cmd_symbol), autoEval(autoEval_) {} + explicit Symbol(const string& value_, bool autoEval_ = true) + : Object(cmd_symbol), value(value_), autoEval(autoEval_) {} + virtual Object* clone() { return new Symbol(value, autoEval); } virtual string name() { return string("symbol"); } virtual ostream& show(ostream& out) { return out << "'" << value << "'"; } bool autoEval; string value; }; -/// @brief object keyword -/// -struct keyword : object { - keyword() : object(cmd_keyword) {} - explicit keyword(program_fn_t fn_, const string& value_) : object(cmd_keyword), fn(fn_), value(value_) {} - virtual object* clone() { return new keyword(fn, value); } +struct Keyword : Object { + Keyword() : Object(cmd_keyword) {} + explicit Keyword(program_fn_t fn_, const string& value_) : Object(cmd_keyword), fn(fn_), value(value_) {} + virtual Object* clone() { return new Keyword(fn, value); } virtual string name() { return string("keyword"); } program_fn_t fn; string value; }; -/// @brief object branch -/// -struct branch : object { - branch() : object(cmd_branch) {} - explicit branch(branch_fn_t fn_, const string& value_) : object(cmd_branch) { +struct Branch : Object { + Branch() : Object(cmd_branch) {} + explicit Branch(branch_fn_t fn_, const string& value_) : Object(cmd_branch) { fn = fn_; arg1 = static_cast(-1); arg2 = static_cast(-1); @@ -211,7 +201,7 @@ struct branch : object { arg_bool = 0; value = value_; } - explicit branch(branch& other) : object(cmd_branch) { + explicit Branch(Branch& other) : Object(cmd_branch) { fn = other.fn; arg1 = other.arg1; arg2 = other.arg2; @@ -219,7 +209,7 @@ struct branch : object { arg_bool = other.arg_bool; value = other.value; } - virtual object* clone() { return new branch(*this); } + virtual Object* clone() { return new Branch(*this); } virtual string name() { return string("branch"); } branch_fn_t fn; size_t arg1, arg2, arg3; diff --git a/src/program.cpp b/src/program.cpp index 918b2ab..1a6b89f 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -205,11 +205,11 @@ vector program::_keywords{ /// autocompletion vector for linenoise autocompletion vector& program::getAutocompletionWords() { - static vector autocompletionWords; - if (autocompletionWords.empty()) + static vector autocompletion_words; + if (autocompletion_words.empty()) for (auto& kw : _keywords) - if (!kw.name.empty()) autocompletionWords.push_back(kw.name); - return autocompletionWords; + if (!kw.name.empty()) autocompletion_words.push_back(kw.name); + return autocompletion_words; } /// @brief run a program on a stack and a heap @@ -228,24 +228,24 @@ ret_value program::run() { ret = preprocess(); if (ret != ret_ok) { // free allocated - for (object* o : *this) delete o; + for (Object* o : *this) delete o; _local_heap.clear(); return ret; } // iterate commands for (size_t i = 0; (go_out == false) && (i < size());) { - object* o = at(i); + Object* o = at(i); switch (o->_type) { // could be an auto-evaluated symbol case cmd_symbol: - auto_rcl(reinterpret_cast(o)); + auto_rcl(reinterpret_cast(o)); i++; break; // a keyword case cmd_keyword: { - keyword* k = reinterpret_cast(o); + Keyword* k = reinterpret_cast(o); // call the matching function (this->*(k->fn))(); switch (_err) { @@ -277,7 +277,7 @@ ret_value program::run() { // a branch keyword case cmd_branch: { // call matching function - branch* b = reinterpret_cast(o); + Branch* b = reinterpret_cast(o); size_t next_cmd = (this->*(b->fn))(*b); switch (next_cmd) { case step_out: // step out @@ -304,7 +304,7 @@ ret_value program::run() { } // free allocated - for (object* o : *this) delete o; + for (Object* o : *this) delete o; _local_heap.clear(); return ret; @@ -336,13 +336,13 @@ ret_value program::preprocess(void) { vector vlayout; int layout_index = -1; // for start-end-step - vector vstartindex; + vector vstart_index; // analyse if-then-else-end branches // analyse start-{next, step} branches for (size_t i = 0; i < size(); i++) { if (at(i)->_type == cmd_branch) { - branch* k = reinterpret_cast(at(i)); + Branch* k = reinterpret_cast(at(i)); if (k->value == "if") { if_layout_t layout; layout.index_if_or_do_or_while = i; @@ -399,33 +399,33 @@ ret_value program::preprocess(void) { vlayout[layout_index].index_else = i; k->arg1 = next; // fill branch1 (if was false) of 'else' k->arg3 = vlayout[layout_index].index_if_or_do_or_while; - reinterpret_cast(at(vlayout[layout_index].index_then_or_unti_or_repeat))->arg2 = + reinterpret_cast(at(vlayout[layout_index].index_then_or_unti_or_repeat))->arg2 = next; // fill branch2 (if was false) of 'then' } else if (k->value == "start") { - vstartindex.push_back(i); + vstart_index.push_back(i); } else if (k->value == "for") { - vstartindex.push_back(i); + vstart_index.push_back(i); k->arg1 = i + 1; // arg1 points on symbol variable } else if (k->value == "next") { - if (vstartindex.size() == 0) { + if (vstart_index.size() == 0) { // error: show it show_syntax_error("missing start or for before next"); return ret_syntax; } - k->arg1 = vstartindex[vstartindex.size() - 1]; // 'next' arg1 = 'start' index - reinterpret_cast(at(vstartindex[vstartindex.size() - 1]))->arg2 = + k->arg1 = vstart_index[vstart_index.size() - 1]; // 'next' arg1 = 'start' index + reinterpret_cast(at(vstart_index[vstart_index.size() - 1]))->arg2 = i; // 'for' or 'start' arg2 = 'next' index - vstartindex.pop_back(); + vstart_index.pop_back(); } else if (k->value == "step") { - if (vstartindex.size() == 0) { + if (vstart_index.size() == 0) { // error: show it show_syntax_error("missing start or for before step"); return ret_syntax; } - k->arg1 = vstartindex[vstartindex.size() - 1]; // fill 'step' branch1 = 'start' index - reinterpret_cast(at(vstartindex[vstartindex.size() - 1]))->arg2 = + k->arg1 = vstart_index[vstart_index.size() - 1]; // fill 'step' branch1 = 'start' index + reinterpret_cast(at(vstart_index[vstart_index.size() - 1]))->arg2 = i; // 'for' or 'start' arg2 = 'next' index - vstartindex.pop_back(); + vstart_index.pop_back(); } else if (k->value == "->") { k->arg1 = i; // arg1 is '->' command index in program } else if (k->value == "do") { @@ -515,8 +515,7 @@ ret_value program::preprocess(void) { } // fill 'repeat' arg1 with 'end+1' - reinterpret_cast(at(vlayout[layout_index].index_then_or_unti_or_repeat))->arg1 = - i + 1; + reinterpret_cast(at(vlayout[layout_index].index_then_or_unti_or_repeat))->arg1 = i + 1; layout_index--; } else { // this end closes an if..then..(else) @@ -527,11 +526,11 @@ ret_value program::preprocess(void) { } if (vlayout[layout_index].index_else != -1) { // fill 'end' branch of 'else' - reinterpret_cast(at(vlayout[layout_index].index_else))->arg2 = i; + reinterpret_cast(at(vlayout[layout_index].index_else))->arg2 = i; } else { // fill 'end' branch of 'then' if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { - reinterpret_cast(at(vlayout[layout_index].index_then_or_unti_or_repeat)) + reinterpret_cast(at(vlayout[layout_index].index_then_or_unti_or_repeat)) ->arg2 = i; } else { // error: show it @@ -550,7 +549,7 @@ ret_value program::preprocess(void) { show_syntax_error("missing end"); return ret_syntax; } - if (vstartindex.size() > 0) { + if (vstart_index.size() > 0) { // error: show it show_syntax_error("missing next or step after for or start"); return ret_syntax; @@ -565,41 +564,41 @@ ret_value program::preprocess(void) { /// @return ret_value see this type /// ret_value program::parse(string& entry) { - static map keywordsMap; + static map keywords_map; vector elements; vector errors; ret_value ret = ret_ok; // prepare map for finding reserved keywords - if (keywordsMap.empty()) + if (keywords_map.empty()) for (auto& kw : _keywords) - if (!kw.name.empty()) keywordsMap[kw.name] = {kw.type, kw.fn}; + if (!kw.name.empty()) keywords_map[kw.name] = {kw.type, kw.fn}; // separate the entry string - if (lexer(entry, keywordsMap, elements, errors)) { + if (lexer(entry, keywords_map, elements, errors)) { // make objects from parsed elements for (Lexer::SynElement& element : elements) { switch (element.type) { case cmd_number: - push_back(new number(*element.re, element.reBase)); + push_back(new Number(*element.re, element.reBase)); break; case cmd_complex: - push_back(new ocomplex(*element.re, *element.im, element.reBase, element.imBase)); + push_back(new Complex(*element.re, *element.im, element.reBase, element.imBase)); break; case cmd_string: - push_back(new ostring(element.value)); + push_back(new String(element.value)); break; case cmd_symbol: - push_back(new symbol(element.value, element.autoEval)); + push_back(new Symbol(element.value, element.autoEval)); break; case cmd_program: - push_back(new oprogram(element.value)); + push_back(new Program(element.value)); break; case cmd_keyword: - push_back(new keyword(element.fn, element.value)); + push_back(new Keyword(element.fn, element.value)); break; case cmd_branch: - push_back(new branch((branch_fn_t)element.fn, element.value)); + push_back(new Branch((branch_fn_t)element.fn, element.value)); break; default: show_error(ret_unknown_err, "error creating program from entry"); @@ -708,9 +707,9 @@ void program::show_stack(bool show_separator) { /// void program::apply_default() { // default float precision, float mode - number::s_mode = number::DEFAULT_MODE; - number::s_digits = number::DEFAULT_DECIMAL_DIGITS; - mpreal::set_default_prec(number::MPFR_DEFAULT_PREC_BITS); + Number::s_mode = Number::DEFAULT_MODE; + Number::s_digits = Number::DEFAULT_DECIMAL_DIGITS; + mpreal::set_default_prec(Number::MPFR_DEFAULT_PREC_BITS); static mp_rnd_t def_rnd = mpreal::get_default_rnd(); mpreal::set_default_rnd(def_rnd); diff --git a/src/program.hpp b/src/program.hpp index a5d6d84..05fb7dc 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -19,7 +19,7 @@ using mpfr::mpreal; #include "stack.hpp" //< program class: the class containing a string parser, all the programs keywords, a stack for running the program -class program : public deque, public Lexer { +class program : public deque, public Lexer { public: program(rpnstack& stk, heap& hp, program* parent = nullptr) : _stack(stk), _heap(hp), _parent(parent) {} virtual ~program() { @@ -79,20 +79,20 @@ class program : public deque, public Lexer { //// // branch - size_t rpn_if(branch& myobj); - size_t rpn_then(branch& myobj); - size_t rpn_else(branch& myobj); - size_t rpn_end(branch& myobj); - size_t rpn_do(branch& myobj); - size_t rpn_until(branch& myobj); + size_t rpn_if(Branch& myobj); + size_t rpn_then(Branch& myobj); + size_t rpn_else(Branch& myobj); + size_t rpn_end(Branch& myobj); + size_t rpn_do(Branch& myobj); + size_t rpn_until(Branch& myobj); void rpn_ift(void); void rpn_ifte(void); - size_t rpn_while(branch& myobj); - size_t rpn_repeat(branch& myobj); - size_t rpn_start(branch& myobj); - size_t rpn_for(branch& myobj); - size_t rpn_next(branch& myobj); - size_t rpn_step(branch& myobj); + size_t rpn_while(Branch& myobj); + size_t rpn_repeat(Branch& myobj); + size_t rpn_start(Branch& myobj); + size_t rpn_for(Branch& myobj); + size_t rpn_next(Branch& myobj); + size_t rpn_step(Branch& myobj); enum { step_out = static_cast(-1), runtime_error = static_cast(-2) }; // complex @@ -138,9 +138,9 @@ class program : public deque, public Lexer { void rpn_atanh(); // program - bool find_variable(string& variable, object*& obj); + bool find_variable(string& variable, Object*& obj); void rpn_eval(void); - int rpn_inprog(branch& myobj); + int rpn_inprog(Branch& inprog_obj); // real void rpn_plus(); @@ -198,7 +198,7 @@ class program : public deque, public Lexer { void rpn_stoinv(void); void rpn_rcl(void); void rpn_edit(void); - void auto_rcl(symbol* symb); + void auto_rcl(Symbol* symb); void rpn_purge(void); void rpn_vars(void); void rpn_clusr(void); diff --git a/src/rpn-branch.cpp b/src/rpn-branch.cpp index 182cc03..d39ca10 100644 --- a/src/rpn-branch.cpp +++ b/src/rpn-branch.cpp @@ -9,12 +9,12 @@ /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_if(branch& myobj) { +size_t program::rpn_if(Branch& myobj) { // myobj.arg1 = 'if' condition evaluation value MIN_ARGUMENTS_RET(1, runtime_error); ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); - if (_stack.value(0) != 0) + if (_stack.value(0) != 0) myobj.arg1 = 1; else myobj.arg1 = 0; @@ -27,20 +27,20 @@ size_t program::rpn_if(branch& myobj) { /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program /// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return runtime_error something went wrong with preprocess, abort Branch /// -size_t program::rpn_then(branch& myobj) { +size_t program::rpn_then(Branch& myobj) { // myobj.arg1 = index of then + 1 // myobj.arg2 = index of else + 1 or end + 1 // myobj.arg3 = index of if // if condition is true -> arg1 (= jump to then + 1) // else -> arg2 (= jump to else + 1 or end + 1) - branch* if_cmd; + Branch* if_cmd; if (myobj.arg3 >= size() || at(myobj.arg3)->_type != cmd_branch) { setErrorContext(ret_missing_operand); return runtime_error; } - if_cmd = reinterpret_cast(at(myobj.arg3)); + if_cmd = reinterpret_cast(at(myobj.arg3)); if (if_cmd->arg1 == 1) return myobj.arg1; else @@ -54,18 +54,18 @@ size_t program::rpn_then(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_else(branch& myobj) { +size_t program::rpn_else(Branch& myobj) { // myobj.arg1 = index of else + 1 // myobj.arg2 = index of end + 1 // myobj.arg3 = index of if // if condition was false -> arg1 (= jump to else + 1) // if condition was true -> arg2 (= jump to end + 1) - branch* if_cmd; + Branch* if_cmd; if (myobj.arg3 >= size() || at(myobj.arg3)->_type != cmd_branch) { setErrorContext(ret_missing_operand); return runtime_error; } - if_cmd = reinterpret_cast(at(myobj.arg3)); + if_cmd = reinterpret_cast(at(myobj.arg3)); if (if_cmd->arg1 == 1) return myobj.arg2; else @@ -79,7 +79,7 @@ size_t program::rpn_else(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_end(branch& myobj) { +size_t program::rpn_end(Branch& myobj) { size_t ret = step_out; // arg1 = index of do+1 in case of do..unti..end @@ -89,7 +89,7 @@ size_t program::rpn_end(branch& myobj) { ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); // check arg - if (_stack.value(0) == 0) ret = myobj.arg1; + if (_stack.value(0) == 0) ret = myobj.arg1; _stack.pop(); } else if (myobj.arg2 != step_out) { // arg2 = index of while+1 in case of while..repeat..end @@ -106,7 +106,7 @@ size_t program::rpn_end(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_do(branch& myobj) { +size_t program::rpn_do(Branch& myobj) { // nothing return step_out; } @@ -116,9 +116,9 @@ size_t program::rpn_do(branch& myobj) { /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program /// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return runtime_error something went wrong with preprocess, abort Branch /// -size_t program::rpn_until(branch& myobj) { +size_t program::rpn_until(Branch& myobj) { // nothing return step_out; } @@ -135,8 +135,8 @@ void program::rpn_ift(void) { ARG_MUST_BE_OF_TYPE(1, cmd_number); // check ift arg - // arg is true if number != 0 or if is nan or +/-inf - if (_stack.value(1) != 0) + // arg is true if Number != 0 or if is nan or +/-inf + if (_stack.value(1) != 0) _stack.erase(1); else _stack.erase(0, 2); @@ -154,7 +154,7 @@ void program::rpn_ifte(void) { ARG_MUST_BE_OF_TYPE(2, cmd_number); // check ifte arg - if (_stack.value(2) != 0) { + if (_stack.value(2) != 0) { _stack.erase(2); _stack.pop(); } else { @@ -170,7 +170,7 @@ void program::rpn_ifte(void) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_while(branch& myobj) { +size_t program::rpn_while(Branch& myobj) { // nothing return step_out; } @@ -182,7 +182,7 @@ size_t program::rpn_while(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_repeat(branch& myobj) { +size_t program::rpn_repeat(Branch& myobj) { size_t ret = step_out; MIN_ARGUMENTS_RET(1, runtime_error); @@ -190,7 +190,7 @@ size_t program::rpn_repeat(branch& myobj) { // check arg // myobj.arg1 is end+1 - if (_stack.value(0) == 0) ret = myobj.arg1; + if (_stack.value(0) == 0) ret = myobj.arg1; _stack.pop(); return ret; @@ -203,7 +203,7 @@ size_t program::rpn_repeat(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_start(branch& myobj) { +size_t program::rpn_start(Branch& myobj) { size_t ret = step_out; MIN_ARGUMENTS_RET(2, runtime_error); @@ -211,8 +211,8 @@ size_t program::rpn_start(branch& myobj) { ARG_MUST_BE_OF_TYPE_RET(1, cmd_number, runtime_error); // loop boundaries - myobj.firstIndex = _stack.value(1); - myobj.lastIndex = _stack.value(0); + myobj.firstIndex = _stack.value(1); + myobj.lastIndex = _stack.value(0); _stack.erase(0, 2); // test value @@ -232,23 +232,23 @@ size_t program::rpn_start(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_for(branch& myobj) { +size_t program::rpn_for(Branch& myobj) { size_t ret; MIN_ARGUMENTS_RET(2, runtime_error); ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); ARG_MUST_BE_OF_TYPE_RET(1, cmd_number, runtime_error); - symbol* sym; + Symbol* sym; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != cmd_symbol) { setErrorContext(ret_missing_operand); return runtime_error; } - sym = reinterpret_cast(at(myobj.arg1)); // arg1 = loop variable index + sym = reinterpret_cast(at(myobj.arg1)); // arg1 = loop variable index // loop boundaries - myobj.firstIndex = _stack.value(1); - myobj.lastIndex = _stack.value(0); + myobj.firstIndex = _stack.value(1); + myobj.lastIndex = _stack.value(0); // test value if (myobj.firstIndex > myobj.lastIndex) { @@ -263,7 +263,7 @@ size_t program::rpn_for(branch& myobj) { delete it->second; _local_heap.erase(it); } - _local_heap[sym->value] = _stack.obj(1).clone(); + _local_heap[sym->value] = _stack.obj(1).clone(); ret = myobj.arg1 + 1; } @@ -279,15 +279,15 @@ size_t program::rpn_for(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_next(branch& myobj) { +size_t program::rpn_next(Branch& myobj) { // arg1 = loop variable index // firstIndex = current point in the loop - branch* start_or_for; + Branch* start_or_for; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != cmd_branch) { setErrorContext(ret_missing_operand); return runtime_error; } - start_or_for = reinterpret_cast(at(myobj.arg1)); + start_or_for = reinterpret_cast(at(myobj.arg1)); if (!myobj.arg_bool) { myobj.arg_bool = true; myobj.firstIndex = start_or_for->firstIndex; @@ -299,16 +299,16 @@ size_t program::rpn_next(branch& myobj) { // for command: increment symbol too if (start_or_for->arg1 != -1) { - object* obj; - symbol* var; + Object* obj; + Symbol* var; if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != cmd_symbol) { setErrorContext(ret_missing_operand); return runtime_error; } - var = reinterpret_cast(at(start_or_for->arg1)); + var = reinterpret_cast(at(start_or_for->arg1)); // store symbol variable (asserted existing in the local heap) - reinterpret_cast(_local_heap[var->value])->value = myobj.firstIndex; + reinterpret_cast(_local_heap[var->value])->value = myobj.firstIndex; } // test value @@ -332,12 +332,12 @@ size_t program::rpn_next(branch& myobj) { /// @return step_out next object to run in the current program is current + 1 /// @return runtime_error something went wrong with preprocess, abort branch /// -size_t program::rpn_step(branch& myobj) { +size_t program::rpn_step(Branch& myobj) { size_t ret; MIN_ARGUMENTS_RET(1, runtime_error); ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); - mpreal step = _stack.value(0); + mpreal step = _stack.value(0); _stack.pop(); // end of loop if step is negative or zero @@ -346,12 +346,12 @@ size_t program::rpn_step(branch& myobj) { } else { // arg1 = loop variable index // firstIndex = current count - branch* start_or_for; + Branch* start_or_for; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != cmd_branch) { setErrorContext(ret_missing_operand); return runtime_error; } - start_or_for = reinterpret_cast(at(myobj.arg1)); + start_or_for = reinterpret_cast(at(myobj.arg1)); if (!myobj.arg_bool) { myobj.arg_bool = true; myobj.firstIndex = start_or_for->firstIndex; @@ -362,17 +362,17 @@ size_t program::rpn_step(branch& myobj) { mpfr_add(myobj.firstIndex.mpfr_ptr(), myobj.firstIndex.mpfr_srcptr(), step.mpfr_srcptr(), MPFR_RNDD); if (start_or_for->arg1 != -1) { - object* obj; - symbol* var; + Object* obj; + Symbol* var; // for command: increment symbol too if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != cmd_symbol) { setErrorContext(ret_missing_operand); return runtime_error; } - var = reinterpret_cast(at(start_or_for->arg1)); + var = reinterpret_cast(at(start_or_for->arg1)); // increase symbol variable - reinterpret_cast(_local_heap[var->value])->value = myobj.firstIndex; + reinterpret_cast(_local_heap[var->value])->value = myobj.firstIndex; } // test loop value is out of range diff --git a/src/rpn-complex.cpp b/src/rpn-complex.cpp index ca94145..d90ca2d 100644 --- a/src/rpn-complex.cpp +++ b/src/rpn-complex.cpp @@ -8,7 +8,7 @@ void program::rpn_re() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - _stack.push_front(new number(real(_stack.value(0)))); + _stack.push_front(new Number(real(_stack.value(0)))); _stack.erase(1); } @@ -18,7 +18,7 @@ void program::rpn_re() { void program::rpn_im() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - _stack.push_front(new number(imag(_stack.value(0)))); + _stack.push_front(new Number(imag(_stack.value(0)))); _stack.erase(1); } @@ -28,7 +28,7 @@ void program::rpn_im() { void program::rpn_arg() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - _stack.push_front(new number(arg(_stack.value(0)))); + _stack.push_front(new Number(arg(_stack.value(0)))); _stack.erase(1); } @@ -38,7 +38,7 @@ void program::rpn_arg() { void program::rpn_conj() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - _stack.value(0) = conj(_stack.value(0)); + _stack.value(0) = conj(_stack.value(0)); } /// @brief r2c keyword implementation @@ -48,7 +48,7 @@ void program::rpn_r2c() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - _stack.push(new ocomplex(_stack.value(1), _stack.value(0), _stack.obj(1).reBase, _stack.obj(0).reBase)); + _stack.push(new Complex(_stack.value(1), _stack.value(0), _stack.obj(1).reBase, _stack.obj(0).reBase)); _stack.erase(1, 2); } @@ -58,8 +58,8 @@ void program::rpn_r2c() { void program::rpn_c2r() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - _stack.push(new number(real(_stack.value(0)), _stack.obj(0).reBase)); - _stack.push(new number(imag(_stack.value(1)), _stack.obj(1).imBase)); + _stack.push(new Number(real(_stack.value(0)), _stack.obj(0).reBase)); + _stack.push(new Number(imag(_stack.value(1)), _stack.obj(1).imBase)); _stack.erase(2); } @@ -69,10 +69,10 @@ void program::rpn_c2r() { void program::rpn_r2p() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - mpreal rho = abs(_stack.value(0)); - mpreal theta = arg(_stack.value(0)); - _stack.value(0).real(rho); - _stack.value(0).imag(theta); + mpreal rho = abs(_stack.value(0)); + mpreal theta = arg(_stack.value(0)); + _stack.value(0).real(rho); + _stack.value(0).imag(theta); } /// @brief p2r keyword implementation @@ -81,5 +81,5 @@ void program::rpn_r2p() { void program::rpn_p2r() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_complex); - _stack.value(0) = polar(abs(_stack.value(0)), arg(_stack.value(0))); + _stack.value(0) = polar(abs(_stack.value(0)), arg(_stack.value(0))); } diff --git a/src/rpn-general.cpp b/src/rpn-general.cpp index a493550..7443805 100644 --- a/src/rpn-general.cpp +++ b/src/rpn-general.cpp @@ -69,14 +69,14 @@ void program::rpn_help() { // show mode cout << "Current float mode is "; - switch (number::s_mode) { - case number::std: + switch (Number::s_mode) { + case Number::std: cout << "'std'"; break; - case number::fix: + case Number::fix: cout << "'fix'"; break; - case number::sci: + case Number::sci: cout << "'sci'"; break; default: @@ -85,7 +85,7 @@ void program::rpn_help() { } // bits precision, decimal digits and rounding mode - cout << " with " << number::s_digits << " digits after the decimal point" << endl; + cout << " with " << Number::s_digits << " digits after the decimal point" << endl; cout << "Current floating point precision is " << static_cast(mpreal::get_default_prec()) << " bits" << endl; vector> rnd{MPFR_ROUND}; for (auto& rn : rnd) @@ -110,12 +110,12 @@ void program::rpn_std() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int digits = static_cast(_stack.value(0).toLong()); + int digits = static_cast(_stack.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - number::s_mode = number::std; - number::s_digits = digits; + Number::s_mode = Number::std; + Number::s_digits = digits; _stack.pop(); } else { setErrorContext(ret_out_of_range); @@ -128,12 +128,12 @@ void program::rpn_fix() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int digits = static_cast(_stack.value(0).toLong()); + int digits = static_cast(_stack.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - number::s_mode = number::fix; - number::s_digits = digits; + Number::s_mode = Number::fix; + Number::s_digits = digits; _stack.pop(); } else { setErrorContext(ret_out_of_range); @@ -146,12 +146,12 @@ void program::rpn_sci() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int digits = static_cast(_stack.value(0).toLong()); + int digits = static_cast(_stack.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - number::s_mode = number::sci; - number::s_digits = digits; + Number::s_mode = Number::sci; + Number::s_digits = digits; _stack.pop(); } else { setErrorContext(ret_out_of_range); @@ -160,11 +160,11 @@ void program::rpn_sci() { /// @brief _version keyword implementation /// -void program::rpn_version() { _stack.push_front(new ostring(RPN_VERSION)); } +void program::rpn_version() { _stack.push_front(new String(RPN_VERSION)); } /// @brief _uname keyword implementation /// -void program::rpn_uname() { _stack.push_front(new ostring(RPN_UNAME)); } +void program::rpn_uname() { _stack.push_front(new String(RPN_UNAME)); } /// @brief history keyword implementation /// @@ -183,7 +183,7 @@ void program::rpn_history() { /// void program::rpn_type() { MIN_ARGUMENTS(1); - _stack.push(new ostring(_stack.at(0)->name())); + _stack.push(new String(_stack.at(0)->name())); _stack.erase(1); } @@ -198,14 +198,14 @@ void program::rpn_precision() { ARG_MUST_BE_OF_TYPE(0, cmd_number); // set precision - int prec = static_cast(_stack.value(0).toLong()); + int prec = static_cast(_stack.value(0).toLong()); if (prec >= MPFR_PREC_MIN && prec <= MPFR_PREC_MAX) { mpreal::set_default_prec(prec); // modify digits seen by user if std mode - if (number::s_mode == number::std) { + if (Number::s_mode == Number::std) { // calc max nb of digits user can see with the current bit precision - number::s_digits = mpfr::bits2digits(mpreal::get_default_prec()); + Number::s_digits = mpfr::bits2digits(mpreal::get_default_prec()); } _stack.pop(); } else { @@ -221,7 +221,7 @@ void program::rpn_round() { map matchRound{MPFR_ROUND}; - auto found = matchRound.find(_stack.value(0)); + auto found = matchRound.find(_stack.value(0)); if (found != matchRound.end()) mpreal::set_default_rnd(found->second); else diff --git a/src/rpn-logs.cpp b/src/rpn-logs.cpp index ffbad53..c0f06c5 100644 --- a/src/rpn-logs.cpp +++ b/src/rpn-logs.cpp @@ -4,16 +4,16 @@ /// @brief e keyword implementation /// -void program::rpn_e(void) { _stack.push(new number(mpfr::const_euler())); } +void program::rpn_e(void) { _stack.push(new Number(mpfr::const_euler())); } /// @brief log10 keyword implementation /// void program::rpn_log10() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = log10(_stack.value(0)); + _stack.value(0) = log10(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = log10(_stack.value(0)); + _stack.value(0) = log10(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -23,9 +23,9 @@ void program::rpn_log10() { void program::rpn_alog10() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); + _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); + _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -35,9 +35,9 @@ void program::rpn_alog10() { void program::rpn_log2() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); + _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); + _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); else setErrorContext(ret_bad_operand_type); } @@ -47,9 +47,9 @@ void program::rpn_log2() { void program::rpn_alog2() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); + _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); + _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -59,9 +59,9 @@ void program::rpn_alog2() { void program::rpn_ln() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = log(_stack.value(0)); + _stack.value(0) = log(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = log(_stack.value(0)); + _stack.value(0) = log(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -71,9 +71,9 @@ void program::rpn_ln() { void program::rpn_exp() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = exp(_stack.value(0)); + _stack.value(0) = exp(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = exp(_stack.value(0)); + _stack.value(0) = exp(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -83,9 +83,9 @@ void program::rpn_exp() { void program::rpn_expm() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = exp(_stack.value(0)) - mpreal(1); + _stack.value(0) = exp(_stack.value(0)) - mpreal(1); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = exp(_stack.value(0)) - mpreal(1); + _stack.value(0) = exp(_stack.value(0)) - mpreal(1); else setErrorContext(ret_bad_operand_type); } @@ -95,9 +95,9 @@ void program::rpn_expm() { void program::rpn_lnp1() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = log(_stack.value(0) + 1); + _stack.value(0) = log(_stack.value(0) + 1); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = log(_stack.value(0) + mpreal(1)); + _stack.value(0) = log(_stack.value(0) + mpreal(1)); else setErrorContext(ret_bad_operand_type); } @@ -107,9 +107,9 @@ void program::rpn_lnp1() { void program::rpn_sinh() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = sinh(_stack.value(0)); + _stack.value(0) = sinh(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = sinh(_stack.value(0)); + _stack.value(0) = sinh(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -119,9 +119,9 @@ void program::rpn_sinh() { void program::rpn_asinh() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = asinh(_stack.value(0)); + _stack.value(0) = asinh(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = asinh(_stack.value(0)); + _stack.value(0) = asinh(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -131,9 +131,9 @@ void program::rpn_asinh() { void program::rpn_cosh() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = cosh(_stack.value(0)); + _stack.value(0) = cosh(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = cosh(_stack.value(0)); + _stack.value(0) = cosh(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -143,9 +143,9 @@ void program::rpn_cosh() { void program::rpn_acosh() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = acosh(_stack.value(0)); + _stack.value(0) = acosh(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = acosh(_stack.value(0)); + _stack.value(0) = acosh(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -155,9 +155,9 @@ void program::rpn_acosh() { void program::rpn_tanh() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = tanh(_stack.value(0)); + _stack.value(0) = tanh(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = tanh(_stack.value(0)); + _stack.value(0) = tanh(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -167,9 +167,9 @@ void program::rpn_tanh() { void program::rpn_atanh() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = atanh(_stack.value(0)); + _stack.value(0) = atanh(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = atanh(_stack.value(0)); + _stack.value(0) = atanh(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } diff --git a/src/rpn-program.cpp b/src/rpn-program.cpp index d6ea074..4e83558 100644 --- a/src/rpn-program.cpp +++ b/src/rpn-program.cpp @@ -9,7 +9,7 @@ /// @return true variable was found /// @return false variable was not found /// -bool program::find_variable(string& variable, object*& obj) { +bool program::find_variable(string& variable, Object*& obj) { bool found = false; program* parent = _parent; @@ -40,14 +40,14 @@ void program::rpn_eval(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_symbol) { // recall a variable - object* obj; - string variable(_stack.value(0)); + Object* obj; + string variable(_stack.value(0)); _stack.pop(); // if variable holds a program, run this program if (find_variable(variable, obj)) { if (obj->_type == cmd_program) { - prog_text = _stack.value(0); + prog_text = _stack.value(0); _stack.pop(); run_prog = true; } else { @@ -59,7 +59,7 @@ void program::rpn_eval(void) { } } else if (_stack.type(0) == cmd_program) { // eval a program - prog_text = _stack.value(0); + prog_text = _stack.value(0); _stack.pop(); run_prog = true; } else { @@ -78,23 +78,23 @@ void program::rpn_eval(void) { } } -/// @brief -> keyword (branch) implementation +/// @brief -> keyword (Branch) implementation /// -int program::rpn_inprog(branch& myobj) { +int program::rpn_inprog(Branch& inprog_obj) { string context("->"); // for showing errors int count_symbols = 0; bool prog_found = false; - if (myobj.arg1 == -1) { + if (inprog_obj.arg1 == -1) { setErrorContext(ret_unknown_err); return -1; } // syntax must be - // -> ... + // -> ... - // find next oprogram object - for (unsigned int i = myobj.arg1 + 1; i < size(); i++) { + // find next Program object + for (unsigned int i = inprog_obj.arg1 + 1; i < size(); i++) { // count symbol if (at(i)->_type == cmd_symbol) { count_symbols++; @@ -117,14 +117,14 @@ int program::rpn_inprog(branch& myobj) { return -1; } - // is missing + // is missing if (!prog_found) { setErrorContext(ret_syntax); show_error(_err, context); return -1; } - // check symbols number vs stack size + // check symbols Number vs stack size if (_stack.size() < count_symbols) { setErrorContext(ret_missing_operand); show_error(_err, context); @@ -132,13 +132,13 @@ int program::rpn_inprog(branch& myobj) { } // load variables - for (unsigned int i = myobj.arg1 + count_symbols; i > myobj.arg1; i--) { - _local_heap[reinterpret_cast(at(i))->value] = _stack.at(0)->clone(); + for (unsigned int i = inprog_obj.arg1 + count_symbols; i > inprog_obj.arg1; i--) { + _local_heap[reinterpret_cast(at(i))->value] = _stack.at(0)->clone(); _stack.pop(); } // run the program - string& entry = reinterpret_cast(at(myobj.arg1 + count_symbols + 1))->value; + string& entry = reinterpret_cast(at(inprog_obj.arg1 + count_symbols + 1))->value; program prog(_stack, _heap, this); // make the program from entry @@ -148,5 +148,5 @@ int program::rpn_inprog(branch& myobj) { } // point on next command - return myobj.arg1 + count_symbols + 2; + return inprog_obj.arg1 + count_symbols + 2; } diff --git a/src/rpn-real.cpp b/src/rpn-real.cpp index 471046e..d9d00a5 100644 --- a/src/rpn-real.cpp +++ b/src/rpn-real.cpp @@ -7,20 +7,20 @@ void program::rpn_plus() { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.value(1) += _stack.value(0); + _stack.value(1) += _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.value(1) += _stack.value(0); + _stack.value(1) += _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.value(1) += _stack.value(0); + _stack.value(1) += _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { - _stack.value(1) += _stack.value(0); + _stack.value(1) += _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { rpn_swap(); - _stack.value(1) += _stack.value(0); + _stack.value(1) += _stack.value(0); _stack.pop(); } else { setErrorContext(ret_bad_operand_type); @@ -32,17 +32,17 @@ void program::rpn_plus() { void program::rpn_minus() { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.value(1) -= _stack.value(0); + _stack.value(1) -= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.value(1) -= _stack.value(0); + _stack.value(1) -= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { - _stack.value(1) -= _stack.value(0); + _stack.value(1) -= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { rpn_swap(); - _stack.value(1) = _stack.value(0) - _stack.value(1); + _stack.value(1) = _stack.value(0) - _stack.value(1); _stack.pop(); } else { setErrorContext(ret_bad_operand_type); @@ -54,17 +54,17 @@ void program::rpn_minus() { void program::rpn_mul() { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.value(1) *= _stack.value(0); + _stack.value(1) *= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.value(1) *= _stack.value(0); + _stack.value(1) *= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { - _stack.value(1) *= _stack.value(0); + _stack.value(1) *= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { rpn_swap(); - _stack.value(1) *= _stack.value(0); + _stack.value(1) *= _stack.value(0); _stack.pop(); } else { setErrorContext(ret_bad_operand_type); @@ -76,17 +76,17 @@ void program::rpn_mul() { void program::rpn_div() { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.value(1) /= _stack.value(0); + _stack.value(1) /= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.value(1) /= _stack.value(0); + _stack.value(1) /= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { - _stack.value(1) /= _stack.value(0); + _stack.value(1) /= _stack.value(0); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { rpn_swap(); - _stack.value(1) = _stack.value(0) / _stack.value(1); + _stack.value(1) = _stack.value(0) / _stack.value(1); _stack.pop(); } else { setErrorContext(ret_bad_operand_type); @@ -98,9 +98,9 @@ void program::rpn_div() { void program::rpn_neg() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = -_stack.value(0); + _stack.value(0) = -_stack.value(0); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = -_stack.value(0); + _stack.value(0) = -_stack.value(0); else setErrorContext(ret_bad_operand_type); } @@ -110,9 +110,9 @@ void program::rpn_neg() { void program::rpn_inv() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = 1 / _stack.value(0); + _stack.value(0) = 1 / _stack.value(0); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = mpreal(1) / _stack.value(0); + _stack.value(0) = mpreal(1) / _stack.value(0); else setErrorContext(ret_bad_operand_type); } @@ -122,24 +122,24 @@ void program::rpn_inv() { void program::rpn_power() { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - if (_stack.value(1) >= 0) { - _stack.value(1) = pow(_stack.value(1), _stack.value(0)); + if (_stack.value(1) >= 0) { + _stack.value(1) = pow(_stack.value(1), _stack.value(0)); _stack.pop(); } else { mpreal zero; - _stack.push(new ocomplex(_stack.value(1), zero, _stack.obj(1).base)); - _stack.value(0) = pow(_stack.value(0), _stack.value(1)); + _stack.push(new Complex(_stack.value(1), zero, _stack.obj(1).base)); + _stack.value(0) = pow(_stack.value(0), _stack.value(1)); _stack.erase(1, 2); } } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.value(1) = pow(_stack.value(1), _stack.value(0)); + _stack.value(1) = pow(_stack.value(1), _stack.value(0)); _stack.pop(); } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { - _stack.value(1) = pow(_stack.value(1), _stack.value(0)); + _stack.value(1) = pow(_stack.value(1), _stack.value(0)); _stack.pop(); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { rpn_swap(); - _stack.value(1) = pow(_stack.value(0), _stack.value(1)); + _stack.value(1) = pow(_stack.value(0), _stack.value(1)); _stack.pop(); } else { setErrorContext(ret_bad_operand_type); @@ -151,18 +151,18 @@ void program::rpn_power() { void program::rpn_squareroot() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) { - if (_stack.value(0) >= 0) { - _stack.value(0) = sqrt(_stack.value(0)); + if (_stack.value(0) >= 0) { + _stack.value(0) = sqrt(_stack.value(0)); } else { // negative number -> square root is complex mpreal zero; - _stack.push(new ocomplex(_stack.value(0), zero, - _stack.obj(0).base)); // TODO(louis) manage new errors - _stack.value(0) = sqrt(_stack.value(0)); + _stack.push(new Complex(_stack.value(0), zero, + _stack.obj(0).base)); // TODO(louis) manage new errors + _stack.value(0) = sqrt(_stack.value(0)); _stack.erase(1); } } else if (_stack.type(0) == cmd_complex) { - _stack.value(0) = sqrt(_stack.value(0)); + _stack.value(0) = sqrt(_stack.value(0)); } else { setErrorContext(ret_bad_operand_type); } @@ -173,10 +173,10 @@ void program::rpn_squareroot() { void program::rpn_hex() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) { - _stack.obj(0).base = 16; + _stack.obj(0).base = 16; } else if (_stack.type(0) == cmd_complex) { - _stack.obj(0).reBase = 16; - _stack.obj(0).imBase = 16; + _stack.obj(0).reBase = 16; + _stack.obj(0).imBase = 16; } else { setErrorContext(ret_bad_operand_type); } @@ -187,10 +187,10 @@ void program::rpn_hex() { void program::rpn_bin() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) { - _stack.obj(0).base = 2; + _stack.obj(0).base = 2; } else if (_stack.type(0) == cmd_complex) { - _stack.obj(0).reBase = 2; - _stack.obj(0).imBase = 2; + _stack.obj(0).reBase = 2; + _stack.obj(0).imBase = 2; } else { setErrorContext(ret_bad_operand_type); } @@ -201,10 +201,10 @@ void program::rpn_bin() { void program::rpn_dec() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) { - _stack.obj(0).base = 10; + _stack.obj(0).base = 10; } else if (_stack.type(0) == cmd_complex) { - _stack.obj(0).reBase = 10; - _stack.obj(0).imBase = 10; + _stack.obj(0).reBase = 10; + _stack.obj(0).imBase = 10; } else { setErrorContext(ret_bad_operand_type); } @@ -215,14 +215,14 @@ void program::rpn_dec() { void program::rpn_base() { MIN_ARGUMENTS(2); if (_stack.type(1) == cmd_number || _stack.type(1) == cmd_complex) { - int base = static_cast(_stack.value(0).toLong()); + int base = static_cast(_stack.value(0).toLong()); _stack.pop(); if (base >= 2 && base <= 62) { if (_stack.type(0) == cmd_number) { - _stack.obj(0).base = base; + _stack.obj(0).base = base; } else { - _stack.obj(0).reBase = base; - _stack.obj(0).imBase = base; + _stack.obj(0).reBase = base; + _stack.obj(0).imBase = base; } } else { setErrorContext(ret_out_of_range); @@ -238,7 +238,7 @@ void program::rpn_purcent() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - _stack.value(1) *= _stack.value(0) / 100; + _stack.value(1) *= _stack.value(0) / 100; _stack.pop(); } @@ -248,7 +248,7 @@ void program::rpn_purcentCH() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - _stack.value(1) = (_stack.value(0) * 100) / _stack.value(1); + _stack.value(1) = (_stack.value(0) * 100) / _stack.value(1); _stack.pop(); } @@ -257,9 +257,9 @@ void program::rpn_purcentCH() { void program::rpn_square() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) *= _stack.value(0); + _stack.value(0) *= _stack.value(0); else if (_stack.at(0)->_type == cmd_complex) - _stack.value(0) *= _stack.value(0); + _stack.value(0) *= _stack.value(0); else setErrorContext(ret_bad_operand_type); } @@ -270,7 +270,7 @@ void program::rpn_modulo() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - _stack.value(1) = fmod(_stack.value(1), _stack.value(0)); + _stack.value(1) = fmod(_stack.value(1), _stack.value(0)); _stack.pop(); } @@ -279,9 +279,9 @@ void program::rpn_modulo() { void program::rpn_abs() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) { - _stack.value(0) = abs(_stack.value(0)); + _stack.value(0) = abs(_stack.value(0)); } else if (_stack.type(0) == cmd_complex) { - _stack.push(new number(abs(_stack.value(0)))); + _stack.push(new Number(abs(_stack.value(0)))); _stack.erase(1); } else { setErrorContext(ret_bad_operand_type); @@ -294,7 +294,7 @@ void program::rpn_fact() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); // fact(n) = gamma(n+1) - _stack.value(0) = gamma(_stack.value(0) + 1); + _stack.value(0) = gamma(_stack.value(0) + 1); } /// @brief sign keyword implementation @@ -302,9 +302,9 @@ void program::rpn_fact() { void program::rpn_sign() { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = sgn(_stack.value(0)); + _stack.value(0) = sgn(_stack.value(0)); else if (_stack.at(0)->_type == cmd_complex) - _stack.value(0) = _stack.value(0) / abs(_stack.value(0)); + _stack.value(0) = _stack.value(0) / abs(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -314,12 +314,12 @@ void program::rpn_sign() { void program::rpn_mant() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - if (!isfinite(_stack.value(0))) { + if (!isfinite(_stack.value(0))) { setErrorContext(ret_out_of_range); return; } mp_exp_t exp; - _stack.value(0) = frexp(_stack.value(0), &exp); + _stack.value(0) = frexp(_stack.value(0), &exp); } /// @brief xpon keyword implementation @@ -327,13 +327,13 @@ void program::rpn_mant() { void program::rpn_xpon() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - if (!isfinite(_stack.value(0))) { + if (!isfinite(_stack.value(0))) { setErrorContext(ret_out_of_range); return; } mp_exp_t exp; - (void)frexp(_stack.value(0), &exp); - _stack.value(0) = exp; + (void)frexp(_stack.value(0), &exp); + _stack.value(0) = exp; } /// @brief floor keyword implementation @@ -341,7 +341,7 @@ void program::rpn_xpon() { void program::rpn_floor() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) = floor(_stack.value(0)); + _stack.value(0) = floor(_stack.value(0)); } /// @brief ceil keyword implementation @@ -349,7 +349,7 @@ void program::rpn_floor() { void program::rpn_ceil() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) = ceil(_stack.value(0)); + _stack.value(0) = ceil(_stack.value(0)); } /// @brief fp keyword implementation @@ -357,7 +357,7 @@ void program::rpn_ceil() { void program::rpn_fp() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) = frac(_stack.value(0)); + _stack.value(0) = frac(_stack.value(0)); } /// @brief ip keyword implementation @@ -365,7 +365,7 @@ void program::rpn_fp() { void program::rpn_ip() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) = trunc(_stack.value(0)); + _stack.value(0) = trunc(_stack.value(0)); } /// @brief min keyword implementation @@ -374,7 +374,7 @@ void program::rpn_min() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - _stack.value(0) = min(_stack.value(0), _stack.value(1)); + _stack.value(0) = min(_stack.value(0), _stack.value(1)); _stack.erase(1); } @@ -384,6 +384,6 @@ void program::rpn_max() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - _stack.value(0) = max(_stack.value(0), _stack.value(1)); + _stack.value(0) = max(_stack.value(0), _stack.value(1)); _stack.erase(1); } diff --git a/src/rpn-stack.cpp b/src/rpn-stack.cpp index 646a845..fa98451 100644 --- a/src/rpn-stack.cpp +++ b/src/rpn-stack.cpp @@ -6,7 +6,7 @@ /// void program::rpn_swap(void) { MIN_ARGUMENTS(2); - object* tmp = _stack.front(); + Object* tmp = _stack.front(); _stack.erase(0, 1, false); _stack.insert(_stack.begin() + 1, tmp); } @@ -31,7 +31,7 @@ void program::rpn_dropn(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int args = static_cast(_stack.value(0).toLong()); + int args = static_cast(_stack.value(0).toLong()); MIN_ARGUMENTS(args + 1); _stack.erase(0, args + 1); } @@ -53,7 +53,7 @@ void program::rpn_dupn(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int args = static_cast(_stack.value(0).toLong()); + int args = static_cast(_stack.value(0).toLong()); _stack.pop(); MIN_ARGUMENTS(args); @@ -74,7 +74,7 @@ void program::rpn_pick(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int to_pick = static_cast(_stack.value(0).toLong()); + int to_pick = static_cast(_stack.value(0).toLong()); _stack.pop(); // treat stack depth errors @@ -90,14 +90,14 @@ void program::rpn_pick(void) { /// void program::rpn_rot(void) { MIN_ARGUMENTS(3); - object* tmp = _stack.at(2); + Object* tmp = _stack.at(2); _stack.erase(2, 1, false); _stack.insert(_stack.begin(), tmp); } /// @brief depth keyword implementation /// -void program::rpn_depth(void) { _stack.push_front(new number(_stack.size())); } +void program::rpn_depth(void) { _stack.push_front(new Number(_stack.size())); } /// @brief roll keyword implementation /// @@ -105,11 +105,11 @@ void program::rpn_roll(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int args = static_cast(_stack.value(0).toLong()); + int args = static_cast(_stack.value(0).toLong()); _stack.pop(); MIN_ARGUMENTS(args); - object* tmp = _stack.at(args - 1); + Object* tmp = _stack.at(args - 1); _stack.erase(args - 1, 1, false); _stack.insert(_stack.begin(), tmp); } @@ -120,11 +120,11 @@ void program::rpn_rolld(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int args = static_cast(_stack.value(0).toLong()); + int args = static_cast(_stack.value(0).toLong()); _stack.pop(); MIN_ARGUMENTS(args); - object* tmp = _stack.at(0); + Object* tmp = _stack.at(0); _stack.erase(0, 1, false); _stack.insert(_stack.begin() + args - 1, tmp); } diff --git a/src/rpn-store.cpp b/src/rpn-store.cpp index 4e9f195..b8a1135 100644 --- a/src/rpn-store.cpp +++ b/src/rpn-store.cpp @@ -10,12 +10,12 @@ void program::rpn_sto(void) { ARG_MUST_BE_OF_TYPE(0, cmd_symbol); // store symbol with first value - const auto it = _heap.find(_stack.value(0)); + const auto it = _heap.find(_stack.value(0)); if (it != _heap.end()) { delete it->second; _heap.erase(it); } - _heap[_stack.value(0)] = _stack.at(1)->clone(); + _heap[_stack.value(0)] = _stack.at(1)->clone(); _stack.erase(0, 2); } @@ -24,7 +24,7 @@ void program::rpn_sto(void) { void program::rpn_stoadd(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (_heap.find(_stack.value(0)) == _heap.end()) { setErrorContext(ret_unknown_variable); return; } @@ -41,7 +41,7 @@ void program::rpn_stoadd(void) { void program::rpn_stosub(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (_heap.find(_stack.value(0)) == _heap.end()) { setErrorContext(ret_unknown_variable); return; } @@ -58,7 +58,7 @@ void program::rpn_stosub(void) { void program::rpn_stomul(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (_heap.find(_stack.value(0)) == _heap.end()) { setErrorContext(ret_unknown_variable); return; } @@ -75,7 +75,7 @@ void program::rpn_stomul(void) { void program::rpn_stodiv(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (_heap.find(_stack.value(0)) == _heap.end()) { setErrorContext(ret_unknown_variable); return; } @@ -92,7 +92,7 @@ void program::rpn_stodiv(void) { void program::rpn_stoneg(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (_heap.find(_stack.value(0)) == _heap.end()) { setErrorContext(ret_unknown_variable); return; } @@ -108,7 +108,7 @@ void program::rpn_stoneg(void) { void program::rpn_stoinv(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (_heap.find(_stack.value(0)) == _heap.end()) { setErrorContext(ret_unknown_variable); return; } @@ -126,8 +126,8 @@ void program::rpn_rcl(void) { ARG_MUST_BE_OF_TYPE(0, cmd_symbol); // recall a variable - object* obj; - string variable(_stack.value(0)); + Object* obj; + string variable(_stack.value(0)); // mind the order of heaps if (find_variable(variable, obj)) { @@ -157,9 +157,9 @@ void program::rpn_edit(void) { /// /// @param symb the smlbol to recall and autoeval /// -void program::auto_rcl(symbol* symb) { +void program::auto_rcl(Symbol* symb) { if (symb->autoEval) { - object* obj; + Object* obj; string variable(symb->value); // mind the order of heaps @@ -180,7 +180,7 @@ void program::rpn_purge(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - const auto i = _heap.find(_stack.value(0)); + const auto i = _heap.find(_stack.value(0)); if (i != _heap.end()) { delete i->second; _heap.erase(i); @@ -193,7 +193,7 @@ void program::rpn_purge(void) { /// @brief vars keyword implementation /// void program::rpn_vars(void) { - object* obj; + Object* obj; program* parent = _parent; string name; int index = 1; diff --git a/src/rpn-string.cpp b/src/rpn-string.cpp index 18b5b1d..dc8e38d 100644 --- a/src/rpn-string.cpp +++ b/src/rpn-string.cpp @@ -14,7 +14,7 @@ void program::rpn_instr() { stringstream ss; ss << _stack.at(0); _stack.pop(); - _stack.push(new ostring(ss.str())); + _stack.push(new String(ss.str())); } } @@ -24,7 +24,7 @@ void program::rpn_strout() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_string); - string entry(_stack.value(0)); + string entry(_stack.value(0)); program prog(_stack, _heap); _stack.pop(); @@ -39,10 +39,10 @@ void program::rpn_strout() { void program::rpn_chr() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - char the_chr = static_cast(_stack.value(0).toLong()); + char the_chr = static_cast(_stack.value(0).toLong()); _stack.pop(); if (the_chr < 32 || the_chr > 126) the_chr = '.'; - _stack.push_front(new ostring(string(1, the_chr))); + _stack.push_front(new String(string(1, the_chr))); } /// @brief num keyword implementation @@ -50,10 +50,10 @@ void program::rpn_chr() { void program::rpn_num() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_string); - if (_stack.value(0).size() > 0) - _stack.push_front(new number(_stack.value(0)[0])); + if (_stack.value(0).size() > 0) + _stack.push_front(new Number(_stack.value(0)[0])); else - _stack.push_front(new number(0)); + _stack.push_front(new Number(0)); _stack.erase(1); } @@ -62,7 +62,7 @@ void program::rpn_num() { void program::rpn_strsize() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_string); - _stack.push_front(new number(_stack.value(0).size())); + _stack.push_front(new Number(_stack.value(0).size())); _stack.erase(1); } @@ -73,9 +73,9 @@ void program::rpn_strpos() { ARG_MUST_BE_OF_TYPE(0, cmd_string); ARG_MUST_BE_OF_TYPE(1, cmd_string); - size_t pos = _stack.value(1).find(_stack.value(0)) + 1; + size_t pos = _stack.value(1).find(_stack.value(0)) + 1; _stack.erase(0, 2); - _stack.push_front(new number(pos)); + _stack.push_front(new Number(pos)); } /// @brief sub keyword implementation @@ -86,11 +86,11 @@ void program::rpn_strsub() { ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(2, cmd_string); - size_t first = _stack.value(1).toULong(); - size_t len = _stack.value(0).toULong() - first + 1; + size_t first = _stack.value(1).toULong(); + size_t len = _stack.value(0).toULong() - first + 1; first--; - if (first > _stack.value(2).size()) first = len = 0; - _stack.push(new ostring(_stack.value(2).substr(first, len))); + if (first > _stack.value(2).size()) first = len = 0; + _stack.push(new String(_stack.value(2).substr(first, len))); _stack.erase(1, 3); } diff --git a/src/rpn-test-framework.cpp b/src/rpn-test-framework.cpp index 0685e8b..d838779 100644 --- a/src/rpn-test-framework.cpp +++ b/src/rpn-test-framework.cpp @@ -76,7 +76,7 @@ void program::rpn_test() { int total_steps = 0; int total_steps_failed = 0; - string test_filename = _stack.value(0); + string test_filename = _stack.value(0); _stack.pop(); cout << endl << "rpn version is " << RPN_VERSION << endl; test(test_filename, total_tests, total_tests_failed, total_steps, total_steps_failed); diff --git a/src/rpn-test.cpp b/src/rpn-test.cpp index 8b80d52..defe403 100644 --- a/src/rpn-test.cpp +++ b/src/rpn-test.cpp @@ -7,10 +7,10 @@ /// @return 0 strings are equal /// @return !0 strings are not equal (see strcmp output) /// -static int cmpStringsOnStackTop(rpnstack& stk) { +static int CmpStringOnStackTop(rpnstack& stk) { // _stack should have 2 strings at level 1 and 2 // this function removes these 2 entries - int res = stk.value(1).compare(stk.value(0)); + int res = stk.value(1).compare(stk.value(0)); stk.erase(0, 2); return res; } @@ -20,10 +20,10 @@ static int cmpStringsOnStackTop(rpnstack& stk) { void program::rpn_sup(void) { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.push_front(new number(_stack.value(1) > _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) > _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.push_front(new number(cmpStringsOnStackTop(_stack) == 1)); + _stack.push_front(new Number(CmpStringOnStackTop(_stack) == 1)); _stack.erase(1, 2); } else { setErrorContext(ret_bad_operand_type); @@ -35,10 +35,10 @@ void program::rpn_sup(void) { void program::rpn_sup_eq(void) { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.push_front(new number(_stack.value(1) >= _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) >= _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.push_front(new number(cmpStringsOnStackTop(_stack) != -1)); + _stack.push_front(new Number(CmpStringOnStackTop(_stack) != -1)); _stack.erase(1, 2); } else { setErrorContext(ret_bad_operand_type); @@ -51,10 +51,10 @@ void program::rpn_inf(void) { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.push_front(new number(_stack.value(1) < _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) < _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.push_front(new number(cmpStringsOnStackTop(_stack) == -1)); + _stack.push_front(new Number(CmpStringOnStackTop(_stack) == -1)); _stack.erase(1, 2); } else { setErrorContext(ret_bad_operand_type); @@ -66,10 +66,10 @@ void program::rpn_inf(void) { void program::rpn_inf_eq(void) { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.push_front(new number(_stack.value(1) <= _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) <= _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.push_front(new number(cmpStringsOnStackTop(_stack) != 1)); + _stack.push_front(new Number(CmpStringOnStackTop(_stack) != 1)); _stack.erase(1, 2); } else { setErrorContext(ret_bad_operand_type); @@ -81,13 +81,13 @@ void program::rpn_inf_eq(void) { void program::rpn_diff(void) { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.push_front(new number(_stack.value(1) != _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) != _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.push_front(new number(_stack.value(1) != _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) != _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.push_front(new number(cmpStringsOnStackTop(_stack) != 0)); + _stack.push_front(new Number(CmpStringOnStackTop(_stack) != 0)); _stack.erase(1, 2); } else { setErrorContext(ret_bad_operand_type); @@ -99,13 +99,13 @@ void program::rpn_diff(void) { void program::rpn_eq(void) { MIN_ARGUMENTS(2); if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { - _stack.push_front(new number(_stack.value(1) == _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) == _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { - _stack.push_front(new number(_stack.value(1) == _stack.value(0))); + _stack.push_front(new Number(_stack.value(1) == _stack.value(0))); _stack.erase(1, 2); } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { - _stack.push_front(new number(cmpStringsOnStackTop(_stack) == 0)); + _stack.push_front(new Number(CmpStringOnStackTop(_stack) == 0)); _stack.erase(1, 2); } else { setErrorContext(ret_bad_operand_type); @@ -118,10 +118,10 @@ void program::rpn_test_and(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - if (_stack.value(0) != 0 && _stack.value(1) != 0) - _stack.push(new number(1)); + if (_stack.value(0) != 0 && _stack.value(1) != 0) + _stack.push(new Number(1)); else - _stack.push(new number(0)); + _stack.push(new Number(0)); _stack.erase(1, 2); } @@ -131,10 +131,10 @@ void program::rpn_test_or(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - if (_stack.value(0) != 0 || _stack.value(1) != 0) - _stack.push(new number(1)); + if (_stack.value(0) != 0 || _stack.value(1) != 0) + _stack.push(new Number(1)); else - _stack.push(new number(0)); + _stack.push(new Number(0)); _stack.erase(1, 2); } @@ -144,10 +144,10 @@ void program::rpn_test_xor(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - if (_stack.value(0) != 0 ^ _stack.value(1) != 0) - _stack.push(new number(1)); + if (_stack.value(0) != 0 ^ _stack.value(1) != 0) + _stack.push(new Number(1)); else - _stack.push(new number(0)); + _stack.push(new Number(0)); _stack.erase(1, 2); } @@ -157,7 +157,7 @@ void program::rpn_test_not(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.push(new number(_stack.value(0) == 0 ? 1 : 0)); + _stack.push(new Number(_stack.value(0) == 0 ? 1 : 0)); _stack.erase(1, 1); } diff --git a/src/rpn-time.cpp b/src/rpn-time.cpp index ad37b3f..08f8bae 100644 --- a/src/rpn-time.cpp +++ b/src/rpn-time.cpp @@ -22,8 +22,8 @@ void program::rpn_time() { // push it // division after push for real precision - _stack.push(new number(date)); - _stack.value(0) /= 10000000000.0; + _stack.push(new Number(date)); + _stack.value(0) /= 10000000000.0; } else { setErrorContext(ret_internal); } @@ -46,10 +46,10 @@ void program::rpn_date() { static_cast(tm->tm_year + 1900); // push it - number* num; + Number* num; // division after push for real precision - _stack.push(new number(date)); - _stack.value(0) /= 1000000.0; + _stack.push(new Number(date)); + _stack.value(0) /= 1000000.0; } else { setErrorContext(ret_internal); } @@ -69,7 +69,7 @@ void program::rpn_ticks() { if (tm != nullptr) { // date in µs date = 1000000.0 * static_cast(ts.tv_sec) + static_cast(ts.tv_nsec / 1000); - _stack.push(new number(date)); + _stack.push(new Number(date)); } else { setErrorContext(ret_internal); } diff --git a/src/rpn-trig.cpp b/src/rpn-trig.cpp index ebce9d6..de01e50 100644 --- a/src/rpn-trig.cpp +++ b/src/rpn-trig.cpp @@ -5,7 +5,7 @@ /// @brief pi keyword implementation /// void program::rpn_pi(void) { - _stack.push_front(new number(mpfr::const_pi())); + _stack.push_front(new Number(mpfr::const_pi())); } /// @brief d->r keyword implementation @@ -13,8 +13,8 @@ void program::rpn_pi(void) { void program::rpn_d2r(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) *= mpfr::const_pi(); - _stack.value(0) /= 180; + _stack.value(0) *= mpfr::const_pi(); + _stack.value(0) /= 180; } /// @brief r->d keyword implementation @@ -22,8 +22,8 @@ void program::rpn_d2r(void) { void program::rpn_r2d(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - _stack.value(0) /= mpfr::const_pi(); - _stack.value(0) *= 180; + _stack.value(0) /= mpfr::const_pi(); + _stack.value(0) *= 180; } /// @brief sin keyword implementation @@ -31,9 +31,9 @@ void program::rpn_r2d(void) { void program::rpn_sin(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = sin(_stack.value(0)); + _stack.value(0) = sin(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = sin(_stack.value(0)); + _stack.value(0) = sin(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -43,9 +43,9 @@ void program::rpn_sin(void) { void program::rpn_asin(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = asin(_stack.value(0)); + _stack.value(0) = asin(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = asin(_stack.value(0)); + _stack.value(0) = asin(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -55,9 +55,9 @@ void program::rpn_asin(void) { void program::rpn_cos(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = cos(_stack.value(0)); + _stack.value(0) = cos(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = cos(_stack.value(0)); + _stack.value(0) = cos(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -67,9 +67,9 @@ void program::rpn_cos(void) { void program::rpn_acos(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = acos(_stack.value(0)); + _stack.value(0) = acos(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = acos(_stack.value(0)); + _stack.value(0) = acos(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -79,9 +79,9 @@ void program::rpn_acos(void) { void program::rpn_tan(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = tan(_stack.value(0)); + _stack.value(0) = tan(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = tan(_stack.value(0)); + _stack.value(0) = tan(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } @@ -91,9 +91,9 @@ void program::rpn_tan(void) { void program::rpn_atan(void) { MIN_ARGUMENTS(1); if (_stack.type(0) == cmd_number) - _stack.value(0) = atan(_stack.value(0)); + _stack.value(0) = atan(_stack.value(0)); else if (_stack.type(0) == cmd_complex) - _stack.value(0) = atan(_stack.value(0)); + _stack.value(0) = atan(_stack.value(0)); else setErrorContext(ret_bad_operand_type); } diff --git a/src/stack.hpp b/src/stack.hpp index 1e55024..21e43bc 100644 --- a/src/stack.hpp +++ b/src/stack.hpp @@ -13,18 +13,18 @@ using namespace std; /// @brief stack object, parens of program, storing execution stack values or programs /// -class rpnstack : public deque { +class rpnstack : public deque { public: rpnstack() {} virtual ~rpnstack() { - for_each(begin(), end(), [](object* o) { delete o; }); + for_each(begin(), end(), [](Object* o) { delete o; }); deque::erase(begin(), end()); } // stack manipulation void erase(size_t first = 0, size_t nb = 1, bool del = true) { size_t last = std::min(first + nb, size()); - if (del) for_each(begin() + first, begin() + last, [](object* o) { delete o; }); + if (del) for_each(begin() + first, begin() + last, [](Object* o) { delete o; }); deque::erase(begin() + first, begin() + last); } @@ -49,12 +49,12 @@ class rpnstack : public deque { return static_cast(at(level))->value; } - void push(object* o) { deque::push_front(o); } + void push(Object* o) { deque::push_front(o); } }; /// @brief heap object, storing variables (=named object) /// -class heap : public map { +class heap : public map { public: heap() {} virtual ~heap() { clear(); } @@ -64,7 +64,7 @@ class heap : public map { map::erase(begin(), end()); } - bool get(const string name, object*& obj) { + bool get(const string name, Object*& obj) { auto i = find(name); if (i != end()) { obj = i->second; From 2a980eb74dd1185a2e3ccc6759d092614aba3dc3 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 25 Feb 2022 14:16:55 +0100 Subject: [PATCH 3/6] enum names and values --- src/input.cpp | 22 +- src/input.hpp | 4 +- src/lexer.cpp | 26 +- src/lexer.hpp | 4 +- src/main.cpp | 10 +- src/object.hpp | 105 ++++---- src/program.cpp | 496 +++++++++++++++++++------------------ src/program.hpp | 30 +-- src/rpn-branch.cpp | 152 ++++++------ src/rpn-complex.cpp | 18 +- src/rpn-general.cpp | 40 +-- src/rpn-logs.cpp | 84 +++---- src/rpn-program.cpp | 28 +-- src/rpn-real.cpp | 152 ++++++------ src/rpn-stack.cpp | 12 +- src/rpn-store.cpp | 36 +-- src/rpn-string.cpp | 22 +- src/rpn-test-framework.cpp | 8 +- src/rpn-test.cpp | 54 ++-- src/rpn-time.cpp | 6 +- src/rpn-trig.cpp | 40 +-- src/stack.hpp | 2 +- 22 files changed, 676 insertions(+), 675 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index 6a0fa67..a272f16 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -4,19 +4,19 @@ #include -vector* Input::_ac_list = nullptr; +vector* Input::ac_list_ = nullptr; Input::Input(string& entry, vector& autocompletion_list, string prompt, string multiline_prompt) - : status(cont) { + : status(InputStatus::kContinue) { char* c_entry = nullptr; bool multiline = false; int entry_len; - _ac_list = &autocompletion_list; + ac_list_ = &autocompletion_list; // linenoise for entry linenoiseSetCompletionCallback(entry_completion_generator); - while (status == cont) { + while (status == InputStatus::kContinue) { // get user entry if (multiline) c_entry = linenoise(multiline_prompt.c_str(), &entry_len); @@ -26,9 +26,9 @@ Input::Input(string& entry, vector& autocompletion_list, string prompt, // Ctrl-C if (linenoiseKeyType() == 1) { if (entry_len > 0 || multiline) - status = abort; + status = InputStatus::kAbort; else - status = ctrlc; + status = InputStatus::kCtrlc; } else if (linenoiseKeyType() == 3) { multiline = true; if (c_entry != nullptr) entry += c_entry; @@ -38,9 +38,9 @@ Input::Input(string& entry, vector& autocompletion_list, string prompt, entry += c_entry; // keep history if (c_entry[0] != 0) (void)linenoiseHistoryAdd(entry.c_str()); - status = ok; + status = InputStatus::kOk; } else { - status = error; + status = InputStatus::kError; } } } @@ -54,16 +54,16 @@ Input::Input(string& entry, vector& autocompletion_list, string prompt, /// @param lc the completion object to add strings with linenoiseAddCompletion() /// void Input::entry_completion_generator(const char* text, linenoiseCompletions* lc) { - if (Input::_ac_list == nullptr || text == nullptr) return; + if (Input::ac_list_ == nullptr || text == nullptr) return; int text_len = strnlen(text, 6); if (text_len == 0) // propose all keywords - for (string& ac : *Input::_ac_list) linenoiseAddCompletion(lc, ac.c_str()); + for (string& ac : *Input::ac_list_) linenoiseAddCompletion(lc, ac.c_str()); else // propose only keywords matching to text begining - for (string& ac : *Input::_ac_list) + for (string& ac : *Input::ac_list_) // compare list entry with text, return if match if (ac.compare(0, text_len, text) == 0) linenoiseAddCompletion(lc, ac.c_str()); } diff --git a/src/input.hpp b/src/input.hpp index f1d9e19..896749c 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -14,13 +14,13 @@ using std::vector; class Input { public: Input(string& entry, vector& autocompletion_list, string prompt = "rpn> ", string multiline_prompt = ">"); - enum { ok, cont, abort, ctrlc, error } status; + enum class InputStatus { kOk, kContinue, kAbort, kCtrlc, kError } status; static void preload(const char* preloadText); private: static void entry_completion_generator(const char* text, linenoiseCompletions* lc); - static vector* _ac_list; + static vector* ac_list_; }; #endif // SRC_INPUT_HPP_ diff --git a/src/lexer.cpp b/src/lexer.cpp index 747a5e2..e8169f7 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -55,13 +55,13 @@ bool Lexer::parseString(string& entry, size_t idx, size_t& next_idx, vector= entry.size()) { - elements.push_back({cmd_symbol, .value = entry.substr(idx, entry.size() - idx)}); + elements.push_back({kSymbol, .value = entry.substr(idx, entry.size() - idx)}); next_idx = entry.size(); if (re != nullptr) delete re; if (im != nullptr) delete im; @@ -219,13 +219,13 @@ bool Lexer::parseComplex(string& entry, size_t idx, size_t& next_idx, vector> token; - elements.push_back({cmd_symbol, .value = token, .autoEval = true}); + elements.push_back({kSymbol, .value = token, .autoEval = true}); next_idx = token.size() + idx; return true; } diff --git a/src/lexer.hpp b/src/lexer.hpp index 217ded8..509124c 100644 --- a/src/lexer.hpp +++ b/src/lexer.hpp @@ -19,7 +19,7 @@ class Lexer { public: // a structure to describe a syntaxical element and its value struct SynElement { - cmd_type_t type; + ObjectType type; string value; mpreal* re; mpreal* im; @@ -35,7 +35,7 @@ class Lexer { }; struct ReservedWord { - cmd_type_t type; + ObjectType type; program_fn_t fn; }; diff --git a/src/main.cpp b/src/main.cpp index bed85d1..8179141 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -94,16 +94,16 @@ int main(int argc, char* argv[]) { program prog(stack, heap); string entry; switch (Input(entry, program::getAutocompletionWords()).status) { - case Input::ok: + case Input::InputStatus::kOk: // user could stop prog with CtrlC CatchSignals(&prog); // run it - if (prog.parse(entry) == ret_ok && prog.run() == ret_good_bye) + if (prog.parse(entry) == kOk && prog.run() == kGoodbye) go_on = false; else prog.show_stack(); break; - case Input::ctrlc: + case Input::InputStatus::kCtrlc: go_on = false; break; default: @@ -125,7 +125,7 @@ int main(int argc, char* argv[]) { // make program ret = prog.parse(entry); - if (ret == ret_ok) { + if (ret == kOk) { // user could stop prog with CtrlC CatchSignals(&prog); @@ -137,6 +137,6 @@ int main(int argc, char* argv[]) { mpfr_free_cache(); - if (ret != ret_ok) return EXIT_FAILURE; + if (ret != kOk) return EXIT_FAILURE; return EXIT_SUCCESS; } diff --git a/src/object.hpp b/src/object.hpp index c491dd7..67e2727 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -20,37 +20,36 @@ using std::complex; // definitions for objects /// typedef enum { - ret_ok, - ret_unknown_err, - ret_missing_operand, - ret_bad_operand_type, - ret_out_of_range, - ret_unknown_variable, - ret_internal, - ret_deadly, - ret_good_bye, - ret_not_impl, - ret_nop, - ret_syntax, - ret_div_by_zero, - ret_runtime_error, - ret_abort_current_entry, - ret_out_of_memory, - ret_bad_value, - ret_test_failed -} ret_value; + kOk, + kUnknownError, + kMissingOperand, + kBadOperandType, + kOutOfRange, + kUnknownVariable, + kInternalError, + kDeadlyError, + kGoodbye, + kNotImplemented, + kNop, + kSyntaxError, + kDivByZero, + kRuntimeError, + kAbortCurrentEntry, + kOutOfMemory, + kBadValue, + kTestFailed +} RetValue; typedef enum { - cmd_undef, - cmd_number, // floating point number - cmd_complex, // complex, couple of floating point numbers - cmd_string, // "string" - cmd_symbol, // 'symbol' - cmd_program, // << instructions >> «instructions» - cmd_keyword, // langage (reserved) keyword - cmd_branch, // langage (reserved) branch keyword - cmd_max -} cmd_type_t; + kUndef, + kNumber, // 3.1416, 1e-1234, 0x12ab, 2b110, 50ba12 + kComplex, // (1,2) + kString, // "string" + kSymbol, // 'symbol' + kProgram, // << instructions >> «instructions» + kKeyword, // langage (reserved) keyword (rot, dup, swap ..) + kBranch // langage (reserved) branch keyword (for, if, then ..) +} ObjectType; class program; class Branch; @@ -61,9 +60,9 @@ typedef size_t (program::*branch_fn_t)(Branch&); /// @brief Object - a generic stack object /// struct Object { - explicit Object(cmd_type_t type = cmd_undef) : _type(type) {} + explicit Object(ObjectType type = kUndef) : _type(type) {} virtual ~Object() {} - cmd_type_t _type; + ObjectType _type; virtual Object* clone() { Object* o = new Object(); if (o != nullptr) *o = *this; @@ -83,9 +82,9 @@ struct Object { /// @brief stack objects derived from Object /// struct Number : Object { - Number() : Object(cmd_number), base(10) {} - explicit Number(const mpreal& value_, int base_ = 10) : Object(cmd_number), base(base_), value(value_) {} - explicit Number(int value_, int base_ = 10) : Object(cmd_number), base(base_), value(value_) {} + Number() : Object(kNumber), base(10) {} + explicit Number(const mpreal& value_, int base_ = 10) : Object(kNumber), base(base_), value(value_) {} + explicit Number(int value_, int base_ = 10) : Object(kNumber), base(base_), value(value_) {} int base; mpreal value; @@ -95,9 +94,9 @@ struct Number : Object { virtual ostream& show(ostream& out) { return showValue(out, value, s_mode, s_digits, base); } // representation mode - typedef enum { std, fix, sci } mode_enum; + typedef enum { kStd, kFix, kSci } mode_enum; static mode_enum s_mode; - static constexpr mode_enum DEFAULT_MODE = Number::std; + static constexpr mode_enum DEFAULT_MODE = Number::kStd; // precision static constexpr mpfr_prec_t MPFR_DEFAULT_PREC_BITS = 128; @@ -109,9 +108,9 @@ struct Number : Object { stringstream format; format << "%." << digits; switch ( mode ) { - case std: format << "R*g"; break; - case fix: format << "R*f"; break; - case sci: format << "R*e"; break; + case kStd: format << "R*g"; break; + case kFix: format << "R*f"; break; + case kSci: format << "R*e"; break; } return format.str(); } @@ -128,13 +127,13 @@ struct Number : Object { /// @brief stack objects inheriting Object /// struct Complex : Object { - Complex() : Object(cmd_complex), reBase(10), imBase(10) {} + Complex() : Object(kComplex), reBase(10), imBase(10) {} explicit Complex(complex& value_, int reb = 10, int imb = 10) - : Object(cmd_complex), reBase(reb), imBase(imb) { + : Object(kComplex), reBase(reb), imBase(imb) { value = value_; } explicit Complex(mpreal& re_, mpreal& im_, int reb = 10, int imb = 10) - : Object(cmd_complex), reBase(reb), imBase(imb) { + : Object(kComplex), reBase(reb), imBase(imb) { value.real(re_); value.imag(im_); } @@ -154,8 +153,8 @@ struct Complex : Object { }; struct String : Object { - String() : Object(cmd_string) {} - explicit String(const string& value_) : Object(cmd_string), value(value_) {} + String() : Object(kString) {} + explicit String(const string& value_) : Object(kString), value(value_) {} virtual Object* clone() { return new String(value); } virtual string name() { return string("string"); } virtual ostream& show(ostream& out) { return out << "\"" << value << "\""; } @@ -163,8 +162,8 @@ struct String : Object { }; struct Program : Object { - Program() : Object(cmd_program) {} - explicit Program(const string& value_) : Object(cmd_program), value(value_) {} + Program() : Object(kProgram) {} + explicit Program(const string& value_) : Object(kProgram), value(value_) {} virtual Object* clone() { return new Program(value); } virtual string name() { return string("program"); } virtual ostream& show(ostream& out) { return out << "«" << value << "»"; } @@ -172,9 +171,9 @@ struct Program : Object { }; struct Symbol : Object { - explicit Symbol(bool autoEval_ = true) : Object(cmd_symbol), autoEval(autoEval_) {} + explicit Symbol(bool autoEval_ = true) : Object(kSymbol), autoEval(autoEval_) {} explicit Symbol(const string& value_, bool autoEval_ = true) - : Object(cmd_symbol), value(value_), autoEval(autoEval_) {} + : Object(kSymbol), value(value_), autoEval(autoEval_) {} virtual Object* clone() { return new Symbol(value, autoEval); } virtual string name() { return string("symbol"); } virtual ostream& show(ostream& out) { return out << "'" << value << "'"; } @@ -183,8 +182,8 @@ struct Symbol : Object { }; struct Keyword : Object { - Keyword() : Object(cmd_keyword) {} - explicit Keyword(program_fn_t fn_, const string& value_) : Object(cmd_keyword), fn(fn_), value(value_) {} + Keyword() : Object(kKeyword) {} + explicit Keyword(program_fn_t fn_, const string& value_) : Object(kKeyword), fn(fn_), value(value_) {} virtual Object* clone() { return new Keyword(fn, value); } virtual string name() { return string("keyword"); } program_fn_t fn; @@ -192,8 +191,8 @@ struct Keyword : Object { }; struct Branch : Object { - Branch() : Object(cmd_branch) {} - explicit Branch(branch_fn_t fn_, const string& value_) : Object(cmd_branch) { + Branch() : Object(kBranch) {} + explicit Branch(branch_fn_t fn_, const string& value_) : Object(kBranch) { fn = fn_; arg1 = static_cast(-1); arg2 = static_cast(-1); @@ -201,7 +200,7 @@ struct Branch : Object { arg_bool = 0; value = value_; } - explicit Branch(Branch& other) : Object(cmd_branch) { + explicit Branch(Branch& other) : Object(kBranch) { fn = other.fn; arg1 = other.arg1; arg2 = other.arg2; diff --git a/src/program.cpp b/src/program.cpp index 1a6b89f..722da4f 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -2,205 +2,205 @@ #include "program.hpp" -//< language reserved keywords (allowed types are cmd_keyword, cmd_branch or cmd_undef) +//< language reserved keywords (allowed types are kKeyword, kBranch or kUndef) vector program::_keywords{ // GENERAL - {cmd_undef, "", nullptr, "\nGENERAL"}, - {cmd_keyword, "nop", &program::rpn_nop, "no operation"}, - {cmd_keyword, "help", &program::rpn_help, "this help message"}, - {cmd_keyword, "h", &program::rpn_help, ""}, - {cmd_keyword, "?", &program::rpn_help, ""}, - {cmd_keyword, "quit", &program::rpn_good_bye, "quit software"}, - {cmd_keyword, "q", &program::rpn_good_bye, ""}, - {cmd_keyword, "exit", &program::rpn_good_bye, ""}, - {cmd_keyword, "test", &program::rpn_test, ""}, // not seen by user - {cmd_keyword, "version", &program::rpn_version, "show rpn version"}, - {cmd_keyword, "uname", &program::rpn_uname, "show rpn complete identification string"}, - {cmd_keyword, "history", &program::rpn_history, "see commands history"}, + {kUndef, "", nullptr, "\nGENERAL"}, + {kKeyword, "nop", &program::rpn_nop, "no operation"}, + {kKeyword, "help", &program::rpn_help, "this help message"}, + {kKeyword, "h", &program::rpn_help, ""}, + {kKeyword, "?", &program::rpn_help, ""}, + {kKeyword, "quit", &program::rpn_good_bye, "quit software"}, + {kKeyword, "q", &program::rpn_good_bye, ""}, + {kKeyword, "exit", &program::rpn_good_bye, ""}, + {kKeyword, "test", &program::rpn_test, ""}, // not seen by user + {kKeyword, "version", &program::rpn_version, "show rpn version"}, + {kKeyword, "uname", &program::rpn_uname, "show rpn complete identification string"}, + {kKeyword, "history", &program::rpn_history, "see commands history"}, // USUAL OPERATIONS ON REALS AND COMPLEXES - {cmd_undef, "", nullptr, "\nUSUAL OPERATIONS ON REALS AND COMPLEXES"}, - {cmd_keyword, "+", &program::rpn_plus, "addition"}, - {cmd_keyword, "-", &program::rpn_minus, "substraction"}, - {cmd_keyword, "*", &program::rpn_mul, "multiplication"}, - {cmd_keyword, "/", &program::rpn_div, "division"}, - {cmd_keyword, "inv", &program::rpn_inv, "inverse"}, - {cmd_keyword, "chs", &program::rpn_neg, "negation"}, - {cmd_keyword, "neg", &program::rpn_neg, ""}, - {cmd_keyword, "^", &program::rpn_power, "power"}, - {cmd_keyword, "pow", &program::rpn_power, ""}, - {cmd_keyword, "sqrt", &program::rpn_squareroot, "rpn_square root"}, - {cmd_keyword, "sq", &program::rpn_square, "rpn_square"}, - {cmd_keyword, "abs", &program::rpn_abs, "absolute value (norm for a complex)"}, - {cmd_keyword, "sign", &program::rpn_sign, "sign of a number or z/|z| for a complex"}, + {kUndef, "", nullptr, "\nUSUAL OPERATIONS ON REALS AND COMPLEXES"}, + {kKeyword, "+", &program::rpn_plus, "addition"}, + {kKeyword, "-", &program::rpn_minus, "substraction"}, + {kKeyword, "*", &program::rpn_mul, "multiplication"}, + {kKeyword, "/", &program::rpn_div, "division"}, + {kKeyword, "inv", &program::rpn_inv, "inverse"}, + {kKeyword, "chs", &program::rpn_neg, "negation"}, + {kKeyword, "neg", &program::rpn_neg, ""}, + {kKeyword, "^", &program::rpn_power, "power"}, + {kKeyword, "pow", &program::rpn_power, ""}, + {kKeyword, "sqrt", &program::rpn_squareroot, "rpn_square root"}, + {kKeyword, "sq", &program::rpn_square, "rpn_square"}, + {kKeyword, "abs", &program::rpn_abs, "absolute value (norm for a complex)"}, + {kKeyword, "sign", &program::rpn_sign, "sign of a number or z/|z| for a complex"}, // OPERATIONS ON REALS - {cmd_undef, "", nullptr, "\nOPERATIONS ON REALS"}, - {cmd_keyword, "%", &program::rpn_purcent, "purcent"}, - {cmd_keyword, "%CH", &program::rpn_purcentCH, "inverse purcent"}, - {cmd_keyword, "mod", &program::rpn_modulo, "modulo"}, - {cmd_keyword, "fact", &program::rpn_fact, "n! for integer n or Gamma(x+1) for fractional x"}, - {cmd_keyword, "mant", &program::rpn_mant, "mantissa of a real number"}, - {cmd_keyword, "xpon", &program::rpn_xpon, "exponant of a real number"}, - {cmd_keyword, "floor", &program::rpn_floor, "largest number <="}, - {cmd_keyword, "ceil", &program::rpn_ceil, "smallest number >="}, - {cmd_keyword, "ip", &program::rpn_ip, "integer part"}, - {cmd_keyword, "fp", &program::rpn_fp, "fractional part"}, - {cmd_keyword, "min", &program::rpn_min, "min of 2 real numbers"}, - {cmd_keyword, "max", &program::rpn_max, "max of 2 real numbers"}, + {kUndef, "", nullptr, "\nOPERATIONS ON REALS"}, + {kKeyword, "%", &program::rpn_purcent, "purcent"}, + {kKeyword, "%CH", &program::rpn_purcentCH, "inverse purcent"}, + {kKeyword, "mod", &program::rpn_modulo, "modulo"}, + {kKeyword, "fact", &program::rpn_fact, "n! for integer n or Gamma(x+1) for fractional x"}, + {kKeyword, "mant", &program::rpn_mant, "mantissa of a real number"}, + {kKeyword, "xpon", &program::rpn_xpon, "exponant of a real number"}, + {kKeyword, "floor", &program::rpn_floor, "largest number <="}, + {kKeyword, "ceil", &program::rpn_ceil, "smallest number >="}, + {kKeyword, "ip", &program::rpn_ip, "integer part"}, + {kKeyword, "fp", &program::rpn_fp, "fractional part"}, + {kKeyword, "min", &program::rpn_min, "min of 2 real numbers"}, + {kKeyword, "max", &program::rpn_max, "max of 2 real numbers"}, // OPERATIONS ON COMPLEXES - {cmd_undef, "", nullptr, "\nOPERATIONS ON COMPLEXES"}, - {cmd_keyword, "re", &program::rpn_re, "complex real part"}, - {cmd_keyword, "im", &program::rpn_im, "complex imaginary part"}, - {cmd_keyword, "conj", &program::rpn_conj, "complex conjugate"}, - {cmd_keyword, "arg", &program::rpn_arg, "complex argument in radians"}, - {cmd_keyword, "c->r", &program::rpn_c2r, "transform a complex in 2 reals"}, - {cmd_keyword, "r->c", &program::rpn_r2c, "transform 2 reals in a complex"}, - {cmd_keyword, "p->r", &program::rpn_p2r, "cartesian to polar"}, - {cmd_keyword, "r->p", &program::rpn_r2p, "polar to cartesian"}, + {kUndef, "", nullptr, "\nOPERATIONS ON COMPLEXES"}, + {kKeyword, "re", &program::rpn_re, "complex real part"}, + {kKeyword, "im", &program::rpn_im, "complex imaginary part"}, + {kKeyword, "conj", &program::rpn_conj, "complex conjugate"}, + {kKeyword, "arg", &program::rpn_arg, "complex argument in radians"}, + {kKeyword, "c->r", &program::rpn_c2r, "transform a complex in 2 reals"}, + {kKeyword, "r->c", &program::rpn_r2c, "transform 2 reals in a complex"}, + {kKeyword, "p->r", &program::rpn_p2r, "cartesian to polar"}, + {kKeyword, "r->p", &program::rpn_r2p, "polar to cartesian"}, // MODE - {cmd_undef, "", nullptr, "\nMODE"}, - {cmd_keyword, "std", &program::rpn_std, "standard floating numbers representation. ex: std"}, - {cmd_keyword, "fix", &program::rpn_fix, "fixed point representation. ex: 6 fix"}, - {cmd_keyword, "sci", &program::rpn_sci, "scientific floating point representation. ex: 20 sci"}, - {cmd_keyword, "prec", &program::rpn_precision, "set float precision in bits. ex: 256 prec"}, - {cmd_keyword, "round", &program::rpn_round, + {kUndef, "", nullptr, "\nMODE"}, + {kKeyword, "std", &program::rpn_std, "standard floating numbers representation. ex: std"}, + {kKeyword, "fix", &program::rpn_fix, "fixed point representation. ex: 6 fix"}, + {kKeyword, "sci", &program::rpn_sci, "scientific floating point representation. ex: 20 sci"}, + {kKeyword, "prec", &program::rpn_precision, "set float precision in bits. ex: 256 prec"}, + {kKeyword, "round", &program::rpn_round, "set float rounding mode.\n\tex: [\"nearest (even)\", \"toward zero\", \"toward " "+inf\", \"toward -inf\", \"away from zero\", \"faithful rounding\", \"nearest (away from zero)\"] round"}, - {cmd_keyword, "default", &program::rpn_default, "set float representation and precision to default"}, - {cmd_keyword, "type", &program::rpn_type, "show type of stack first entry"}, - {cmd_keyword, "hex", &program::rpn_hex, "hexadecimal representation, applies on stack level 0 only"}, - {cmd_keyword, "dec", &program::rpn_dec, "decimal representation, applies on stack level 0 only"}, - {cmd_keyword, "bin", &program::rpn_bin, "binary representation, applies on stack level 0 only"}, - {cmd_keyword, "base", &program::rpn_base, "arbitrary base representation, applies on stack level 0 only"}, + {kKeyword, "default", &program::rpn_default, "set float representation and precision to default"}, + {kKeyword, "type", &program::rpn_type, "show type of stack first entry"}, + {kKeyword, "hex", &program::rpn_hex, "hexadecimal representation, applies on stack level 0 only"}, + {kKeyword, "dec", &program::rpn_dec, "decimal representation, applies on stack level 0 only"}, + {kKeyword, "bin", &program::rpn_bin, "binary representation, applies on stack level 0 only"}, + {kKeyword, "base", &program::rpn_base, "arbitrary base representation, applies on stack level 0 only"}, // TESTS - {cmd_undef, "", nullptr, "\nTEST"}, - {cmd_keyword, ">", &program::rpn_sup, "binary operator >"}, - {cmd_keyword, ">=", &program::rpn_sup_eq, "binary operator >="}, - {cmd_keyword, "<", &program::rpn_inf, "binary operator <"}, - {cmd_keyword, "<=", &program::rpn_inf_eq, "binary operator <="}, - {cmd_keyword, "!=", &program::rpn_diff, "binary operator != (different)"}, - {cmd_keyword, "==", &program::rpn_eq, "binary operator == (equal)"}, - {cmd_keyword, "and", &program::rpn_test_and, "boolean operator and"}, - {cmd_keyword, "or", &program::rpn_test_or, "boolean operator or"}, - {cmd_keyword, "xor", &program::rpn_test_xor, "boolean operator xor"}, - {cmd_keyword, "not", &program::rpn_test_not, "boolean operator not"}, - {cmd_keyword, "same", &program::rpn_same, "boolean operator same (equal)"}, + {kUndef, "", nullptr, "\nTEST"}, + {kKeyword, ">", &program::rpn_sup, "binary operator >"}, + {kKeyword, ">=", &program::rpn_sup_eq, "binary operator >="}, + {kKeyword, "<", &program::rpn_inf, "binary operator <"}, + {kKeyword, "<=", &program::rpn_inf_eq, "binary operator <="}, + {kKeyword, "!=", &program::rpn_diff, "binary operator != (different)"}, + {kKeyword, "==", &program::rpn_eq, "binary operator == (equal)"}, + {kKeyword, "and", &program::rpn_test_and, "boolean operator and"}, + {kKeyword, "or", &program::rpn_test_or, "boolean operator or"}, + {kKeyword, "xor", &program::rpn_test_xor, "boolean operator xor"}, + {kKeyword, "not", &program::rpn_test_not, "boolean operator not"}, + {kKeyword, "same", &program::rpn_same, "boolean operator same (equal)"}, // STACK - {cmd_undef, "", nullptr, "\nSTACK"}, - {cmd_keyword, "swap", &program::rpn_swap, "swap 2 first stack entries"}, - {cmd_keyword, "drop", &program::rpn_drop, "drop first stack entry"}, - {cmd_keyword, "drop2", &program::rpn_drop2, "drop 2 first stack entries"}, - {cmd_keyword, "dropn", &program::rpn_dropn, "drop n first stack entries"}, - {cmd_keyword, "del", &program::rpn_erase, "drop all stack entries"}, - {cmd_keyword, "erase", &program::rpn_erase, ""}, - {cmd_keyword, "rot", &program::rpn_rot, "rotate 3 first stack entries"}, - {cmd_keyword, "dup", &program::rpn_dup, "duplicate first stack entry"}, - {cmd_keyword, "dup2", &program::rpn_dup2, "duplicate 2 first stack entries"}, - {cmd_keyword, "dupn", &program::rpn_dupn, "duplicate n first stack entries"}, - {cmd_keyword, "pick", &program::rpn_pick, "push a copy of the given stack level onto the stack"}, - {cmd_keyword, "depth", &program::rpn_depth, "give stack depth"}, - {cmd_keyword, "roll", &program::rpn_roll, "move a stack entry to the top of the stack"}, - {cmd_keyword, "rolld", &program::rpn_rolld, "move the element on top of the stack to a higher stack position"}, - {cmd_keyword, "over", &program::rpn_over, "push a copy of the element in stack level 2 onto the stack"}, + {kUndef, "", nullptr, "\nSTACK"}, + {kKeyword, "swap", &program::rpn_swap, "swap 2 first stack entries"}, + {kKeyword, "drop", &program::rpn_drop, "drop first stack entry"}, + {kKeyword, "drop2", &program::rpn_drop2, "drop 2 first stack entries"}, + {kKeyword, "dropn", &program::rpn_dropn, "drop n first stack entries"}, + {kKeyword, "del", &program::rpn_erase, "drop all stack entries"}, + {kKeyword, "erase", &program::rpn_erase, ""}, + {kKeyword, "rot", &program::rpn_rot, "rotate 3 first stack entries"}, + {kKeyword, "dup", &program::rpn_dup, "duplicate first stack entry"}, + {kKeyword, "dup2", &program::rpn_dup2, "duplicate 2 first stack entries"}, + {kKeyword, "dupn", &program::rpn_dupn, "duplicate n first stack entries"}, + {kKeyword, "pick", &program::rpn_pick, "push a copy of the given stack level onto the stack"}, + {kKeyword, "depth", &program::rpn_depth, "give stack depth"}, + {kKeyword, "roll", &program::rpn_roll, "move a stack entry to the top of the stack"}, + {kKeyword, "rolld", &program::rpn_rolld, "move the element on top of the stack to a higher stack position"}, + {kKeyword, "over", &program::rpn_over, "push a copy of the element in stack level 2 onto the stack"}, // STRING - {cmd_undef, "", nullptr, "\nSTRING"}, - {cmd_keyword, "->str", &program::rpn_instr, "convert an object into a string"}, - {cmd_keyword, "str->", &program::rpn_strout, "convert a string into an object"}, - {cmd_keyword, "chr", &program::rpn_chr, "convert ASCII character code in stack level 1 into a string"}, - {cmd_keyword, "num", &program::rpn_num, + {kUndef, "", nullptr, "\nSTRING"}, + {kKeyword, "->str", &program::rpn_instr, "convert an object into a string"}, + {kKeyword, "str->", &program::rpn_strout, "convert a string into an object"}, + {kKeyword, "chr", &program::rpn_chr, "convert ASCII character code in stack level 1 into a string"}, + {kKeyword, "num", &program::rpn_num, "return ASCII code of the first character of the string in stack level 1 as a real number"}, - {cmd_keyword, "size", &program::rpn_strsize, "return the length of the string"}, - {cmd_keyword, "pos", &program::rpn_strpos, "seach for the string in level 1 within the string in level 2"}, - {cmd_keyword, "sub", &program::rpn_strsub, "return a substring of the string in level 3"}, + {kKeyword, "size", &program::rpn_strsize, "return the length of the string"}, + {kKeyword, "pos", &program::rpn_strpos, "seach for the string in level 1 within the string in level 2"}, + {kKeyword, "sub", &program::rpn_strsub, "return a substring of the string in level 3"}, // BRANCH - {cmd_undef, "", nullptr, "\nBRANCH"}, - {cmd_branch, "if", (program_fn_t)&program::rpn_if, + {kUndef, "", nullptr, "\nBRANCH"}, + {kBranch, "if", (program_fn_t)&program::rpn_if, "if then else " "end"}, - {cmd_branch, "then", (program_fn_t)&program::rpn_then, "used with if"}, - {cmd_branch, "else", (program_fn_t)&program::rpn_else, "used with if"}, - {cmd_branch, "end", (program_fn_t)&program::rpn_end, "used with various branch instructions"}, - {cmd_branch, "start", (program_fn_t)&program::rpn_start, " start next| step"}, - {cmd_branch, "for", (program_fn_t)&program::rpn_for, + {kBranch, "then", (program_fn_t)&program::rpn_then, "used with if"}, + {kBranch, "else", (program_fn_t)&program::rpn_else, "used with if"}, + {kBranch, "end", (program_fn_t)&program::rpn_end, "used with various branch instructions"}, + {kBranch, "start", (program_fn_t)&program::rpn_start, " start next| step"}, + {kBranch, "for", (program_fn_t)&program::rpn_for, " for next| step"}, - {cmd_branch, "next", (program_fn_t)&program::rpn_next, "used with start and for"}, - {cmd_branch, "step", (program_fn_t)&program::rpn_step, "used with start and for"}, - {cmd_keyword, "ift", &program::rpn_ift, "similar to if-then-end, ift"}, - {cmd_keyword, "ifte", &program::rpn_ifte, + {kBranch, "next", (program_fn_t)&program::rpn_next, "used with start and for"}, + {kBranch, "step", (program_fn_t)&program::rpn_step, "used with start and for"}, + {kKeyword, "ift", &program::rpn_ift, "similar to if-then-end, ift"}, + {kKeyword, "ifte", &program::rpn_ifte, "similar to if-then-else-end, " " ifte"}, - {cmd_branch, "do", (program_fn_t)&program::rpn_do, "do until end"}, - {cmd_branch, "until", (program_fn_t)&program::rpn_until, "used with do"}, - {cmd_branch, "while", (program_fn_t)&program::rpn_while, "while repeat end"}, - {cmd_branch, "repeat", (program_fn_t)&program::rpn_repeat, "used with while"}, + {kBranch, "do", (program_fn_t)&program::rpn_do, "do until end"}, + {kBranch, "until", (program_fn_t)&program::rpn_until, "used with do"}, + {kBranch, "while", (program_fn_t)&program::rpn_while, "while repeat end"}, + {kBranch, "repeat", (program_fn_t)&program::rpn_repeat, "used with while"}, // STORE - {cmd_undef, "", nullptr, "\nSTORE"}, - {cmd_keyword, "sto", &program::rpn_sto, "store a variable. ex: 1 'name' sto"}, - {cmd_keyword, "rcl", &program::rpn_rcl, "recall a variable. ex: 'name' rcl"}, - {cmd_keyword, "purge", &program::rpn_purge, "delete a variable. ex: 'name' purge"}, - {cmd_keyword, "vars", &program::rpn_vars, "list all variables"}, - {cmd_keyword, "clusr", &program::rpn_clusr, "erase all variables"}, - {cmd_keyword, "edit", &program::rpn_edit, "edit a variable content"}, - {cmd_keyword, "sto+", &program::rpn_stoadd, "add to a stored variable. ex: 1 'name' sto+ 'name' 2 sto+"}, - {cmd_keyword, "sto-", &program::rpn_stosub, "substract to a stored variable. ex: 1 'name' sto- 'name' 2 sto-"}, - {cmd_keyword, "sto*", &program::rpn_stomul, "multiply a stored variable. ex: 3 'name' sto* 'name' 2 sto*"}, - {cmd_keyword, "sto/", &program::rpn_stodiv, "divide a stored variable. ex: 3 'name' sto/ 'name' 2 sto/"}, - {cmd_keyword, "sneg", &program::rpn_stoneg, "negate a variable. ex: 'name' sneg"}, - {cmd_keyword, "sinv", &program::rpn_stoinv, "inverse a variable. ex: 1 'name' sinv"}, + {kUndef, "", nullptr, "\nSTORE"}, + {kKeyword, "sto", &program::rpn_sto, "store a variable. ex: 1 'name' sto"}, + {kKeyword, "rcl", &program::rpn_rcl, "recall a variable. ex: 'name' rcl"}, + {kKeyword, "purge", &program::rpn_purge, "delete a variable. ex: 'name' purge"}, + {kKeyword, "vars", &program::rpn_vars, "list all variables"}, + {kKeyword, "clusr", &program::rpn_clusr, "erase all variables"}, + {kKeyword, "edit", &program::rpn_edit, "edit a variable content"}, + {kKeyword, "sto+", &program::rpn_stoadd, "add to a stored variable. ex: 1 'name' sto+ 'name' 2 sto+"}, + {kKeyword, "sto-", &program::rpn_stosub, "substract to a stored variable. ex: 1 'name' sto- 'name' 2 sto-"}, + {kKeyword, "sto*", &program::rpn_stomul, "multiply a stored variable. ex: 3 'name' sto* 'name' 2 sto*"}, + {kKeyword, "sto/", &program::rpn_stodiv, "divide a stored variable. ex: 3 'name' sto/ 'name' 2 sto/"}, + {kKeyword, "sneg", &program::rpn_stoneg, "negate a variable. ex: 'name' sneg"}, + {kKeyword, "sinv", &program::rpn_stoinv, "inverse a variable. ex: 1 'name' sinv"}, // PROGRAM - {cmd_undef, "", nullptr, "\nPROGRAM"}, - {cmd_keyword, "eval", &program::rpn_eval, "evaluate (run) a program, or recall a variable. ex: 'my_prog' eval"}, - {cmd_branch, "->", (program_fn_t)&program::rpn_inprog, + {kUndef, "", nullptr, "\nPROGRAM"}, + {kKeyword, "eval", &program::rpn_eval, "evaluate (run) a program, or recall a variable. ex: 'my_prog' eval"}, + {kBranch, "->", (program_fn_t)&program::rpn_inprog, "load program local variables. ex: << -> n m << 0 n m for i i + next >> " ">>"}, // TRIG ON REALS AND COMPLEXES - {cmd_undef, "", nullptr, "\nTRIG ON REALS AND COMPLEXES"}, - {cmd_keyword, "pi", &program::rpn_pi, "pi constant"}, - {cmd_keyword, "sin", &program::rpn_sin, "sinus"}, - {cmd_keyword, "asin", &program::rpn_asin, "arg sinus"}, - {cmd_keyword, "cos", &program::rpn_cos, "cosinus"}, - {cmd_keyword, "acos", &program::rpn_acos, "arg cosinus"}, - {cmd_keyword, "tan", &program::rpn_tan, "tangent"}, - {cmd_keyword, "atan", &program::rpn_atan, "arg tangent"}, - {cmd_keyword, "d->r", &program::rpn_d2r, "convert degrees to radians"}, - {cmd_keyword, "r->d", &program::rpn_r2d, "convert radians to degrees"}, + {kUndef, "", nullptr, "\nTRIG ON REALS AND COMPLEXES"}, + {kKeyword, "pi", &program::rpn_pi, "pi constant"}, + {kKeyword, "sin", &program::rpn_sin, "sinus"}, + {kKeyword, "asin", &program::rpn_asin, "arg sinus"}, + {kKeyword, "cos", &program::rpn_cos, "cosinus"}, + {kKeyword, "acos", &program::rpn_acos, "arg cosinus"}, + {kKeyword, "tan", &program::rpn_tan, "tangent"}, + {kKeyword, "atan", &program::rpn_atan, "arg tangent"}, + {kKeyword, "d->r", &program::rpn_d2r, "convert degrees to radians"}, + {kKeyword, "r->d", &program::rpn_r2d, "convert radians to degrees"}, // LOGS ON REALS AND COMPLEXES - {cmd_undef, "", nullptr, "\nLOGS ON REALS AND COMPLEXES"}, - {cmd_keyword, "e", &program::rpn_e, "Euler constant"}, - {cmd_keyword, "ln", &program::rpn_ln, "logarithm base e"}, - {cmd_keyword, "log", &program::rpn_ln, ""}, - {cmd_keyword, "lnp1", &program::rpn_lnp1, "ln(1+x) which is useful when x is close to 0"}, - {cmd_keyword, "exp", &program::rpn_exp, "exponential"}, - {cmd_keyword, "expm", &program::rpn_expm, "exp(x)-1 which is useful when x is close to 0"}, - {cmd_keyword, "log10", &program::rpn_log10, "logarithm base 10"}, - {cmd_keyword, "alog10", &program::rpn_alog10, "exponential base 10"}, - {cmd_keyword, "exp10", &program::rpn_alog10, ""}, - {cmd_keyword, "log2", &program::rpn_log2, "logarithm base 2"}, - {cmd_keyword, "alog2", &program::rpn_alog2, "exponential base 2"}, - {cmd_keyword, "exp2", &program::rpn_alog2, ""}, - {cmd_keyword, "sinh", &program::rpn_sinh, "hyperbolic sine"}, - {cmd_keyword, "asinh", &program::rpn_asinh, "inverse hyperbolic sine"}, - {cmd_keyword, "cosh", &program::rpn_cosh, "hyperbolic cosine"}, - {cmd_keyword, "acosh", &program::rpn_acosh, "inverse hyperbolic cosine"}, - {cmd_keyword, "tanh", &program::rpn_tanh, "hyperbolic tangent"}, - {cmd_keyword, "atanh", &program::rpn_atanh, "inverse hyperbolic tangent"}, + {kUndef, "", nullptr, "\nLOGS ON REALS AND COMPLEXES"}, + {kKeyword, "e", &program::rpn_e, "Euler constant"}, + {kKeyword, "ln", &program::rpn_ln, "logarithm base e"}, + {kKeyword, "log", &program::rpn_ln, ""}, + {kKeyword, "lnp1", &program::rpn_lnp1, "ln(1+x) which is useful when x is close to 0"}, + {kKeyword, "exp", &program::rpn_exp, "exponential"}, + {kKeyword, "expm", &program::rpn_expm, "exp(x)-1 which is useful when x is close to 0"}, + {kKeyword, "log10", &program::rpn_log10, "logarithm base 10"}, + {kKeyword, "alog10", &program::rpn_alog10, "exponential base 10"}, + {kKeyword, "exp10", &program::rpn_alog10, ""}, + {kKeyword, "log2", &program::rpn_log2, "logarithm base 2"}, + {kKeyword, "alog2", &program::rpn_alog2, "exponential base 2"}, + {kKeyword, "exp2", &program::rpn_alog2, ""}, + {kKeyword, "sinh", &program::rpn_sinh, "hyperbolic sine"}, + {kKeyword, "asinh", &program::rpn_asinh, "inverse hyperbolic sine"}, + {kKeyword, "cosh", &program::rpn_cosh, "hyperbolic cosine"}, + {kKeyword, "acosh", &program::rpn_acosh, "inverse hyperbolic cosine"}, + {kKeyword, "tanh", &program::rpn_tanh, "hyperbolic tangent"}, + {kKeyword, "atanh", &program::rpn_atanh, "inverse hyperbolic tangent"}, // TIME AND DATE - {cmd_undef, "", nullptr, "\nTIME AND DATE"}, - {cmd_keyword, "time", &program::rpn_time, "time in local format"}, - {cmd_keyword, "date", &program::rpn_date, "date in local format"}, - {cmd_keyword, "ticks", &program::rpn_ticks, "system tick in µs"}, + {kUndef, "", nullptr, "\nTIME AND DATE"}, + {kKeyword, "time", &program::rpn_time, "time in local format"}, + {kKeyword, "date", &program::rpn_date, "date in local format"}, + {kKeyword, "ticks", &program::rpn_ticks, "system tick in µs"}, }; /// autocompletion vector for linenoise autocompletion @@ -214,19 +214,19 @@ vector& program::getAutocompletionWords() { /// @brief run a program on a stack and a heap /// -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::run() { +RetValue program::run() { bool go_out = false; - ret_value ret = ret_ok; - cmd_type_t type; + RetValue ret = kOk; + ObjectType type; - _err = ret_ok; + _err = kOk; _err_context = ""; // branches for 'if' ret = preprocess(); - if (ret != ret_ok) { + if (ret != kOk) { // free allocated for (Object* o : *this) delete o; _local_heap.clear(); @@ -238,36 +238,36 @@ ret_value program::run() { Object* o = at(i); switch (o->_type) { // could be an auto-evaluated symbol - case cmd_symbol: + case kSymbol: auto_rcl(reinterpret_cast(o)); i++; break; // a keyword - case cmd_keyword: { + case kKeyword: { Keyword* k = reinterpret_cast(o); // call the matching function (this->*(k->fn))(); switch (_err) { // no pb -> go on - case ret_ok: + case kOk: break; // explicit go out software - case ret_good_bye: + case kGoodbye: go_out = true; - ret = ret_good_bye; + ret = kGoodbye; break; default: // error: abort prog go_out = true; // test error: make rpn return EXIT_FAILURE - if (_err == ret_test_failed) ret = ret_test_failed; + if (_err == kTestFailed) ret = kTestFailed; // error: show it - if (show_error(_err, _err_context) == ret_deadly) + if (show_error(_err, _err_context) == kDeadlyError) // pb showing error -> go out software - ret = ret_good_bye; + ret = kGoodbye; break; } i++; @@ -275,15 +275,15 @@ ret_value program::run() { } // a branch keyword - case cmd_branch: { + case kBranch: { // call matching function Branch* b = reinterpret_cast(o); size_t next_cmd = (this->*(b->fn))(*b); switch (next_cmd) { - case step_out: // step out + case kStepOut: // step out i++; // meaning 'next command' break; - case runtime_error: // runtime error + case kRtError: // runtime error (void)show_error(_err, _err_context); go_out = true; break; @@ -315,9 +315,9 @@ ret_value program::run() { /// inner members of branch or keyword objects are filled by this function /// these inner members store for example the index of the next keyword to execute etc. /// -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::preprocess(void) { +RetValue program::preprocess(void) { struct if_layout_t { if_layout_t() : index_then_or_unti_or_repeat(-1), @@ -341,7 +341,7 @@ ret_value program::preprocess(void) { // analyse if-then-else-end branches // analyse start-{next, step} branches for (size_t i = 0; i < size(); i++) { - if (at(i)->_type == cmd_branch) { + if (at(i)->_type == kBranch) { Branch* k = reinterpret_cast(at(i)); if (k->value == "if") { if_layout_t layout; @@ -350,51 +350,51 @@ ret_value program::preprocess(void) { layout_index++; } else if (k->value == "then") { size_t next = i + 1; - if (next >= size()) next = step_out; + if (next >= size()) next = kStepOut; // nothing after 'then' -> error - if (next == step_out) { + if (next == kStepOut) { // error: show it show_syntax_error("missing end after then"); - return ret_syntax; + return kSyntaxError; } if (layout_index < 0) { // error: show it show_syntax_error("missing if before then"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { // error: show it show_syntax_error("duplicate then"); - return ret_syntax; + return kSyntaxError; } vlayout[layout_index].index_then_or_unti_or_repeat = i; k->arg1 = next; k->arg3 = vlayout[layout_index].index_if_or_do_or_while; } else if (k->value == "else") { size_t next = i + 1; - if (next >= size()) next = step_out; + if (next >= size()) next = kStepOut; // nothing after 'else' -> error - if (next == step_out) { + if (next == kStepOut) { // error: show it show_syntax_error("missing end after else"); - return ret_syntax; + return kSyntaxError; } if (layout_index < 0) { // error: show it show_syntax_error("missing if before else"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat == -1) { // error: show it show_syntax_error("missing then before else"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_else != -1) { // error: show it show_syntax_error("duplicate else"); - return ret_syntax; + return kSyntaxError; } vlayout[layout_index].index_else = i; k->arg1 = next; // fill branch1 (if was false) of 'else' @@ -410,7 +410,7 @@ ret_value program::preprocess(void) { if (vstart_index.size() == 0) { // error: show it show_syntax_error("missing start or for before next"); - return ret_syntax; + return kSyntaxError; } k->arg1 = vstart_index[vstart_index.size() - 1]; // 'next' arg1 = 'start' index reinterpret_cast(at(vstart_index[vstart_index.size() - 1]))->arg2 = @@ -420,7 +420,7 @@ ret_value program::preprocess(void) { if (vstart_index.size() == 0) { // error: show it show_syntax_error("missing start or for before step"); - return ret_syntax; + return kSyntaxError; } k->arg1 = vstart_index[vstart_index.size() - 1]; // fill 'step' branch1 = 'start' index reinterpret_cast(at(vstart_index[vstart_index.size() - 1]))->arg2 = @@ -436,23 +436,23 @@ ret_value program::preprocess(void) { layout_index++; } else if (k->value == "until") { size_t next = i + 1; - if (next >= size()) next = step_out; + if (next >= size()) next = kStepOut; // nothing after 'unti' -> error - if (next == step_out) { + if (next == kStepOut) { // error: show it show_syntax_error("missing end"); - return ret_syntax; + return kSyntaxError; } if (layout_index < 0 || !vlayout[layout_index].is_do_unti) { // error: show it show_syntax_error("missing do"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { // error: show it show_syntax_error("duplicate until"); - return ret_syntax; + return kSyntaxError; } vlayout[layout_index].index_then_or_unti_or_repeat = i; } else if (k->value == "while") { @@ -465,35 +465,35 @@ ret_value program::preprocess(void) { if (layout_index < 0 || !vlayout[layout_index].is_while_repeat) { // error: show it show_syntax_error("missing while"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { // error: show it show_syntax_error("duplicate repeat"); - return ret_syntax; + return kSyntaxError; } vlayout[layout_index].index_then_or_unti_or_repeat = i; } else if (k->value == "end") { size_t next = i + 1; - if (next >= size()) next = step_out; + if (next >= size()) next = kStepOut; if (layout_index < 0) { // error: show it show_syntax_error("missing branch instruction before end"); - return ret_syntax; + return kSyntaxError; } else { if (vlayout[layout_index].is_do_unti) { // this end closes a do..unti if (vlayout[layout_index].index_end != -1) { // error: show it show_syntax_error("duplicate end"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat == -1) { // error: show it show_syntax_error("missing until"); - return ret_syntax; + return kSyntaxError; } k->arg1 = vlayout[layout_index].index_if_or_do_or_while + 1; @@ -503,7 +503,7 @@ ret_value program::preprocess(void) { if (vlayout[layout_index].index_end != -1) { // error: show it show_syntax_error("duplicate end"); - return ret_syntax; + return kSyntaxError; } k->arg2 = vlayout[layout_index].index_if_or_do_or_while + 1; @@ -511,7 +511,7 @@ ret_value program::preprocess(void) { if (vlayout[layout_index].index_then_or_unti_or_repeat == -1) { // error: show it show_syntax_error("missing repeat"); - return ret_syntax; + return kSyntaxError; } // fill 'repeat' arg1 with 'end+1' @@ -522,7 +522,7 @@ ret_value program::preprocess(void) { if (vlayout[layout_index].index_end != -1) { // error: show it show_syntax_error("duplicate end"); - return ret_syntax; + return kSyntaxError; } if (vlayout[layout_index].index_else != -1) { // fill 'end' branch of 'else' @@ -535,7 +535,7 @@ ret_value program::preprocess(void) { } else { // error: show it show_syntax_error("missing then"); - return ret_syntax; + return kSyntaxError; } } layout_index--; @@ -547,27 +547,27 @@ ret_value program::preprocess(void) { if (layout_index >= 0) { // error: show it show_syntax_error("missing end"); - return ret_syntax; + return kSyntaxError; } if (vstart_index.size() > 0) { // error: show it show_syntax_error("missing next or step after for or start"); - return ret_syntax; + return kSyntaxError; } - return ret_ok; + return kOk; } /// @brief parse an entry string: cut it into objects chunks and add them to a program /// /// @param entry the entry string /// @param prog the program to be filled -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::parse(string& entry) { +RetValue program::parse(string& entry) { static map keywords_map; vector elements; vector errors; - ret_value ret = ret_ok; + RetValue ret = kOk; // prepare map for finding reserved keywords if (keywords_map.empty()) @@ -579,29 +579,29 @@ ret_value program::parse(string& entry) { // make objects from parsed elements for (Lexer::SynElement& element : elements) { switch (element.type) { - case cmd_number: + case kNumber: push_back(new Number(*element.re, element.reBase)); break; - case cmd_complex: + case kComplex: push_back(new Complex(*element.re, *element.im, element.reBase, element.imBase)); break; - case cmd_string: + case kString: push_back(new String(element.value)); break; - case cmd_symbol: + case kSymbol: push_back(new Symbol(element.value, element.autoEval)); break; - case cmd_program: + case kProgram: push_back(new Program(element.value)); break; - case cmd_keyword: + case kKeyword: push_back(new Keyword(element.fn, element.value)); break; - case cmd_branch: + case kBranch: push_back(new Branch((branch_fn_t)element.fn, element.value)); break; default: - show_error(ret_unknown_err, "error creating program from entry"); + show_error(kUnknownError, "error creating program from entry"); break; } if (element.re != nullptr) delete element.re; @@ -616,27 +616,29 @@ ret_value program::parse(string& entry) { /// @brief show the last error set /// -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::show_error() { - ret_value ret; +RetValue program::show_error() { + RetValue ret; // clang-format off - vector errorStrings {"ok", "unknown command", "missing operand", "bad operand type", - "out of range", "unknown variable", "internal error, aborting", "deadly", "goodbye", "not implemented", - "no operation", "syntax error", "division by zero", "runtime error", "aborted current entry", "out of memory", - "bad value", "test failed"}; + map errorStrings{{kOk, "ok"}, {kUnknownError, "unknown command"}, + {kMissingOperand, "missing operand"}, {kBadOperandType, "bad operand type"}, + {kOutOfRange, "out of range"}, {kUnknownVariable, "unknown variable"}, + {kInternalError, "internal error, aborting"}, {kDeadlyError, "deadly"}, + {kGoodbye, "goodbye"}, {kNotImplemented, "not implemented"}, + {kNop, "no operation"}, {kSyntaxError, "syntax error"}, + {kDivByZero, "division by zero"}, {kRuntimeError, "runtime error"}, + {kAbortCurrentEntry, "aborted current entry"}, {kOutOfMemory, "out of memory"}, + {kBadValue, "bad value"}, {kTestFailed, "test failed"} + }; // clang-format on - // show last recorded error - if (static_cast(_err) < errorStrings.size()) - cerr << _err_context << ": error " << _err << ": " << errorStrings[_err] << endl; - else - cerr << _err_context << " (unknown error code)" << endl; + cerr << _err_context << ": error " << _err << ": " << errorStrings[_err] << endl; switch (_err) { - case ret_internal: - case ret_deadly: - ret = ret_deadly; + case kInternalError: + case kDeadlyError: + ret = kDeadlyError; default: - ret = ret_ok; + ret = kOk; } return ret; @@ -646,9 +648,9 @@ ret_value program::show_error() { /// /// @param err the error to record /// @param context a context string -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::show_error(ret_value err, string& context) { +RetValue program::show_error(RetValue err, string& context) { // record error _err = err; _err_context = context; @@ -659,9 +661,9 @@ ret_value program::show_error(ret_value err, string& context) { /// /// @param err the error to record /// @param context a context string -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::show_error(ret_value err, const char* context) { +RetValue program::show_error(RetValue err, const char* context) { // record error _err = err; _err_context = context; @@ -672,20 +674,20 @@ ret_value program::show_error(ret_value err, const char* context) { /// /// @param err the error to record /// @param context a context string -/// @return ret_value see this type +/// @return RetValue see this type /// void program::show_syntax_error(const char* context) { // record error - _err = ret_syntax; + _err = kSyntaxError; _err_context = context; (void)show_error(); } /// @brief return the last error set /// -/// @return ret_value see this type +/// @return RetValue see this type /// -ret_value program::get_err(void) { return _err; } +RetValue program::get_err(void) { return _err; } /// @brief show a stack (show its different objects) /// generally a stack is associated to a running program diff --git a/src/program.hpp b/src/program.hpp index 05fb7dc..6a7b3da 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -28,19 +28,19 @@ class program : public deque, public Lexer { } // parser - ret_value parse(string& entry); - static ret_value get_fn(const char* fn_name, program_fn_t& fn, cmd_type_t& type); + RetValue parse(string& entry); + static RetValue get_fn(const char* fn_name, program_fn_t& fn, ObjectType& type); // running - ret_value run(); + RetValue run(); void stop(); - ret_value preprocess(void); + RetValue preprocess(void); - ret_value show_error(); - ret_value show_error(ret_value err, string& context); - ret_value show_error(ret_value err, const char* context); + RetValue show_error(); + RetValue show_error(RetValue err, string& context); + RetValue show_error(RetValue err, const char* context); void show_syntax_error(const char* context); - ret_value get_err(void); + RetValue get_err(void); void show_stack(bool show_separator = true); @@ -50,7 +50,7 @@ class program : public deque, public Lexer { private: // current error and its context - ret_value _err; + RetValue _err; string _err_context; // global stack holding results for user @@ -68,7 +68,7 @@ class program : public deque, public Lexer { private: // keywords struct keyword_t { - cmd_type_t type; + ObjectType type; string name; program_fn_t fn; string comment; @@ -93,7 +93,7 @@ class program : public deque, public Lexer { size_t rpn_for(Branch& myobj); size_t rpn_next(Branch& myobj); size_t rpn_step(Branch& myobj); - enum { step_out = static_cast(-1), runtime_error = static_cast(-2) }; + enum { kStepOut = static_cast(-1), kRtError = static_cast(-2) }; // complex void rpn_re(); @@ -259,7 +259,7 @@ class program : public deque, public Lexer { #define MIN_ARGUMENTS(num) \ do { \ if ((num) >= 0 && _stack.size() < (num)) { \ - setErrorContext(ret_missing_operand); \ + setErrorContext(kMissingOperand); \ return; \ } \ } while (0) @@ -267,7 +267,7 @@ class program : public deque, public Lexer { #define MIN_ARGUMENTS_RET(num, ret) \ do { \ if ((num) >= 0 && _stack.size() < (num)) { \ - setErrorContext(ret_missing_operand); \ + setErrorContext(kMissingOperand); \ return (ret); \ } \ } while (0) @@ -275,7 +275,7 @@ class program : public deque, public Lexer { #define ARG_MUST_BE_OF_TYPE(num, type) \ do { \ if ((num) >= 0 && _stack.at(num)->_type != (type)) { \ - setErrorContext(ret_bad_operand_type); \ + setErrorContext(kBadOperandType); \ return; \ } \ } while (0) @@ -283,7 +283,7 @@ class program : public deque, public Lexer { #define ARG_MUST_BE_OF_TYPE_RET(num, type, ret) \ do { \ if ((num) >= 0 && _stack.at(num)->_type != (type)) { \ - setErrorContext(ret_bad_operand_type); \ + setErrorContext(kBadOperandType); \ return (ret); \ } \ } while (0) diff --git a/src/rpn-branch.cpp b/src/rpn-branch.cpp index d39ca10..7d12d9b 100644 --- a/src/rpn-branch.cpp +++ b/src/rpn-branch.cpp @@ -6,28 +6,28 @@ /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_if(Branch& myobj) { // myobj.arg1 = 'if' condition evaluation value - MIN_ARGUMENTS_RET(1, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); + MIN_ARGUMENTS_RET(1, kRtError); + ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); if (_stack.value(0) != 0) myobj.arg1 = 1; else myobj.arg1 = 0; _stack.pop(); - return step_out; + return kStepOut; } /// @brief then keyword (branch) implementation /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort Branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort Branch /// size_t program::rpn_then(Branch& myobj) { // myobj.arg1 = index of then + 1 @@ -36,9 +36,9 @@ size_t program::rpn_then(Branch& myobj) { // if condition is true -> arg1 (= jump to then + 1) // else -> arg2 (= jump to else + 1 or end + 1) Branch* if_cmd; - if (myobj.arg3 >= size() || at(myobj.arg3)->_type != cmd_branch) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (myobj.arg3 >= size() || at(myobj.arg3)->_type != kBranch) { + setErrorContext(kMissingOperand); + return kRtError; } if_cmd = reinterpret_cast(at(myobj.arg3)); if (if_cmd->arg1 == 1) @@ -51,8 +51,8 @@ size_t program::rpn_then(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_else(Branch& myobj) { // myobj.arg1 = index of else + 1 @@ -61,9 +61,9 @@ size_t program::rpn_else(Branch& myobj) { // if condition was false -> arg1 (= jump to else + 1) // if condition was true -> arg2 (= jump to end + 1) Branch* if_cmd; - if (myobj.arg3 >= size() || at(myobj.arg3)->_type != cmd_branch) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (myobj.arg3 >= size() || at(myobj.arg3)->_type != kBranch) { + setErrorContext(kMissingOperand); + return kRtError; } if_cmd = reinterpret_cast(at(myobj.arg3)); if (if_cmd->arg1 == 1) @@ -76,22 +76,22 @@ size_t program::rpn_else(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_end(Branch& myobj) { - size_t ret = step_out; + size_t ret = kStepOut; // arg1 = index of do+1 in case of do..unti..end if (myobj.arg1 != -1) { // in a template do..until..end - MIN_ARGUMENTS_RET(1, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); + MIN_ARGUMENTS_RET(1, kRtError); + ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); // check arg if (_stack.value(0) == 0) ret = myobj.arg1; _stack.pop(); - } else if (myobj.arg2 != step_out) { + } else if (myobj.arg2 != kStepOut) { // arg2 = index of while+1 in case of while..repeat..end ret = myobj.arg2; } @@ -103,36 +103,36 @@ size_t program::rpn_end(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_do(Branch& myobj) { // nothing - return step_out; + return kStepOut; } /// @brief until keyword (branch) implementation /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort Branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort Branch /// size_t program::rpn_until(Branch& myobj) { // nothing - return step_out; + return kStepOut; } /// @brief ift keyword (branch) implementation /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// void program::rpn_ift(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(1, kNumber); // check ift arg // arg is true if Number != 0 or if is nan or +/-inf @@ -146,12 +146,12 @@ void program::rpn_ift(void) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// void program::rpn_ifte(void) { MIN_ARGUMENTS(3); - ARG_MUST_BE_OF_TYPE(2, cmd_number); + ARG_MUST_BE_OF_TYPE(2, kNumber); // check ifte arg if (_stack.value(2) != 0) { @@ -167,26 +167,26 @@ void program::rpn_ifte(void) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_while(Branch& myobj) { // nothing - return step_out; + return kStepOut; } /// @brief repeat keyword (branch) implementation /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_repeat(Branch& myobj) { - size_t ret = step_out; + size_t ret = kStepOut; - MIN_ARGUMENTS_RET(1, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); + MIN_ARGUMENTS_RET(1, kRtError); + ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); // check arg // myobj.arg1 is end+1 @@ -200,15 +200,15 @@ size_t program::rpn_repeat(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_start(Branch& myobj) { - size_t ret = step_out; + size_t ret = kStepOut; - MIN_ARGUMENTS_RET(2, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(1, cmd_number, runtime_error); + MIN_ARGUMENTS_RET(2, kRtError); + ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); + ARG_MUST_BE_OF_TYPE_RET(1, kNumber, kRtError); // loop boundaries myobj.firstIndex = _stack.value(1); @@ -229,20 +229,20 @@ size_t program::rpn_start(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_for(Branch& myobj) { size_t ret; - MIN_ARGUMENTS_RET(2, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(1, cmd_number, runtime_error); + MIN_ARGUMENTS_RET(2, kRtError); + ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); + ARG_MUST_BE_OF_TYPE_RET(1, kNumber, kRtError); Symbol* sym; - if (myobj.arg1 >= size() || at(myobj.arg1)->_type != cmd_symbol) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kSymbol) { + setErrorContext(kMissingOperand); + return kRtError; } sym = reinterpret_cast(at(myobj.arg1)); // arg1 = loop variable index @@ -276,16 +276,16 @@ size_t program::rpn_for(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_next(Branch& myobj) { // arg1 = loop variable index // firstIndex = current point in the loop Branch* start_or_for; - if (myobj.arg1 >= size() || at(myobj.arg1)->_type != cmd_branch) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kBranch) { + setErrorContext(kMissingOperand); + return kRtError; } start_or_for = reinterpret_cast(at(myobj.arg1)); if (!myobj.arg_bool) { @@ -301,9 +301,9 @@ size_t program::rpn_next(Branch& myobj) { if (start_or_for->arg1 != -1) { Object* obj; Symbol* var; - if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != cmd_symbol) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != kSymbol) { + setErrorContext(kMissingOperand); + return kRtError; } var = reinterpret_cast(at(start_or_for->arg1)); @@ -315,7 +315,7 @@ size_t program::rpn_next(Branch& myobj) { if (myobj.firstIndex > start_or_for->lastIndex) { // end of loop myobj.arg_bool = false; // init again next time - return step_out; + return kStepOut; } else { // for command: next instruction will be after symbol variable if (start_or_for->arg1 != -1) return start_or_for->arg1 + 1; @@ -329,27 +329,27 @@ size_t program::rpn_next(Branch& myobj) { /// /// @param myobj the current branch object /// @return size_t index of the next object to run in the current program -/// @return step_out next object to run in the current program is current + 1 -/// @return runtime_error something went wrong with preprocess, abort branch +/// @return kStepOut next object to run in the current program is current + 1 +/// @return kRtError something went wrong with preprocess, abort branch /// size_t program::rpn_step(Branch& myobj) { size_t ret; - MIN_ARGUMENTS_RET(1, runtime_error); - ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, runtime_error); + MIN_ARGUMENTS_RET(1, kRtError); + ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); mpreal step = _stack.value(0); _stack.pop(); // end of loop if step is negative or zero if (step <= 0) { - ret = step_out; + ret = kStepOut; } else { // arg1 = loop variable index // firstIndex = current count Branch* start_or_for; - if (myobj.arg1 >= size() || at(myobj.arg1)->_type != cmd_branch) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kBranch) { + setErrorContext(kMissingOperand); + return kRtError; } start_or_for = reinterpret_cast(at(myobj.arg1)); if (!myobj.arg_bool) { @@ -366,9 +366,9 @@ size_t program::rpn_step(Branch& myobj) { Symbol* var; // for command: increment symbol too - if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != cmd_symbol) { - setErrorContext(ret_missing_operand); - return runtime_error; + if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != kSymbol) { + setErrorContext(kMissingOperand); + return kRtError; } var = reinterpret_cast(at(start_or_for->arg1)); // increase symbol variable @@ -379,7 +379,7 @@ size_t program::rpn_step(Branch& myobj) { if (myobj.firstIndex > start_or_for->lastIndex) { // end of loop myobj.arg_bool = false; // init again next time - ret = step_out; + ret = kStepOut; } else { // for command: next instruction will be after symbol variable if (start_or_for->arg1 != -1) ret = start_or_for->arg1 + 1; diff --git a/src/rpn-complex.cpp b/src/rpn-complex.cpp index d90ca2d..b46716f 100644 --- a/src/rpn-complex.cpp +++ b/src/rpn-complex.cpp @@ -7,7 +7,7 @@ /// void program::rpn_re() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); _stack.push_front(new Number(real(_stack.value(0)))); _stack.erase(1); } @@ -17,7 +17,7 @@ void program::rpn_re() { /// void program::rpn_im() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); _stack.push_front(new Number(imag(_stack.value(0)))); _stack.erase(1); } @@ -27,7 +27,7 @@ void program::rpn_im() { /// void program::rpn_arg() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); _stack.push_front(new Number(arg(_stack.value(0)))); _stack.erase(1); } @@ -37,7 +37,7 @@ void program::rpn_arg() { /// void program::rpn_conj() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); _stack.value(0) = conj(_stack.value(0)); } @@ -46,8 +46,8 @@ void program::rpn_conj() { /// void program::rpn_r2c() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); _stack.push(new Complex(_stack.value(1), _stack.value(0), _stack.obj(1).reBase, _stack.obj(0).reBase)); _stack.erase(1, 2); } @@ -57,7 +57,7 @@ void program::rpn_r2c() { /// void program::rpn_c2r() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); _stack.push(new Number(real(_stack.value(0)), _stack.obj(0).reBase)); _stack.push(new Number(imag(_stack.value(1)), _stack.obj(1).imBase)); _stack.erase(2); @@ -68,7 +68,7 @@ void program::rpn_c2r() { /// void program::rpn_r2p() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); mpreal rho = abs(_stack.value(0)); mpreal theta = arg(_stack.value(0)); _stack.value(0).real(rho); @@ -80,6 +80,6 @@ void program::rpn_r2p() { /// void program::rpn_p2r() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_complex); + ARG_MUST_BE_OF_TYPE(0, kComplex); _stack.value(0) = polar(abs(_stack.value(0)), arg(_stack.value(0))); } diff --git a/src/rpn-general.cpp b/src/rpn-general.cpp index 7443805..f7d51b5 100644 --- a/src/rpn-general.cpp +++ b/src/rpn-general.cpp @@ -40,7 +40,7 @@ void program::rpn_nop() { /// @brief quit keyword implementation /// -void program::rpn_good_bye() { setErrorContext(ret_good_bye); } +void program::rpn_good_bye() { setErrorContext(kGoodbye); } /// @brief nop keyword implementation /// the result is written on stdout @@ -60,23 +60,23 @@ void program::rpn_help() { for (auto& kw : _keywords) if (!kw.comment.empty()) { // titles in bold - if (kw.type == cmd_undef) cout << ATTR_BOLD; + if (kw.type == kUndef) cout << ATTR_BOLD; // show title or keyword + comment cout << kw.name << '\t' << kw.comment << endl; - if (kw.type == cmd_undef) cout << ATTR_OFF; + if (kw.type == kUndef) cout << ATTR_OFF; } cout << endl; // show mode cout << "Current float mode is "; switch (Number::s_mode) { - case Number::std: + case Number::kStd: cout << "'std'"; break; - case Number::fix: + case Number::kFix: cout << "'fix'"; break; - case Number::sci: + case Number::kSci: cout << "'sci'"; break; default: @@ -108,17 +108,17 @@ static bool check_decimal_digits(int precision) { return precision >= 0; } /// void program::rpn_std() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int digits = static_cast(_stack.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - Number::s_mode = Number::std; + Number::s_mode = Number::kStd; Number::s_digits = digits; _stack.pop(); } else { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); } } @@ -126,17 +126,17 @@ void program::rpn_std() { /// void program::rpn_fix() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int digits = static_cast(_stack.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - Number::s_mode = Number::fix; + Number::s_mode = Number::kFix; Number::s_digits = digits; _stack.pop(); } else { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); } } @@ -144,17 +144,17 @@ void program::rpn_fix() { /// void program::rpn_sci() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int digits = static_cast(_stack.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - Number::s_mode = Number::sci; + Number::s_mode = Number::kSci; Number::s_digits = digits; _stack.pop(); } else { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); } } @@ -195,7 +195,7 @@ void program::rpn_default() { program::apply_default(); } /// void program::rpn_precision() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); // set precision int prec = static_cast(_stack.value(0).toLong()); @@ -203,13 +203,13 @@ void program::rpn_precision() { mpreal::set_default_prec(prec); // modify digits seen by user if std mode - if (Number::s_mode == Number::std) { + if (Number::s_mode == Number::kStd) { // calc max nb of digits user can see with the current bit precision Number::s_digits = mpfr::bits2digits(mpreal::get_default_prec()); } _stack.pop(); } else { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); } } @@ -217,7 +217,7 @@ void program::rpn_precision() { /// void program::rpn_round() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kString); map matchRound{MPFR_ROUND}; @@ -225,6 +225,6 @@ void program::rpn_round() { if (found != matchRound.end()) mpreal::set_default_rnd(found->second); else - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); _stack.pop(); } diff --git a/src/rpn-logs.cpp b/src/rpn-logs.cpp index c0f06c5..2c5796a 100644 --- a/src/rpn-logs.cpp +++ b/src/rpn-logs.cpp @@ -10,166 +10,166 @@ void program::rpn_e(void) { _stack.push(new Number(mpfr::const_euler())); } /// void program::rpn_log10() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = log10(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = log10(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief alog10 keyword implementation /// void program::rpn_alog10() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief log2 keyword implementation /// void program::rpn_log2() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief alog2 keyword implementation /// void program::rpn_alog2() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief ln keyword implementation /// void program::rpn_ln() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = log(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = log(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief exp keyword implementation /// void program::rpn_exp() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = exp(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = exp(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief expm keyword implementation /// void program::rpn_expm() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = exp(_stack.value(0)) - mpreal(1); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = exp(_stack.value(0)) - mpreal(1); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief lnp1 keyword implementation /// void program::rpn_lnp1() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = log(_stack.value(0) + 1); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = log(_stack.value(0) + mpreal(1)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief sinh keyword implementation /// void program::rpn_sinh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = sinh(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = sinh(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief asinh keyword implementation /// void program::rpn_asinh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = asinh(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = asinh(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief cosh keyword implementation /// void program::rpn_cosh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = cosh(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = cosh(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief acosh keyword implementation /// void program::rpn_acosh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = acosh(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = acosh(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief tanh keyword implementation /// void program::rpn_tanh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = tanh(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = tanh(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief atanh keyword implementation /// void program::rpn_atanh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = atanh(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = atanh(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } diff --git a/src/rpn-program.cpp b/src/rpn-program.cpp index 4e83558..b7afa8e 100644 --- a/src/rpn-program.cpp +++ b/src/rpn-program.cpp @@ -38,7 +38,7 @@ void program::rpn_eval(void) { string prog_text; MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_symbol) { + if (_stack.type(0) == kSymbol) { // recall a variable Object* obj; string variable(_stack.value(0)); @@ -46,7 +46,7 @@ void program::rpn_eval(void) { // if variable holds a program, run this program if (find_variable(variable, obj)) { - if (obj->_type == cmd_program) { + if (obj->_type == kProgram) { prog_text = _stack.value(0); _stack.pop(); run_prog = true; @@ -55,15 +55,15 @@ void program::rpn_eval(void) { _stack.push_front(obj); } } else { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); } - } else if (_stack.type(0) == cmd_program) { + } else if (_stack.type(0) == kProgram) { // eval a program prog_text = _stack.value(0); _stack.pop(); run_prog = true; } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } // run prog if any @@ -71,7 +71,7 @@ void program::rpn_eval(void) { program prog(_stack, _heap, this); // make program from entry - if (prog.parse(prog_text) == ret_ok) { + if (prog.parse(prog_text) == kOk) { // run it prog.run(); } @@ -86,7 +86,7 @@ int program::rpn_inprog(Branch& inprog_obj) { bool prog_found = false; if (inprog_obj.arg1 == -1) { - setErrorContext(ret_unknown_err); + setErrorContext(kUnknownError); return -1; } @@ -96,15 +96,15 @@ int program::rpn_inprog(Branch& inprog_obj) { // find next Program object for (unsigned int i = inprog_obj.arg1 + 1; i < size(); i++) { // count symbol - if (at(i)->_type == cmd_symbol) { + if (at(i)->_type == kSymbol) { count_symbols++; - } else if (at(i)->_type == cmd_program) { + } else if (at(i)->_type == kProgram) { // stop if prog prog_found = true; break; } else { // found something other than symbol - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); show_error(_err, context); return -1; } @@ -112,21 +112,21 @@ int program::rpn_inprog(Branch& inprog_obj) { // found 0 symbols if (count_symbols == 0) { - setErrorContext(ret_syntax); + setErrorContext(kSyntaxError); show_error(_err, context); return -1; } // is missing if (!prog_found) { - setErrorContext(ret_syntax); + setErrorContext(kSyntaxError); show_error(_err, context); return -1; } // check symbols Number vs stack size if (_stack.size() < count_symbols) { - setErrorContext(ret_missing_operand); + setErrorContext(kMissingOperand); show_error(_err, context); return -1; } @@ -142,7 +142,7 @@ int program::rpn_inprog(Branch& inprog_obj) { program prog(_stack, _heap, this); // make the program from entry - if (prog.parse(entry) == ret_ok) { + if (prog.parse(entry) == kOk) { // run it prog.run(); } diff --git a/src/rpn-real.cpp b/src/rpn-real.cpp index d9d00a5..5f59bd3 100644 --- a/src/rpn-real.cpp +++ b/src/rpn-real.cpp @@ -6,24 +6,24 @@ /// void program::rpn_plus() { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.value(1) += _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + } else if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.value(1) += _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.value(1) += _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { _stack.value(1) += _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { rpn_swap(); _stack.value(1) += _stack.value(0); _stack.pop(); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -31,21 +31,21 @@ void program::rpn_plus() { /// void program::rpn_minus() { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.value(1) -= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.value(1) -= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { _stack.value(1) -= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { rpn_swap(); _stack.value(1) = _stack.value(0) - _stack.value(1); _stack.pop(); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -53,21 +53,21 @@ void program::rpn_minus() { /// void program::rpn_mul() { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.value(1) *= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.value(1) *= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { _stack.value(1) *= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { rpn_swap(); _stack.value(1) *= _stack.value(0); _stack.pop(); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -75,21 +75,21 @@ void program::rpn_mul() { /// void program::rpn_div() { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.value(1) /= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.value(1) /= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { _stack.value(1) /= _stack.value(0); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { rpn_swap(); _stack.value(1) = _stack.value(0) / _stack.value(1); _stack.pop(); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -97,31 +97,31 @@ void program::rpn_div() { /// void program::rpn_neg() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = -_stack.value(0); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = -_stack.value(0); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief inv keyword implementation /// void program::rpn_inv() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = 1 / _stack.value(0); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = mpreal(1) / _stack.value(0); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief power keyword implementation /// void program::rpn_power() { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { if (_stack.value(1) >= 0) { _stack.value(1) = pow(_stack.value(1), _stack.value(0)); _stack.pop(); @@ -131,18 +131,18 @@ void program::rpn_power() { _stack.value(0) = pow(_stack.value(0), _stack.value(1)); _stack.erase(1, 2); } - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.value(1) = pow(_stack.value(1), _stack.value(0)); _stack.pop(); - } else if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { _stack.value(1) = pow(_stack.value(1), _stack.value(0)); _stack.pop(); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_number) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { rpn_swap(); _stack.value(1) = pow(_stack.value(0), _stack.value(1)); _stack.pop(); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -150,7 +150,7 @@ void program::rpn_power() { /// void program::rpn_squareroot() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) { + if (_stack.type(0) == kNumber) { if (_stack.value(0) >= 0) { _stack.value(0) = sqrt(_stack.value(0)); } else { @@ -161,10 +161,10 @@ void program::rpn_squareroot() { _stack.value(0) = sqrt(_stack.value(0)); _stack.erase(1); } - } else if (_stack.type(0) == cmd_complex) { + } else if (_stack.type(0) == kComplex) { _stack.value(0) = sqrt(_stack.value(0)); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -172,13 +172,13 @@ void program::rpn_squareroot() { /// void program::rpn_hex() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) { + if (_stack.type(0) == kNumber) { _stack.obj(0).base = 16; - } else if (_stack.type(0) == cmd_complex) { + } else if (_stack.type(0) == kComplex) { _stack.obj(0).reBase = 16; _stack.obj(0).imBase = 16; } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -186,13 +186,13 @@ void program::rpn_hex() { /// void program::rpn_bin() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) { + if (_stack.type(0) == kNumber) { _stack.obj(0).base = 2; - } else if (_stack.type(0) == cmd_complex) { + } else if (_stack.type(0) == kComplex) { _stack.obj(0).reBase = 2; _stack.obj(0).imBase = 2; } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -200,13 +200,13 @@ void program::rpn_bin() { /// void program::rpn_dec() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) { + if (_stack.type(0) == kNumber) { _stack.obj(0).base = 10; - } else if (_stack.type(0) == cmd_complex) { + } else if (_stack.type(0) == kComplex) { _stack.obj(0).reBase = 10; _stack.obj(0).imBase = 10; } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -214,21 +214,21 @@ void program::rpn_dec() { /// void program::rpn_base() { MIN_ARGUMENTS(2); - if (_stack.type(1) == cmd_number || _stack.type(1) == cmd_complex) { + if (_stack.type(1) == kNumber || _stack.type(1) == kComplex) { int base = static_cast(_stack.value(0).toLong()); _stack.pop(); if (base >= 2 && base <= 62) { - if (_stack.type(0) == cmd_number) { + if (_stack.type(0) == kNumber) { _stack.obj(0).base = base; } else { _stack.obj(0).reBase = base; _stack.obj(0).imBase = base; } } else { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); } } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -236,8 +236,8 @@ void program::rpn_base() { /// void program::rpn_purcent() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); _stack.value(1) *= _stack.value(0) / 100; _stack.pop(); } @@ -246,8 +246,8 @@ void program::rpn_purcent() { /// void program::rpn_purcentCH() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); _stack.value(1) = (_stack.value(0) * 100) / _stack.value(1); _stack.pop(); } @@ -256,20 +256,20 @@ void program::rpn_purcentCH() { /// void program::rpn_square() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) *= _stack.value(0); - else if (_stack.at(0)->_type == cmd_complex) + else if (_stack.at(0)->_type == kComplex) _stack.value(0) *= _stack.value(0); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief mod keyword implementation /// void program::rpn_modulo() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); _stack.value(1) = fmod(_stack.value(1), _stack.value(0)); _stack.pop(); } @@ -278,13 +278,13 @@ void program::rpn_modulo() { /// void program::rpn_abs() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) { + if (_stack.type(0) == kNumber) { _stack.value(0) = abs(_stack.value(0)); - } else if (_stack.type(0) == cmd_complex) { + } else if (_stack.type(0) == kComplex) { _stack.push(new Number(abs(_stack.value(0)))); _stack.erase(1); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -292,7 +292,7 @@ void program::rpn_abs() { /// void program::rpn_fact() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); // fact(n) = gamma(n+1) _stack.value(0) = gamma(_stack.value(0) + 1); } @@ -301,21 +301,21 @@ void program::rpn_fact() { /// void program::rpn_sign() { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = sgn(_stack.value(0)); - else if (_stack.at(0)->_type == cmd_complex) + else if (_stack.at(0)->_type == kComplex) _stack.value(0) = _stack.value(0) / abs(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief mant keyword implementation /// void program::rpn_mant() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); if (!isfinite(_stack.value(0))) { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); return; } mp_exp_t exp; @@ -326,9 +326,9 @@ void program::rpn_mant() { /// void program::rpn_xpon() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); if (!isfinite(_stack.value(0))) { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); return; } mp_exp_t exp; @@ -340,7 +340,7 @@ void program::rpn_xpon() { /// void program::rpn_floor() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.value(0) = floor(_stack.value(0)); } @@ -348,7 +348,7 @@ void program::rpn_floor() { /// void program::rpn_ceil() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.value(0) = ceil(_stack.value(0)); } @@ -356,7 +356,7 @@ void program::rpn_ceil() { /// void program::rpn_fp() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.value(0) = frac(_stack.value(0)); } @@ -364,7 +364,7 @@ void program::rpn_fp() { /// void program::rpn_ip() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.value(0) = trunc(_stack.value(0)); } @@ -372,8 +372,8 @@ void program::rpn_ip() { /// void program::rpn_min() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); _stack.value(0) = min(_stack.value(0), _stack.value(1)); _stack.erase(1); } @@ -382,8 +382,8 @@ void program::rpn_min() { /// void program::rpn_max() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); _stack.value(0) = max(_stack.value(0), _stack.value(1)); _stack.erase(1); } diff --git a/src/rpn-stack.cpp b/src/rpn-stack.cpp index fa98451..63cedbf 100644 --- a/src/rpn-stack.cpp +++ b/src/rpn-stack.cpp @@ -29,7 +29,7 @@ void program::rpn_drop2(void) { /// void program::rpn_dropn(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int args = static_cast(_stack.value(0).toLong()); MIN_ARGUMENTS(args + 1); @@ -51,7 +51,7 @@ void program::rpn_dup(void) { /// void program::rpn_dupn(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int args = static_cast(_stack.value(0).toLong()); _stack.pop(); @@ -72,14 +72,14 @@ void program::rpn_dup2(void) { /// void program::rpn_pick(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int to_pick = static_cast(_stack.value(0).toLong()); _stack.pop(); // treat stack depth errors if ((to_pick == 0) || (to_pick > _stack.size())) { - setErrorContext(ret_out_of_range); + setErrorContext(kOutOfRange); return; } @@ -103,7 +103,7 @@ void program::rpn_depth(void) { _stack.push_front(new Number(_stack.size())); } /// void program::rpn_roll(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int args = static_cast(_stack.value(0).toLong()); _stack.pop(); @@ -118,7 +118,7 @@ void program::rpn_roll(void) { /// void program::rpn_rolld(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); int args = static_cast(_stack.value(0).toLong()); _stack.pop(); diff --git a/src/rpn-store.cpp b/src/rpn-store.cpp index b8a1135..6d3e6d7 100644 --- a/src/rpn-store.cpp +++ b/src/rpn-store.cpp @@ -7,7 +7,7 @@ /// void program::rpn_sto(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); // store symbol with first value const auto it = _heap.find(_stack.value(0)); @@ -23,9 +23,9 @@ void program::rpn_sto(void) { /// void program::rpn_stoadd(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); if (_heap.find(_stack.value(0)) == _heap.end()) { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); return; } rpn_dup(); @@ -40,9 +40,9 @@ void program::rpn_stoadd(void) { /// void program::rpn_stosub(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); if (_heap.find(_stack.value(0)) == _heap.end()) { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); return; } rpn_dup(); @@ -57,9 +57,9 @@ void program::rpn_stosub(void) { /// void program::rpn_stomul(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); if (_heap.find(_stack.value(0)) == _heap.end()) { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); return; } rpn_dup(); @@ -74,9 +74,9 @@ void program::rpn_stomul(void) { /// void program::rpn_stodiv(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); if (_heap.find(_stack.value(0)) == _heap.end()) { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); return; } rpn_dup(); @@ -91,9 +91,9 @@ void program::rpn_stodiv(void) { /// void program::rpn_stoneg(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); if (_heap.find(_stack.value(0)) == _heap.end()) { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); return; } rpn_dup(); @@ -107,9 +107,9 @@ void program::rpn_stoneg(void) { /// void program::rpn_stoinv(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); if (_heap.find(_stack.value(0)) == _heap.end()) { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); return; } rpn_dup(); @@ -123,7 +123,7 @@ void program::rpn_stoinv(void) { /// void program::rpn_rcl(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); // recall a variable Object* obj; @@ -134,7 +134,7 @@ void program::rpn_rcl(void) { (void)_stack.pop(); _stack.push_front(obj->clone()); } else { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); } } @@ -165,7 +165,7 @@ void program::auto_rcl(Symbol* symb) { // mind the order of heaps if (find_variable(variable, obj)) { _stack.push_front(obj->clone()); - if (obj->_type == cmd_program) rpn_eval(); + if (obj->_type == kProgram) rpn_eval(); } else { _stack.push_front(symb->clone()); } @@ -178,14 +178,14 @@ void program::auto_rcl(Symbol* symb) { /// void program::rpn_purge(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_symbol); + ARG_MUST_BE_OF_TYPE(0, kSymbol); const auto i = _heap.find(_stack.value(0)); if (i != _heap.end()) { delete i->second; _heap.erase(i); } else { - setErrorContext(ret_unknown_variable); + setErrorContext(kUnknownVariable); } _stack.pop(); } diff --git a/src/rpn-string.cpp b/src/rpn-string.cpp index dc8e38d..6412c8a 100644 --- a/src/rpn-string.cpp +++ b/src/rpn-string.cpp @@ -10,7 +10,7 @@ void program::rpn_instr() { MIN_ARGUMENTS(1); // stringify only if not already a string - if (_stack.type(0) != cmd_string) { + if (_stack.type(0) != kString) { stringstream ss; ss << _stack.at(0); _stack.pop(); @@ -22,14 +22,14 @@ void program::rpn_instr() { /// void program::rpn_strout() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kString); string entry(_stack.value(0)); program prog(_stack, _heap); _stack.pop(); // make program from string in stack level 1 - if (prog.parse(entry) == ret_ok) + if (prog.parse(entry) == kOk) // run it prog.run(); } @@ -38,7 +38,7 @@ void program::rpn_strout() { /// void program::rpn_chr() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); char the_chr = static_cast(_stack.value(0).toLong()); _stack.pop(); if (the_chr < 32 || the_chr > 126) the_chr = '.'; @@ -49,7 +49,7 @@ void program::rpn_chr() { /// void program::rpn_num() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kString); if (_stack.value(0).size() > 0) _stack.push_front(new Number(_stack.value(0)[0])); else @@ -61,7 +61,7 @@ void program::rpn_num() { /// void program::rpn_strsize() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kString); _stack.push_front(new Number(_stack.value(0).size())); _stack.erase(1); } @@ -70,8 +70,8 @@ void program::rpn_strsize() { /// void program::rpn_strpos() { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_string); - ARG_MUST_BE_OF_TYPE(1, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kString); + ARG_MUST_BE_OF_TYPE(1, kString); size_t pos = _stack.value(1).find(_stack.value(0)) + 1; _stack.erase(0, 2); @@ -82,9 +82,9 @@ void program::rpn_strpos() { /// void program::rpn_strsub() { MIN_ARGUMENTS(3); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); - ARG_MUST_BE_OF_TYPE(2, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); + ARG_MUST_BE_OF_TYPE(2, kString); size_t first = _stack.value(1).toULong(); size_t len = _stack.value(0).toULong() - first + 1; diff --git a/src/rpn-test-framework.cpp b/src/rpn-test-framework.cpp index d838779..eaad02c 100644 --- a/src/rpn-test-framework.cpp +++ b/src/rpn-test-framework.cpp @@ -69,7 +69,7 @@ static void testShowResult(string title, int tests, int tests_failed, int steps, /// void program::rpn_test() { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_string); + ARG_MUST_BE_OF_TYPE(0, kString); int total_tests = 0; int total_tests_failed = 0; @@ -84,7 +84,7 @@ void program::rpn_test() { // notify to caller that test succeeded or not if (total_tests_failed > 0) { - _err = ret_test_failed; + _err = kTestFailed; _err_context = string("rpn version ") + RPN_VERSION + ", test file " + test_filename; } } @@ -113,7 +113,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail if (test_file.is_open()) { string test_title; string entry; - ret_value ret; + RetValue ret; rpnstack stk; heap hp; bool failed = false; @@ -246,7 +246,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail if (!entry.empty()) { program prog(stk, hp); ret = prog.parse(entry); - if (ret == ret_ok) { + if (ret == kOk) { // run it (void)prog.run(); last_err = static_cast(prog.get_err()); diff --git a/src/rpn-test.cpp b/src/rpn-test.cpp index defe403..06dac2e 100644 --- a/src/rpn-test.cpp +++ b/src/rpn-test.cpp @@ -19,14 +19,14 @@ static int CmpStringOnStackTop(rpnstack& stk) { /// void program::rpn_sup(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.push_front(new Number(_stack.value(1) > _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + } else if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.push_front(new Number(CmpStringOnStackTop(_stack) == 1)); _stack.erase(1, 2); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -34,14 +34,14 @@ void program::rpn_sup(void) { /// void program::rpn_sup_eq(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.push_front(new Number(_stack.value(1) >= _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + } else if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.push_front(new Number(CmpStringOnStackTop(_stack) != -1)); _stack.erase(1, 2); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -50,14 +50,14 @@ void program::rpn_sup_eq(void) { void program::rpn_inf(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.push_front(new Number(_stack.value(1) < _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + } else if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.push_front(new Number(CmpStringOnStackTop(_stack) == -1)); _stack.erase(1, 2); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -65,14 +65,14 @@ void program::rpn_inf(void) { /// void program::rpn_inf_eq(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.push_front(new Number(_stack.value(1) <= _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + } else if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.push_front(new Number(CmpStringOnStackTop(_stack) != 1)); _stack.erase(1, 2); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -80,17 +80,17 @@ void program::rpn_inf_eq(void) { /// void program::rpn_diff(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.push_front(new Number(_stack.value(1) != _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.push_front(new Number(_stack.value(1) != _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + } else if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.push_front(new Number(CmpStringOnStackTop(_stack) != 0)); _stack.erase(1, 2); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -98,17 +98,17 @@ void program::rpn_diff(void) { /// void program::rpn_eq(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == cmd_number && _stack.type(1) == cmd_number) { + if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { _stack.push_front(new Number(_stack.value(1) == _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_complex && _stack.type(1) == cmd_complex) { + } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { _stack.push_front(new Number(_stack.value(1) == _stack.value(0))); _stack.erase(1, 2); - } else if (_stack.type(0) == cmd_string && _stack.type(1) == cmd_string) { + } else if (_stack.type(0) == kString && _stack.type(1) == kString) { _stack.push_front(new Number(CmpStringOnStackTop(_stack) == 0)); _stack.erase(1, 2); } else { - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } } @@ -116,8 +116,8 @@ void program::rpn_eq(void) { /// void program::rpn_test_and(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); if (_stack.value(0) != 0 && _stack.value(1) != 0) _stack.push(new Number(1)); else @@ -129,8 +129,8 @@ void program::rpn_test_and(void) { /// void program::rpn_test_or(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); if (_stack.value(0) != 0 || _stack.value(1) != 0) _stack.push(new Number(1)); else @@ -142,8 +142,8 @@ void program::rpn_test_or(void) { /// void program::rpn_test_xor(void) { MIN_ARGUMENTS(2); - ARG_MUST_BE_OF_TYPE(0, cmd_number); - ARG_MUST_BE_OF_TYPE(1, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); + ARG_MUST_BE_OF_TYPE(1, kNumber); if (_stack.value(0) != 0 ^ _stack.value(1) != 0) _stack.push(new Number(1)); else @@ -155,7 +155,7 @@ void program::rpn_test_xor(void) { /// void program::rpn_test_not(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.push(new Number(_stack.value(0) == 0 ? 1 : 0)); _stack.erase(1, 1); diff --git a/src/rpn-time.cpp b/src/rpn-time.cpp index 08f8bae..d97956a 100644 --- a/src/rpn-time.cpp +++ b/src/rpn-time.cpp @@ -25,7 +25,7 @@ void program::rpn_time() { _stack.push(new Number(date)); _stack.value(0) /= 10000000000.0; } else { - setErrorContext(ret_internal); + setErrorContext(kInternalError); } } @@ -51,7 +51,7 @@ void program::rpn_date() { _stack.push(new Number(date)); _stack.value(0) /= 1000000.0; } else { - setErrorContext(ret_internal); + setErrorContext(kInternalError); } } @@ -71,6 +71,6 @@ void program::rpn_ticks() { date = 1000000.0 * static_cast(ts.tv_sec) + static_cast(ts.tv_nsec / 1000); _stack.push(new Number(date)); } else { - setErrorContext(ret_internal); + setErrorContext(kInternalError); } } diff --git a/src/rpn-trig.cpp b/src/rpn-trig.cpp index de01e50..8af66a1 100644 --- a/src/rpn-trig.cpp +++ b/src/rpn-trig.cpp @@ -12,7 +12,7 @@ void program::rpn_pi(void) { /// void program::rpn_d2r(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.value(0) *= mpfr::const_pi(); _stack.value(0) /= 180; } @@ -21,7 +21,7 @@ void program::rpn_d2r(void) { /// void program::rpn_r2d(void) { MIN_ARGUMENTS(1); - ARG_MUST_BE_OF_TYPE(0, cmd_number); + ARG_MUST_BE_OF_TYPE(0, kNumber); _stack.value(0) /= mpfr::const_pi(); _stack.value(0) *= 180; } @@ -30,70 +30,70 @@ void program::rpn_r2d(void) { /// void program::rpn_sin(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = sin(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = sin(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief asin keyword implementation /// void program::rpn_asin(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = asin(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = asin(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief cos keyword implementation /// void program::rpn_cos(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = cos(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = cos(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief acos keyword implementation /// void program::rpn_acos(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = acos(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = acos(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief tan keyword implementation /// void program::rpn_tan(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = tan(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = tan(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } /// @brief atan keyword implementation /// void program::rpn_atan(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == cmd_number) + if (_stack.type(0) == kNumber) _stack.value(0) = atan(_stack.value(0)); - else if (_stack.type(0) == cmd_complex) + else if (_stack.type(0) == kComplex) _stack.value(0) = atan(_stack.value(0)); else - setErrorContext(ret_bad_operand_type); + setErrorContext(kBadOperandType); } diff --git a/src/stack.hpp b/src/stack.hpp index 21e43bc..fc69912 100644 --- a/src/stack.hpp +++ b/src/stack.hpp @@ -32,7 +32,7 @@ class rpnstack : public deque { // access helpers // - cmd_type_t type(int level) { + ObjectType type(int level) { // carefull: caller must ensure that level is correct return at(level)->_type; } From 327229dda4249b82f824956ca178a3e17afdb621 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 25 Feb 2022 14:52:48 +0100 Subject: [PATCH 4/6] struct and class names --- src/lexer.cpp | 14 +- src/lexer.hpp | 6 +- src/object.cpp | 4 +- src/object.hpp | 72 +++++----- src/program.cpp | 62 ++++----- src/program.hpp | 39 +++--- src/rpn-branch.cpp | 76 +++++----- src/rpn-complex.cpp | 34 ++--- src/rpn-general.cpp | 50 +++---- src/rpn-logs.cpp | 114 +++++++-------- src/rpn-program.cpp | 50 +++---- src/rpn-real.cpp | 278 ++++++++++++++++++------------------- src/rpn-stack.cpp | 68 ++++----- src/rpn-store.cpp | 62 ++++----- src/rpn-string.cpp | 50 +++---- src/rpn-test-framework.cpp | 8 +- src/rpn-test.cpp | 116 ++++++++-------- src/rpn-time.cpp | 10 +- src/rpn-trig.cpp | 58 ++++---- 19 files changed, 586 insertions(+), 585 deletions(-) diff --git a/src/lexer.cpp b/src/lexer.cpp index e8169f7..7479504 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -71,7 +71,7 @@ bool Lexer::parseSymbol(string& entry, size_t idx, size_t& next_idx, vector& elements) { mpreal* re = nullptr; mpreal* im = nullptr; - int reBase, imBase = 10; + int re_base, im_base = 10; if (idx + 1 == entry.size()) { elements.push_back({kSymbol, .value = entry.substr(idx, entry.size() - idx)}); next_idx = entry.size(); return true; // complex format error, return a symbol } - if (!getNumberAt(entry, idx + 1, next_idx, reBase, &re, ',')) { + if (!getNumberAt(entry, idx + 1, next_idx, re_base, &re, ',')) { elements.push_back({kSymbol, .value = entry.substr(idx, entry.size() - idx)}); next_idx = entry.size(); return true; // complex format error, return a symbol @@ -218,14 +218,14 @@ bool Lexer::parseComplex(string& entry, size_t idx, size_t& next_idx, vector> token; - elements.push_back({kSymbol, .value = token, .autoEval = true}); + elements.push_back({kSymbol, .value = token, .auto_eval = true}); next_idx = token.size() + idx; return true; } diff --git a/src/lexer.hpp b/src/lexer.hpp index 509124c..c921537 100644 --- a/src/lexer.hpp +++ b/src/lexer.hpp @@ -23,10 +23,10 @@ class Lexer { string value; mpreal* re; mpreal* im; - int reBase; - int imBase; + int re_base; + int im_base; program_fn_t fn; - bool autoEval; + bool auto_eval; }; struct SynError { diff --git a/src/object.cpp b/src/object.cpp index 3ab1baa..43ba057 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -3,5 +3,5 @@ #include "object.hpp" // number statics -Number::mode_enum Number::s_mode = Number::DEFAULT_MODE; -int Number::s_digits = DEFAULT_DECIMAL_DIGITS; +Number::mode_enum Number::mode = Number::DEFAULT_MODE; +int Number::digits = kDefaultDecimalDigits; diff --git a/src/object.hpp b/src/object.hpp index 67e2727..760b9c0 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -6,14 +6,14 @@ #include using mpfr::mpreal; -#include -#include -#include #include +#include +#include +#include +using std::complex; using std::ostream; using std::string; using std::stringstream; -using std::complex; #include "mpreal-out.hpp" @@ -83,25 +83,25 @@ struct Object { /// struct Number : Object { Number() : Object(kNumber), base(10) {} - explicit Number(const mpreal& value_, int base_ = 10) : Object(kNumber), base(base_), value(value_) {} - explicit Number(int value_, int base_ = 10) : Object(kNumber), base(base_), value(value_) {} + explicit Number(const mpreal& value__, int base__ = 10) : Object(kNumber), base(base__), value(value__) {} + explicit Number(int value__, int base__ = 10) : Object(kNumber), base(base__), value(value__) {} int base; mpreal value; virtual Object* clone() { return new Number(value, base); } virtual string name() { return string("number"); } - virtual ostream& show(ostream& out) { return showValue(out, value, s_mode, s_digits, base); } + virtual ostream& show(ostream& out) { return showValue(out, value, mode, digits, base); } // representation mode typedef enum { kStd, kFix, kSci } mode_enum; - static mode_enum s_mode; + static mode_enum mode; static constexpr mode_enum DEFAULT_MODE = Number::kStd; // precision - static constexpr mpfr_prec_t MPFR_DEFAULT_PREC_BITS = 128; - static constexpr int DEFAULT_DECIMAL_DIGITS = 38; - static int s_digits; + static constexpr mpfr_prec_t kMpfrDefaultPrecBits = 128; + static constexpr int kDefaultDecimalDigits = 38; + static int digits; // clang-format off static string _makeNumberFormat(mode_enum mode, int digits) { @@ -118,7 +118,7 @@ struct Number : Object { static ostream& showValue(ostream& out, const mpreal& value, mode_enum mode, int digits, int base) { if (base == 10) - return MprealOutput10Base(out, _makeNumberFormat(s_mode, s_digits), value); + return MprealOutput10Base(out, _makeNumberFormat(mode, digits), value); else return MprealOutputNBase(out, base, value); } @@ -127,34 +127,34 @@ struct Number : Object { /// @brief stack objects inheriting Object /// struct Complex : Object { - Complex() : Object(kComplex), reBase(10), imBase(10) {} - explicit Complex(complex& value_, int reb = 10, int imb = 10) - : Object(kComplex), reBase(reb), imBase(imb) { - value = value_; + Complex() : Object(kComplex), re_base(10), im_base(10) {} + explicit Complex(complex& value__, int re_base__ = 10, int im_base__ = 10) + : Object(kComplex), re_base(re_base__), im_base(im_base__) { + value = value__; } - explicit Complex(mpreal& re_, mpreal& im_, int reb = 10, int imb = 10) - : Object(kComplex), reBase(reb), imBase(imb) { - value.real(re_); - value.imag(im_); + explicit Complex(mpreal& re__, mpreal& im__, int re_base__ = 10, int im_base__ = 10) + : Object(kComplex), re_base(re_base__), im_base(im_base__) { + value.real(re__); + value.imag(im__); } - int reBase, imBase; + int re_base, im_base; complex value; - virtual Object* clone() { return new Complex(value, reBase, imBase); } + virtual Object* clone() { return new Complex(value, re_base, im_base); } virtual string name() { return string("complex"); } virtual ostream& show(ostream& out) { out << '('; - Number::showValue(out, value.real(), Number::s_mode, Number::s_digits, reBase); + Number::showValue(out, value.real(), Number::mode, Number::digits, re_base); out << ','; - Number::showValue(out, value.imag(), Number::s_mode, Number::s_digits, imBase); + Number::showValue(out, value.imag(), Number::mode, Number::digits, im_base); return out << ')'; } }; struct String : Object { String() : Object(kString) {} - explicit String(const string& value_) : Object(kString), value(value_) {} + explicit String(const string& value__) : Object(kString), value(value__) {} virtual Object* clone() { return new String(value); } virtual string name() { return string("string"); } virtual ostream& show(ostream& out) { return out << "\"" << value << "\""; } @@ -163,7 +163,7 @@ struct String : Object { struct Program : Object { Program() : Object(kProgram) {} - explicit Program(const string& value_) : Object(kProgram), value(value_) {} + explicit Program(const string& value__) : Object(kProgram), value(value__) {} virtual Object* clone() { return new Program(value); } virtual string name() { return string("program"); } virtual ostream& show(ostream& out) { return out << "«" << value << "»"; } @@ -171,19 +171,19 @@ struct Program : Object { }; struct Symbol : Object { - explicit Symbol(bool autoEval_ = true) : Object(kSymbol), autoEval(autoEval_) {} - explicit Symbol(const string& value_, bool autoEval_ = true) - : Object(kSymbol), value(value_), autoEval(autoEval_) {} - virtual Object* clone() { return new Symbol(value, autoEval); } + explicit Symbol(bool auto_eval__ = true) : Object(kSymbol), auto_eval(auto_eval__) {} + explicit Symbol(const string& value__, bool auto_eval__ = true) + : Object(kSymbol), value(value__), auto_eval(auto_eval__) {} + virtual Object* clone() { return new Symbol(value, auto_eval); } virtual string name() { return string("symbol"); } virtual ostream& show(ostream& out) { return out << "'" << value << "'"; } - bool autoEval; + bool auto_eval; string value; }; struct Keyword : Object { Keyword() : Object(kKeyword) {} - explicit Keyword(program_fn_t fn_, const string& value_) : Object(kKeyword), fn(fn_), value(value_) {} + explicit Keyword(program_fn_t fn__, const string& value__) : Object(kKeyword), fn(fn__), value(value__) {} virtual Object* clone() { return new Keyword(fn, value); } virtual string name() { return string("keyword"); } program_fn_t fn; @@ -192,13 +192,13 @@ struct Keyword : Object { struct Branch : Object { Branch() : Object(kBranch) {} - explicit Branch(branch_fn_t fn_, const string& value_) : Object(kBranch) { - fn = fn_; + explicit Branch(branch_fn_t fn__, const string& value__) : Object(kBranch) { + fn = fn__; arg1 = static_cast(-1); arg2 = static_cast(-1); arg3 = static_cast(-1); arg_bool = 0; - value = value_; + value = value__; } explicit Branch(Branch& other) : Object(kBranch) { fn = other.fn; @@ -212,7 +212,7 @@ struct Branch : Object { virtual string name() { return string("branch"); } branch_fn_t fn; size_t arg1, arg2, arg3; - mpreal firstIndex, lastIndex; + mpreal first_index, last_index; bool arg_bool; string value; }; diff --git a/src/program.cpp b/src/program.cpp index 722da4f..30c9550 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -3,7 +3,7 @@ #include "program.hpp" //< language reserved keywords (allowed types are kKeyword, kBranch or kUndef) -vector program::_keywords{ +vector program::keywords_{ // GENERAL {kUndef, "", nullptr, "\nGENERAL"}, {kKeyword, "nop", &program::rpn_nop, "no operation"}, @@ -207,7 +207,7 @@ vector program::_keywords{ vector& program::getAutocompletionWords() { static vector autocompletion_words; if (autocompletion_words.empty()) - for (auto& kw : _keywords) + for (auto& kw : keywords_) if (!kw.name.empty()) autocompletion_words.push_back(kw.name); return autocompletion_words; } @@ -221,15 +221,15 @@ RetValue program::run() { RetValue ret = kOk; ObjectType type; - _err = kOk; - _err_context = ""; + err_ = kOk; + err_context_ = ""; // branches for 'if' ret = preprocess(); if (ret != kOk) { // free allocated for (Object* o : *this) delete o; - _local_heap.clear(); + local_heap_.clear(); return ret; } @@ -248,7 +248,7 @@ RetValue program::run() { Keyword* k = reinterpret_cast(o); // call the matching function (this->*(k->fn))(); - switch (_err) { + switch (err_) { // no pb -> go on case kOk: break; @@ -262,10 +262,10 @@ RetValue program::run() { go_out = true; // test error: make rpn return EXIT_FAILURE - if (_err == kTestFailed) ret = kTestFailed; + if (err_ == kTestFailed) ret = kTestFailed; // error: show it - if (show_error(_err, _err_context) == kDeadlyError) + if (show_error(err_, err_context_) == kDeadlyError) // pb showing error -> go out software ret = kGoodbye; break; @@ -284,7 +284,7 @@ RetValue program::run() { i++; // meaning 'next command' break; case kRtError: // runtime error - (void)show_error(_err, _err_context); + (void)show_error(err_, err_context_); go_out = true; break; default: @@ -297,7 +297,7 @@ RetValue program::run() { default: // not a command, but a stack entry, manage it // copy the program stack entry to the running stack - _stack.push_front(o->clone()); + stack_.push_front(o->clone()); i++; break; } @@ -305,7 +305,7 @@ RetValue program::run() { // free allocated for (Object* o : *this) delete o; - _local_heap.clear(); + local_heap_.clear(); return ret; } @@ -571,7 +571,7 @@ RetValue program::parse(string& entry) { // prepare map for finding reserved keywords if (keywords_map.empty()) - for (auto& kw : _keywords) + for (auto& kw : keywords_) if (!kw.name.empty()) keywords_map[kw.name] = {kw.type, kw.fn}; // separate the entry string @@ -580,16 +580,16 @@ RetValue program::parse(string& entry) { for (Lexer::SynElement& element : elements) { switch (element.type) { case kNumber: - push_back(new Number(*element.re, element.reBase)); + push_back(new Number(*element.re, element.re_base)); break; case kComplex: - push_back(new Complex(*element.re, *element.im, element.reBase, element.imBase)); + push_back(new Complex(*element.re, *element.im, element.re_base, element.im_base)); break; case kString: push_back(new String(element.value)); break; case kSymbol: - push_back(new Symbol(element.value, element.autoEval)); + push_back(new Symbol(element.value, element.auto_eval)); break; case kProgram: push_back(new Program(element.value)); @@ -632,8 +632,8 @@ RetValue program::show_error() { {kBadValue, "bad value"}, {kTestFailed, "test failed"} }; // clang-format on - cerr << _err_context << ": error " << _err << ": " << errorStrings[_err] << endl; - switch (_err) { + cerr << err_context_ << ": error " << err_ << ": " << errorStrings[err_] << endl; + switch (err_) { case kInternalError: case kDeadlyError: ret = kDeadlyError; @@ -652,8 +652,8 @@ RetValue program::show_error() { /// RetValue program::show_error(RetValue err, string& context) { // record error - _err = err; - _err_context = context; + err_ = err; + err_context_ = context; return show_error(); } @@ -665,8 +665,8 @@ RetValue program::show_error(RetValue err, string& context) { /// RetValue program::show_error(RetValue err, const char* context) { // record error - _err = err; - _err_context = context; + err_ = err; + err_context_ = context; return show_error(); } @@ -678,8 +678,8 @@ RetValue program::show_error(RetValue err, const char* context) { /// void program::show_syntax_error(const char* context) { // record error - _err = kSyntaxError; - _err_context = context; + err_ = kSyntaxError; + err_context_ = context; (void)show_error(); } @@ -687,7 +687,7 @@ void program::show_syntax_error(const char* context) { /// /// @return RetValue see this type /// -RetValue program::get_err(void) { return _err; } +RetValue program::get_err(void) { return err_; } /// @brief show a stack (show its different objects) /// generally a stack is associated to a running program @@ -695,12 +695,12 @@ RetValue program::get_err(void) { return _err; } /// @param show_separator whether to show a stack level prefix or not /// void program::show_stack(bool show_separator) { - if (_stack.size() == 1) { - cout << _stack[0] << endl; + if (stack_.size() == 1) { + cout << stack_[0] << endl; } else { - for (int i = _stack.size() - 1; i >= 0; i--) { + for (int i = stack_.size() - 1; i >= 0; i--) { if (show_separator) cout << i + 1 << "> "; - cout << _stack[i] << endl; + cout << stack_[i] << endl; } } } @@ -709,9 +709,9 @@ void program::show_stack(bool show_separator) { /// void program::apply_default() { // default float precision, float mode - Number::s_mode = Number::DEFAULT_MODE; - Number::s_digits = Number::DEFAULT_DECIMAL_DIGITS; - mpreal::set_default_prec(Number::MPFR_DEFAULT_PREC_BITS); + Number::mode = Number::DEFAULT_MODE; + Number::digits = Number::kDefaultDecimalDigits; + mpreal::set_default_prec(Number::kMpfrDefaultPrecBits); static mp_rnd_t def_rnd = mpreal::get_default_rnd(); mpreal::set_default_rnd(def_rnd); diff --git a/src/program.hpp b/src/program.hpp index 6a7b3da..b3dab55 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -21,9 +21,10 @@ using mpfr::mpreal; //< program class: the class containing a string parser, all the programs keywords, a stack for running the program class program : public deque, public Lexer { public: - program(rpnstack& stk, heap& hp, program* parent = nullptr) : _stack(stk), _heap(hp), _parent(parent) {} + program(rpnstack& stack__, heap& heap__, program* parent__ = nullptr) + : stack_(stack__), heap_(heap__), parent_(parent__) {} virtual ~program() { - _local_heap.clear(); + local_heap_.clear(); clear(); } @@ -50,20 +51,20 @@ class program : public deque, public Lexer { private: // current error and its context - RetValue _err; - string _err_context; + RetValue err_; + string err_context_; // global stack holding results for user - rpnstack& _stack; + rpnstack& stack_; // global heap (sto, rcl) - heap& _heap; + heap& heap_; // local heap for local loop variables (for..next) - heap _local_heap; + heap local_heap_; // parent prog for inheriting heaps - program* _parent; + program* parent_; private: // keywords @@ -73,7 +74,7 @@ class program : public deque, public Lexer { program_fn_t fn; string comment; }; - static vector _keywords; + static vector keywords_; // keywords implementation //// @@ -252,38 +253,38 @@ class program : public deque, public Lexer { #define setErrorContext(err) \ do { \ - _err = (err); \ - _err_context = __FUNCTION__; \ + err_ = (err); \ + err_context_ = __FUNCTION__; \ } while (0) #define MIN_ARGUMENTS(num) \ do { \ - if ((num) >= 0 && _stack.size() < (num)) { \ - setErrorContext(kMissingOperand); \ + if ((num) >= 0 && stack_.size() < (num)) { \ + setErrorContext(kMissingOperand); \ return; \ } \ } while (0) #define MIN_ARGUMENTS_RET(num, ret) \ do { \ - if ((num) >= 0 && _stack.size() < (num)) { \ - setErrorContext(kMissingOperand); \ + if ((num) >= 0 && stack_.size() < (num)) { \ + setErrorContext(kMissingOperand); \ return (ret); \ } \ } while (0) #define ARG_MUST_BE_OF_TYPE(num, type) \ do { \ - if ((num) >= 0 && _stack.at(num)->_type != (type)) { \ - setErrorContext(kBadOperandType); \ + if ((num) >= 0 && stack_.at(num)->_type != (type)) { \ + setErrorContext(kBadOperandType); \ return; \ } \ } while (0) #define ARG_MUST_BE_OF_TYPE_RET(num, type, ret) \ do { \ - if ((num) >= 0 && _stack.at(num)->_type != (type)) { \ - setErrorContext(kBadOperandType); \ + if ((num) >= 0 && stack_.at(num)->_type != (type)) { \ + setErrorContext(kBadOperandType); \ return (ret); \ } \ } while (0) diff --git a/src/rpn-branch.cpp b/src/rpn-branch.cpp index 7d12d9b..efab5dc 100644 --- a/src/rpn-branch.cpp +++ b/src/rpn-branch.cpp @@ -14,11 +14,11 @@ size_t program::rpn_if(Branch& myobj) { MIN_ARGUMENTS_RET(1, kRtError); ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); - if (_stack.value(0) != 0) + if (stack_.value(0) != 0) myobj.arg1 = 1; else myobj.arg1 = 0; - _stack.pop(); + stack_.pop(); return kStepOut; } @@ -89,8 +89,8 @@ size_t program::rpn_end(Branch& myobj) { ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); // check arg - if (_stack.value(0) == 0) ret = myobj.arg1; - _stack.pop(); + if (stack_.value(0) == 0) ret = myobj.arg1; + stack_.pop(); } else if (myobj.arg2 != kStepOut) { // arg2 = index of while+1 in case of while..repeat..end ret = myobj.arg2; @@ -136,10 +136,10 @@ void program::rpn_ift(void) { // check ift arg // arg is true if Number != 0 or if is nan or +/-inf - if (_stack.value(1) != 0) - _stack.erase(1); + if (stack_.value(1) != 0) + stack_.erase(1); else - _stack.erase(0, 2); + stack_.erase(0, 2); } /// @brief ifte keyword (branch) implementation @@ -154,12 +154,12 @@ void program::rpn_ifte(void) { ARG_MUST_BE_OF_TYPE(2, kNumber); // check ifte arg - if (_stack.value(2) != 0) { - _stack.erase(2); - _stack.pop(); + if (stack_.value(2) != 0) { + stack_.erase(2); + stack_.pop(); } else { - _stack.erase(2); - _stack.erase(1); + stack_.erase(2); + stack_.erase(1); } } @@ -190,8 +190,8 @@ size_t program::rpn_repeat(Branch& myobj) { // check arg // myobj.arg1 is end+1 - if (_stack.value(0) == 0) ret = myobj.arg1; - _stack.pop(); + if (stack_.value(0) == 0) ret = myobj.arg1; + stack_.pop(); return ret; } @@ -211,12 +211,12 @@ size_t program::rpn_start(Branch& myobj) { ARG_MUST_BE_OF_TYPE_RET(1, kNumber, kRtError); // loop boundaries - myobj.firstIndex = _stack.value(1); - myobj.lastIndex = _stack.value(0); - _stack.erase(0, 2); + myobj.first_index = stack_.value(1); + myobj.last_index = stack_.value(0); + stack_.erase(0, 2); // test value - if (myobj.firstIndex > myobj.lastIndex) + if (myobj.first_index > myobj.last_index) // last boundary lower than first boundary // -> next command shall be after 'next' // arg2 holds index of 'next' @@ -247,27 +247,27 @@ size_t program::rpn_for(Branch& myobj) { sym = reinterpret_cast(at(myobj.arg1)); // arg1 = loop variable index // loop boundaries - myobj.firstIndex = _stack.value(1); - myobj.lastIndex = _stack.value(0); + myobj.first_index = stack_.value(1); + myobj.last_index = stack_.value(0); // test value - if (myobj.firstIndex > myobj.lastIndex) { + if (myobj.first_index > myobj.last_index) { // last boundary lower than first boundary // -> next command shall be after 'next' // arg2 holds index of 'next' ret = myobj.arg2 + 1; } else { // store symbol with first value - auto it = _local_heap.find(sym->value); - if (it != _local_heap.end()) { + auto it = local_heap_.find(sym->value); + if (it != local_heap_.end()) { delete it->second; - _local_heap.erase(it); + local_heap_.erase(it); } - _local_heap[sym->value] = _stack.obj(1).clone(); + local_heap_[sym->value] = stack_.obj(1).clone(); ret = myobj.arg1 + 1; } - _stack.erase(0, 2); + stack_.erase(0, 2); return ret; } @@ -281,7 +281,7 @@ size_t program::rpn_for(Branch& myobj) { /// size_t program::rpn_next(Branch& myobj) { // arg1 = loop variable index - // firstIndex = current point in the loop + // first_index = current point in the loop Branch* start_or_for; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kBranch) { setErrorContext(kMissingOperand); @@ -290,12 +290,12 @@ size_t program::rpn_next(Branch& myobj) { start_or_for = reinterpret_cast(at(myobj.arg1)); if (!myobj.arg_bool) { myobj.arg_bool = true; - myobj.firstIndex = start_or_for->firstIndex; + myobj.first_index = start_or_for->first_index; } // increment then test // carefull: round toward minus infinity to avoid missing last boundary (because growing step) - mpfr_add(myobj.firstIndex.mpfr_ptr(), myobj.firstIndex.mpfr_srcptr(), mpreal(1).mpfr_srcptr(), MPFR_RNDD); + mpfr_add(myobj.first_index.mpfr_ptr(), myobj.first_index.mpfr_srcptr(), mpreal(1).mpfr_srcptr(), MPFR_RNDD); // for command: increment symbol too if (start_or_for->arg1 != -1) { @@ -308,11 +308,11 @@ size_t program::rpn_next(Branch& myobj) { var = reinterpret_cast(at(start_or_for->arg1)); // store symbol variable (asserted existing in the local heap) - reinterpret_cast(_local_heap[var->value])->value = myobj.firstIndex; + reinterpret_cast(local_heap_[var->value])->value = myobj.first_index; } // test value - if (myobj.firstIndex > start_or_for->lastIndex) { + if (myobj.first_index > start_or_for->last_index) { // end of loop myobj.arg_bool = false; // init again next time return kStepOut; @@ -337,15 +337,15 @@ size_t program::rpn_step(Branch& myobj) { MIN_ARGUMENTS_RET(1, kRtError); ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); - mpreal step = _stack.value(0); - _stack.pop(); + mpreal step = stack_.value(0); + stack_.pop(); // end of loop if step is negative or zero if (step <= 0) { ret = kStepOut; } else { // arg1 = loop variable index - // firstIndex = current count + // first_index = current count Branch* start_or_for; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kBranch) { setErrorContext(kMissingOperand); @@ -354,12 +354,12 @@ size_t program::rpn_step(Branch& myobj) { start_or_for = reinterpret_cast(at(myobj.arg1)); if (!myobj.arg_bool) { myobj.arg_bool = true; - myobj.firstIndex = start_or_for->firstIndex; + myobj.first_index = start_or_for->first_index; } // increment then test // carefull: round toward minus infinity to avoid missing last boundary (because growing step) - mpfr_add(myobj.firstIndex.mpfr_ptr(), myobj.firstIndex.mpfr_srcptr(), step.mpfr_srcptr(), MPFR_RNDD); + mpfr_add(myobj.first_index.mpfr_ptr(), myobj.first_index.mpfr_srcptr(), step.mpfr_srcptr(), MPFR_RNDD); if (start_or_for->arg1 != -1) { Object* obj; @@ -372,11 +372,11 @@ size_t program::rpn_step(Branch& myobj) { } var = reinterpret_cast(at(start_or_for->arg1)); // increase symbol variable - reinterpret_cast(_local_heap[var->value])->value = myobj.firstIndex; + reinterpret_cast(local_heap_[var->value])->value = myobj.first_index; } // test loop value is out of range - if (myobj.firstIndex > start_or_for->lastIndex) { + if (myobj.first_index > start_or_for->last_index) { // end of loop myobj.arg_bool = false; // init again next time ret = kStepOut; diff --git a/src/rpn-complex.cpp b/src/rpn-complex.cpp index b46716f..e3c24c2 100644 --- a/src/rpn-complex.cpp +++ b/src/rpn-complex.cpp @@ -8,8 +8,8 @@ void program::rpn_re() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - _stack.push_front(new Number(real(_stack.value(0)))); - _stack.erase(1); + stack_.push_front(new Number(real(stack_.value(0)))); + stack_.erase(1); } /// @brief im keyword implementation @@ -18,8 +18,8 @@ void program::rpn_re() { void program::rpn_im() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - _stack.push_front(new Number(imag(_stack.value(0)))); - _stack.erase(1); + stack_.push_front(new Number(imag(stack_.value(0)))); + stack_.erase(1); } /// @brief arg keyword implementation @@ -28,8 +28,8 @@ void program::rpn_im() { void program::rpn_arg() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - _stack.push_front(new Number(arg(_stack.value(0)))); - _stack.erase(1); + stack_.push_front(new Number(arg(stack_.value(0)))); + stack_.erase(1); } /// @brief conj keyword implementation @@ -38,7 +38,7 @@ void program::rpn_arg() { void program::rpn_conj() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - _stack.value(0) = conj(_stack.value(0)); + stack_.value(0) = conj(stack_.value(0)); } /// @brief r2c keyword implementation @@ -48,8 +48,8 @@ void program::rpn_r2c() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - _stack.push(new Complex(_stack.value(1), _stack.value(0), _stack.obj(1).reBase, _stack.obj(0).reBase)); - _stack.erase(1, 2); + stack_.push(new Complex(stack_.value(1), stack_.value(0), stack_.obj(1).re_base, stack_.obj(0).re_base)); + stack_.erase(1, 2); } /// @brief c2r keyword implementation @@ -58,9 +58,9 @@ void program::rpn_r2c() { void program::rpn_c2r() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - _stack.push(new Number(real(_stack.value(0)), _stack.obj(0).reBase)); - _stack.push(new Number(imag(_stack.value(1)), _stack.obj(1).imBase)); - _stack.erase(2); + stack_.push(new Number(real(stack_.value(0)), stack_.obj(0).re_base)); + stack_.push(new Number(imag(stack_.value(1)), stack_.obj(1).im_base)); + stack_.erase(2); } /// @brief r2p keyword implementation @@ -69,10 +69,10 @@ void program::rpn_c2r() { void program::rpn_r2p() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - mpreal rho = abs(_stack.value(0)); - mpreal theta = arg(_stack.value(0)); - _stack.value(0).real(rho); - _stack.value(0).imag(theta); + mpreal rho = abs(stack_.value(0)); + mpreal theta = arg(stack_.value(0)); + stack_.value(0).real(rho); + stack_.value(0).imag(theta); } /// @brief p2r keyword implementation @@ -81,5 +81,5 @@ void program::rpn_r2p() { void program::rpn_p2r() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); - _stack.value(0) = polar(abs(_stack.value(0)), arg(_stack.value(0))); + stack_.value(0) = polar(abs(stack_.value(0)), arg(stack_.value(0))); } diff --git a/src/rpn-general.cpp b/src/rpn-general.cpp index f7d51b5..23e44d2 100644 --- a/src/rpn-general.cpp +++ b/src/rpn-general.cpp @@ -57,7 +57,7 @@ void program::rpn_help() { // keywords unsigned int i = 0; - for (auto& kw : _keywords) + for (auto& kw : keywords_) if (!kw.comment.empty()) { // titles in bold if (kw.type == kUndef) cout << ATTR_BOLD; @@ -69,7 +69,7 @@ void program::rpn_help() { // show mode cout << "Current float mode is "; - switch (Number::s_mode) { + switch (Number::mode) { case Number::kStd: cout << "'std'"; break; @@ -85,7 +85,7 @@ void program::rpn_help() { } // bits precision, decimal digits and rounding mode - cout << " with " << Number::s_digits << " digits after the decimal point" << endl; + cout << " with " << Number::digits << " digits after the decimal point" << endl; cout << "Current floating point precision is " << static_cast(mpreal::get_default_prec()) << " bits" << endl; vector> rnd{MPFR_ROUND}; for (auto& rn : rnd) @@ -110,13 +110,13 @@ void program::rpn_std() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int digits = static_cast(_stack.value(0).toLong()); + int digits = static_cast(stack_.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - Number::s_mode = Number::kStd; - Number::s_digits = digits; - _stack.pop(); + Number::mode = Number::kStd; + Number::digits = digits; + stack_.pop(); } else { setErrorContext(kOutOfRange); } @@ -128,13 +128,13 @@ void program::rpn_fix() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int digits = static_cast(_stack.value(0).toLong()); + int digits = static_cast(stack_.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - Number::s_mode = Number::kFix; - Number::s_digits = digits; - _stack.pop(); + Number::mode = Number::kFix; + Number::digits = digits; + stack_.pop(); } else { setErrorContext(kOutOfRange); } @@ -146,13 +146,13 @@ void program::rpn_sci() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int digits = static_cast(_stack.value(0).toLong()); + int digits = static_cast(stack_.value(0).toLong()); if (check_decimal_digits(digits)) { // set mode, decimal digits and print format - Number::s_mode = Number::kSci; - Number::s_digits = digits; - _stack.pop(); + Number::mode = Number::kSci; + Number::digits = digits; + stack_.pop(); } else { setErrorContext(kOutOfRange); } @@ -160,11 +160,11 @@ void program::rpn_sci() { /// @brief _version keyword implementation /// -void program::rpn_version() { _stack.push_front(new String(RPN_VERSION)); } +void program::rpn_version() { stack_.push_front(new String(RPN_VERSION)); } /// @brief _uname keyword implementation /// -void program::rpn_uname() { _stack.push_front(new String(RPN_UNAME)); } +void program::rpn_uname() { stack_.push_front(new String(RPN_UNAME)); } /// @brief history keyword implementation /// @@ -183,8 +183,8 @@ void program::rpn_history() { /// void program::rpn_type() { MIN_ARGUMENTS(1); - _stack.push(new String(_stack.at(0)->name())); - _stack.erase(1); + stack_.push(new String(stack_.at(0)->name())); + stack_.erase(1); } /// @brief default keyword implementation @@ -198,16 +198,16 @@ void program::rpn_precision() { ARG_MUST_BE_OF_TYPE(0, kNumber); // set precision - int prec = static_cast(_stack.value(0).toLong()); + int prec = static_cast(stack_.value(0).toLong()); if (prec >= MPFR_PREC_MIN && prec <= MPFR_PREC_MAX) { mpreal::set_default_prec(prec); // modify digits seen by user if std mode - if (Number::s_mode == Number::kStd) { + if (Number::mode == Number::kStd) { // calc max nb of digits user can see with the current bit precision - Number::s_digits = mpfr::bits2digits(mpreal::get_default_prec()); + Number::digits = mpfr::bits2digits(mpreal::get_default_prec()); } - _stack.pop(); + stack_.pop(); } else { setErrorContext(kOutOfRange); } @@ -221,10 +221,10 @@ void program::rpn_round() { map matchRound{MPFR_ROUND}; - auto found = matchRound.find(_stack.value(0)); + auto found = matchRound.find(stack_.value(0)); if (found != matchRound.end()) mpreal::set_default_rnd(found->second); else setErrorContext(kOutOfRange); - _stack.pop(); + stack_.pop(); } diff --git a/src/rpn-logs.cpp b/src/rpn-logs.cpp index 2c5796a..224ec5b 100644 --- a/src/rpn-logs.cpp +++ b/src/rpn-logs.cpp @@ -4,16 +4,16 @@ /// @brief e keyword implementation /// -void program::rpn_e(void) { _stack.push(new Number(mpfr::const_euler())); } +void program::rpn_e(void) { stack_.push(new Number(mpfr::const_euler())); } /// @brief log10 keyword implementation /// void program::rpn_log10() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = log10(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = log10(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = log10(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = log10(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -22,10 +22,10 @@ void program::rpn_log10() { /// void program::rpn_alog10() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = exp(log(mpreal(10)) * _stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = exp(log(mpreal(10)) * stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = exp(log(mpreal(10)) * stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -34,10 +34,10 @@ void program::rpn_alog10() { /// void program::rpn_log2() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); - else if (_stack.type(0) == kComplex) - _stack.value(0) = log(_stack.value(0)) / mpfr::const_log2(); + if (stack_.type(0) == kNumber) + stack_.value(0) = log(stack_.value(0)) / mpfr::const_log2(); + else if (stack_.type(0) == kComplex) + stack_.value(0) = log(stack_.value(0)) / mpfr::const_log2(); else setErrorContext(kBadOperandType); } @@ -46,10 +46,10 @@ void program::rpn_log2() { /// void program::rpn_alog2() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = exp(mpfr::const_log2() * _stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = exp(mpfr::const_log2() * stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = exp(mpfr::const_log2() * stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -58,10 +58,10 @@ void program::rpn_alog2() { /// void program::rpn_ln() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = log(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = log(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = log(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = log(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -70,10 +70,10 @@ void program::rpn_ln() { /// void program::rpn_exp() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = exp(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = exp(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = exp(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = exp(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -82,10 +82,10 @@ void program::rpn_exp() { /// void program::rpn_expm() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = exp(_stack.value(0)) - mpreal(1); - else if (_stack.type(0) == kComplex) - _stack.value(0) = exp(_stack.value(0)) - mpreal(1); + if (stack_.type(0) == kNumber) + stack_.value(0) = exp(stack_.value(0)) - mpreal(1); + else if (stack_.type(0) == kComplex) + stack_.value(0) = exp(stack_.value(0)) - mpreal(1); else setErrorContext(kBadOperandType); } @@ -94,10 +94,10 @@ void program::rpn_expm() { /// void program::rpn_lnp1() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = log(_stack.value(0) + 1); - else if (_stack.type(0) == kComplex) - _stack.value(0) = log(_stack.value(0) + mpreal(1)); + if (stack_.type(0) == kNumber) + stack_.value(0) = log(stack_.value(0) + 1); + else if (stack_.type(0) == kComplex) + stack_.value(0) = log(stack_.value(0) + mpreal(1)); else setErrorContext(kBadOperandType); } @@ -106,10 +106,10 @@ void program::rpn_lnp1() { /// void program::rpn_sinh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = sinh(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = sinh(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = sinh(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = sinh(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -118,10 +118,10 @@ void program::rpn_sinh() { /// void program::rpn_asinh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = asinh(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = asinh(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = asinh(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = asinh(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -130,10 +130,10 @@ void program::rpn_asinh() { /// void program::rpn_cosh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = cosh(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = cosh(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = cosh(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = cosh(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -142,10 +142,10 @@ void program::rpn_cosh() { /// void program::rpn_acosh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = acosh(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = acosh(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = acosh(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = acosh(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -154,10 +154,10 @@ void program::rpn_acosh() { /// void program::rpn_tanh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = tanh(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = tanh(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = tanh(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = tanh(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -166,10 +166,10 @@ void program::rpn_tanh() { /// void program::rpn_atanh() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = atanh(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = atanh(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = atanh(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = atanh(stack_.value(0)); else setErrorContext(kBadOperandType); } diff --git a/src/rpn-program.cpp b/src/rpn-program.cpp index b7afa8e..93e1ab0 100644 --- a/src/rpn-program.cpp +++ b/src/rpn-program.cpp @@ -11,20 +11,20 @@ /// bool program::find_variable(string& variable, Object*& obj) { bool found = false; - program* parent = _parent; + program* parent = parent_; - if (_local_heap.get(variable, obj)) { + if (local_heap_.get(variable, obj)) { found = true; } else { while (parent != nullptr) { - if (parent->_local_heap.get(variable, obj)) { + if (parent->local_heap_.get(variable, obj)) { found = true; break; } - parent = parent->_parent; + parent = parent->parent_; } if (!found) { - if (_heap.get(variable, obj)) found = true; + if (heap_.get(variable, obj)) found = true; } } @@ -38,29 +38,29 @@ void program::rpn_eval(void) { string prog_text; MIN_ARGUMENTS(1); - if (_stack.type(0) == kSymbol) { + if (stack_.type(0) == kSymbol) { // recall a variable Object* obj; - string variable(_stack.value(0)); - _stack.pop(); + string variable(stack_.value(0)); + stack_.pop(); // if variable holds a program, run this program if (find_variable(variable, obj)) { if (obj->_type == kProgram) { - prog_text = _stack.value(0); - _stack.pop(); + prog_text = stack_.value(0); + stack_.pop(); run_prog = true; } else { - // else recall this variable (i.e. stack its content) - _stack.push_front(obj); + // else recall this variable (i.e. stack_ its content) + stack_.push_front(obj); } } else { setErrorContext(kUnknownVariable); } - } else if (_stack.type(0) == kProgram) { + } else if (stack_.type(0) == kProgram) { // eval a program - prog_text = _stack.value(0); - _stack.pop(); + prog_text = stack_.value(0); + stack_.pop(); run_prog = true; } else { setErrorContext(kBadOperandType); @@ -68,7 +68,7 @@ void program::rpn_eval(void) { // run prog if any if (run_prog) { - program prog(_stack, _heap, this); + program prog(stack_, heap_, this); // make program from entry if (prog.parse(prog_text) == kOk) { @@ -105,7 +105,7 @@ int program::rpn_inprog(Branch& inprog_obj) { } else { // found something other than symbol setErrorContext(kBadOperandType); - show_error(_err, context); + show_error(err_, context); return -1; } } @@ -113,33 +113,33 @@ int program::rpn_inprog(Branch& inprog_obj) { // found 0 symbols if (count_symbols == 0) { setErrorContext(kSyntaxError); - show_error(_err, context); + show_error(err_, context); return -1; } // is missing if (!prog_found) { setErrorContext(kSyntaxError); - show_error(_err, context); + show_error(err_, context); return -1; } - // check symbols Number vs stack size - if (_stack.size() < count_symbols) { + // check symbols Number vs stack_ size + if (stack_.size() < count_symbols) { setErrorContext(kMissingOperand); - show_error(_err, context); + show_error(err_, context); return -1; } // load variables for (unsigned int i = inprog_obj.arg1 + count_symbols; i > inprog_obj.arg1; i--) { - _local_heap[reinterpret_cast(at(i))->value] = _stack.at(0)->clone(); - _stack.pop(); + local_heap_[reinterpret_cast(at(i))->value] = stack_.at(0)->clone(); + stack_.pop(); } // run the program string& entry = reinterpret_cast(at(inprog_obj.arg1 + count_symbols + 1))->value; - program prog(_stack, _heap, this); + program prog(stack_, heap_, this); // make the program from entry if (prog.parse(entry) == kOk) { diff --git a/src/rpn-real.cpp b/src/rpn-real.cpp index 5f59bd3..17b0eef 100644 --- a/src/rpn-real.cpp +++ b/src/rpn-real.cpp @@ -6,22 +6,22 @@ /// void program::rpn_plus() { MIN_ARGUMENTS(2); - if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.value(1) += _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.value(1) += _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.value(1) += _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { - _stack.value(1) += _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { + if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.value(1) += stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.value(1) += stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.value(1) += stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kNumber && stack_.type(1) == kComplex) { + stack_.value(1) += stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { rpn_swap(); - _stack.value(1) += _stack.value(0); - _stack.pop(); + stack_.value(1) += stack_.value(0); + stack_.pop(); } else { setErrorContext(kBadOperandType); } @@ -31,19 +31,19 @@ void program::rpn_plus() { /// void program::rpn_minus() { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.value(1) -= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.value(1) -= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { - _stack.value(1) -= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.value(1) -= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.value(1) -= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kNumber && stack_.type(1) == kComplex) { + stack_.value(1) -= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { rpn_swap(); - _stack.value(1) = _stack.value(0) - _stack.value(1); - _stack.pop(); + stack_.value(1) = stack_.value(0) - stack_.value(1); + stack_.pop(); } else { setErrorContext(kBadOperandType); } @@ -53,19 +53,19 @@ void program::rpn_minus() { /// void program::rpn_mul() { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.value(1) *= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.value(1) *= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { - _stack.value(1) *= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.value(1) *= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.value(1) *= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kNumber && stack_.type(1) == kComplex) { + stack_.value(1) *= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { rpn_swap(); - _stack.value(1) *= _stack.value(0); - _stack.pop(); + stack_.value(1) *= stack_.value(0); + stack_.pop(); } else { setErrorContext(kBadOperandType); } @@ -75,19 +75,19 @@ void program::rpn_mul() { /// void program::rpn_div() { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.value(1) /= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.value(1) /= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { - _stack.value(1) /= _stack.value(0); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.value(1) /= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.value(1) /= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kNumber && stack_.type(1) == kComplex) { + stack_.value(1) /= stack_.value(0); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { rpn_swap(); - _stack.value(1) = _stack.value(0) / _stack.value(1); - _stack.pop(); + stack_.value(1) = stack_.value(0) / stack_.value(1); + stack_.pop(); } else { setErrorContext(kBadOperandType); } @@ -97,10 +97,10 @@ void program::rpn_div() { /// void program::rpn_neg() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = -_stack.value(0); - else if (_stack.type(0) == kComplex) - _stack.value(0) = -_stack.value(0); + if (stack_.type(0) == kNumber) + stack_.value(0) = -stack_.value(0); + else if (stack_.type(0) == kComplex) + stack_.value(0) = -stack_.value(0); else setErrorContext(kBadOperandType); } @@ -109,10 +109,10 @@ void program::rpn_neg() { /// void program::rpn_inv() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = 1 / _stack.value(0); - else if (_stack.type(0) == kComplex) - _stack.value(0) = mpreal(1) / _stack.value(0); + if (stack_.type(0) == kNumber) + stack_.value(0) = 1 / stack_.value(0); + else if (stack_.type(0) == kComplex) + stack_.value(0) = mpreal(1) / stack_.value(0); else setErrorContext(kBadOperandType); } @@ -121,26 +121,26 @@ void program::rpn_inv() { /// void program::rpn_power() { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - if (_stack.value(1) >= 0) { - _stack.value(1) = pow(_stack.value(1), _stack.value(0)); - _stack.pop(); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + if (stack_.value(1) >= 0) { + stack_.value(1) = pow(stack_.value(1), stack_.value(0)); + stack_.pop(); } else { mpreal zero; - _stack.push(new Complex(_stack.value(1), zero, _stack.obj(1).base)); - _stack.value(0) = pow(_stack.value(0), _stack.value(1)); - _stack.erase(1, 2); + stack_.push(new Complex(stack_.value(1), zero, stack_.obj(1).base)); + stack_.value(0) = pow(stack_.value(0), stack_.value(1)); + stack_.erase(1, 2); } - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.value(1) = pow(_stack.value(1), _stack.value(0)); - _stack.pop(); - } else if (_stack.type(0) == kNumber && _stack.type(1) == kComplex) { - _stack.value(1) = pow(_stack.value(1), _stack.value(0)); - _stack.pop(); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kNumber) { + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.value(1) = pow(stack_.value(1), stack_.value(0)); + stack_.pop(); + } else if (stack_.type(0) == kNumber && stack_.type(1) == kComplex) { + stack_.value(1) = pow(stack_.value(1), stack_.value(0)); + stack_.pop(); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { rpn_swap(); - _stack.value(1) = pow(_stack.value(0), _stack.value(1)); - _stack.pop(); + stack_.value(1) = pow(stack_.value(0), stack_.value(1)); + stack_.pop(); } else { setErrorContext(kBadOperandType); } @@ -150,19 +150,19 @@ void program::rpn_power() { /// void program::rpn_squareroot() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) { - if (_stack.value(0) >= 0) { - _stack.value(0) = sqrt(_stack.value(0)); + if (stack_.type(0) == kNumber) { + if (stack_.value(0) >= 0) { + stack_.value(0) = sqrt(stack_.value(0)); } else { // negative number -> square root is complex mpreal zero; - _stack.push(new Complex(_stack.value(0), zero, - _stack.obj(0).base)); // TODO(louis) manage new errors - _stack.value(0) = sqrt(_stack.value(0)); - _stack.erase(1); + stack_.push(new Complex(stack_.value(0), zero, + stack_.obj(0).base)); // TODO(louis) manage new errors + stack_.value(0) = sqrt(stack_.value(0)); + stack_.erase(1); } - } else if (_stack.type(0) == kComplex) { - _stack.value(0) = sqrt(_stack.value(0)); + } else if (stack_.type(0) == kComplex) { + stack_.value(0) = sqrt(stack_.value(0)); } else { setErrorContext(kBadOperandType); } @@ -172,11 +172,11 @@ void program::rpn_squareroot() { /// void program::rpn_hex() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) { - _stack.obj(0).base = 16; - } else if (_stack.type(0) == kComplex) { - _stack.obj(0).reBase = 16; - _stack.obj(0).imBase = 16; + if (stack_.type(0) == kNumber) { + stack_.obj(0).base = 16; + } else if (stack_.type(0) == kComplex) { + stack_.obj(0).re_base = 16; + stack_.obj(0).im_base = 16; } else { setErrorContext(kBadOperandType); } @@ -186,11 +186,11 @@ void program::rpn_hex() { /// void program::rpn_bin() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) { - _stack.obj(0).base = 2; - } else if (_stack.type(0) == kComplex) { - _stack.obj(0).reBase = 2; - _stack.obj(0).imBase = 2; + if (stack_.type(0) == kNumber) { + stack_.obj(0).base = 2; + } else if (stack_.type(0) == kComplex) { + stack_.obj(0).re_base = 2; + stack_.obj(0).im_base = 2; } else { setErrorContext(kBadOperandType); } @@ -200,11 +200,11 @@ void program::rpn_bin() { /// void program::rpn_dec() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) { - _stack.obj(0).base = 10; - } else if (_stack.type(0) == kComplex) { - _stack.obj(0).reBase = 10; - _stack.obj(0).imBase = 10; + if (stack_.type(0) == kNumber) { + stack_.obj(0).base = 10; + } else if (stack_.type(0) == kComplex) { + stack_.obj(0).re_base = 10; + stack_.obj(0).im_base = 10; } else { setErrorContext(kBadOperandType); } @@ -214,15 +214,15 @@ void program::rpn_dec() { /// void program::rpn_base() { MIN_ARGUMENTS(2); - if (_stack.type(1) == kNumber || _stack.type(1) == kComplex) { - int base = static_cast(_stack.value(0).toLong()); - _stack.pop(); + if (stack_.type(1) == kNumber || stack_.type(1) == kComplex) { + int base = static_cast(stack_.value(0).toLong()); + stack_.pop(); if (base >= 2 && base <= 62) { - if (_stack.type(0) == kNumber) { - _stack.obj(0).base = base; + if (stack_.type(0) == kNumber) { + stack_.obj(0).base = base; } else { - _stack.obj(0).reBase = base; - _stack.obj(0).imBase = base; + stack_.obj(0).re_base = base; + stack_.obj(0).im_base = base; } } else { setErrorContext(kOutOfRange); @@ -238,8 +238,8 @@ void program::rpn_purcent() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - _stack.value(1) *= _stack.value(0) / 100; - _stack.pop(); + stack_.value(1) *= stack_.value(0) / 100; + stack_.pop(); } /// @brief %CH keyword implementation @@ -248,18 +248,18 @@ void program::rpn_purcentCH() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - _stack.value(1) = (_stack.value(0) * 100) / _stack.value(1); - _stack.pop(); + stack_.value(1) = (stack_.value(0) * 100) / stack_.value(1); + stack_.pop(); } /// @brief sq keyword implementation /// void program::rpn_square() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) *= _stack.value(0); - else if (_stack.at(0)->_type == kComplex) - _stack.value(0) *= _stack.value(0); + if (stack_.type(0) == kNumber) + stack_.value(0) *= stack_.value(0); + else if (stack_.at(0)->_type == kComplex) + stack_.value(0) *= stack_.value(0); else setErrorContext(kBadOperandType); } @@ -270,19 +270,19 @@ void program::rpn_modulo() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - _stack.value(1) = fmod(_stack.value(1), _stack.value(0)); - _stack.pop(); + stack_.value(1) = fmod(stack_.value(1), stack_.value(0)); + stack_.pop(); } /// @brief abs keyword implementation /// void program::rpn_abs() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) { - _stack.value(0) = abs(_stack.value(0)); - } else if (_stack.type(0) == kComplex) { - _stack.push(new Number(abs(_stack.value(0)))); - _stack.erase(1); + if (stack_.type(0) == kNumber) { + stack_.value(0) = abs(stack_.value(0)); + } else if (stack_.type(0) == kComplex) { + stack_.push(new Number(abs(stack_.value(0)))); + stack_.erase(1); } else { setErrorContext(kBadOperandType); } @@ -294,17 +294,17 @@ void program::rpn_fact() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); // fact(n) = gamma(n+1) - _stack.value(0) = gamma(_stack.value(0) + 1); + stack_.value(0) = gamma(stack_.value(0) + 1); } /// @brief sign keyword implementation /// void program::rpn_sign() { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = sgn(_stack.value(0)); - else if (_stack.at(0)->_type == kComplex) - _stack.value(0) = _stack.value(0) / abs(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = sgn(stack_.value(0)); + else if (stack_.at(0)->_type == kComplex) + stack_.value(0) = stack_.value(0) / abs(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -314,12 +314,12 @@ void program::rpn_sign() { void program::rpn_mant() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - if (!isfinite(_stack.value(0))) { + if (!isfinite(stack_.value(0))) { setErrorContext(kOutOfRange); return; } mp_exp_t exp; - _stack.value(0) = frexp(_stack.value(0), &exp); + stack_.value(0) = frexp(stack_.value(0), &exp); } /// @brief xpon keyword implementation @@ -327,13 +327,13 @@ void program::rpn_mant() { void program::rpn_xpon() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - if (!isfinite(_stack.value(0))) { + if (!isfinite(stack_.value(0))) { setErrorContext(kOutOfRange); return; } mp_exp_t exp; - (void)frexp(_stack.value(0), &exp); - _stack.value(0) = exp; + (void)frexp(stack_.value(0), &exp); + stack_.value(0) = exp; } /// @brief floor keyword implementation @@ -341,7 +341,7 @@ void program::rpn_xpon() { void program::rpn_floor() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.value(0) = floor(_stack.value(0)); + stack_.value(0) = floor(stack_.value(0)); } /// @brief ceil keyword implementation @@ -349,7 +349,7 @@ void program::rpn_floor() { void program::rpn_ceil() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.value(0) = ceil(_stack.value(0)); + stack_.value(0) = ceil(stack_.value(0)); } /// @brief fp keyword implementation @@ -357,7 +357,7 @@ void program::rpn_ceil() { void program::rpn_fp() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.value(0) = frac(_stack.value(0)); + stack_.value(0) = frac(stack_.value(0)); } /// @brief ip keyword implementation @@ -365,7 +365,7 @@ void program::rpn_fp() { void program::rpn_ip() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.value(0) = trunc(_stack.value(0)); + stack_.value(0) = trunc(stack_.value(0)); } /// @brief min keyword implementation @@ -374,8 +374,8 @@ void program::rpn_min() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - _stack.value(0) = min(_stack.value(0), _stack.value(1)); - _stack.erase(1); + stack_.value(0) = min(stack_.value(0), stack_.value(1)); + stack_.erase(1); } /// @brief max keyword implementation @@ -384,6 +384,6 @@ void program::rpn_max() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - _stack.value(0) = max(_stack.value(0), _stack.value(1)); - _stack.erase(1); + stack_.value(0) = max(stack_.value(0), stack_.value(1)); + stack_.erase(1); } diff --git a/src/rpn-stack.cpp b/src/rpn-stack.cpp index 63cedbf..66e2247 100644 --- a/src/rpn-stack.cpp +++ b/src/rpn-stack.cpp @@ -6,23 +6,23 @@ /// void program::rpn_swap(void) { MIN_ARGUMENTS(2); - Object* tmp = _stack.front(); - _stack.erase(0, 1, false); - _stack.insert(_stack.begin() + 1, tmp); + Object* tmp = stack_.front(); + stack_.erase(0, 1, false); + stack_.insert(stack_.begin() + 1, tmp); } /// @brief drop keyword implementation /// void program::rpn_drop(void) { MIN_ARGUMENTS(1); - _stack.pop(); + stack_.pop(); } /// @brief drop2 keyword implementation /// void program::rpn_drop2(void) { MIN_ARGUMENTS(2); - _stack.erase(0, 2); + stack_.erase(0, 2); } /// @brief dropn keyword implementation @@ -31,20 +31,20 @@ void program::rpn_dropn(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int args = static_cast(_stack.value(0).toLong()); + int args = static_cast(stack_.value(0).toLong()); MIN_ARGUMENTS(args + 1); - _stack.erase(0, args + 1); + stack_.erase(0, args + 1); } /// @brief erase / del keyword implementation /// -void program::rpn_erase(void) { _stack.erase(0, _stack.size()); } +void program::rpn_erase(void) { stack_.erase(0, stack_.size()); } /// @brief dup keyword implementation /// void program::rpn_dup(void) { MIN_ARGUMENTS(1); - _stack.push_front(_stack.at(0)->clone()); + stack_.push_front(stack_.at(0)->clone()); } /// @brief dupn keyword implementation @@ -53,19 +53,19 @@ void program::rpn_dupn(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int args = static_cast(_stack.value(0).toLong()); - _stack.pop(); + int args = static_cast(stack_.value(0).toLong()); + stack_.pop(); MIN_ARGUMENTS(args); - for (int i = 0; i < args; i++) _stack.push_front(_stack.at(args - 1)->clone()); + for (int i = 0; i < args; i++) stack_.push_front(stack_.at(args - 1)->clone()); } /// @brief dup2 keyword implementation /// void program::rpn_dup2(void) { MIN_ARGUMENTS(2); - _stack.push_front(_stack.at(1)->clone()); - _stack.push_front(_stack.at(1)->clone()); + stack_.push_front(stack_.at(1)->clone()); + stack_.push_front(stack_.at(1)->clone()); } /// @brief pick keyword implementation @@ -74,30 +74,30 @@ void program::rpn_pick(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int to_pick = static_cast(_stack.value(0).toLong()); - _stack.pop(); + int to_pick = static_cast(stack_.value(0).toLong()); + stack_.pop(); - // treat stack depth errors - if ((to_pick == 0) || (to_pick > _stack.size())) { + // treat stack_ depth errors + if ((to_pick == 0) || (to_pick > stack_.size())) { setErrorContext(kOutOfRange); return; } - _stack.push_front(_stack.at(to_pick - 1)->clone()); + stack_.push_front(stack_.at(to_pick - 1)->clone()); } /// @brief rot keyword implementation /// void program::rpn_rot(void) { MIN_ARGUMENTS(3); - Object* tmp = _stack.at(2); - _stack.erase(2, 1, false); - _stack.insert(_stack.begin(), tmp); + Object* tmp = stack_.at(2); + stack_.erase(2, 1, false); + stack_.insert(stack_.begin(), tmp); } /// @brief depth keyword implementation /// -void program::rpn_depth(void) { _stack.push_front(new Number(_stack.size())); } +void program::rpn_depth(void) { stack_.push_front(new Number(stack_.size())); } /// @brief roll keyword implementation /// @@ -105,13 +105,13 @@ void program::rpn_roll(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - int args = static_cast(_stack.value(0).toLong()); - _stack.pop(); + int args = static_cast(stack_.value(0).toLong()); + stack_.pop(); MIN_ARGUMENTS(args); - Object* tmp = _stack.at(args - 1); - _stack.erase(args - 1, 1, false); - _stack.insert(_stack.begin(), tmp); + Object* tmp = stack_.at(args - 1); + stack_.erase(args - 1, 1, false); + stack_.insert(stack_.begin(), tmp); } /// @brief rolld keyword implementation @@ -120,18 +120,18 @@ void program::rpn_rolld(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); - int args = static_cast(_stack.value(0).toLong()); - _stack.pop(); + int args = static_cast(stack_.value(0).toLong()); + stack_.pop(); MIN_ARGUMENTS(args); - Object* tmp = _stack.at(0); - _stack.erase(0, 1, false); - _stack.insert(_stack.begin() + args - 1, tmp); + Object* tmp = stack_.at(0); + stack_.erase(0, 1, false); + stack_.insert(stack_.begin() + args - 1, tmp); } /// @brief over keyword implementation /// void program::rpn_over(void) { MIN_ARGUMENTS(2); - _stack.push_front(_stack.at(1)->clone()); + stack_.push_front(stack_.at(1)->clone()); } diff --git a/src/rpn-store.cpp b/src/rpn-store.cpp index 6d3e6d7..c9ea63d 100644 --- a/src/rpn-store.cpp +++ b/src/rpn-store.cpp @@ -10,13 +10,13 @@ void program::rpn_sto(void) { ARG_MUST_BE_OF_TYPE(0, kSymbol); // store symbol with first value - const auto it = _heap.find(_stack.value(0)); - if (it != _heap.end()) { + const auto it = heap_.find(stack_.value(0)); + if (it != heap_.end()) { delete it->second; - _heap.erase(it); + heap_.erase(it); } - _heap[_stack.value(0)] = _stack.at(1)->clone(); - _stack.erase(0, 2); + heap_[stack_.value(0)] = stack_.at(1)->clone(); + stack_.erase(0, 2); } /// @brief sto+ keyword implementation @@ -24,7 +24,7 @@ void program::rpn_sto(void) { void program::rpn_stoadd(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (heap_.find(stack_.value(0)) == heap_.end()) { setErrorContext(kUnknownVariable); return; } @@ -41,7 +41,7 @@ void program::rpn_stoadd(void) { void program::rpn_stosub(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (heap_.find(stack_.value(0)) == heap_.end()) { setErrorContext(kUnknownVariable); return; } @@ -58,7 +58,7 @@ void program::rpn_stosub(void) { void program::rpn_stomul(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (heap_.find(stack_.value(0)) == heap_.end()) { setErrorContext(kUnknownVariable); return; } @@ -75,7 +75,7 @@ void program::rpn_stomul(void) { void program::rpn_stodiv(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (heap_.find(stack_.value(0)) == heap_.end()) { setErrorContext(kUnknownVariable); return; } @@ -92,7 +92,7 @@ void program::rpn_stodiv(void) { void program::rpn_stoneg(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (heap_.find(stack_.value(0)) == heap_.end()) { setErrorContext(kUnknownVariable); return; } @@ -108,7 +108,7 @@ void program::rpn_stoneg(void) { void program::rpn_stoinv(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); - if (_heap.find(_stack.value(0)) == _heap.end()) { + if (heap_.find(stack_.value(0)) == heap_.end()) { setErrorContext(kUnknownVariable); return; } @@ -127,12 +127,12 @@ void program::rpn_rcl(void) { // recall a variable Object* obj; - string variable(_stack.value(0)); + string variable(stack_.value(0)); // mind the order of heaps if (find_variable(variable, obj)) { - (void)_stack.pop(); - _stack.push_front(obj->clone()); + (void)stack_.pop(); + stack_.push_front(obj->clone()); } else { setErrorContext(kUnknownVariable); } @@ -145,9 +145,9 @@ void program::rpn_edit(void) { ostringstream st; - // re-write stack objet in a stream - _stack.at(0)->show(st); - _stack.pop(); + // re-write stack_ objet in a stream + stack_.at(0)->show(st); + stack_.pop(); // set it as the linenoise line entry Input::preload(st.str().c_str()); @@ -158,19 +158,19 @@ void program::rpn_edit(void) { /// @param symb the smlbol to recall and autoeval /// void program::auto_rcl(Symbol* symb) { - if (symb->autoEval) { + if (symb->auto_eval) { Object* obj; string variable(symb->value); // mind the order of heaps if (find_variable(variable, obj)) { - _stack.push_front(obj->clone()); + stack_.push_front(obj->clone()); if (obj->_type == kProgram) rpn_eval(); } else { - _stack.push_front(symb->clone()); + stack_.push_front(symb->clone()); } } else { - _stack.push_front(symb->clone()); + stack_.push_front(symb->clone()); } } @@ -180,46 +180,46 @@ void program::rpn_purge(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); - const auto i = _heap.find(_stack.value(0)); - if (i != _heap.end()) { + const auto i = heap_.find(stack_.value(0)); + if (i != heap_.end()) { delete i->second; - _heap.erase(i); + heap_.erase(i); } else { setErrorContext(kUnknownVariable); } - _stack.pop(); + stack_.pop(); } /// @brief vars keyword implementation /// void program::rpn_vars(void) { Object* obj; - program* parent = _parent; + program* parent = parent_; string name; int index = 1; // heap variables - for (auto i : _heap) { + for (auto i : heap_) { cout << "var " << index++ << ": name '" << i.first << "', type " << i.second->name() << ", value "; i.second->show(cout) << endl; } // local variables - for (auto i : _local_heap) { + for (auto i : local_heap_) { cout << "var " << index++ << ": name '" << i.first << "', type " << i.second->name() << ", value "; i.second->show(cout) << endl; } // parents local variables while (parent != nullptr) { - for (auto i : parent->_local_heap) { + for (auto i : parent->local_heap_) { cout << "parent var " << index++ << ": name '" << i.first << "', type " << i.second->name() << ", value "; obj->show(cout) << endl; } - parent = parent->_parent; + parent = parent->parent_; } } /// @brief clusr keyword implementation /// -void program::rpn_clusr(void) { _heap.clear(); } +void program::rpn_clusr(void) { heap_.clear(); } diff --git a/src/rpn-string.cpp b/src/rpn-string.cpp index 6412c8a..4908265 100644 --- a/src/rpn-string.cpp +++ b/src/rpn-string.cpp @@ -10,11 +10,11 @@ void program::rpn_instr() { MIN_ARGUMENTS(1); // stringify only if not already a string - if (_stack.type(0) != kString) { + if (stack_.type(0) != kString) { stringstream ss; - ss << _stack.at(0); - _stack.pop(); - _stack.push(new String(ss.str())); + ss << stack_.at(0); + stack_.pop(); + stack_.push(new String(ss.str())); } } @@ -24,11 +24,11 @@ void program::rpn_strout() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); - string entry(_stack.value(0)); - program prog(_stack, _heap); - _stack.pop(); + string entry(stack_.value(0)); + program prog(stack_, heap_); + stack_.pop(); - // make program from string in stack level 1 + // make program from string in stack_ level 1 if (prog.parse(entry) == kOk) // run it prog.run(); @@ -39,10 +39,10 @@ void program::rpn_strout() { void program::rpn_chr() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - char the_chr = static_cast(_stack.value(0).toLong()); - _stack.pop(); + char the_chr = static_cast(stack_.value(0).toLong()); + stack_.pop(); if (the_chr < 32 || the_chr > 126) the_chr = '.'; - _stack.push_front(new String(string(1, the_chr))); + stack_.push_front(new String(string(1, the_chr))); } /// @brief num keyword implementation @@ -50,11 +50,11 @@ void program::rpn_chr() { void program::rpn_num() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); - if (_stack.value(0).size() > 0) - _stack.push_front(new Number(_stack.value(0)[0])); + if (stack_.value(0).size() > 0) + stack_.push_front(new Number(stack_.value(0)[0])); else - _stack.push_front(new Number(0)); - _stack.erase(1); + stack_.push_front(new Number(0)); + stack_.erase(1); } /// @brief size keyword implementation @@ -62,8 +62,8 @@ void program::rpn_num() { void program::rpn_strsize() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); - _stack.push_front(new Number(_stack.value(0).size())); - _stack.erase(1); + stack_.push_front(new Number(stack_.value(0).size())); + stack_.erase(1); } /// @brief pos keyword implementation @@ -73,9 +73,9 @@ void program::rpn_strpos() { ARG_MUST_BE_OF_TYPE(0, kString); ARG_MUST_BE_OF_TYPE(1, kString); - size_t pos = _stack.value(1).find(_stack.value(0)) + 1; - _stack.erase(0, 2); - _stack.push_front(new Number(pos)); + size_t pos = stack_.value(1).find(stack_.value(0)) + 1; + stack_.erase(0, 2); + stack_.push_front(new Number(pos)); } /// @brief sub keyword implementation @@ -86,11 +86,11 @@ void program::rpn_strsub() { ARG_MUST_BE_OF_TYPE(1, kNumber); ARG_MUST_BE_OF_TYPE(2, kString); - size_t first = _stack.value(1).toULong(); - size_t len = _stack.value(0).toULong() - first + 1; + size_t first = stack_.value(1).toULong(); + size_t len = stack_.value(0).toULong() - first + 1; first--; - if (first > _stack.value(2).size()) first = len = 0; - _stack.push(new String(_stack.value(2).substr(first, len))); - _stack.erase(1, 3); + if (first > stack_.value(2).size()) first = len = 0; + stack_.push(new String(stack_.value(2).substr(first, len))); + stack_.erase(1, 3); } diff --git a/src/rpn-test-framework.cpp b/src/rpn-test-framework.cpp index eaad02c..d2a91bb 100644 --- a/src/rpn-test-framework.cpp +++ b/src/rpn-test-framework.cpp @@ -76,16 +76,16 @@ void program::rpn_test() { int total_steps = 0; int total_steps_failed = 0; - string test_filename = _stack.value(0); - _stack.pop(); + string test_filename = stack_.value(0); + stack_.pop(); cout << endl << "rpn version is " << RPN_VERSION << endl; test(test_filename, total_tests, total_tests_failed, total_steps, total_steps_failed); testShowResult("\nTotal", total_tests, total_tests_failed, total_steps, total_steps_failed); // notify to caller that test succeeded or not if (total_tests_failed > 0) { - _err = kTestFailed; - _err_context = string("rpn version ") + RPN_VERSION + ", test file " + test_filename; + err_ = kTestFailed; + err_context_ = string("rpn version ") + RPN_VERSION + ", test file " + test_filename; } } diff --git a/src/rpn-test.cpp b/src/rpn-test.cpp index 06dac2e..57df584 100644 --- a/src/rpn-test.cpp +++ b/src/rpn-test.cpp @@ -2,13 +2,13 @@ #include "program.hpp" -/// @brief compared 2 strings on top of the stack +/// @brief compared 2 strings on top of the stack_ /// /// @return 0 strings are equal /// @return !0 strings are not equal (see strcmp output) /// static int CmpStringOnStackTop(rpnstack& stk) { - // _stack should have 2 strings at level 1 and 2 + // stack_ should have 2 strings at level 1 and 2 // this function removes these 2 entries int res = stk.value(1).compare(stk.value(0)); stk.erase(0, 2); @@ -19,12 +19,12 @@ static int CmpStringOnStackTop(rpnstack& stk) { /// void program::rpn_sup(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.push_front(new Number(_stack.value(1) > _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.push_front(new Number(CmpStringOnStackTop(_stack) == 1)); - _stack.erase(1, 2); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.push_front(new Number(stack_.value(1) > stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.push_front(new Number(CmpStringOnStackTop(stack_) == 1)); + stack_.erase(1, 2); } else { setErrorContext(kBadOperandType); } @@ -34,12 +34,12 @@ void program::rpn_sup(void) { /// void program::rpn_sup_eq(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.push_front(new Number(_stack.value(1) >= _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.push_front(new Number(CmpStringOnStackTop(_stack) != -1)); - _stack.erase(1, 2); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.push_front(new Number(stack_.value(1) >= stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.push_front(new Number(CmpStringOnStackTop(stack_) != -1)); + stack_.erase(1, 2); } else { setErrorContext(kBadOperandType); } @@ -50,12 +50,12 @@ void program::rpn_sup_eq(void) { void program::rpn_inf(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.push_front(new Number(_stack.value(1) < _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.push_front(new Number(CmpStringOnStackTop(_stack) == -1)); - _stack.erase(1, 2); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.push_front(new Number(stack_.value(1) < stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.push_front(new Number(CmpStringOnStackTop(stack_) == -1)); + stack_.erase(1, 2); } else { setErrorContext(kBadOperandType); } @@ -65,12 +65,12 @@ void program::rpn_inf(void) { /// void program::rpn_inf_eq(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.push_front(new Number(_stack.value(1) <= _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.push_front(new Number(CmpStringOnStackTop(_stack) != 1)); - _stack.erase(1, 2); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.push_front(new Number(stack_.value(1) <= stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.push_front(new Number(CmpStringOnStackTop(stack_) != 1)); + stack_.erase(1, 2); } else { setErrorContext(kBadOperandType); } @@ -80,15 +80,15 @@ void program::rpn_inf_eq(void) { /// void program::rpn_diff(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.push_front(new Number(_stack.value(1) != _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.push_front(new Number(_stack.value(1) != _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.push_front(new Number(CmpStringOnStackTop(_stack) != 0)); - _stack.erase(1, 2); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.push_front(new Number(stack_.value(1) != stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.push_front(new Number(stack_.value(1) != stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.push_front(new Number(CmpStringOnStackTop(stack_) != 0)); + stack_.erase(1, 2); } else { setErrorContext(kBadOperandType); } @@ -98,15 +98,15 @@ void program::rpn_diff(void) { /// void program::rpn_eq(void) { MIN_ARGUMENTS(2); - if (_stack.type(0) == kNumber && _stack.type(1) == kNumber) { - _stack.push_front(new Number(_stack.value(1) == _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kComplex && _stack.type(1) == kComplex) { - _stack.push_front(new Number(_stack.value(1) == _stack.value(0))); - _stack.erase(1, 2); - } else if (_stack.type(0) == kString && _stack.type(1) == kString) { - _stack.push_front(new Number(CmpStringOnStackTop(_stack) == 0)); - _stack.erase(1, 2); + if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { + stack_.push_front(new Number(stack_.value(1) == stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kComplex && stack_.type(1) == kComplex) { + stack_.push_front(new Number(stack_.value(1) == stack_.value(0))); + stack_.erase(1, 2); + } else if (stack_.type(0) == kString && stack_.type(1) == kString) { + stack_.push_front(new Number(CmpStringOnStackTop(stack_) == 0)); + stack_.erase(1, 2); } else { setErrorContext(kBadOperandType); } @@ -118,11 +118,11 @@ void program::rpn_test_and(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - if (_stack.value(0) != 0 && _stack.value(1) != 0) - _stack.push(new Number(1)); + if (stack_.value(0) != 0 && stack_.value(1) != 0) + stack_.push(new Number(1)); else - _stack.push(new Number(0)); - _stack.erase(1, 2); + stack_.push(new Number(0)); + stack_.erase(1, 2); } /// @brief or keyword implementation @@ -131,11 +131,11 @@ void program::rpn_test_or(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - if (_stack.value(0) != 0 || _stack.value(1) != 0) - _stack.push(new Number(1)); + if (stack_.value(0) != 0 || stack_.value(1) != 0) + stack_.push(new Number(1)); else - _stack.push(new Number(0)); - _stack.erase(1, 2); + stack_.push(new Number(0)); + stack_.erase(1, 2); } /// @brief xor keyword implementation @@ -144,11 +144,11 @@ void program::rpn_test_xor(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); - if (_stack.value(0) != 0 ^ _stack.value(1) != 0) - _stack.push(new Number(1)); + if (stack_.value(0) != 0 ^ stack_.value(1) != 0) + stack_.push(new Number(1)); else - _stack.push(new Number(0)); - _stack.erase(1, 2); + stack_.push(new Number(0)); + stack_.erase(1, 2); } /// @brief not keyword implementation @@ -157,8 +157,8 @@ void program::rpn_test_not(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.push(new Number(_stack.value(0) == 0 ? 1 : 0)); - _stack.erase(1, 1); + stack_.push(new Number(stack_.value(0) == 0 ? 1 : 0)); + stack_.erase(1, 1); } /// @brief test same implementation diff --git a/src/rpn-time.cpp b/src/rpn-time.cpp index d97956a..3a8cb1f 100644 --- a/src/rpn-time.cpp +++ b/src/rpn-time.cpp @@ -22,8 +22,8 @@ void program::rpn_time() { // push it // division after push for real precision - _stack.push(new Number(date)); - _stack.value(0) /= 10000000000.0; + stack_.push(new Number(date)); + stack_.value(0) /= 10000000000.0; } else { setErrorContext(kInternalError); } @@ -48,8 +48,8 @@ void program::rpn_date() { // push it Number* num; // division after push for real precision - _stack.push(new Number(date)); - _stack.value(0) /= 1000000.0; + stack_.push(new Number(date)); + stack_.value(0) /= 1000000.0; } else { setErrorContext(kInternalError); } @@ -69,7 +69,7 @@ void program::rpn_ticks() { if (tm != nullptr) { // date in µs date = 1000000.0 * static_cast(ts.tv_sec) + static_cast(ts.tv_nsec / 1000); - _stack.push(new Number(date)); + stack_.push(new Number(date)); } else { setErrorContext(kInternalError); } diff --git a/src/rpn-trig.cpp b/src/rpn-trig.cpp index 8af66a1..61c7da8 100644 --- a/src/rpn-trig.cpp +++ b/src/rpn-trig.cpp @@ -5,7 +5,7 @@ /// @brief pi keyword implementation /// void program::rpn_pi(void) { - _stack.push_front(new Number(mpfr::const_pi())); + stack_.push_front(new Number(mpfr::const_pi())); } /// @brief d->r keyword implementation @@ -13,8 +13,8 @@ void program::rpn_pi(void) { void program::rpn_d2r(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.value(0) *= mpfr::const_pi(); - _stack.value(0) /= 180; + stack_.value(0) *= mpfr::const_pi(); + stack_.value(0) /= 180; } /// @brief r->d keyword implementation @@ -22,18 +22,18 @@ void program::rpn_d2r(void) { void program::rpn_r2d(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); - _stack.value(0) /= mpfr::const_pi(); - _stack.value(0) *= 180; + stack_.value(0) /= mpfr::const_pi(); + stack_.value(0) *= 180; } /// @brief sin keyword implementation /// void program::rpn_sin(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = sin(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = sin(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = sin(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = sin(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -42,10 +42,10 @@ void program::rpn_sin(void) { /// void program::rpn_asin(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = asin(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = asin(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = asin(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = asin(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -54,10 +54,10 @@ void program::rpn_asin(void) { /// void program::rpn_cos(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = cos(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = cos(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = cos(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = cos(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -66,10 +66,10 @@ void program::rpn_cos(void) { /// void program::rpn_acos(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = acos(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = acos(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = acos(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = acos(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -78,10 +78,10 @@ void program::rpn_acos(void) { /// void program::rpn_tan(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = tan(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = tan(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = tan(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = tan(stack_.value(0)); else setErrorContext(kBadOperandType); } @@ -90,10 +90,10 @@ void program::rpn_tan(void) { /// void program::rpn_atan(void) { MIN_ARGUMENTS(1); - if (_stack.type(0) == kNumber) - _stack.value(0) = atan(_stack.value(0)); - else if (_stack.type(0) == kComplex) - _stack.value(0) = atan(_stack.value(0)); + if (stack_.type(0) == kNumber) + stack_.value(0) = atan(stack_.value(0)); + else if (stack_.type(0) == kComplex) + stack_.value(0) = atan(stack_.value(0)); else setErrorContext(kBadOperandType); } From b7774f20d979c2c42e6f7484074375a2fa62c789 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 25 Feb 2022 15:32:36 +0100 Subject: [PATCH 5/6] method names --- Changelog.md | 9 +- src/input.cpp | 6 +- src/input.hpp | 7 +- src/lexer.cpp | 50 ++--- src/lexer.hpp | 30 ++- src/main.cpp | 18 +- src/mpreal-out.hpp | 3 +- src/object.cpp | 2 +- src/object.hpp | 67 +++---- src/program.cpp | 384 ++++++++++++++++++------------------- src/program.hpp | 313 +++++++++++++++--------------- src/rpn-branch.cpp | 44 ++--- src/rpn-complex.cpp | 16 +- src/rpn-general.cpp | 42 ++-- src/rpn-logs.cpp | 58 +++--- src/rpn-program.cpp | 40 ++-- src/rpn-real.cpp | 100 +++++----- src/rpn-stack.cpp | 42 ++-- src/rpn-store.cpp | 142 +++++++------- src/rpn-string.cpp | 18 +- src/rpn-test-framework.cpp | 32 ++-- src/rpn-test.cpp | 34 ++-- src/rpn-time.cpp | 12 +- src/rpn-trig.cpp | 30 +-- src/stack.hpp | 4 +- 25 files changed, 741 insertions(+), 762 deletions(-) diff --git a/Changelog.md b/Changelog.md index 30360a0..18bc9a7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -21,14 +21,7 @@ Changelog - cpplint used with a CPPLINT.cfg removing some warnings - Test files are now markdown (.md) files, tests result are slightly changed - Delivery as flatpak and snap -- it seems cosh was giving sinh (!) - -grosse perte en performances (!) -- v2.3.2 fibo: 0,01s user 0,01s system 97% cpu 0,017 total -- v3.0.0 fibo: 2,60s user 0,00s system 99% cpu 2,611 total -- facteur 150 environ -cf https://gmplib.org/manual/Custom-Allocation -cf https://www.geeksforgeeks.org/overloading-new-delete-operator-c/ +- error string are slightly different, althought error codes are still the same New - `«` and `»` are now valid as program delimiters. `<<` and `>>` are still valid diff --git a/src/input.cpp b/src/input.cpp index a272f16..7c84f5b 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -15,7 +15,7 @@ Input::Input(string& entry, vector& autocompletion_list, string prompt, ac_list_ = &autocompletion_list; // linenoise for entry - linenoiseSetCompletionCallback(entry_completion_generator); + linenoiseSetCompletionCallback(EntryCompletionGenerator); while (status == InputStatus::kContinue) { // get user entry if (multiline) @@ -53,7 +53,7 @@ Input::Input(string& entry, vector& autocompletion_list, string prompt, /// @param text the text after wich the user entered TAB /// @param lc the completion object to add strings with linenoiseAddCompletion() /// -void Input::entry_completion_generator(const char* text, linenoiseCompletions* lc) { +void Input::EntryCompletionGenerator(const char* text, linenoiseCompletions* lc) { if (Input::ac_list_ == nullptr || text == nullptr) return; int text_len = strnlen(text, 6); @@ -68,6 +68,6 @@ void Input::entry_completion_generator(const char* text, linenoiseCompletions* l if (ac.compare(0, text_len, text) == 0) linenoiseAddCompletion(lc, ac.c_str()); } -void Input::preload(const char* preloadText) { +void Input::Preload(const char* preloadText) { if (preloadText != nullptr) linenoisePreloadBuffer(preloadText); } diff --git a/src/input.hpp b/src/input.hpp index 896749c..171775b 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -6,8 +6,7 @@ #include #include #include -using std::string; -using std::vector; +using std::string, std::vector; #include "linenoise.h" @@ -16,10 +15,10 @@ class Input { Input(string& entry, vector& autocompletion_list, string prompt = "rpn> ", string multiline_prompt = ">"); enum class InputStatus { kOk, kContinue, kAbort, kCtrlc, kError } status; - static void preload(const char* preloadText); + static void Preload(const char* preloadText); private: - static void entry_completion_generator(const char* text, linenoiseCompletions* lc); + static void EntryCompletionGenerator(const char* text, linenoiseCompletions* lc); static vector* ac_list_; }; diff --git a/src/lexer.cpp b/src/lexer.cpp index 7479504..98b4ab8 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -2,7 +2,7 @@ #include "lexer.hpp" -bool Lexer::lexer(string& entry, map& keywords, vector& elements, +bool Lexer::Analyse(string& entry, map& keywords, vector& elements, vector& errors) { size_t jump; for (size_t i = 0; i < entry.size(); i++) { @@ -10,31 +10,31 @@ bool Lexer::lexer(string& entry, map& keywords, vector& keywords, vector& errors, +bool Lexer::ParseString(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { // here we are sure that entry[0] is at least '"' for (size_t i = idx + 1; i < entry.size(); i++) { @@ -66,7 +66,7 @@ bool Lexer::parseString(string& entry, size_t idx, size_t& next_idx, vector& errors, +bool Lexer::ParseSymbol(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { // here we are sure that entry[0] is at least '\'' for (size_t i = idx + 1; i < entry.size(); i++) { @@ -81,7 +81,7 @@ bool Lexer::parseSymbol(string& entry, size_t idx, size_t& next_idx, vector& errors, +bool Lexer::ParseProgram(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { // here we are sure that entry is at least "<<" // find last ">>" or "»" @@ -92,7 +92,7 @@ bool Lexer::parseProgram(string& entry, size_t idx, size_t& next_idx, vector' && entry[i + 1] == '>') || (entry.substr(i, 2) == "»")) { if (countNested == 0) { string prg = entry.substr(idx + 2, i - idx - 2); - trim(prg); + Trim(prg); elements.push_back({kProgram, .value = prg}); next_idx = i + 2; return true; @@ -102,13 +102,13 @@ bool Lexer::parseProgram(string& entry, size_t idx, size_t& next_idx, vector 62) return false; if (numberIdx != 0) token = token.substr(numberIdx); *r = new mpreal; @@ -180,11 +180,11 @@ bool Lexer::getNumberAt(string& entry, size_t idx, size_t& next_idx, int& base, return false; } -bool Lexer::parseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, +bool Lexer::ParseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { mpreal* r = nullptr; int base = 10; - if (getNumberAt(entry, idx, next_idx, base, &r)) { + if (GetNUmberAt(entry, idx, next_idx, base, &r)) { elements.push_back({kNumber, .re = r, .re_base = base}); return true; } else { @@ -193,7 +193,7 @@ bool Lexer::parseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, +bool Lexer::ParseComplex(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements) { mpreal* re = nullptr; mpreal* im = nullptr; @@ -203,7 +203,7 @@ bool Lexer::parseComplex(string& entry, size_t idx, size_t& next_idx, vector& elements, +bool Lexer::ParseReserved(string& entry, size_t idx, size_t& next_idx, vector& elements, map& keywords) { stringstream ss(entry.substr(idx)); string token; @@ -245,7 +245,7 @@ bool Lexer::parseReserved(string& entry, size_t idx, size_t& next_idx, vector& elements) { +bool Lexer::ParseUnknown(string& entry, size_t idx, size_t& next_idx, vector& elements) { stringstream ss(entry.substr(idx)); string token; ss >> token; diff --git a/src/lexer.hpp b/src/lexer.hpp index c921537..532ee23 100644 --- a/src/lexer.hpp +++ b/src/lexer.hpp @@ -7,11 +7,9 @@ using mpfr::mpreal; #include -#include #include -using std::map; -using std::string; -using std::vector; +#include +using std::map, std::string, std::vector; #include "object.hpp" @@ -49,27 +47,27 @@ class Lexer { /// @param[in] keywords reserved keywords /// @return false=errors, the lexer must stop /// - bool lexer(string& entry, map& keywords, vector& elements, - vector& errors); + bool Analyse(string& entry, map& keywords, vector& elements, + vector& errors); private: - bool parseString(string& entry, size_t idx, size_t& next_idx, vector& errors, + bool ParseString(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseSymbol(string& entry, size_t idx, size_t& next_idx, vector& errors, + bool ParseSymbol(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseProgram(string& entry, size_t idx, size_t& next_idx, vector& errors, + bool ParseProgram(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, + bool ParseNumber(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseComplex(string& entry, size_t idx, size_t& next_idx, vector& errors, + bool ParseComplex(string& entry, size_t idx, size_t& next_idx, vector& errors, vector& elements); - bool parseReserved(string& entry, size_t idx, size_t& next_idx, vector& elements, + bool ParseReserved(string& entry, size_t idx, size_t& next_idx, vector& elements, map& keywords); - bool parseUnknown(string& entry, size_t idx, size_t& next_idx, vector& elements); + bool ParseUnknown(string& entry, size_t idx, size_t& next_idx, vector& elements); - void trim(string& s); - int getBaseAt(string& entry, size_t& next_idx, bool& positive); - bool getNumberAt(string& entry, size_t idx, size_t& next_idx, int& base, mpreal** r, char delim = ' '); + void Trim(string& s); + int GetBaseAt(string& entry, size_t& next_idx, bool& positive); + bool GetNUmberAt(string& entry, size_t idx, size_t& next_idx, int& base, mpreal** r, char delim = ' '); }; #endif // SRC_LEXER_HPP_ diff --git a/src/main.cpp b/src/main.cpp index 8179141..9f5d2b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ static void ExitInteractive() { struct passwd* pw = getpwuid(getuid()); if (pw != nullptr) { stringstream history_path; - history_path << pw->pw_dir << "/.rpn_history"; + history_path << pw->pw_dir << "/.RpnHistory"; // trunc current history ofstream history(history_path.str(), ios_base::out | ios_base::trunc); @@ -38,7 +38,7 @@ static void EnterInteractive() { struct passwd* pw = getpwuid(getuid()); if (pw != nullptr) { stringstream history_path; - history_path << pw->pw_dir << "/.rpn_history"; + history_path << pw->pw_dir << "/.RpnHistory"; // don't care about errors linenoiseHistorySetMaxLen(100); @@ -78,7 +78,7 @@ int main(int argc, char* argv[]) { bool go_on = true; // apply default configuration - program::apply_default(); + program::ApplyDefault(); // run with interactive prompt if (argc == 1) { @@ -93,15 +93,15 @@ int main(int argc, char* argv[]) { // make program from interactive entry program prog(stack, heap); string entry; - switch (Input(entry, program::getAutocompletionWords()).status) { + switch (Input(entry, program::GetAutocompletionWords()).status) { case Input::InputStatus::kOk: // user could stop prog with CtrlC CatchSignals(&prog); // run it - if (prog.parse(entry) == kOk && prog.run() == kGoodbye) + if (prog.Parse(entry) == kOk && prog.Run() == kGoodbye) go_on = false; else - prog.show_stack(); + prog.ShowStack(); break; case Input::InputStatus::kCtrlc: go_on = false; @@ -124,14 +124,14 @@ int main(int argc, char* argv[]) { for (i = 1; i < argc; i++) entry += string(argv[i]) + ' '; // make program - ret = prog.parse(entry); + ret = prog.Parse(entry); if (ret == kOk) { // user could stop prog with CtrlC CatchSignals(&prog); // run it - ret = prog.run(); - prog.show_stack(false); + ret = prog.Run(); + prog.ShowStack(false); } } diff --git a/src/mpreal-out.hpp b/src/mpreal-out.hpp index ddab132..a830da6 100644 --- a/src/mpreal-out.hpp +++ b/src/mpreal-out.hpp @@ -8,8 +8,7 @@ using mpfr::mpreal; #include #include -using std::string; -using std::ostream; +using std::string, std::ostream; ostream& MprealOutput10Base(ostream& out, const string& fmt, const mpreal& value); ostream& MprealOutputNBase(ostream& out, int base, const mpreal& value); diff --git a/src/object.cpp b/src/object.cpp index 43ba057..840266e 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -3,5 +3,5 @@ #include "object.hpp" // number statics -Number::mode_enum Number::mode = Number::DEFAULT_MODE; +Number::mode_enum Number::mode = Number::kDefaultMode; int Number::digits = kDefaultDecimalDigits; diff --git a/src/object.hpp b/src/object.hpp index 760b9c0..68eff2e 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -10,10 +10,7 @@ using mpfr::mpreal; #include #include #include -using std::complex; -using std::ostream; -using std::string; -using std::stringstream; +using std::complex, std::ostream, std::string, std::stringstream; #include "mpreal-out.hpp" @@ -63,20 +60,18 @@ struct Object { explicit Object(ObjectType type = kUndef) : _type(type) {} virtual ~Object() {} ObjectType _type; - virtual Object* clone() { + virtual Object* Clone() { Object* o = new Object(); if (o != nullptr) *o = *this; return o; } - virtual string name() { return string("Object"); } - virtual ostream& show(ostream& out) { - out << "(" << name() << " - unknown representation)"; + virtual string Name() { return string("Object"); } + virtual ostream& Show(ostream& out) { + out << "(" << Name() << " - unknown representation)"; return out; } - friend ostream& operator<<(ostream& os, Object* o) { return o->show(os); } - - unsigned int size() { return sizeof(*this); } + friend ostream& operator<<(ostream& os, Object* o) { return o->Show(os); } }; /// @brief stack objects derived from Object @@ -89,14 +84,14 @@ struct Number : Object { int base; mpreal value; - virtual Object* clone() { return new Number(value, base); } - virtual string name() { return string("number"); } - virtual ostream& show(ostream& out) { return showValue(out, value, mode, digits, base); } + virtual Object* Clone() { return new Number(value, base); } + virtual string Name() { return string("number"); } + virtual ostream& Show(ostream& out) { return ShowValue(out, value, mode, digits, base); } // representation mode typedef enum { kStd, kFix, kSci } mode_enum; static mode_enum mode; - static constexpr mode_enum DEFAULT_MODE = Number::kStd; + static constexpr mode_enum kDefaultMode = Number::kStd; // precision static constexpr mpfr_prec_t kMpfrDefaultPrecBits = 128; @@ -104,7 +99,7 @@ struct Number : Object { static int digits; // clang-format off - static string _makeNumberFormat(mode_enum mode, int digits) { + static string MakeNumberFormat(mode_enum mode, int digits) { stringstream format; format << "%." << digits; switch ( mode ) { @@ -116,9 +111,9 @@ struct Number : Object { } // clang-format on - static ostream& showValue(ostream& out, const mpreal& value, mode_enum mode, int digits, int base) { + static ostream& ShowValue(ostream& out, const mpreal& value, mode_enum mode, int digits, int base) { if (base == 10) - return MprealOutput10Base(out, _makeNumberFormat(mode, digits), value); + return MprealOutput10Base(out, MakeNumberFormat(mode, digits), value); else return MprealOutputNBase(out, base, value); } @@ -141,13 +136,13 @@ struct Complex : Object { int re_base, im_base; complex value; - virtual Object* clone() { return new Complex(value, re_base, im_base); } - virtual string name() { return string("complex"); } - virtual ostream& show(ostream& out) { + virtual Object* Clone() { return new Complex(value, re_base, im_base); } + virtual string Name() { return string("complex"); } + virtual ostream& Show(ostream& out) { out << '('; - Number::showValue(out, value.real(), Number::mode, Number::digits, re_base); + Number::ShowValue(out, value.real(), Number::mode, Number::digits, re_base); out << ','; - Number::showValue(out, value.imag(), Number::mode, Number::digits, im_base); + Number::ShowValue(out, value.imag(), Number::mode, Number::digits, im_base); return out << ')'; } }; @@ -155,18 +150,18 @@ struct Complex : Object { struct String : Object { String() : Object(kString) {} explicit String(const string& value__) : Object(kString), value(value__) {} - virtual Object* clone() { return new String(value); } - virtual string name() { return string("string"); } - virtual ostream& show(ostream& out) { return out << "\"" << value << "\""; } + virtual Object* Clone() { return new String(value); } + virtual string Name() { return string("string"); } + virtual ostream& Show(ostream& out) { return out << "\"" << value << "\""; } string value; }; struct Program : Object { Program() : Object(kProgram) {} explicit Program(const string& value__) : Object(kProgram), value(value__) {} - virtual Object* clone() { return new Program(value); } - virtual string name() { return string("program"); } - virtual ostream& show(ostream& out) { return out << "«" << value << "»"; } + virtual Object* Clone() { return new Program(value); } + virtual string Name() { return string("program"); } + virtual ostream& Show(ostream& out) { return out << "«" << value << "»"; } string value; }; @@ -174,9 +169,9 @@ struct Symbol : Object { explicit Symbol(bool auto_eval__ = true) : Object(kSymbol), auto_eval(auto_eval__) {} explicit Symbol(const string& value__, bool auto_eval__ = true) : Object(kSymbol), value(value__), auto_eval(auto_eval__) {} - virtual Object* clone() { return new Symbol(value, auto_eval); } - virtual string name() { return string("symbol"); } - virtual ostream& show(ostream& out) { return out << "'" << value << "'"; } + virtual Object* Clone() { return new Symbol(value, auto_eval); } + virtual string Name() { return string("symbol"); } + virtual ostream& Show(ostream& out) { return out << "'" << value << "'"; } bool auto_eval; string value; }; @@ -184,8 +179,8 @@ struct Symbol : Object { struct Keyword : Object { Keyword() : Object(kKeyword) {} explicit Keyword(program_fn_t fn__, const string& value__) : Object(kKeyword), fn(fn__), value(value__) {} - virtual Object* clone() { return new Keyword(fn, value); } - virtual string name() { return string("keyword"); } + virtual Object* Clone() { return new Keyword(fn, value); } + virtual string Name() { return string("keyword"); } program_fn_t fn; string value; }; @@ -208,8 +203,8 @@ struct Branch : Object { arg_bool = other.arg_bool; value = other.value; } - virtual Object* clone() { return new Branch(*this); } - virtual string name() { return string("branch"); } + virtual Object* Clone() { return new Branch(*this); } + virtual string Name() { return string("branch"); } branch_fn_t fn; size_t arg1, arg2, arg3; mpreal first_index, last_index; diff --git a/src/program.cpp b/src/program.cpp index 30c9550..0276800 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -6,205 +6,205 @@ vector program::keywords_{ // GENERAL {kUndef, "", nullptr, "\nGENERAL"}, - {kKeyword, "nop", &program::rpn_nop, "no operation"}, - {kKeyword, "help", &program::rpn_help, "this help message"}, - {kKeyword, "h", &program::rpn_help, ""}, - {kKeyword, "?", &program::rpn_help, ""}, - {kKeyword, "quit", &program::rpn_good_bye, "quit software"}, - {kKeyword, "q", &program::rpn_good_bye, ""}, - {kKeyword, "exit", &program::rpn_good_bye, ""}, - {kKeyword, "test", &program::rpn_test, ""}, // not seen by user - {kKeyword, "version", &program::rpn_version, "show rpn version"}, - {kKeyword, "uname", &program::rpn_uname, "show rpn complete identification string"}, - {kKeyword, "history", &program::rpn_history, "see commands history"}, + {kKeyword, "nop", &program::RpnNop, "no operation"}, + {kKeyword, "help", &program::RpnHelp, "this help message"}, + {kKeyword, "h", &program::RpnHelp, ""}, + {kKeyword, "?", &program::RpnHelp, ""}, + {kKeyword, "quit", &program::RpnQuit, "quit software"}, + {kKeyword, "q", &program::RpnQuit, ""}, + {kKeyword, "exit", &program::RpnQuit, ""}, + {kKeyword, "test", &program::RpnTest, ""}, // not seen by user + {kKeyword, "version", &program::RpnVersion, "show rpn version"}, + {kKeyword, "uname", &program::RpnUname, "show rpn complete identification string"}, + {kKeyword, "history", &program::RpnHistory, "see commands history"}, // USUAL OPERATIONS ON REALS AND COMPLEXES {kUndef, "", nullptr, "\nUSUAL OPERATIONS ON REALS AND COMPLEXES"}, - {kKeyword, "+", &program::rpn_plus, "addition"}, - {kKeyword, "-", &program::rpn_minus, "substraction"}, - {kKeyword, "*", &program::rpn_mul, "multiplication"}, - {kKeyword, "/", &program::rpn_div, "division"}, - {kKeyword, "inv", &program::rpn_inv, "inverse"}, - {kKeyword, "chs", &program::rpn_neg, "negation"}, - {kKeyword, "neg", &program::rpn_neg, ""}, - {kKeyword, "^", &program::rpn_power, "power"}, - {kKeyword, "pow", &program::rpn_power, ""}, - {kKeyword, "sqrt", &program::rpn_squareroot, "rpn_square root"}, - {kKeyword, "sq", &program::rpn_square, "rpn_square"}, - {kKeyword, "abs", &program::rpn_abs, "absolute value (norm for a complex)"}, - {kKeyword, "sign", &program::rpn_sign, "sign of a number or z/|z| for a complex"}, + {kKeyword, "+", &program::RpnPlus, "addition"}, + {kKeyword, "-", &program::RpnMinus, "substraction"}, + {kKeyword, "*", &program::RpnMul, "multiplication"}, + {kKeyword, "/", &program::RpnDiv, "division"}, + {kKeyword, "inv", &program::RpnInv, "inverse"}, + {kKeyword, "chs", &program::RpnNeg, "negation"}, + {kKeyword, "neg", &program::RpnNeg, ""}, + {kKeyword, "^", &program::RpnPower, "power"}, + {kKeyword, "pow", &program::RpnPower, ""}, + {kKeyword, "sqrt", &program::RpnSquareroot, "RpnSquare root"}, + {kKeyword, "sq", &program::RpnSquare, "RpnSquare"}, + {kKeyword, "abs", &program::RpnAbs, "absolute value (norm for a complex)"}, + {kKeyword, "sign", &program::RpnSign, "sign of a number or z/|z| for a complex"}, // OPERATIONS ON REALS {kUndef, "", nullptr, "\nOPERATIONS ON REALS"}, - {kKeyword, "%", &program::rpn_purcent, "purcent"}, - {kKeyword, "%CH", &program::rpn_purcentCH, "inverse purcent"}, - {kKeyword, "mod", &program::rpn_modulo, "modulo"}, - {kKeyword, "fact", &program::rpn_fact, "n! for integer n or Gamma(x+1) for fractional x"}, - {kKeyword, "mant", &program::rpn_mant, "mantissa of a real number"}, - {kKeyword, "xpon", &program::rpn_xpon, "exponant of a real number"}, - {kKeyword, "floor", &program::rpn_floor, "largest number <="}, - {kKeyword, "ceil", &program::rpn_ceil, "smallest number >="}, - {kKeyword, "ip", &program::rpn_ip, "integer part"}, - {kKeyword, "fp", &program::rpn_fp, "fractional part"}, - {kKeyword, "min", &program::rpn_min, "min of 2 real numbers"}, - {kKeyword, "max", &program::rpn_max, "max of 2 real numbers"}, + {kKeyword, "%", &program::RpnPurcent, "purcent"}, + {kKeyword, "%CH", &program::RpnPurcentCH, "inverse purcent"}, + {kKeyword, "mod", &program::RpnModulo, "modulo"}, + {kKeyword, "fact", &program::RpnFact, "n! for integer n or Gamma(x+1) for fractional x"}, + {kKeyword, "mant", &program::RpnMant, "mantissa of a real number"}, + {kKeyword, "xpon", &program::RpnXpon, "exponant of a real number"}, + {kKeyword, "floor", &program::RpnFloor, "largest number <="}, + {kKeyword, "ceil", &program::RpnCeil, "smallest number >="}, + {kKeyword, "ip", &program::RpnIp, "integer part"}, + {kKeyword, "fp", &program::RpnFp, "fractional part"}, + {kKeyword, "min", &program::RpnMin, "min of 2 real numbers"}, + {kKeyword, "max", &program::RpnMax, "max of 2 real numbers"}, // OPERATIONS ON COMPLEXES {kUndef, "", nullptr, "\nOPERATIONS ON COMPLEXES"}, - {kKeyword, "re", &program::rpn_re, "complex real part"}, - {kKeyword, "im", &program::rpn_im, "complex imaginary part"}, - {kKeyword, "conj", &program::rpn_conj, "complex conjugate"}, - {kKeyword, "arg", &program::rpn_arg, "complex argument in radians"}, - {kKeyword, "c->r", &program::rpn_c2r, "transform a complex in 2 reals"}, - {kKeyword, "r->c", &program::rpn_r2c, "transform 2 reals in a complex"}, - {kKeyword, "p->r", &program::rpn_p2r, "cartesian to polar"}, - {kKeyword, "r->p", &program::rpn_r2p, "polar to cartesian"}, + {kKeyword, "re", &program::RpnReal, "complex real part"}, + {kKeyword, "im", &program::RpnImag, "complex imaginary part"}, + {kKeyword, "conj", &program::RpnConj, "complex conjugate"}, + {kKeyword, "arg", &program::RpnArg, "complex argument in radians"}, + {kKeyword, "c->r", &program::RpnC2r, "transform a complex in 2 reals"}, + {kKeyword, "r->c", &program::RpnR2c, "transform 2 reals in a complex"}, + {kKeyword, "p->r", &program::RpnP2r, "cartesian to polar"}, + {kKeyword, "r->p", &program::RpnR2p, "polar to cartesian"}, // MODE {kUndef, "", nullptr, "\nMODE"}, - {kKeyword, "std", &program::rpn_std, "standard floating numbers representation. ex: std"}, - {kKeyword, "fix", &program::rpn_fix, "fixed point representation. ex: 6 fix"}, - {kKeyword, "sci", &program::rpn_sci, "scientific floating point representation. ex: 20 sci"}, - {kKeyword, "prec", &program::rpn_precision, "set float precision in bits. ex: 256 prec"}, - {kKeyword, "round", &program::rpn_round, + {kKeyword, "std", &program::RpnStd, "standard floating numbers representation. ex: std"}, + {kKeyword, "fix", &program::RpnFix, "fixed point representation. ex: 6 fix"}, + {kKeyword, "sci", &program::RpnSci, "scientific floating point representation. ex: 20 sci"}, + {kKeyword, "prec", &program::RpnPrecision, "set float precision in bits. ex: 256 prec"}, + {kKeyword, "round", &program::RpnRound, "set float rounding mode.\n\tex: [\"nearest (even)\", \"toward zero\", \"toward " "+inf\", \"toward -inf\", \"away from zero\", \"faithful rounding\", \"nearest (away from zero)\"] round"}, - {kKeyword, "default", &program::rpn_default, "set float representation and precision to default"}, - {kKeyword, "type", &program::rpn_type, "show type of stack first entry"}, - {kKeyword, "hex", &program::rpn_hex, "hexadecimal representation, applies on stack level 0 only"}, - {kKeyword, "dec", &program::rpn_dec, "decimal representation, applies on stack level 0 only"}, - {kKeyword, "bin", &program::rpn_bin, "binary representation, applies on stack level 0 only"}, - {kKeyword, "base", &program::rpn_base, "arbitrary base representation, applies on stack level 0 only"}, + {kKeyword, "default", &program::RpnDefault, "set float representation and precision to default"}, + {kKeyword, "type", &program::RpnType, "show type of stack first entry"}, + {kKeyword, "hex", &program::RpnHex, "hexadecimal representation, applies on stack level 0 only"}, + {kKeyword, "dec", &program::RpnDec, "decimal representation, applies on stack level 0 only"}, + {kKeyword, "bin", &program::RpnBin, "binary representation, applies on stack level 0 only"}, + {kKeyword, "base", &program::RpnBase, "arbitrary base representation, applies on stack level 0 only"}, // TESTS {kUndef, "", nullptr, "\nTEST"}, - {kKeyword, ">", &program::rpn_sup, "binary operator >"}, - {kKeyword, ">=", &program::rpn_sup_eq, "binary operator >="}, - {kKeyword, "<", &program::rpn_inf, "binary operator <"}, - {kKeyword, "<=", &program::rpn_inf_eq, "binary operator <="}, - {kKeyword, "!=", &program::rpn_diff, "binary operator != (different)"}, - {kKeyword, "==", &program::rpn_eq, "binary operator == (equal)"}, - {kKeyword, "and", &program::rpn_test_and, "boolean operator and"}, - {kKeyword, "or", &program::rpn_test_or, "boolean operator or"}, - {kKeyword, "xor", &program::rpn_test_xor, "boolean operator xor"}, - {kKeyword, "not", &program::rpn_test_not, "boolean operator not"}, - {kKeyword, "same", &program::rpn_same, "boolean operator same (equal)"}, + {kKeyword, ">", &program::RpnSup, "binary operator >"}, + {kKeyword, ">=", &program::RpnSupEq, "binary operator >="}, + {kKeyword, "<", &program::RpnInf, "binary operator <"}, + {kKeyword, "<=", &program::RpnInfEq, "binary operator <="}, + {kKeyword, "!=", &program::RpnDiff, "binary operator != (different)"}, + {kKeyword, "==", &program::RpnEq, "binary operator == (equal)"}, + {kKeyword, "and", &program::RpnTestAnd, "boolean operator and"}, + {kKeyword, "or", &program::RpnTestOr, "boolean operator or"}, + {kKeyword, "xor", &program::RpnTestXor, "boolean operator xor"}, + {kKeyword, "not", &program::RpnTestNot, "boolean operator not"}, + {kKeyword, "same", &program::RpnSame, "boolean operator same (equal)"}, // STACK {kUndef, "", nullptr, "\nSTACK"}, - {kKeyword, "swap", &program::rpn_swap, "swap 2 first stack entries"}, - {kKeyword, "drop", &program::rpn_drop, "drop first stack entry"}, - {kKeyword, "drop2", &program::rpn_drop2, "drop 2 first stack entries"}, - {kKeyword, "dropn", &program::rpn_dropn, "drop n first stack entries"}, - {kKeyword, "del", &program::rpn_erase, "drop all stack entries"}, - {kKeyword, "erase", &program::rpn_erase, ""}, - {kKeyword, "rot", &program::rpn_rot, "rotate 3 first stack entries"}, - {kKeyword, "dup", &program::rpn_dup, "duplicate first stack entry"}, - {kKeyword, "dup2", &program::rpn_dup2, "duplicate 2 first stack entries"}, - {kKeyword, "dupn", &program::rpn_dupn, "duplicate n first stack entries"}, - {kKeyword, "pick", &program::rpn_pick, "push a copy of the given stack level onto the stack"}, - {kKeyword, "depth", &program::rpn_depth, "give stack depth"}, - {kKeyword, "roll", &program::rpn_roll, "move a stack entry to the top of the stack"}, - {kKeyword, "rolld", &program::rpn_rolld, "move the element on top of the stack to a higher stack position"}, - {kKeyword, "over", &program::rpn_over, "push a copy of the element in stack level 2 onto the stack"}, + {kKeyword, "swap", &program::RpnSwap, "swap 2 first stack entries"}, + {kKeyword, "drop", &program::RpnDrop, "drop first stack entry"}, + {kKeyword, "drop2", &program::RpnDrop2, "drop 2 first stack entries"}, + {kKeyword, "dropn", &program::RpnDropn, "drop n first stack entries"}, + {kKeyword, "del", &program::RpnErase, "drop all stack entries"}, + {kKeyword, "erase", &program::RpnErase, ""}, + {kKeyword, "rot", &program::RpnRot, "rotate 3 first stack entries"}, + {kKeyword, "dup", &program::RpnDup, "duplicate first stack entry"}, + {kKeyword, "dup2", &program::RpnDup2, "duplicate 2 first stack entries"}, + {kKeyword, "dupn", &program::RpnDupn, "duplicate n first stack entries"}, + {kKeyword, "pick", &program::RpnPick, "push a copy of the given stack level onto the stack"}, + {kKeyword, "depth", &program::RpnDepth, "give stack depth"}, + {kKeyword, "roll", &program::RpnRoll, "move a stack entry to the top of the stack"}, + {kKeyword, "rolld", &program::RpnRolld, "move the element on top of the stack to a higher stack position"}, + {kKeyword, "over", &program::RpnOver, "push a copy of the element in stack level 2 onto the stack"}, // STRING {kUndef, "", nullptr, "\nSTRING"}, - {kKeyword, "->str", &program::rpn_instr, "convert an object into a string"}, - {kKeyword, "str->", &program::rpn_strout, "convert a string into an object"}, - {kKeyword, "chr", &program::rpn_chr, "convert ASCII character code in stack level 1 into a string"}, - {kKeyword, "num", &program::rpn_num, + {kKeyword, "->str", &program::RpnInstr, "convert an object into a string"}, + {kKeyword, "str->", &program::RpnStrout, "convert a string into an object"}, + {kKeyword, "chr", &program::RpnChr, "convert ASCII character code in stack level 1 into a string"}, + {kKeyword, "num", &program::RpnNum, "return ASCII code of the first character of the string in stack level 1 as a real number"}, - {kKeyword, "size", &program::rpn_strsize, "return the length of the string"}, - {kKeyword, "pos", &program::rpn_strpos, "seach for the string in level 1 within the string in level 2"}, - {kKeyword, "sub", &program::rpn_strsub, "return a substring of the string in level 3"}, + {kKeyword, "size", &program::RpnStrsize, "return the length of the string"}, + {kKeyword, "pos", &program::RpnStrpos, "seach for the string in level 1 within the string in level 2"}, + {kKeyword, "sub", &program::RpnStrsub, "return a substring of the string in level 3"}, // BRANCH {kUndef, "", nullptr, "\nBRANCH"}, - {kBranch, "if", (program_fn_t)&program::rpn_if, + {kBranch, "if", (program_fn_t)&program::RpnIf, "if then else " "end"}, - {kBranch, "then", (program_fn_t)&program::rpn_then, "used with if"}, - {kBranch, "else", (program_fn_t)&program::rpn_else, "used with if"}, - {kBranch, "end", (program_fn_t)&program::rpn_end, "used with various branch instructions"}, - {kBranch, "start", (program_fn_t)&program::rpn_start, " start next| step"}, - {kBranch, "for", (program_fn_t)&program::rpn_for, + {kBranch, "then", (program_fn_t)&program::RpnThen, "used with if"}, + {kBranch, "else", (program_fn_t)&program::RpnElse, "used with if"}, + {kBranch, "end", (program_fn_t)&program::RpnEnd, "used with various branch instructions"}, + {kBranch, "start", (program_fn_t)&program::RpnStart, " start next| step"}, + {kBranch, "for", (program_fn_t)&program::RpnFor, " for next| step"}, - {kBranch, "next", (program_fn_t)&program::rpn_next, "used with start and for"}, - {kBranch, "step", (program_fn_t)&program::rpn_step, "used with start and for"}, - {kKeyword, "ift", &program::rpn_ift, "similar to if-then-end, ift"}, - {kKeyword, "ifte", &program::rpn_ifte, + {kBranch, "next", (program_fn_t)&program::RpnNext, "used with start and for"}, + {kBranch, "step", (program_fn_t)&program::RpnStep, "used with start and for"}, + {kKeyword, "ift", &program::RpnIft, "similar to if-then-end, ift"}, + {kKeyword, "ifte", &program::RpnIfte, "similar to if-then-else-end, " " ifte"}, - {kBranch, "do", (program_fn_t)&program::rpn_do, "do until end"}, - {kBranch, "until", (program_fn_t)&program::rpn_until, "used with do"}, - {kBranch, "while", (program_fn_t)&program::rpn_while, "while repeat end"}, - {kBranch, "repeat", (program_fn_t)&program::rpn_repeat, "used with while"}, + {kBranch, "do", (program_fn_t)&program::RpnDo, "do until end"}, + {kBranch, "until", (program_fn_t)&program::RpnUntil, "used with do"}, + {kBranch, "while", (program_fn_t)&program::RpnWhile, "while repeat end"}, + {kBranch, "repeat", (program_fn_t)&program::RpnRepeat, "used with while"}, // STORE {kUndef, "", nullptr, "\nSTORE"}, - {kKeyword, "sto", &program::rpn_sto, "store a variable. ex: 1 'name' sto"}, - {kKeyword, "rcl", &program::rpn_rcl, "recall a variable. ex: 'name' rcl"}, - {kKeyword, "purge", &program::rpn_purge, "delete a variable. ex: 'name' purge"}, - {kKeyword, "vars", &program::rpn_vars, "list all variables"}, - {kKeyword, "clusr", &program::rpn_clusr, "erase all variables"}, - {kKeyword, "edit", &program::rpn_edit, "edit a variable content"}, - {kKeyword, "sto+", &program::rpn_stoadd, "add to a stored variable. ex: 1 'name' sto+ 'name' 2 sto+"}, - {kKeyword, "sto-", &program::rpn_stosub, "substract to a stored variable. ex: 1 'name' sto- 'name' 2 sto-"}, - {kKeyword, "sto*", &program::rpn_stomul, "multiply a stored variable. ex: 3 'name' sto* 'name' 2 sto*"}, - {kKeyword, "sto/", &program::rpn_stodiv, "divide a stored variable. ex: 3 'name' sto/ 'name' 2 sto/"}, - {kKeyword, "sneg", &program::rpn_stoneg, "negate a variable. ex: 'name' sneg"}, - {kKeyword, "sinv", &program::rpn_stoinv, "inverse a variable. ex: 1 'name' sinv"}, + {kKeyword, "sto", &program::RpnSto, "store a variable. ex: 1 'name' sto"}, + {kKeyword, "rcl", &program::RpnRcl, "recall a variable. ex: 'name' rcl"}, + {kKeyword, "purge", &program::RpnPurge, "delete a variable. ex: 'name' purge"}, + {kKeyword, "vars", &program::RpnVars, "list all variables"}, + {kKeyword, "clusr", &program::RpnClusr, "erase all variables"}, + {kKeyword, "edit", &program::RpnEdit, "edit a variable content"}, + {kKeyword, "sto+", &program::RpnStoadd, "add to a stored variable. ex: 1 'name' sto+ 'name' 2 sto+"}, + {kKeyword, "sto-", &program::RpnStosub, "substract to a stored variable. ex: 1 'name' sto- 'name' 2 sto-"}, + {kKeyword, "sto*", &program::RpnStomul, "multiply a stored variable. ex: 3 'name' sto* 'name' 2 sto*"}, + {kKeyword, "sto/", &program::RpnStodiv, "divide a stored variable. ex: 3 'name' sto/ 'name' 2 sto/"}, + {kKeyword, "sneg", &program::RpnStoneg, "negate a variable. ex: 'name' sneg"}, + {kKeyword, "sinv", &program::RpnStoinv, "inverse a variable. ex: 1 'name' sinv"}, // PROGRAM {kUndef, "", nullptr, "\nPROGRAM"}, - {kKeyword, "eval", &program::rpn_eval, "evaluate (run) a program, or recall a variable. ex: 'my_prog' eval"}, - {kBranch, "->", (program_fn_t)&program::rpn_inprog, + {kKeyword, "eval", &program::RpnEval, "evaluate (run) a program, or recall a variable. ex: 'my_prog' eval"}, + {kBranch, "->", (program_fn_t)&program::RpnInprog, "load program local variables. ex: << -> n m << 0 n m for i i + next >> " ">>"}, // TRIG ON REALS AND COMPLEXES {kUndef, "", nullptr, "\nTRIG ON REALS AND COMPLEXES"}, - {kKeyword, "pi", &program::rpn_pi, "pi constant"}, - {kKeyword, "sin", &program::rpn_sin, "sinus"}, - {kKeyword, "asin", &program::rpn_asin, "arg sinus"}, - {kKeyword, "cos", &program::rpn_cos, "cosinus"}, - {kKeyword, "acos", &program::rpn_acos, "arg cosinus"}, - {kKeyword, "tan", &program::rpn_tan, "tangent"}, - {kKeyword, "atan", &program::rpn_atan, "arg tangent"}, - {kKeyword, "d->r", &program::rpn_d2r, "convert degrees to radians"}, - {kKeyword, "r->d", &program::rpn_r2d, "convert radians to degrees"}, + {kKeyword, "pi", &program::RpnPi, "pi constant"}, + {kKeyword, "sin", &program::RpnSin, "sinus"}, + {kKeyword, "asin", &program::RpnAsin, "arg sinus"}, + {kKeyword, "cos", &program::RpnCos, "cosinus"}, + {kKeyword, "acos", &program::RpnAcos, "arg cosinus"}, + {kKeyword, "tan", &program::RpnTan, "tangent"}, + {kKeyword, "atan", &program::RpnAtan, "arg tangent"}, + {kKeyword, "d->r", &program::RpnD2r, "convert degrees to radians"}, + {kKeyword, "r->d", &program::RpnR2d, "convert radians to degrees"}, // LOGS ON REALS AND COMPLEXES {kUndef, "", nullptr, "\nLOGS ON REALS AND COMPLEXES"}, - {kKeyword, "e", &program::rpn_e, "Euler constant"}, - {kKeyword, "ln", &program::rpn_ln, "logarithm base e"}, - {kKeyword, "log", &program::rpn_ln, ""}, - {kKeyword, "lnp1", &program::rpn_lnp1, "ln(1+x) which is useful when x is close to 0"}, - {kKeyword, "exp", &program::rpn_exp, "exponential"}, - {kKeyword, "expm", &program::rpn_expm, "exp(x)-1 which is useful when x is close to 0"}, - {kKeyword, "log10", &program::rpn_log10, "logarithm base 10"}, - {kKeyword, "alog10", &program::rpn_alog10, "exponential base 10"}, - {kKeyword, "exp10", &program::rpn_alog10, ""}, - {kKeyword, "log2", &program::rpn_log2, "logarithm base 2"}, - {kKeyword, "alog2", &program::rpn_alog2, "exponential base 2"}, - {kKeyword, "exp2", &program::rpn_alog2, ""}, - {kKeyword, "sinh", &program::rpn_sinh, "hyperbolic sine"}, - {kKeyword, "asinh", &program::rpn_asinh, "inverse hyperbolic sine"}, - {kKeyword, "cosh", &program::rpn_cosh, "hyperbolic cosine"}, - {kKeyword, "acosh", &program::rpn_acosh, "inverse hyperbolic cosine"}, - {kKeyword, "tanh", &program::rpn_tanh, "hyperbolic tangent"}, - {kKeyword, "atanh", &program::rpn_atanh, "inverse hyperbolic tangent"}, + {kKeyword, "e", &program::RpnE, "Euler constant"}, + {kKeyword, "ln", &program::RpnLn, "logarithm base e"}, + {kKeyword, "log", &program::RpnLn, ""}, + {kKeyword, "lnp1", &program::RpnLnp1, "ln(1+x) which is useful when x is close to 0"}, + {kKeyword, "exp", &program::RpnExp, "exponential"}, + {kKeyword, "expm", &program::RpnExpm, "exp(x)-1 which is useful when x is close to 0"}, + {kKeyword, "log10", &program::RpnLog10, "logarithm base 10"}, + {kKeyword, "alog10", &program::RpnAlog10, "exponential base 10"}, + {kKeyword, "exp10", &program::RpnAlog10, ""}, + {kKeyword, "log2", &program::RpnLog2, "logarithm base 2"}, + {kKeyword, "alog2", &program::RpnAlog2, "exponential base 2"}, + {kKeyword, "exp2", &program::RpnAlog2, ""}, + {kKeyword, "sinh", &program::RpnSinh, "hyperbolic sine"}, + {kKeyword, "asinh", &program::RpnAsinh, "inverse hyperbolic sine"}, + {kKeyword, "cosh", &program::RpnCosh, "hyperbolic cosine"}, + {kKeyword, "acosh", &program::RpnAcosh, "inverse hyperbolic cosine"}, + {kKeyword, "tanh", &program::RpnTanh, "hyperbolic tangent"}, + {kKeyword, "atanh", &program::RpnAtanh, "inverse hyperbolic tangent"}, // TIME AND DATE {kUndef, "", nullptr, "\nTIME AND DATE"}, - {kKeyword, "time", &program::rpn_time, "time in local format"}, - {kKeyword, "date", &program::rpn_date, "date in local format"}, - {kKeyword, "ticks", &program::rpn_ticks, "system tick in µs"}, + {kKeyword, "time", &program::RpnTime, "time in local format"}, + {kKeyword, "date", &program::RpnDate, "date in local format"}, + {kKeyword, "ticks", &program::RpnTicks, "system tick in µs"}, }; /// autocompletion vector for linenoise autocompletion -vector& program::getAutocompletionWords() { +vector& program::GetAutocompletionWords() { static vector autocompletion_words; if (autocompletion_words.empty()) for (auto& kw : keywords_) @@ -216,7 +216,7 @@ vector& program::getAutocompletionWords() { /// /// @return RetValue see this type /// -RetValue program::run() { +RetValue program::Run() { bool go_out = false; RetValue ret = kOk; ObjectType type; @@ -225,7 +225,7 @@ RetValue program::run() { err_context_ = ""; // branches for 'if' - ret = preprocess(); + ret = Preprocess(); if (ret != kOk) { // free allocated for (Object* o : *this) delete o; @@ -239,7 +239,7 @@ RetValue program::run() { switch (o->_type) { // could be an auto-evaluated symbol case kSymbol: - auto_rcl(reinterpret_cast(o)); + AutoRcl(reinterpret_cast(o)); i++; break; @@ -265,7 +265,7 @@ RetValue program::run() { if (err_ == kTestFailed) ret = kTestFailed; // error: show it - if (show_error(err_, err_context_) == kDeadlyError) + if (ShowError(err_, err_context_) == kDeadlyError) // pb showing error -> go out software ret = kGoodbye; break; @@ -284,7 +284,7 @@ RetValue program::run() { i++; // meaning 'next command' break; case kRtError: // runtime error - (void)show_error(err_, err_context_); + (void)ShowError(err_, err_context_); go_out = true; break; default: @@ -297,7 +297,7 @@ RetValue program::run() { default: // not a command, but a stack entry, manage it // copy the program stack entry to the running stack - stack_.push_front(o->clone()); + stack_.push_front(o->Clone()); i++; break; } @@ -317,7 +317,7 @@ RetValue program::run() { /// /// @return RetValue see this type /// -RetValue program::preprocess(void) { +RetValue program::Preprocess() { struct if_layout_t { if_layout_t() : index_then_or_unti_or_repeat(-1), @@ -355,17 +355,17 @@ RetValue program::preprocess(void) { // nothing after 'then' -> error if (next == kStepOut) { // error: show it - show_syntax_error("missing end after then"); + ShowSyntaxError("missing end after then"); return kSyntaxError; } if (layout_index < 0) { // error: show it - show_syntax_error("missing if before then"); + ShowSyntaxError("missing if before then"); return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { // error: show it - show_syntax_error("duplicate then"); + ShowSyntaxError("duplicate then"); return kSyntaxError; } vlayout[layout_index].index_then_or_unti_or_repeat = i; @@ -378,22 +378,22 @@ RetValue program::preprocess(void) { // nothing after 'else' -> error if (next == kStepOut) { // error: show it - show_syntax_error("missing end after else"); + ShowSyntaxError("missing end after else"); return kSyntaxError; } if (layout_index < 0) { // error: show it - show_syntax_error("missing if before else"); + ShowSyntaxError("missing if before else"); return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat == -1) { // error: show it - show_syntax_error("missing then before else"); + ShowSyntaxError("missing then before else"); return kSyntaxError; } if (vlayout[layout_index].index_else != -1) { // error: show it - show_syntax_error("duplicate else"); + ShowSyntaxError("duplicate else"); return kSyntaxError; } vlayout[layout_index].index_else = i; @@ -409,7 +409,7 @@ RetValue program::preprocess(void) { } else if (k->value == "next") { if (vstart_index.size() == 0) { // error: show it - show_syntax_error("missing start or for before next"); + ShowSyntaxError("missing start or for before next"); return kSyntaxError; } k->arg1 = vstart_index[vstart_index.size() - 1]; // 'next' arg1 = 'start' index @@ -419,7 +419,7 @@ RetValue program::preprocess(void) { } else if (k->value == "step") { if (vstart_index.size() == 0) { // error: show it - show_syntax_error("missing start or for before step"); + ShowSyntaxError("missing start or for before step"); return kSyntaxError; } k->arg1 = vstart_index[vstart_index.size() - 1]; // fill 'step' branch1 = 'start' index @@ -441,17 +441,17 @@ RetValue program::preprocess(void) { // nothing after 'unti' -> error if (next == kStepOut) { // error: show it - show_syntax_error("missing end"); + ShowSyntaxError("missing end"); return kSyntaxError; } if (layout_index < 0 || !vlayout[layout_index].is_do_unti) { // error: show it - show_syntax_error("missing do"); + ShowSyntaxError("missing do"); return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { // error: show it - show_syntax_error("duplicate until"); + ShowSyntaxError("duplicate until"); return kSyntaxError; } vlayout[layout_index].index_then_or_unti_or_repeat = i; @@ -464,12 +464,12 @@ RetValue program::preprocess(void) { } else if (k->value == "repeat") { if (layout_index < 0 || !vlayout[layout_index].is_while_repeat) { // error: show it - show_syntax_error("missing while"); + ShowSyntaxError("missing while"); return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat != -1) { // error: show it - show_syntax_error("duplicate repeat"); + ShowSyntaxError("duplicate repeat"); return kSyntaxError; } vlayout[layout_index].index_then_or_unti_or_repeat = i; @@ -479,20 +479,20 @@ RetValue program::preprocess(void) { if (layout_index < 0) { // error: show it - show_syntax_error("missing branch instruction before end"); + ShowSyntaxError("missing branch instruction before end"); return kSyntaxError; } else { if (vlayout[layout_index].is_do_unti) { // this end closes a do..unti if (vlayout[layout_index].index_end != -1) { // error: show it - show_syntax_error("duplicate end"); + ShowSyntaxError("duplicate end"); return kSyntaxError; } if (vlayout[layout_index].index_then_or_unti_or_repeat == -1) { // error: show it - show_syntax_error("missing until"); + ShowSyntaxError("missing until"); return kSyntaxError; } @@ -502,7 +502,7 @@ RetValue program::preprocess(void) { // this end closes a while..repeat if (vlayout[layout_index].index_end != -1) { // error: show it - show_syntax_error("duplicate end"); + ShowSyntaxError("duplicate end"); return kSyntaxError; } @@ -510,7 +510,7 @@ RetValue program::preprocess(void) { if (vlayout[layout_index].index_then_or_unti_or_repeat == -1) { // error: show it - show_syntax_error("missing repeat"); + ShowSyntaxError("missing repeat"); return kSyntaxError; } @@ -521,7 +521,7 @@ RetValue program::preprocess(void) { // this end closes an if..then..(else) if (vlayout[layout_index].index_end != -1) { // error: show it - show_syntax_error("duplicate end"); + ShowSyntaxError("duplicate end"); return kSyntaxError; } if (vlayout[layout_index].index_else != -1) { @@ -534,7 +534,7 @@ RetValue program::preprocess(void) { ->arg2 = i; } else { // error: show it - show_syntax_error("missing then"); + ShowSyntaxError("missing then"); return kSyntaxError; } } @@ -546,12 +546,12 @@ RetValue program::preprocess(void) { } if (layout_index >= 0) { // error: show it - show_syntax_error("missing end"); + ShowSyntaxError("missing end"); return kSyntaxError; } if (vstart_index.size() > 0) { // error: show it - show_syntax_error("missing next or step after for or start"); + ShowSyntaxError("missing next or step after for or start"); return kSyntaxError; } return kOk; @@ -563,7 +563,7 @@ RetValue program::preprocess(void) { /// @param prog the program to be filled /// @return RetValue see this type /// -RetValue program::parse(string& entry) { +RetValue program::Parse(string& entry) { static map keywords_map; vector elements; vector errors; @@ -575,7 +575,7 @@ RetValue program::parse(string& entry) { if (!kw.name.empty()) keywords_map[kw.name] = {kw.type, kw.fn}; // separate the entry string - if (lexer(entry, keywords_map, elements, errors)) { + if (Analyse(entry, keywords_map, elements, errors)) { // make objects from parsed elements for (Lexer::SynElement& element : elements) { switch (element.type) { @@ -601,14 +601,14 @@ RetValue program::parse(string& entry) { push_back(new Branch((branch_fn_t)element.fn, element.value)); break; default: - show_error(kUnknownError, "error creating program from entry"); + ShowError(kUnknownError, "error creating program from entry"); break; } if (element.re != nullptr) delete element.re; if (element.im != nullptr) delete element.im; } } else { - for (SynError& err : errors) show_syntax_error(err.err.c_str()); + for (SynError& err : errors) ShowSyntaxError(err.err.c_str()); } return ret; @@ -618,7 +618,7 @@ RetValue program::parse(string& entry) { /// /// @return RetValue see this type /// -RetValue program::show_error() { +RetValue program::ShowError() { RetValue ret; // clang-format off map errorStrings{{kOk, "ok"}, {kUnknownError, "unknown command"}, @@ -650,11 +650,11 @@ RetValue program::show_error() { /// @param context a context string /// @return RetValue see this type /// -RetValue program::show_error(RetValue err, string& context) { +RetValue program::ShowError(RetValue err, string& context) { // record error err_ = err; err_context_ = context; - return show_error(); + return ShowError(); } /// @brief record an error as the last error set and show it @@ -663,11 +663,11 @@ RetValue program::show_error(RetValue err, string& context) { /// @param context a context string /// @return RetValue see this type /// -RetValue program::show_error(RetValue err, const char* context) { +RetValue program::ShowError(RetValue err, const char* context) { // record error err_ = err; err_context_ = context; - return show_error(); + return ShowError(); } /// @brief set the last error as being a syntax error and show it @@ -676,25 +676,25 @@ RetValue program::show_error(RetValue err, const char* context) { /// @param context a context string /// @return RetValue see this type /// -void program::show_syntax_error(const char* context) { +void program::ShowSyntaxError(const char* context) { // record error err_ = kSyntaxError; err_context_ = context; - (void)show_error(); + (void)ShowError(); } /// @brief return the last error set /// /// @return RetValue see this type /// -RetValue program::get_err(void) { return err_; } +RetValue program::GetLastError(void) { return err_; } /// @brief show a stack (show its different objects) /// generally a stack is associated to a running program /// /// @param show_separator whether to show a stack level prefix or not /// -void program::show_stack(bool show_separator) { +void program::ShowStack(bool show_separator) { if (stack_.size() == 1) { cout << stack_[0] << endl; } else { @@ -707,9 +707,9 @@ void program::show_stack(bool show_separator) { /// @brief apply default precision mode and digits /// -void program::apply_default() { +void program::ApplyDefault() { // default float precision, float mode - Number::mode = Number::DEFAULT_MODE; + Number::mode = Number::kDefaultMode; Number::digits = Number::kDefaultDecimalDigits; mpreal::set_default_prec(Number::kMpfrDefaultPrecBits); diff --git a/src/program.hpp b/src/program.hpp index b3dab55..1df080f 100644 --- a/src/program.hpp +++ b/src/program.hpp @@ -29,25 +29,23 @@ class program : public deque, public Lexer { } // parser - RetValue parse(string& entry); - static RetValue get_fn(const char* fn_name, program_fn_t& fn, ObjectType& type); + RetValue Parse(string& entry); // running - RetValue run(); - void stop(); - RetValue preprocess(void); + RetValue Run(); + RetValue Preprocess(); - RetValue show_error(); - RetValue show_error(RetValue err, string& context); - RetValue show_error(RetValue err, const char* context); - void show_syntax_error(const char* context); - RetValue get_err(void); + RetValue ShowError(); + RetValue ShowError(RetValue err, string& context); + RetValue ShowError(RetValue err, const char* context); + void ShowSyntaxError(const char* context); + RetValue GetLastError(void); - void show_stack(bool show_separator = true); + void ShowStack(bool show_separator = true); - static void apply_default(); + static void ApplyDefault(); - static vector& getAutocompletionWords(); + static vector& GetAutocompletionWords(); private: // current error and its context @@ -80,178 +78,177 @@ class program : public deque, public Lexer { //// // branch - size_t rpn_if(Branch& myobj); - size_t rpn_then(Branch& myobj); - size_t rpn_else(Branch& myobj); - size_t rpn_end(Branch& myobj); - size_t rpn_do(Branch& myobj); - size_t rpn_until(Branch& myobj); - void rpn_ift(void); - void rpn_ifte(void); - size_t rpn_while(Branch& myobj); - size_t rpn_repeat(Branch& myobj); - size_t rpn_start(Branch& myobj); - size_t rpn_for(Branch& myobj); - size_t rpn_next(Branch& myobj); - size_t rpn_step(Branch& myobj); + size_t RpnIf(Branch& myobj); + size_t RpnThen(Branch& myobj); + size_t RpnElse(Branch& myobj); + size_t RpnEnd(Branch& myobj); + size_t RpnDo(Branch& myobj); + size_t RpnUntil(Branch& myobj); + void RpnIft(void); + void RpnIfte(void); + size_t RpnWhile(Branch& myobj); + size_t RpnRepeat(Branch& myobj); + size_t RpnStart(Branch& myobj); + size_t RpnFor(Branch& myobj); + size_t RpnNext(Branch& myobj); + size_t RpnStep(Branch& myobj); enum { kStepOut = static_cast(-1), kRtError = static_cast(-2) }; // complex - void rpn_re(); - void rpn_im(); - void rpn_arg(); - void rpn_conj(); - void rpn_r2c(); - void rpn_c2r(); - void rpn_r2p(); - void rpn_p2r(); + void RpnReal(); + void RpnImag(); + void RpnArg(); + void RpnConj(); + void RpnR2c(); + void RpnC2r(); + void RpnR2p(); + void RpnP2r(); // general - void rpn_nop(); - void rpn_good_bye(); - void rpn_help(); - void rpn_std(); - void rpn_fix(); - void rpn_sci(); - void rpn_version(); - void rpn_uname(); - void rpn_history(); - void rpn_type(); - void rpn_default(); - void rpn_precision(); - void rpn_round(); + void RpnNop(); + void RpnQuit(); + void RpnHelp(); + void RpnStd(); + void RpnFix(); + void RpnSci(); + void RpnVersion(); + void RpnUname(); + void RpnHistory(); + void RpnType(); + void RpnDefault(); + void RpnPrecision(); + void RpnRound(); // logs - void rpn_e(void); - void rpn_log10(); - void rpn_alog10(); - void rpn_log2(); - void rpn_alog2(); - void rpn_ln(); - void rpn_exp(); - void rpn_expm(); - void rpn_lnp1(); - void rpn_sinh(); - void rpn_asinh(); - void rpn_cosh(); - void rpn_acosh(); - void rpn_tanh(); - void rpn_atanh(); + void RpnE(void); + void RpnLog10(); + void RpnAlog10(); + void RpnLog2(); + void RpnAlog2(); + void RpnLn(); + void RpnExp(); + void RpnExpm(); + void RpnLnp1(); + void RpnSinh(); + void RpnAsinh(); + void RpnCosh(); + void RpnAcosh(); + void RpnTanh(); + void RpnAtanh(); // program - bool find_variable(string& variable, Object*& obj); - void rpn_eval(void); - int rpn_inprog(Branch& inprog_obj); + bool FindVariable(string& variable, Object*& obj); + void RpnEval(void); + int RpnInprog(Branch& inprog_obj); // real - void rpn_plus(); - void rpn_minus(); - void rpn_mul(); - void do_divide_complexes(); - void rpn_div(); - void rpn_neg(); - void rpn_inv(); - void rpn_purcent(); - void rpn_purcentCH(); - void rpn_power(); - void rpn_squareroot(); - void rpn_square(); - void rpn_modulo(); - void rpn_abs(); - void rpn_hex(); - void rpn_bin(); - void rpn_dec(); - void rpn_base(); - void rpn_fact(); - void rpn_sign(); - void rpn_mant(); - void rpn_xpon(); - void rpn_floor(); - void rpn_ceil(); - void rpn_fp(); - void rpn_ip(); - void rpn_min(); - void rpn_max(); + void RpnPlus(); + void RpnMinus(); + void RpnMul(); + void RpnDiv(); + void RpnNeg(); + void RpnInv(); + void RpnPurcent(); + void RpnPurcentCH(); + void RpnPower(); + void RpnSquareroot(); + void RpnSquare(); + void RpnModulo(); + void RpnAbs(); + void RpnHex(); + void RpnBin(); + void RpnDec(); + void RpnBase(); + void RpnFact(); + void RpnSign(); + void RpnMant(); + void RpnXpon(); + void RpnFloor(); + void RpnCeil(); + void RpnFp(); + void RpnIp(); + void RpnMin(); + void RpnMax(); // stack - void rpn_swap(void); - void rpn_drop(void); - void rpn_drop2(void); - void rpn_dropn(void); - void rpn_erase(void); - void rpn_dup(void); - void rpn_dup2(void); - void rpn_dupn(void); - void rpn_pick(void); - void rpn_rot(void); - void rpn_depth(void); - void rpn_roll(void); - void rpn_rolld(void); - void rpn_over(void); + void RpnSwap(void); + void RpnDrop(void); + void RpnDrop2(void); + void RpnDropn(void); + void RpnErase(void); + void RpnDup(void); + void RpnDup2(void); + void RpnDupn(void); + void RpnPick(void); + void RpnRot(void); + void RpnDepth(void); + void RpnRoll(void); + void RpnRolld(void); + void RpnOver(void); // store - void rpn_sto(void); - void rpn_stoadd(void); - void rpn_stosub(void); - void rpn_stomul(void); - void rpn_stodiv(void); - void rpn_stoneg(void); - void rpn_stoinv(void); - void rpn_rcl(void); - void rpn_edit(void); - void auto_rcl(Symbol* symb); - void rpn_purge(void); - void rpn_vars(void); - void rpn_clusr(void); + void RpnSto(void); + void RpnStoadd(void); + void RpnStosub(void); + void RpnStomul(void); + void RpnStodiv(void); + void RpnStoneg(void); + void RpnStoinv(void); + void RpnRcl(void); + void RpnEdit(void); + void AutoRcl(Symbol* symb); + void RpnPurge(void); + void RpnVars(void); + void RpnClusr(void); // string - void rpn_instr(); - void rpn_strout(); - void rpn_chr(); - void rpn_num(); - void rpn_strsize(); - void rpn_strpos(); - void rpn_strsub(); + void RpnInstr(); + void RpnStrout(); + void RpnChr(); + void RpnNum(); + void RpnStrsize(); + void RpnStrpos(); + void RpnStrsub(); // test-core - void rpn_test(); - void test(string test_filename, int& total_tests, int& total_tests_failed, int& total_steps, + void RpnTest(); + void RunTestFile(string test_filename, int& total_tests, int& total_tests_failed, int& total_steps, int& total_steps_failed); // test - void rpn_sup(void); - void rpn_sup_eq(void); - void rpn_inf(void); - void rpn_inf_eq(void); - void rpn_diff(void); - void rpn_eq(void); - void rpn_test_and(void); - void rpn_test_or(void); - void rpn_test_xor(void); - void rpn_test_not(void); - void rpn_same(void); + void RpnSup(void); + void RpnSupEq(void); + void RpnInf(void); + void RpnInfEq(void); + void RpnDiff(void); + void RpnEq(void); + void RpnTestAnd(void); + void RpnTestOr(void); + void RpnTestXor(void); + void RpnTestNot(void); + void RpnSame(void); // trig - void rpn_pi(void); - void rpn_d2r(void); - void rpn_r2d(void); - void rpn_sin(void); - void rpn_asin(void); - void rpn_cos(void); - void rpn_acos(void); - void rpn_tan(void); - void rpn_atan(void); + void RpnPi(void); + void RpnD2r(void); + void RpnR2d(void); + void RpnSin(void); + void RpnAsin(void); + void RpnCos(void); + void RpnAcos(void); + void RpnTan(void); + void RpnAtan(void); // time - void rpn_time(); - void rpn_date(); - void rpn_ticks(); + void RpnTime(); + void RpnDate(); + void RpnTicks(); }; // convenience macros for rpn_xx function // carefull : some of these macros modify program flow -#define setErrorContext(err) \ +#define ERROR_CONTEXT(err) \ do { \ err_ = (err); \ err_context_ = __FUNCTION__; \ @@ -260,7 +257,7 @@ class program : public deque, public Lexer { #define MIN_ARGUMENTS(num) \ do { \ if ((num) >= 0 && stack_.size() < (num)) { \ - setErrorContext(kMissingOperand); \ + ERROR_CONTEXT(kMissingOperand); \ return; \ } \ } while (0) @@ -268,7 +265,7 @@ class program : public deque, public Lexer { #define MIN_ARGUMENTS_RET(num, ret) \ do { \ if ((num) >= 0 && stack_.size() < (num)) { \ - setErrorContext(kMissingOperand); \ + ERROR_CONTEXT(kMissingOperand); \ return (ret); \ } \ } while (0) @@ -276,7 +273,7 @@ class program : public deque, public Lexer { #define ARG_MUST_BE_OF_TYPE(num, type) \ do { \ if ((num) >= 0 && stack_.at(num)->_type != (type)) { \ - setErrorContext(kBadOperandType); \ + ERROR_CONTEXT(kBadOperandType); \ return; \ } \ } while (0) @@ -284,7 +281,7 @@ class program : public deque, public Lexer { #define ARG_MUST_BE_OF_TYPE_RET(num, type, ret) \ do { \ if ((num) >= 0 && stack_.at(num)->_type != (type)) { \ - setErrorContext(kBadOperandType); \ + ERROR_CONTEXT(kBadOperandType); \ return (ret); \ } \ } while (0) diff --git a/src/rpn-branch.cpp b/src/rpn-branch.cpp index efab5dc..133c2f8 100644 --- a/src/rpn-branch.cpp +++ b/src/rpn-branch.cpp @@ -9,7 +9,7 @@ /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_if(Branch& myobj) { +size_t program::RpnIf(Branch& myobj) { // myobj.arg1 = 'if' condition evaluation value MIN_ARGUMENTS_RET(1, kRtError); ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); @@ -29,7 +29,7 @@ size_t program::rpn_if(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort Branch /// -size_t program::rpn_then(Branch& myobj) { +size_t program::RpnThen(Branch& myobj) { // myobj.arg1 = index of then + 1 // myobj.arg2 = index of else + 1 or end + 1 // myobj.arg3 = index of if @@ -37,7 +37,7 @@ size_t program::rpn_then(Branch& myobj) { // else -> arg2 (= jump to else + 1 or end + 1) Branch* if_cmd; if (myobj.arg3 >= size() || at(myobj.arg3)->_type != kBranch) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } if_cmd = reinterpret_cast(at(myobj.arg3)); @@ -54,7 +54,7 @@ size_t program::rpn_then(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_else(Branch& myobj) { +size_t program::RpnElse(Branch& myobj) { // myobj.arg1 = index of else + 1 // myobj.arg2 = index of end + 1 // myobj.arg3 = index of if @@ -62,7 +62,7 @@ size_t program::rpn_else(Branch& myobj) { // if condition was true -> arg2 (= jump to end + 1) Branch* if_cmd; if (myobj.arg3 >= size() || at(myobj.arg3)->_type != kBranch) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } if_cmd = reinterpret_cast(at(myobj.arg3)); @@ -79,7 +79,7 @@ size_t program::rpn_else(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_end(Branch& myobj) { +size_t program::RpnEnd(Branch& myobj) { size_t ret = kStepOut; // arg1 = index of do+1 in case of do..unti..end @@ -106,7 +106,7 @@ size_t program::rpn_end(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_do(Branch& myobj) { +size_t program::RpnDo(Branch& myobj) { // nothing return kStepOut; } @@ -118,7 +118,7 @@ size_t program::rpn_do(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort Branch /// -size_t program::rpn_until(Branch& myobj) { +size_t program::RpnUntil(Branch& myobj) { // nothing return kStepOut; } @@ -130,7 +130,7 @@ size_t program::rpn_until(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -void program::rpn_ift(void) { +void program::RpnIft(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -149,7 +149,7 @@ void program::rpn_ift(void) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -void program::rpn_ifte(void) { +void program::RpnIfte(void) { MIN_ARGUMENTS(3); ARG_MUST_BE_OF_TYPE(2, kNumber); @@ -170,7 +170,7 @@ void program::rpn_ifte(void) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_while(Branch& myobj) { +size_t program::RpnWhile(Branch& myobj) { // nothing return kStepOut; } @@ -182,7 +182,7 @@ size_t program::rpn_while(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_repeat(Branch& myobj) { +size_t program::RpnRepeat(Branch& myobj) { size_t ret = kStepOut; MIN_ARGUMENTS_RET(1, kRtError); @@ -203,7 +203,7 @@ size_t program::rpn_repeat(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_start(Branch& myobj) { +size_t program::RpnStart(Branch& myobj) { size_t ret = kStepOut; MIN_ARGUMENTS_RET(2, kRtError); @@ -232,7 +232,7 @@ size_t program::rpn_start(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_for(Branch& myobj) { +size_t program::RpnFor(Branch& myobj) { size_t ret; MIN_ARGUMENTS_RET(2, kRtError); @@ -241,7 +241,7 @@ size_t program::rpn_for(Branch& myobj) { Symbol* sym; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kSymbol) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } sym = reinterpret_cast(at(myobj.arg1)); // arg1 = loop variable index @@ -263,7 +263,7 @@ size_t program::rpn_for(Branch& myobj) { delete it->second; local_heap_.erase(it); } - local_heap_[sym->value] = stack_.obj(1).clone(); + local_heap_[sym->value] = stack_.obj(1).Clone(); ret = myobj.arg1 + 1; } @@ -279,12 +279,12 @@ size_t program::rpn_for(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_next(Branch& myobj) { +size_t program::RpnNext(Branch& myobj) { // arg1 = loop variable index // first_index = current point in the loop Branch* start_or_for; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kBranch) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } start_or_for = reinterpret_cast(at(myobj.arg1)); @@ -302,7 +302,7 @@ size_t program::rpn_next(Branch& myobj) { Object* obj; Symbol* var; if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != kSymbol) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } var = reinterpret_cast(at(start_or_for->arg1)); @@ -332,7 +332,7 @@ size_t program::rpn_next(Branch& myobj) { /// @return kStepOut next object to run in the current program is current + 1 /// @return kRtError something went wrong with preprocess, abort branch /// -size_t program::rpn_step(Branch& myobj) { +size_t program::RpnStep(Branch& myobj) { size_t ret; MIN_ARGUMENTS_RET(1, kRtError); ARG_MUST_BE_OF_TYPE_RET(0, kNumber, kRtError); @@ -348,7 +348,7 @@ size_t program::rpn_step(Branch& myobj) { // first_index = current count Branch* start_or_for; if (myobj.arg1 >= size() || at(myobj.arg1)->_type != kBranch) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } start_or_for = reinterpret_cast(at(myobj.arg1)); @@ -367,7 +367,7 @@ size_t program::rpn_step(Branch& myobj) { // for command: increment symbol too if (start_or_for->arg1 >= size() || at(start_or_for->arg1)->_type != kSymbol) { - setErrorContext(kMissingOperand); + ERROR_CONTEXT(kMissingOperand); return kRtError; } var = reinterpret_cast(at(start_or_for->arg1)); diff --git a/src/rpn-complex.cpp b/src/rpn-complex.cpp index e3c24c2..c0b01e5 100644 --- a/src/rpn-complex.cpp +++ b/src/rpn-complex.cpp @@ -5,7 +5,7 @@ /// @brief re keyword implementation /// the result is stacked on current program stack /// -void program::rpn_re() { +void program::RpnReal() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); stack_.push_front(new Number(real(stack_.value(0)))); @@ -15,7 +15,7 @@ void program::rpn_re() { /// @brief im keyword implementation /// the result is stacked on current program stack /// -void program::rpn_im() { +void program::RpnImag() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); stack_.push_front(new Number(imag(stack_.value(0)))); @@ -25,7 +25,7 @@ void program::rpn_im() { /// @brief arg keyword implementation /// the result is stacked on current program stack /// -void program::rpn_arg() { +void program::RpnArg() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); stack_.push_front(new Number(arg(stack_.value(0)))); @@ -35,7 +35,7 @@ void program::rpn_arg() { /// @brief conj keyword implementation /// the result is stacked on current program stack /// -void program::rpn_conj() { +void program::RpnConj() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); stack_.value(0) = conj(stack_.value(0)); @@ -44,7 +44,7 @@ void program::rpn_conj() { /// @brief r2c keyword implementation /// the result is stacked on current program stack /// -void program::rpn_r2c() { +void program::RpnR2c() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -55,7 +55,7 @@ void program::rpn_r2c() { /// @brief c2r keyword implementation /// the result is stacked on current program stack /// -void program::rpn_c2r() { +void program::RpnC2r() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); stack_.push(new Number(real(stack_.value(0)), stack_.obj(0).re_base)); @@ -66,7 +66,7 @@ void program::rpn_c2r() { /// @brief r2p keyword implementation /// the result is stacked on current program stack /// -void program::rpn_r2p() { +void program::RpnR2p() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); mpreal rho = abs(stack_.value(0)); @@ -78,7 +78,7 @@ void program::rpn_r2p() { /// @brief p2r keyword implementation /// the result is stacked on current program stack /// -void program::rpn_p2r() { +void program::RpnP2r() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kComplex); stack_.value(0) = polar(abs(stack_.value(0)), arg(stack_.value(0))); diff --git a/src/rpn-general.cpp b/src/rpn-general.cpp index 23e44d2..abe720e 100644 --- a/src/rpn-general.cpp +++ b/src/rpn-general.cpp @@ -3,9 +3,7 @@ #include #include #include -using std::cout; -using std::string; -using std::pair; +using std::cout, std::string, std::pair; #include "linenoise.h" #include "program.hpp" @@ -34,18 +32,18 @@ static const char _syntax[] = ATTR_BOLD "Syntax" ATTR_OFF ": rpn [command]\nwith /// @brief nop keyword implementation /// -void program::rpn_nop() { +void program::RpnNop() { // nop } /// @brief quit keyword implementation /// -void program::rpn_good_bye() { setErrorContext(kGoodbye); } +void program::RpnQuit() { ERROR_CONTEXT(kGoodbye); } /// @brief nop keyword implementation /// the result is written on stdout /// -void program::rpn_help() { +void program::RpnHelp() { // software name cout << endl << ATTR_BOLD << RPN_UNAME << ATTR_OFF << endl; @@ -106,7 +104,7 @@ static bool check_decimal_digits(int precision) { return precision >= 0; } /// @brief std keyword implementation /// -void program::rpn_std() { +void program::RpnStd() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -118,13 +116,13 @@ void program::rpn_std() { Number::digits = digits; stack_.pop(); } else { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); } } /// @brief fix keyword implementation /// -void program::rpn_fix() { +void program::RpnFix() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -136,13 +134,13 @@ void program::rpn_fix() { Number::digits = digits; stack_.pop(); } else { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); } } /// @brief sci keyword implementation /// -void program::rpn_sci() { +void program::RpnSci() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -154,21 +152,21 @@ void program::rpn_sci() { Number::digits = digits; stack_.pop(); } else { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); } } /// @brief _version keyword implementation /// -void program::rpn_version() { stack_.push_front(new String(RPN_VERSION)); } +void program::RpnVersion() { stack_.push_front(new String(RPN_VERSION)); } /// @brief _uname keyword implementation /// -void program::rpn_uname() { stack_.push_front(new String(RPN_UNAME)); } +void program::RpnUname() { stack_.push_front(new String(RPN_UNAME)); } /// @brief history keyword implementation /// -void program::rpn_history() { +void program::RpnHistory() { // see command history on stdout int index = 0; char* line = linenoiseHistoryLine(index); @@ -181,19 +179,19 @@ void program::rpn_history() { /// @brief type keyword implementation /// -void program::rpn_type() { +void program::RpnType() { MIN_ARGUMENTS(1); - stack_.push(new String(stack_.at(0)->name())); + stack_.push(new String(stack_.at(0)->Name())); stack_.erase(1); } /// @brief default keyword implementation /// -void program::rpn_default() { program::apply_default(); } +void program::RpnDefault() { program::ApplyDefault(); } /// @brief prec keyword implementation /// -void program::rpn_precision() { +void program::RpnPrecision() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -209,13 +207,13 @@ void program::rpn_precision() { } stack_.pop(); } else { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); } } /// @brief round keyword implementation /// -void program::rpn_round() { +void program::RpnRound() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); @@ -225,6 +223,6 @@ void program::rpn_round() { if (found != matchRound.end()) mpreal::set_default_rnd(found->second); else - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); stack_.pop(); } diff --git a/src/rpn-logs.cpp b/src/rpn-logs.cpp index 224ec5b..1568dd8 100644 --- a/src/rpn-logs.cpp +++ b/src/rpn-logs.cpp @@ -4,172 +4,172 @@ /// @brief e keyword implementation /// -void program::rpn_e(void) { stack_.push(new Number(mpfr::const_euler())); } +void program::RpnE(void) { stack_.push(new Number(mpfr::const_euler())); } /// @brief log10 keyword implementation /// -void program::rpn_log10() { +void program::RpnLog10() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = log10(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = log10(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief alog10 keyword implementation /// -void program::rpn_alog10() { +void program::RpnAlog10() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = exp(log(mpreal(10)) * stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = exp(log(mpreal(10)) * stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief log2 keyword implementation /// -void program::rpn_log2() { +void program::RpnLog2() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = log(stack_.value(0)) / mpfr::const_log2(); else if (stack_.type(0) == kComplex) stack_.value(0) = log(stack_.value(0)) / mpfr::const_log2(); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief alog2 keyword implementation /// -void program::rpn_alog2() { +void program::RpnAlog2() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = exp(mpfr::const_log2() * stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = exp(mpfr::const_log2() * stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief ln keyword implementation /// -void program::rpn_ln() { +void program::RpnLn() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = log(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = log(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief exp keyword implementation /// -void program::rpn_exp() { +void program::RpnExp() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = exp(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = exp(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief expm keyword implementation /// -void program::rpn_expm() { +void program::RpnExpm() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = exp(stack_.value(0)) - mpreal(1); else if (stack_.type(0) == kComplex) stack_.value(0) = exp(stack_.value(0)) - mpreal(1); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief lnp1 keyword implementation /// -void program::rpn_lnp1() { +void program::RpnLnp1() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = log(stack_.value(0) + 1); else if (stack_.type(0) == kComplex) stack_.value(0) = log(stack_.value(0) + mpreal(1)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief sinh keyword implementation /// -void program::rpn_sinh() { +void program::RpnSinh() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = sinh(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = sinh(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief asinh keyword implementation /// -void program::rpn_asinh() { +void program::RpnAsinh() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = asinh(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = asinh(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief cosh keyword implementation /// -void program::rpn_cosh() { +void program::RpnCosh() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = cosh(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = cosh(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief acosh keyword implementation /// -void program::rpn_acosh() { +void program::RpnAcosh() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = acosh(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = acosh(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief tanh keyword implementation /// -void program::rpn_tanh() { +void program::RpnTanh() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = tanh(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = tanh(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief atanh keyword implementation /// -void program::rpn_atanh() { +void program::RpnAtanh() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = atanh(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = atanh(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } diff --git a/src/rpn-program.cpp b/src/rpn-program.cpp index 93e1ab0..7bc7006 100644 --- a/src/rpn-program.cpp +++ b/src/rpn-program.cpp @@ -9,7 +9,7 @@ /// @return true variable was found /// @return false variable was not found /// -bool program::find_variable(string& variable, Object*& obj) { +bool program::FindVariable(string& variable, Object*& obj) { bool found = false; program* parent = parent_; @@ -33,7 +33,7 @@ bool program::find_variable(string& variable, Object*& obj) { /// @brief eval keyword implementation /// -void program::rpn_eval(void) { +void program::RpnEval(void) { bool run_prog = false; string prog_text; @@ -45,7 +45,7 @@ void program::rpn_eval(void) { stack_.pop(); // if variable holds a program, run this program - if (find_variable(variable, obj)) { + if (FindVariable(variable, obj)) { if (obj->_type == kProgram) { prog_text = stack_.value(0); stack_.pop(); @@ -55,7 +55,7 @@ void program::rpn_eval(void) { stack_.push_front(obj); } } else { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); } } else if (stack_.type(0) == kProgram) { // eval a program @@ -63,7 +63,7 @@ void program::rpn_eval(void) { stack_.pop(); run_prog = true; } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } // run prog if any @@ -71,22 +71,22 @@ void program::rpn_eval(void) { program prog(stack_, heap_, this); // make program from entry - if (prog.parse(prog_text) == kOk) { + if (prog.Parse(prog_text) == kOk) { // run it - prog.run(); + prog.Run(); } } } /// @brief -> keyword (Branch) implementation /// -int program::rpn_inprog(Branch& inprog_obj) { +int program::RpnInprog(Branch& inprog_obj) { string context("->"); // for showing errors int count_symbols = 0; bool prog_found = false; if (inprog_obj.arg1 == -1) { - setErrorContext(kUnknownError); + ERROR_CONTEXT(kUnknownError); return -1; } @@ -104,36 +104,36 @@ int program::rpn_inprog(Branch& inprog_obj) { break; } else { // found something other than symbol - setErrorContext(kBadOperandType); - show_error(err_, context); + ERROR_CONTEXT(kBadOperandType); + ShowError(err_, context); return -1; } } // found 0 symbols if (count_symbols == 0) { - setErrorContext(kSyntaxError); - show_error(err_, context); + ERROR_CONTEXT(kSyntaxError); + ShowError(err_, context); return -1; } // is missing if (!prog_found) { - setErrorContext(kSyntaxError); - show_error(err_, context); + ERROR_CONTEXT(kSyntaxError); + ShowError(err_, context); return -1; } // check symbols Number vs stack_ size if (stack_.size() < count_symbols) { - setErrorContext(kMissingOperand); - show_error(err_, context); + ERROR_CONTEXT(kMissingOperand); + ShowError(err_, context); return -1; } // load variables for (unsigned int i = inprog_obj.arg1 + count_symbols; i > inprog_obj.arg1; i--) { - local_heap_[reinterpret_cast(at(i))->value] = stack_.at(0)->clone(); + local_heap_[reinterpret_cast(at(i))->value] = stack_.at(0)->Clone(); stack_.pop(); } @@ -142,9 +142,9 @@ int program::rpn_inprog(Branch& inprog_obj) { program prog(stack_, heap_, this); // make the program from entry - if (prog.parse(entry) == kOk) { + if (prog.Parse(entry) == kOk) { // run it - prog.run(); + prog.Run(); } // point on next command diff --git a/src/rpn-real.cpp b/src/rpn-real.cpp index 17b0eef..53750c3 100644 --- a/src/rpn-real.cpp +++ b/src/rpn-real.cpp @@ -4,7 +4,7 @@ /// @brief + keyword implementation /// -void program::rpn_plus() { +void program::RpnPlus() { MIN_ARGUMENTS(2); if (stack_.type(0) == kString && stack_.type(1) == kString) { stack_.value(1) += stack_.value(0); @@ -19,17 +19,17 @@ void program::rpn_plus() { stack_.value(1) += stack_.value(0); stack_.pop(); } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { - rpn_swap(); + RpnSwap(); stack_.value(1) += stack_.value(0); stack_.pop(); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief - keyword implementation /// -void program::rpn_minus() { +void program::RpnMinus() { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.value(1) -= stack_.value(0); @@ -41,17 +41,17 @@ void program::rpn_minus() { stack_.value(1) -= stack_.value(0); stack_.pop(); } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { - rpn_swap(); + RpnSwap(); stack_.value(1) = stack_.value(0) - stack_.value(1); stack_.pop(); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief * keyword implementation /// -void program::rpn_mul() { +void program::RpnMul() { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.value(1) *= stack_.value(0); @@ -63,17 +63,17 @@ void program::rpn_mul() { stack_.value(1) *= stack_.value(0); stack_.pop(); } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { - rpn_swap(); + RpnSwap(); stack_.value(1) *= stack_.value(0); stack_.pop(); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief / keyword implementation /// -void program::rpn_div() { +void program::RpnDiv() { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.value(1) /= stack_.value(0); @@ -85,41 +85,41 @@ void program::rpn_div() { stack_.value(1) /= stack_.value(0); stack_.pop(); } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { - rpn_swap(); + RpnSwap(); stack_.value(1) = stack_.value(0) / stack_.value(1); stack_.pop(); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief neg keyword implementation /// -void program::rpn_neg() { +void program::RpnNeg() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = -stack_.value(0); else if (stack_.type(0) == kComplex) stack_.value(0) = -stack_.value(0); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief inv keyword implementation /// -void program::rpn_inv() { +void program::RpnInv() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = 1 / stack_.value(0); else if (stack_.type(0) == kComplex) stack_.value(0) = mpreal(1) / stack_.value(0); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief power keyword implementation /// -void program::rpn_power() { +void program::RpnPower() { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { if (stack_.value(1) >= 0) { @@ -138,17 +138,17 @@ void program::rpn_power() { stack_.value(1) = pow(stack_.value(1), stack_.value(0)); stack_.pop(); } else if (stack_.type(0) == kComplex && stack_.type(1) == kNumber) { - rpn_swap(); + RpnSwap(); stack_.value(1) = pow(stack_.value(0), stack_.value(1)); stack_.pop(); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief sqrt keyword implementation /// -void program::rpn_squareroot() { +void program::RpnSquareroot() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) { if (stack_.value(0) >= 0) { @@ -164,13 +164,13 @@ void program::rpn_squareroot() { } else if (stack_.type(0) == kComplex) { stack_.value(0) = sqrt(stack_.value(0)); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief hex keyword implementation /// -void program::rpn_hex() { +void program::RpnHex() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) { stack_.obj(0).base = 16; @@ -178,13 +178,13 @@ void program::rpn_hex() { stack_.obj(0).re_base = 16; stack_.obj(0).im_base = 16; } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief bin keyword implementation /// -void program::rpn_bin() { +void program::RpnBin() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) { stack_.obj(0).base = 2; @@ -192,13 +192,13 @@ void program::rpn_bin() { stack_.obj(0).re_base = 2; stack_.obj(0).im_base = 2; } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief dec keyword implementation /// -void program::rpn_dec() { +void program::RpnDec() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) { stack_.obj(0).base = 10; @@ -206,13 +206,13 @@ void program::rpn_dec() { stack_.obj(0).re_base = 10; stack_.obj(0).im_base = 10; } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief base keyword implementation /// -void program::rpn_base() { +void program::RpnBase() { MIN_ARGUMENTS(2); if (stack_.type(1) == kNumber || stack_.type(1) == kComplex) { int base = static_cast(stack_.value(0).toLong()); @@ -225,16 +225,16 @@ void program::rpn_base() { stack_.obj(0).im_base = base; } } else { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); } } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief % (purcent) keyword implementation /// -void program::rpn_purcent() { +void program::RpnPurcent() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -244,7 +244,7 @@ void program::rpn_purcent() { /// @brief %CH keyword implementation /// -void program::rpn_purcentCH() { +void program::RpnPurcentCH() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -254,19 +254,19 @@ void program::rpn_purcentCH() { /// @brief sq keyword implementation /// -void program::rpn_square() { +void program::RpnSquare() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) *= stack_.value(0); else if (stack_.at(0)->_type == kComplex) stack_.value(0) *= stack_.value(0); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief mod keyword implementation /// -void program::rpn_modulo() { +void program::RpnModulo() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -276,7 +276,7 @@ void program::rpn_modulo() { /// @brief abs keyword implementation /// -void program::rpn_abs() { +void program::RpnAbs() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) { stack_.value(0) = abs(stack_.value(0)); @@ -284,13 +284,13 @@ void program::rpn_abs() { stack_.push(new Number(abs(stack_.value(0)))); stack_.erase(1); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief fact (factorial) keyword implementation /// -void program::rpn_fact() { +void program::RpnFact() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); // fact(n) = gamma(n+1) @@ -299,23 +299,23 @@ void program::rpn_fact() { /// @brief sign keyword implementation /// -void program::rpn_sign() { +void program::RpnSign() { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = sgn(stack_.value(0)); else if (stack_.at(0)->_type == kComplex) stack_.value(0) = stack_.value(0) / abs(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief mant keyword implementation /// -void program::rpn_mant() { +void program::RpnMant() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); if (!isfinite(stack_.value(0))) { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); return; } mp_exp_t exp; @@ -324,11 +324,11 @@ void program::rpn_mant() { /// @brief xpon keyword implementation /// -void program::rpn_xpon() { +void program::RpnXpon() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); if (!isfinite(stack_.value(0))) { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); return; } mp_exp_t exp; @@ -338,7 +338,7 @@ void program::rpn_xpon() { /// @brief floor keyword implementation /// -void program::rpn_floor() { +void program::RpnFloor() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); stack_.value(0) = floor(stack_.value(0)); @@ -346,7 +346,7 @@ void program::rpn_floor() { /// @brief ceil keyword implementation /// -void program::rpn_ceil() { +void program::RpnCeil() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); stack_.value(0) = ceil(stack_.value(0)); @@ -354,7 +354,7 @@ void program::rpn_ceil() { /// @brief fp keyword implementation /// -void program::rpn_fp() { +void program::RpnFp() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); stack_.value(0) = frac(stack_.value(0)); @@ -362,7 +362,7 @@ void program::rpn_fp() { /// @brief ip keyword implementation /// -void program::rpn_ip() { +void program::RpnIp() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); stack_.value(0) = trunc(stack_.value(0)); @@ -370,7 +370,7 @@ void program::rpn_ip() { /// @brief min keyword implementation /// -void program::rpn_min() { +void program::RpnMin() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -380,7 +380,7 @@ void program::rpn_min() { /// @brief max keyword implementation /// -void program::rpn_max() { +void program::RpnMax() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); diff --git a/src/rpn-stack.cpp b/src/rpn-stack.cpp index 66e2247..f4e3d1d 100644 --- a/src/rpn-stack.cpp +++ b/src/rpn-stack.cpp @@ -4,7 +4,7 @@ /// @brief swap keyword implementation /// -void program::rpn_swap(void) { +void program::RpnSwap(void) { MIN_ARGUMENTS(2); Object* tmp = stack_.front(); stack_.erase(0, 1, false); @@ -13,21 +13,21 @@ void program::rpn_swap(void) { /// @brief drop keyword implementation /// -void program::rpn_drop(void) { +void program::RpnDrop(void) { MIN_ARGUMENTS(1); stack_.pop(); } /// @brief drop2 keyword implementation /// -void program::rpn_drop2(void) { +void program::RpnDrop2(void) { MIN_ARGUMENTS(2); stack_.erase(0, 2); } /// @brief dropn keyword implementation /// -void program::rpn_dropn(void) { +void program::RpnDropn(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -38,18 +38,18 @@ void program::rpn_dropn(void) { /// @brief erase / del keyword implementation /// -void program::rpn_erase(void) { stack_.erase(0, stack_.size()); } +void program::RpnErase(void) { stack_.erase(0, stack_.size()); } /// @brief dup keyword implementation /// -void program::rpn_dup(void) { +void program::RpnDup(void) { MIN_ARGUMENTS(1); - stack_.push_front(stack_.at(0)->clone()); + stack_.push_front(stack_.at(0)->Clone()); } /// @brief dupn keyword implementation /// -void program::rpn_dupn(void) { +void program::RpnDupn(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -57,20 +57,20 @@ void program::rpn_dupn(void) { stack_.pop(); MIN_ARGUMENTS(args); - for (int i = 0; i < args; i++) stack_.push_front(stack_.at(args - 1)->clone()); + for (int i = 0; i < args; i++) stack_.push_front(stack_.at(args - 1)->Clone()); } /// @brief dup2 keyword implementation /// -void program::rpn_dup2(void) { +void program::RpnDup2(void) { MIN_ARGUMENTS(2); - stack_.push_front(stack_.at(1)->clone()); - stack_.push_front(stack_.at(1)->clone()); + stack_.push_front(stack_.at(1)->Clone()); + stack_.push_front(stack_.at(1)->Clone()); } /// @brief pick keyword implementation /// -void program::rpn_pick(void) { +void program::RpnPick(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -79,16 +79,16 @@ void program::rpn_pick(void) { // treat stack_ depth errors if ((to_pick == 0) || (to_pick > stack_.size())) { - setErrorContext(kOutOfRange); + ERROR_CONTEXT(kOutOfRange); return; } - stack_.push_front(stack_.at(to_pick - 1)->clone()); + stack_.push_front(stack_.at(to_pick - 1)->Clone()); } /// @brief rot keyword implementation /// -void program::rpn_rot(void) { +void program::RpnRot(void) { MIN_ARGUMENTS(3); Object* tmp = stack_.at(2); stack_.erase(2, 1, false); @@ -97,11 +97,11 @@ void program::rpn_rot(void) { /// @brief depth keyword implementation /// -void program::rpn_depth(void) { stack_.push_front(new Number(stack_.size())); } +void program::RpnDepth(void) { stack_.push_front(new Number(stack_.size())); } /// @brief roll keyword implementation /// -void program::rpn_roll(void) { +void program::RpnRoll(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -116,7 +116,7 @@ void program::rpn_roll(void) { /// @brief rolld keyword implementation /// -void program::rpn_rolld(void) { +void program::RpnRolld(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -131,7 +131,7 @@ void program::rpn_rolld(void) { /// @brief over keyword implementation /// -void program::rpn_over(void) { +void program::RpnOver(void) { MIN_ARGUMENTS(2); - stack_.push_front(stack_.at(1)->clone()); + stack_.push_front(stack_.at(1)->Clone()); } diff --git a/src/rpn-store.cpp b/src/rpn-store.cpp index c9ea63d..e387703 100644 --- a/src/rpn-store.cpp +++ b/src/rpn-store.cpp @@ -5,7 +5,7 @@ /// @brief sto keyword implementation /// -void program::rpn_sto(void) { +void program::RpnSto(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); @@ -15,113 +15,113 @@ void program::rpn_sto(void) { delete it->second; heap_.erase(it); } - heap_[stack_.value(0)] = stack_.at(1)->clone(); + heap_[stack_.value(0)] = stack_.at(1)->Clone(); stack_.erase(0, 2); } /// @brief sto+ keyword implementation /// -void program::rpn_stoadd(void) { +void program::RpnStoadd(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); if (heap_.find(stack_.value(0)) == heap_.end()) { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); return; } - rpn_dup(); - rpn_rcl(); // TODO(louis) is rcl the good one? it will recall local variables too - rpn_rot(); - rpn_plus(); - rpn_swap(); - rpn_sto(); + RpnDup(); + RpnRcl(); // TODO(louis) is rcl the good one? it will recall local variables too + RpnRot(); + RpnPlus(); + RpnSwap(); + RpnSto(); } /// @brief sto- keyword implementation /// -void program::rpn_stosub(void) { +void program::RpnStosub(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); if (heap_.find(stack_.value(0)) == heap_.end()) { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); return; } - rpn_dup(); - rpn_rcl(); - rpn_rot(); - rpn_minus(); - rpn_swap(); - rpn_sto(); + RpnDup(); + RpnRcl(); + RpnRot(); + RpnMinus(); + RpnSwap(); + RpnSto(); } /// @brief sto* keyword implementation /// -void program::rpn_stomul(void) { +void program::RpnStomul(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); if (heap_.find(stack_.value(0)) == heap_.end()) { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); return; } - rpn_dup(); - rpn_rcl(); - rpn_rot(); - rpn_mul(); - rpn_swap(); - rpn_sto(); + RpnDup(); + RpnRcl(); + RpnRot(); + RpnMul(); + RpnSwap(); + RpnSto(); } /// @brief sto/ keyword implementation /// -void program::rpn_stodiv(void) { +void program::RpnStodiv(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kSymbol); if (heap_.find(stack_.value(0)) == heap_.end()) { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); return; } - rpn_dup(); - rpn_rcl(); - rpn_rot(); - rpn_div(); - rpn_swap(); - rpn_sto(); + RpnDup(); + RpnRcl(); + RpnRot(); + RpnDiv(); + RpnSwap(); + RpnSto(); } /// @brief stosneg keyword implementation /// -void program::rpn_stoneg(void) { +void program::RpnStoneg(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); if (heap_.find(stack_.value(0)) == heap_.end()) { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); return; } - rpn_dup(); - rpn_rcl(); - rpn_neg(); - rpn_swap(); - rpn_sto(); + RpnDup(); + RpnRcl(); + RpnNeg(); + RpnSwap(); + RpnSto(); } /// @brief sinv keyword implementation /// -void program::rpn_stoinv(void) { +void program::RpnStoinv(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); if (heap_.find(stack_.value(0)) == heap_.end()) { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); return; } - rpn_dup(); - rpn_rcl(); - rpn_inv(); - rpn_swap(); - rpn_sto(); + RpnDup(); + RpnRcl(); + RpnInv(); + RpnSwap(); + RpnSto(); } /// @brief rcl keyword implementation /// -void program::rpn_rcl(void) { +void program::RpnRcl(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); @@ -130,53 +130,53 @@ void program::rpn_rcl(void) { string variable(stack_.value(0)); // mind the order of heaps - if (find_variable(variable, obj)) { + if (FindVariable(variable, obj)) { (void)stack_.pop(); - stack_.push_front(obj->clone()); + stack_.push_front(obj->Clone()); } else { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); } } /// @brief edit keyword implementation /// -void program::rpn_edit(void) { +void program::RpnEdit(void) { MIN_ARGUMENTS(1); ostringstream st; // re-write stack_ objet in a stream - stack_.at(0)->show(st); + stack_.at(0)->Show(st); stack_.pop(); // set it as the linenoise line entry - Input::preload(st.str().c_str()); + Input::Preload(st.str().c_str()); } /// @brief recall then eval a symbol variable if it is auto-evaluable /// /// @param symb the smlbol to recall and autoeval /// -void program::auto_rcl(Symbol* symb) { +void program::AutoRcl(Symbol* symb) { if (symb->auto_eval) { Object* obj; string variable(symb->value); // mind the order of heaps - if (find_variable(variable, obj)) { - stack_.push_front(obj->clone()); - if (obj->_type == kProgram) rpn_eval(); + if (FindVariable(variable, obj)) { + stack_.push_front(obj->Clone()); + if (obj->_type == kProgram) RpnEval(); } else { - stack_.push_front(symb->clone()); + stack_.push_front(symb->Clone()); } } else { - stack_.push_front(symb->clone()); + stack_.push_front(symb->Clone()); } } /// @brief purge keyword implementation /// -void program::rpn_purge(void) { +void program::RpnPurge(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kSymbol); @@ -185,14 +185,14 @@ void program::rpn_purge(void) { delete i->second; heap_.erase(i); } else { - setErrorContext(kUnknownVariable); + ERROR_CONTEXT(kUnknownVariable); } stack_.pop(); } /// @brief vars keyword implementation /// -void program::rpn_vars(void) { +void program::RpnVars(void) { Object* obj; program* parent = parent_; string name; @@ -200,21 +200,21 @@ void program::rpn_vars(void) { // heap variables for (auto i : heap_) { - cout << "var " << index++ << ": name '" << i.first << "', type " << i.second->name() << ", value "; - i.second->show(cout) << endl; + cout << "var " << index++ << ": name '" << i.first << "', type " << i.second->Name() << ", value "; + i.second->Show(cout) << endl; } // local variables for (auto i : local_heap_) { - cout << "var " << index++ << ": name '" << i.first << "', type " << i.second->name() << ", value "; - i.second->show(cout) << endl; + cout << "var " << index++ << ": name '" << i.first << "', type " << i.second->Name() << ", value "; + i.second->Show(cout) << endl; } // parents local variables while (parent != nullptr) { for (auto i : parent->local_heap_) { - cout << "parent var " << index++ << ": name '" << i.first << "', type " << i.second->name() << ", value "; - obj->show(cout) << endl; + cout << "parent var " << index++ << ": name '" << i.first << "', type " << i.second->Name() << ", value "; + obj->Show(cout) << endl; } parent = parent->parent_; } @@ -222,4 +222,4 @@ void program::rpn_vars(void) { /// @brief clusr keyword implementation /// -void program::rpn_clusr(void) { heap_.clear(); } +void program::RpnClusr(void) { heap_.clear(); } diff --git a/src/rpn-string.cpp b/src/rpn-string.cpp index 4908265..69199cf 100644 --- a/src/rpn-string.cpp +++ b/src/rpn-string.cpp @@ -6,7 +6,7 @@ /// @brief ->str keyword implementation /// -void program::rpn_instr() { +void program::RpnInstr() { MIN_ARGUMENTS(1); // stringify only if not already a string @@ -20,7 +20,7 @@ void program::rpn_instr() { /// @brief str-> keyword implementation /// -void program::rpn_strout() { +void program::RpnStrout() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); @@ -29,14 +29,14 @@ void program::rpn_strout() { stack_.pop(); // make program from string in stack_ level 1 - if (prog.parse(entry) == kOk) + if (prog.Parse(entry) == kOk) // run it - prog.run(); + prog.Run(); } /// @brief chr keyword implementation /// -void program::rpn_chr() { +void program::RpnChr() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); char the_chr = static_cast(stack_.value(0).toLong()); @@ -47,7 +47,7 @@ void program::rpn_chr() { /// @brief num keyword implementation /// -void program::rpn_num() { +void program::RpnNum() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); if (stack_.value(0).size() > 0) @@ -59,7 +59,7 @@ void program::rpn_num() { /// @brief size keyword implementation /// -void program::rpn_strsize() { +void program::RpnStrsize() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); stack_.push_front(new Number(stack_.value(0).size())); @@ -68,7 +68,7 @@ void program::rpn_strsize() { /// @brief pos keyword implementation /// -void program::rpn_strpos() { +void program::RpnStrpos() { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kString); ARG_MUST_BE_OF_TYPE(1, kString); @@ -80,7 +80,7 @@ void program::rpn_strpos() { /// @brief sub keyword implementation /// -void program::rpn_strsub() { +void program::RpnStrsub() { MIN_ARGUMENTS(3); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); diff --git a/src/rpn-test-framework.cpp b/src/rpn-test-framework.cpp index d2a91bb..7e68476 100644 --- a/src/rpn-test-framework.cpp +++ b/src/rpn-test-framework.cpp @@ -11,7 +11,7 @@ static const char FG_RED[] = "\33[31m"; static const char FG_GREEN[] = "\33[32m"; static const char COLOR_OFF[] = "\33[m"; -static void findAndReplaceAll(string& data, string toSearch, string replaceStr) { +static void FindAndReplaceAll(string& data, string toSearch, string replaceStr) { // Get the first occurrence size_t pos = data.find(toSearch); // Repeat till end is reached @@ -28,17 +28,17 @@ static void findAndReplaceAll(string& data, string toSearch, string replaceStr) /// @param stack_is the output string /// @param stk the stack /// -static void getStackAsString(string& stack_is, rpnstack& stk) { +static void GetStackAsString(string& stack_is, rpnstack& stk) { ostringstream st; if (stk.empty()) { stack_is.clear(); return; } - stk[stk.size() - 1]->show(st); + stk[stk.size() - 1]->Show(st); stack_is += st.str(); for (int i = static_cast(stk.size()) - 2; i >= 0; i--) { ostringstream st; - stk[i]->show(st); + stk[i]->Show(st); stack_is += ", " + st.str(); } } @@ -51,7 +51,7 @@ static void getStackAsString(string& stack_is, rpnstack& stk) { /// @param steps steps nb /// @param steps_failed failed steps nb /// -static void testShowResult(string title, int tests, int tests_failed, int steps, int steps_failed) { +static void ShowTestResult(string title, int tests, int tests_failed, int steps, int steps_failed) { if (!title.empty()) cout << title << ": "; cout << "run " << tests << " tests: " << tests - tests_failed << " passed, "; if (tests_failed > 0) cout << FG_RED; @@ -67,7 +67,7 @@ static void testShowResult(string title, int tests, int tests_failed, int steps, /// @brief test keyword implementation /// -void program::rpn_test() { +void program::RpnTest() { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kString); @@ -79,8 +79,8 @@ void program::rpn_test() { string test_filename = stack_.value(0); stack_.pop(); cout << endl << "rpn version is " << RPN_VERSION << endl; - test(test_filename, total_tests, total_tests_failed, total_steps, total_steps_failed); - testShowResult("\nTotal", total_tests, total_tests_failed, total_steps, total_steps_failed); + RunTestFile(test_filename, total_tests, total_tests_failed, total_steps, total_steps_failed); + ShowTestResult("\nTotal", total_tests, total_tests_failed, total_steps, total_steps_failed); // notify to caller that test succeeded or not if (total_tests_failed > 0) { @@ -97,7 +97,7 @@ void program::rpn_test() { /// @param total_steps the total steps nb /// @param total_steps_failed the total failed steps nb /// -void program::test(string test_filename, int& total_tests, int& total_tests_failed, int& total_steps, +void program::RunTestFile(string test_filename, int& total_tests, int& total_tests_failed, int& total_steps, int& total_steps_failed) { const string stack_size("-> stack size should be "); const string stack_value("-> stack should be "); @@ -131,7 +131,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail if (entry.empty()) continue; if (entry.substr(0, 8) == "@include") { - test(entry.substr(9), total_tests, total_tests_failed, total_steps, total_steps_failed); + RunTestFile(entry.substr(9), total_tests, total_tests_failed, total_steps, total_steps_failed); } else if (entry.substr(0, 2) == "# ") { cout << endl << test_filename << ": " << entry.substr(2) << endl; } else if (entry.substr(0, 3) == "## ") { @@ -179,7 +179,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail string stack_should_be = entry.substr(stack_value.size()); string stack_is; - getStackAsString(stack_is, stk); + GetStackAsString(stack_is, stk); if (stack_is != stack_should_be) { // count fail test and step @@ -242,14 +242,14 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail failed = true; } else { // parse entry and run line - findAndReplaceAll(entry, "`", ""); + FindAndReplaceAll(entry, "`", ""); if (!entry.empty()) { program prog(stk, hp); - ret = prog.parse(entry); + ret = prog.Parse(entry); if (ret == kOk) { // run it - (void)prog.run(); - last_err = static_cast(prog.get_err()); + (void)prog.Run(); + last_err = static_cast(prog.GetLastError()); } } } @@ -264,7 +264,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail // conclusion: show and keep for total if (tests != 0) { - testShowResult("", tests, tests_failed, steps, steps_failed); + ShowTestResult("", tests, tests_failed, steps, steps_failed); total_tests += tests; total_tests_failed += tests_failed; diff --git a/src/rpn-test.cpp b/src/rpn-test.cpp index 57df584..2ce25a5 100644 --- a/src/rpn-test.cpp +++ b/src/rpn-test.cpp @@ -17,7 +17,7 @@ static int CmpStringOnStackTop(rpnstack& stk) { /// @brief > keyword implementation /// -void program::rpn_sup(void) { +void program::RpnSup(void) { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.push_front(new Number(stack_.value(1) > stack_.value(0))); @@ -26,13 +26,13 @@ void program::rpn_sup(void) { stack_.push_front(new Number(CmpStringOnStackTop(stack_) == 1)); stack_.erase(1, 2); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief >= keyword implementation /// -void program::rpn_sup_eq(void) { +void program::RpnSupEq(void) { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.push_front(new Number(stack_.value(1) >= stack_.value(0))); @@ -41,13 +41,13 @@ void program::rpn_sup_eq(void) { stack_.push_front(new Number(CmpStringOnStackTop(stack_) != -1)); stack_.erase(1, 2); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief < keyword implementation /// -void program::rpn_inf(void) { +void program::RpnInf(void) { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { @@ -57,13 +57,13 @@ void program::rpn_inf(void) { stack_.push_front(new Number(CmpStringOnStackTop(stack_) == -1)); stack_.erase(1, 2); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief <= keyword implementation /// -void program::rpn_inf_eq(void) { +void program::RpnInfEq(void) { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.push_front(new Number(stack_.value(1) <= stack_.value(0))); @@ -72,13 +72,13 @@ void program::rpn_inf_eq(void) { stack_.push_front(new Number(CmpStringOnStackTop(stack_) != 1)); stack_.erase(1, 2); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief != keyword implementation /// -void program::rpn_diff(void) { +void program::RpnDiff(void) { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.push_front(new Number(stack_.value(1) != stack_.value(0))); @@ -90,13 +90,13 @@ void program::rpn_diff(void) { stack_.push_front(new Number(CmpStringOnStackTop(stack_) != 0)); stack_.erase(1, 2); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief == keyword implementation /// -void program::rpn_eq(void) { +void program::RpnEq(void) { MIN_ARGUMENTS(2); if (stack_.type(0) == kNumber && stack_.type(1) == kNumber) { stack_.push_front(new Number(stack_.value(1) == stack_.value(0))); @@ -108,13 +108,13 @@ void program::rpn_eq(void) { stack_.push_front(new Number(CmpStringOnStackTop(stack_) == 0)); stack_.erase(1, 2); } else { - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } } /// @brief and keyword implementation /// -void program::rpn_test_and(void) { +void program::RpnTestAnd(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -127,7 +127,7 @@ void program::rpn_test_and(void) { /// @brief or keyword implementation /// -void program::rpn_test_or(void) { +void program::RpnTestOr(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -140,7 +140,7 @@ void program::rpn_test_or(void) { /// @brief xor keyword implementation /// -void program::rpn_test_xor(void) { +void program::RpnTestXor(void) { MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, kNumber); ARG_MUST_BE_OF_TYPE(1, kNumber); @@ -153,7 +153,7 @@ void program::rpn_test_xor(void) { /// @brief not keyword implementation /// -void program::rpn_test_not(void) { +void program::RpnTestNot(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); @@ -163,4 +163,4 @@ void program::rpn_test_not(void) { /// @brief test same implementation /// -void program::rpn_same(void) { rpn_eq(); } +void program::RpnSame(void) { RpnEq(); } diff --git a/src/rpn-time.cpp b/src/rpn-time.cpp index 3a8cb1f..84e9e71 100644 --- a/src/rpn-time.cpp +++ b/src/rpn-time.cpp @@ -6,7 +6,7 @@ /// @brief time keyword implementation /// -void program::rpn_time() { +void program::RpnTime() { struct timespec ts; struct tm* tm; double date; @@ -25,13 +25,13 @@ void program::rpn_time() { stack_.push(new Number(date)); stack_.value(0) /= 10000000000.0; } else { - setErrorContext(kInternalError); + ERROR_CONTEXT(kInternalError); } } /// @brief date keyword implementation /// -void program::rpn_date() { +void program::RpnDate() { struct timespec ts; struct tm* tm; double date; @@ -51,13 +51,13 @@ void program::rpn_date() { stack_.push(new Number(date)); stack_.value(0) /= 1000000.0; } else { - setErrorContext(kInternalError); + ERROR_CONTEXT(kInternalError); } } /// @brief ticks keyword implementation /// -void program::rpn_ticks() { +void program::RpnTicks() { struct timespec ts; struct tm* tm; double date; @@ -71,6 +71,6 @@ void program::rpn_ticks() { date = 1000000.0 * static_cast(ts.tv_sec) + static_cast(ts.tv_nsec / 1000); stack_.push(new Number(date)); } else { - setErrorContext(kInternalError); + ERROR_CONTEXT(kInternalError); } } diff --git a/src/rpn-trig.cpp b/src/rpn-trig.cpp index 61c7da8..0fa99f1 100644 --- a/src/rpn-trig.cpp +++ b/src/rpn-trig.cpp @@ -4,13 +4,13 @@ /// @brief pi keyword implementation /// -void program::rpn_pi(void) { +void program::RpnPi(void) { stack_.push_front(new Number(mpfr::const_pi())); } /// @brief d->r keyword implementation /// -void program::rpn_d2r(void) { +void program::RpnD2r(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); stack_.value(0) *= mpfr::const_pi(); @@ -19,7 +19,7 @@ void program::rpn_d2r(void) { /// @brief r->d keyword implementation /// -void program::rpn_r2d(void) { +void program::RpnR2d(void) { MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, kNumber); stack_.value(0) /= mpfr::const_pi(); @@ -28,72 +28,72 @@ void program::rpn_r2d(void) { /// @brief sin keyword implementation /// -void program::rpn_sin(void) { +void program::RpnSin(void) { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = sin(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = sin(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief asin keyword implementation /// -void program::rpn_asin(void) { +void program::RpnAsin(void) { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = asin(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = asin(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief cos keyword implementation /// -void program::rpn_cos(void) { +void program::RpnCos(void) { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = cos(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = cos(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief acos keyword implementation /// -void program::rpn_acos(void) { +void program::RpnAcos(void) { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = acos(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = acos(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief tan keyword implementation /// -void program::rpn_tan(void) { +void program::RpnTan(void) { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = tan(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = tan(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } /// @brief atan keyword implementation /// -void program::rpn_atan(void) { +void program::RpnAtan(void) { MIN_ARGUMENTS(1); if (stack_.type(0) == kNumber) stack_.value(0) = atan(stack_.value(0)); else if (stack_.type(0) == kComplex) stack_.value(0) = atan(stack_.value(0)); else - setErrorContext(kBadOperandType); + ERROR_CONTEXT(kBadOperandType); } diff --git a/src/stack.hpp b/src/stack.hpp index fc69912..abc216e 100644 --- a/src/stack.hpp +++ b/src/stack.hpp @@ -3,11 +3,11 @@ #ifndef SRC_STACK_HPP_ #define SRC_STACK_HPP_ -#include #include #include #include -using namespace std; +#include +using std::deque, std::map, std::string; #include "object.hpp" From 25dc731559e978b560ea4415f2f9083a2be134b9 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 25 Feb 2022 16:13:20 +0100 Subject: [PATCH 6/6] cpp files to cc, hpp files to h --- CMakeLists.txt | 38 +++++++++---------- src/{input.cpp => input.cc} | 2 +- src/{input.hpp => input.h} | 0 src/{lexer.cpp => lexer.cc} | 2 +- src/{lexer.hpp => lexer.h} | 2 +- src/{main.cpp => main.cc} | 4 +- src/{mpreal-out.cpp => mpreal-out.cc} | 2 +- src/{mpreal-out.hpp => mpreal-out.h} | 0 src/{object.cpp => object.cc} | 2 +- src/{object.hpp => object.h} | 8 ++-- src/{program.cpp => program.cc} | 2 +- src/{program.hpp => program.h} | 6 +-- src/{rpn-branch.cpp => rpn-branch.cc} | 2 +- src/{rpn-complex.cpp => rpn-complex.cc} | 2 +- src/{rpn-general.cpp => rpn-general.cc} | 2 +- src/{rpn-logs.cpp => rpn-logs.cc} | 2 +- src/{rpn-program.cpp => rpn-program.cc} | 2 +- src/{rpn-real.cpp => rpn-real.cc} | 2 +- src/{rpn-stack.cpp => rpn-stack.cc} | 2 +- src/{rpn-store.cpp => rpn-store.cc} | 4 +- src/{rpn-string.cpp => rpn-string.cc} | 2 +- ...st-framework.cpp => rpn-test-framework.cc} | 2 +- src/{rpn-test.cpp => rpn-test.cc} | 2 +- src/{rpn-time.cpp => rpn-time.cc} | 2 +- src/{rpn-trig.cpp => rpn-trig.cc} | 2 +- src/{stack.hpp => stack.h} | 2 +- 26 files changed, 49 insertions(+), 49 deletions(-) rename src/{input.cpp => input.cc} (99%) rename src/{input.hpp => input.h} (100%) rename src/{lexer.cpp => lexer.cc} (99%) rename src/{lexer.hpp => lexer.h} (99%) rename src/{main.cpp => main.cc} (98%) rename src/{mpreal-out.cpp => mpreal-out.cc} (99%) rename src/{mpreal-out.hpp => mpreal-out.h} (100%) rename src/{object.cpp => object.cc} (87%) rename src/{object.hpp => object.h} (98%) rename src/{program.cpp => program.cc} (99%) rename src/{program.hpp => program.h} (99%) rename src/{rpn-branch.cpp => rpn-branch.cc} (99%) rename src/{rpn-complex.cpp => rpn-complex.cc} (99%) rename src/{rpn-general.cpp => rpn-general.cc} (99%) rename src/{rpn-logs.cpp => rpn-logs.cc} (99%) rename src/{rpn-program.cpp => rpn-program.cc} (99%) rename src/{rpn-real.cpp => rpn-real.cc} (99%) rename src/{rpn-stack.cpp => rpn-stack.cc} (99%) rename src/{rpn-store.cpp => rpn-store.cc} (99%) rename src/{rpn-string.cpp => rpn-string.cc} (99%) rename src/{rpn-test-framework.cpp => rpn-test-framework.cc} (99%) rename src/{rpn-test.cpp => rpn-test.cc} (99%) rename src/{rpn-time.cpp => rpn-time.cc} (98%) rename src/{rpn-trig.cpp => rpn-trig.cc} (99%) rename src/{stack.hpp => stack.h} (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 801a9d0..2a75c5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,25 +51,25 @@ include_directories(${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/linenoise-ng # build add_executable( rpn - ${PROJECT_SOURCE_DIR}/src/main.cpp - ${PROJECT_SOURCE_DIR}/src/object.cpp - ${PROJECT_SOURCE_DIR}/src/mpreal-out.cpp - ${PROJECT_SOURCE_DIR}/src/program.cpp - ${PROJECT_SOURCE_DIR}/src/lexer.cpp - ${PROJECT_SOURCE_DIR}/src/input.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-branch.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-complex.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-general.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-logs.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-program.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-real.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-stack.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-store.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-string.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-test.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-test-framework.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-time.cpp - ${PROJECT_SOURCE_DIR}/src/rpn-trig.cpp + ${PROJECT_SOURCE_DIR}/src/main.cc + ${PROJECT_SOURCE_DIR}/src/object.cc + ${PROJECT_SOURCE_DIR}/src/mpreal-out.cc + ${PROJECT_SOURCE_DIR}/src/program.cc + ${PROJECT_SOURCE_DIR}/src/lexer.cc + ${PROJECT_SOURCE_DIR}/src/input.cc + ${PROJECT_SOURCE_DIR}/src/rpn-branch.cc + ${PROJECT_SOURCE_DIR}/src/rpn-complex.cc + ${PROJECT_SOURCE_DIR}/src/rpn-general.cc + ${PROJECT_SOURCE_DIR}/src/rpn-logs.cc + ${PROJECT_SOURCE_DIR}/src/rpn-program.cc + ${PROJECT_SOURCE_DIR}/src/rpn-real.cc + ${PROJECT_SOURCE_DIR}/src/rpn-stack.cc + ${PROJECT_SOURCE_DIR}/src/rpn-store.cc + ${PROJECT_SOURCE_DIR}/src/rpn-string.cc + ${PROJECT_SOURCE_DIR}/src/rpn-test.cc + ${PROJECT_SOURCE_DIR}/src/rpn-test-framework.cc + ${PROJECT_SOURCE_DIR}/src/rpn-time.cc + ${PROJECT_SOURCE_DIR}/src/rpn-trig.cc ${PROJECT_SOURCE_DIR}/linenoise-ng/src/ConvertUTF.cpp ${PROJECT_SOURCE_DIR}/linenoise-ng/src/linenoise.cpp ${PROJECT_SOURCE_DIR}/linenoise-ng/src/wcwidth.cpp diff --git a/src/input.cpp b/src/input.cc similarity index 99% rename from src/input.cpp rename to src/input.cc index 7c84f5b..c9c50bf 100644 --- a/src/input.cpp +++ b/src/input.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "input.hpp" +#include "input.h" #include diff --git a/src/input.hpp b/src/input.h similarity index 100% rename from src/input.hpp rename to src/input.h diff --git a/src/lexer.cpp b/src/lexer.cc similarity index 99% rename from src/lexer.cpp rename to src/lexer.cc index 98b4ab8..79cee56 100644 --- a/src/lexer.cpp +++ b/src/lexer.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "lexer.hpp" +#include "lexer.h" bool Lexer::Analyse(string& entry, map& keywords, vector& elements, vector& errors) { diff --git a/src/lexer.hpp b/src/lexer.h similarity index 99% rename from src/lexer.hpp rename to src/lexer.h index 532ee23..e31ba0a 100644 --- a/src/lexer.hpp +++ b/src/lexer.h @@ -11,7 +11,7 @@ using mpfr::mpreal; #include using std::map, std::string, std::vector; -#include "object.hpp" +#include "object.h" class Lexer { public: diff --git a/src/main.cpp b/src/main.cc similarity index 98% rename from src/main.cpp rename to src/main.cc index 9f5d2b5..374b0f9 100644 --- a/src/main.cpp +++ b/src/main.cc @@ -9,8 +9,8 @@ using std::cerr; // internal includes -#include "input.hpp" -#include "program.hpp" +#include "input.h" +#include "program.h" /// @brief actions to be done at rpn exit /// diff --git a/src/mpreal-out.cpp b/src/mpreal-out.cc similarity index 99% rename from src/mpreal-out.cpp rename to src/mpreal-out.cc index 6b71028..dfb5d4b 100644 --- a/src/mpreal-out.cpp +++ b/src/mpreal-out.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "mpreal-out.hpp" +#include "mpreal-out.h" #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #define MAX(h, i) ((h) > (i) ? (h) : (i)) diff --git a/src/mpreal-out.hpp b/src/mpreal-out.h similarity index 100% rename from src/mpreal-out.hpp rename to src/mpreal-out.h diff --git a/src/object.cpp b/src/object.cc similarity index 87% rename from src/object.cpp rename to src/object.cc index 840266e..36deefd 100644 --- a/src/object.cpp +++ b/src/object.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "object.hpp" +#include "object.h" // number statics Number::mode_enum Number::mode = Number::kDefaultMode; diff --git a/src/object.hpp b/src/object.h similarity index 98% rename from src/object.hpp rename to src/object.h index 68eff2e..bf56f39 100644 --- a/src/object.hpp +++ b/src/object.h @@ -1,7 +1,7 @@ // Copyright (c) 2014-2022 Louis Rubet -#ifndef SRC_OBJECT_HPP_ -#define SRC_OBJECT_HPP_ +#ifndef SRC_OBJECT_H_ +#define SRC_OBJECT_H_ #include using mpfr::mpreal; @@ -12,7 +12,7 @@ using mpfr::mpreal; #include using std::complex, std::ostream, std::string, std::stringstream; -#include "mpreal-out.hpp" +#include "mpreal-out.h" // definitions for objects /// @@ -212,4 +212,4 @@ struct Branch : Object { string value; }; -#endif // SRC_OBJECT_HPP_ +#endif // SRC_OBJECT_H_ diff --git a/src/program.cpp b/src/program.cc similarity index 99% rename from src/program.cpp rename to src/program.cc index 0276800..12e5ef4 100644 --- a/src/program.cpp +++ b/src/program.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" //< language reserved keywords (allowed types are kKeyword, kBranch or kUndef) vector program::keywords_{ diff --git a/src/program.hpp b/src/program.h similarity index 99% rename from src/program.hpp rename to src/program.h index 1df080f..cfe5827 100644 --- a/src/program.hpp +++ b/src/program.h @@ -14,9 +14,9 @@ using namespace std; using mpfr::mpreal; // internal includes -#include "lexer.hpp" -#include "object.hpp" -#include "stack.hpp" +#include "lexer.h" +#include "object.h" +#include "stack.h" //< program class: the class containing a string parser, all the programs keywords, a stack for running the program class program : public deque, public Lexer { diff --git a/src/rpn-branch.cpp b/src/rpn-branch.cc similarity index 99% rename from src/rpn-branch.cpp rename to src/rpn-branch.cc index 133c2f8..e72d0cf 100644 --- a/src/rpn-branch.cpp +++ b/src/rpn-branch.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief if keyword (branch) implementation /// diff --git a/src/rpn-complex.cpp b/src/rpn-complex.cc similarity index 99% rename from src/rpn-complex.cpp rename to src/rpn-complex.cc index c0b01e5..55d3aed 100644 --- a/src/rpn-complex.cpp +++ b/src/rpn-complex.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief re keyword implementation /// the result is stacked on current program stack diff --git a/src/rpn-general.cpp b/src/rpn-general.cc similarity index 99% rename from src/rpn-general.cpp rename to src/rpn-general.cc index abe720e..7437f8f 100644 --- a/src/rpn-general.cpp +++ b/src/rpn-general.cc @@ -6,7 +6,7 @@ using std::cout, std::string, std::pair; #include "linenoise.h" -#include "program.hpp" +#include "program.h" #include "version.h" // description diff --git a/src/rpn-logs.cpp b/src/rpn-logs.cc similarity index 99% rename from src/rpn-logs.cpp rename to src/rpn-logs.cc index 1568dd8..379ad79 100644 --- a/src/rpn-logs.cpp +++ b/src/rpn-logs.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief e keyword implementation /// diff --git a/src/rpn-program.cpp b/src/rpn-program.cc similarity index 99% rename from src/rpn-program.cpp rename to src/rpn-program.cc index 7bc7006..c4d0199 100644 --- a/src/rpn-program.cpp +++ b/src/rpn-program.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief find variable by its name in local heap, successive parents heaps, global heap /// diff --git a/src/rpn-real.cpp b/src/rpn-real.cc similarity index 99% rename from src/rpn-real.cpp rename to src/rpn-real.cc index 53750c3..93f9901 100644 --- a/src/rpn-real.cpp +++ b/src/rpn-real.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief + keyword implementation /// diff --git a/src/rpn-stack.cpp b/src/rpn-stack.cc similarity index 99% rename from src/rpn-stack.cpp rename to src/rpn-stack.cc index f4e3d1d..92b2892 100644 --- a/src/rpn-stack.cpp +++ b/src/rpn-stack.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief swap keyword implementation /// diff --git a/src/rpn-store.cpp b/src/rpn-store.cc similarity index 99% rename from src/rpn-store.cpp rename to src/rpn-store.cc index e387703..3e9c316 100644 --- a/src/rpn-store.cpp +++ b/src/rpn-store.cc @@ -1,7 +1,7 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "input.hpp" -#include "program.hpp" +#include "input.h" +#include "program.h" /// @brief sto keyword implementation /// diff --git a/src/rpn-string.cpp b/src/rpn-string.cc similarity index 99% rename from src/rpn-string.cpp rename to src/rpn-string.cc index 69199cf..83b3340 100644 --- a/src/rpn-string.cpp +++ b/src/rpn-string.cc @@ -2,7 +2,7 @@ #include -#include "program.hpp" +#include "program.h" /// @brief ->str keyword implementation /// diff --git a/src/rpn-test-framework.cpp b/src/rpn-test-framework.cc similarity index 99% rename from src/rpn-test-framework.cpp rename to src/rpn-test-framework.cc index 7e68476..f0e4742 100644 --- a/src/rpn-test-framework.cpp +++ b/src/rpn-test-framework.cc @@ -3,7 +3,7 @@ #include using std::string; -#include "program.hpp" +#include "program.h" #include "version.h" // foreground colors diff --git a/src/rpn-test.cpp b/src/rpn-test.cc similarity index 99% rename from src/rpn-test.cpp rename to src/rpn-test.cc index 2ce25a5..fe610b0 100644 --- a/src/rpn-test.cpp +++ b/src/rpn-test.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief compared 2 strings on top of the stack_ /// diff --git a/src/rpn-time.cpp b/src/rpn-time.cc similarity index 98% rename from src/rpn-time.cpp rename to src/rpn-time.cc index 84e9e71..f763dba 100644 --- a/src/rpn-time.cpp +++ b/src/rpn-time.cc @@ -2,7 +2,7 @@ #include -#include "program.hpp" +#include "program.h" /// @brief time keyword implementation /// diff --git a/src/rpn-trig.cpp b/src/rpn-trig.cc similarity index 99% rename from src/rpn-trig.cpp rename to src/rpn-trig.cc index 0fa99f1..d31ceb3 100644 --- a/src/rpn-trig.cpp +++ b/src/rpn-trig.cc @@ -1,6 +1,6 @@ // Copyright (c) 2014-2022 Louis Rubet -#include "program.hpp" +#include "program.h" /// @brief pi keyword implementation /// diff --git a/src/stack.hpp b/src/stack.h similarity index 98% rename from src/stack.hpp rename to src/stack.h index abc216e..96cf050 100644 --- a/src/stack.hpp +++ b/src/stack.h @@ -9,7 +9,7 @@ #include using std::deque, std::map, std::string; -#include "object.hpp" +#include "object.h" /// @brief stack object, parens of program, storing execution stack values or programs ///