From 14adfd873e4259762c580692b2b725e220fb7ed2 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Fri, 21 Apr 2017 13:58:44 +0200 Subject: [PATCH] #34: refactored stack::pop_back() for easier code --- src/rpn-binary.h | 3 +-- src/rpn-branch.h | 2 +- src/rpn-general.h | 13 ++++--------- src/rpn-program.h | 9 ++++----- src/rpn-real.h | 36 ++++++++++++------------------------ src/rpn-stack.h | 21 ++++++++++----------- src/rpn-store.h | 13 +++++-------- src/rpn-string.h | 6 ++---- src/rpn-test.h | 27 +++++++++------------------ src/stack.h | 8 +++++++- 10 files changed, 55 insertions(+), 83 deletions(-) diff --git a/src/rpn-binary.h b/src/rpn-binary.h index 7b25510..db9e57b 100644 --- a/src/rpn-binary.h +++ b/src/rpn-binary.h @@ -23,8 +23,7 @@ void rtob() MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - number* left = (number*)_stack->back(); - _stack->pop_back(); + number* left = (number*)_stack->pop_back(); binary bin; bin.set(mpfr_get_sj(&left->_value.mpfr, s_mpfr_rnd)); diff --git a/src/rpn-branch.h b/src/rpn-branch.h index 3cddf11..3bde523 100644 --- a/src/rpn-branch.h +++ b/src/rpn-branch.h @@ -10,7 +10,7 @@ int rpn_if(branch& myobj) myobj.arg1 = 1; else myobj.arg1 = 0; - _stack->pop_back(); + (void)_stack->pop_back(); return -1; } diff --git a/src/rpn-general.h b/src/rpn-general.h index 942a885..062e52f 100644 --- a/src/rpn-general.h +++ b/src/rpn-general.h @@ -16,9 +16,7 @@ void verbose() MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int verbose = int(((number*)_stack->back())->_value); - _stack->pop_back(); - + int verbose = int(((number*)_stack->pop_back())->_value); g_verbose = verbose; } @@ -84,8 +82,7 @@ void std() { ARG_MUST_BE_OF_TYPE(0, cmd_number); - int precision = int(((number*)_stack->back())->_value); - _stack->pop_back(); + int precision = int(((number*)_stack->pop_back())->_value); number::s_default_precision = int(precision); } @@ -103,8 +100,7 @@ void fix() MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int precision = int(((number*)_stack->back())->_value); - _stack->pop_back(); + int precision = int(((number*)_stack->pop_back())->_value); number::s_current_precision = int(precision); number::s_mode = number::fix; @@ -120,8 +116,7 @@ void sci() MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - int precision = int(((number*)_stack->back())->_value); - _stack->pop_back(); + int precision = int(((number*)_stack->pop_back())->_value); number::s_current_precision = int(precision); number::s_mode = number::sci; diff --git a/src/rpn-program.h b/src/rpn-program.h index 11e205b..230ed7d 100644 --- a/src/rpn-program.h +++ b/src/rpn-program.h @@ -22,13 +22,13 @@ void eval(void) if (type == cmd_program) { prog_text = ((oprogram*)obj)->_value; - _stack->pop_back(); + (void)_stack->pop_back(); run_prog = true; } else { // else recall variable (i.e. stack its content) - _stack->pop_back(); + (void)_stack->pop_back(); _stack->push_back(obj, size, type); } } @@ -38,8 +38,7 @@ void eval(void) else if (IS_ARG_TYPE(0, cmd_program)) { // eval a program - prog_text = ((oprogram*)_stack->back())->_value; - _stack->pop_back(); + prog_text = ((oprogram*)_stack->pop_back())->_value; run_prog = true; } else @@ -125,7 +124,7 @@ int inprog(branch& myobj) for (unsigned int i = myobj.arg1 + count_symbols; i > myobj.arg1; i--) { local_heap.add(string(((symbol*)seq_obj(i))->_value), _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0)); - _stack->pop_back(); + (void)_stack->pop_back(); } // run the program diff --git a/src/rpn-real.h b/src/rpn-real.h index 56ea95a..5955f83 100644 --- a/src/rpn-real.h +++ b/src/rpn-real.h @@ -7,8 +7,7 @@ void plus() { ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_add(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); } @@ -17,8 +16,7 @@ void plus() { ARG_MUST_BE_OF_TYPE(1, cmd_binary); - binary* right = (binary*)_stack->back(); - _stack->pop_back(); + binary* right = (binary*)_stack->pop_back(); binary* left = (binary*)_stack->back(); left->_value += right->_value; } @@ -35,8 +33,7 @@ void minus() { ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_sub(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); } @@ -45,8 +42,7 @@ void minus() { ARG_MUST_BE_OF_TYPE(1, cmd_binary); - binary* right = (binary*)_stack->back(); - _stack->pop_back(); + binary* right = (binary*)_stack->pop_back(); binary* left = (binary*)_stack->back(); left->_value -= right->_value; } @@ -61,8 +57,7 @@ void mul() { ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); } @@ -71,8 +66,7 @@ void mul() { ARG_MUST_BE_OF_TYPE(1, cmd_binary); - binary* right = (binary*)_stack->back(); - _stack->pop_back(); + binary* right = (binary*)_stack->pop_back(); binary* left = (binary*)_stack->back(); left->_value *= right->_value; } @@ -89,8 +83,7 @@ void div() { ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_div(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); } @@ -104,8 +97,7 @@ void div() } else { - binary* right = (binary*)_stack->back(); - _stack->pop_back(); + binary* right = (binary*)_stack->pop_back(); binary* left = (binary*)_stack->back(); left->_value /= right->_value; } @@ -146,8 +138,7 @@ void purcent() ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); @@ -160,8 +151,7 @@ void purcentCH() ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_mul_si(&right->_value.mpfr, &right->_value.mpfr, 100L, s_mpfr_rnd)); @@ -174,8 +164,7 @@ void power() ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_pow(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); } @@ -204,8 +193,7 @@ void modulo() ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); CHECK_MPFR(mpfr_fmod(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); diff --git a/src/rpn-stack.h b/src/rpn-stack.h index b17ca1a..15945a1 100644 --- a/src/rpn-stack.h +++ b/src/rpn-stack.h @@ -4,8 +4,8 @@ void swap(void) MIN_ARGUMENTS(2); _stack->copy_obj_to_local(0, 0); _stack->copy_obj_to_local(1, 1); - _stack->pop_back(); - _stack->pop_back(); + (void)_stack->pop_back(); + (void)_stack->pop_back(); _stack->push_obj_from_local(0); _stack->push_obj_from_local(1); } @@ -13,20 +13,20 @@ void swap(void) void drop(void) { MIN_ARGUMENTS(1); - _stack->pop_back(); + (void)_stack->pop_back(); } void drop2(void) { MIN_ARGUMENTS(2); - _stack->pop_back(); - _stack->pop_back(); + (void)_stack->pop_back(); + (void)_stack->pop_back(); } void erase(void) { while(_stack->size()>0) - _stack->pop_back(); + (void)_stack->pop_back(); } void dup(void) @@ -50,8 +50,7 @@ void pick(void) MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_number); - unsigned int to_pick = (unsigned int)int(((number*)_stack->back())->_value); - _stack->pop_back(); + unsigned int to_pick = (unsigned int)int(((number*)_stack->pop_back())->_value); // treat stack depth errors if ((to_pick == 0) || (to_pick > _stack->size())) @@ -69,9 +68,9 @@ void rot(void) _stack->copy_obj_to_local(0, 0); _stack->copy_obj_to_local(1, 1); _stack->copy_obj_to_local(2, 2); - _stack->pop_back(); - _stack->pop_back(); - _stack->pop_back(); + (void)_stack->pop_back(); + (void)_stack->pop_back(); + (void)_stack->pop_back(); _stack->push_obj_from_local(1); _stack->push_obj_from_local(0); _stack->push_obj_from_local(2); diff --git a/src/rpn-store.h b/src/rpn-store.h index 61580d9..171f3ed 100644 --- a/src/rpn-store.h +++ b/src/rpn-store.h @@ -4,10 +4,9 @@ void sto(void) MIN_ARGUMENTS(2); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - string name(((symbol*)_stack->get_obj(0))->_value); - _stack->pop_back(); + string name(((symbol*)_stack->pop_back())->_value); _global_heap->add(name, _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0)); - _stack->pop_back(); + (void)_stack->pop_back(); } void rcl(void) @@ -26,7 +25,7 @@ void rcl(void) || ((_parent_local_heap != NULL) && (_parent_local_heap->get(variable, obj, size, type))) || _global_heap->get(variable, obj, size, type)) { - _stack->pop_back(); + (void)_stack->pop_back(); _stack->push_back(obj, size, type); } else @@ -57,8 +56,7 @@ void edit(void) } // re-write stack objet in a stream - ((object*)_stack->back())->show(out); - _stack->pop_back(); + ((object*)_stack->pop_back())->show(out); // edit: stuff chars using readline facility string str = out.str(); @@ -100,7 +98,7 @@ void purge(void) MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_symbol); - string name(((symbol*)_stack->back())->_value); + string name(((symbol*)_stack->pop_back())->_value); // mind the order of heaps bool done = false; @@ -114,7 +112,6 @@ void purge(void) else done = true; - _stack->pop_back(); if (!done) ERR_CONTEXT(ret_unknown_variable); } diff --git a/src/rpn-string.h b/src/rpn-string.h index dbd7ec4..43bec79 100644 --- a/src/rpn-string.h +++ b/src/rpn-string.h @@ -7,8 +7,7 @@ void instr() { // write the object in stack(0) in a string and remove this obj stringstream out; - ((object*)_stack->back())->show(out); - _stack->pop_back(); + ((object*)_stack->pop_back())->show(out); // reserve the correct size in stack _stack->push_back(NULL, out.str().size(), cmd_string, true); @@ -24,8 +23,7 @@ void strout() MIN_ARGUMENTS(1); ARG_MUST_BE_OF_TYPE(0, cmd_string); - string entry(((ostring*)_stack->back())->_value); - _stack->pop_back(); + string entry(((ostring*)_stack->pop_back())->_value); program prog; diff --git a/src/rpn-test.h b/src/rpn-test.h index 846feed..4ef1d4a 100644 --- a/src/rpn-test.h +++ b/src/rpn-test.h @@ -4,8 +4,7 @@ void sup(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) > 0) @@ -20,8 +19,7 @@ void sup_eq(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) >= 0) @@ -36,8 +34,7 @@ void inf(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) < 0) @@ -52,8 +49,7 @@ void inf_eq(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) <= 0) @@ -68,8 +64,7 @@ void diff(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) != 0) @@ -84,8 +79,7 @@ void eq(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) == 0) @@ -100,8 +94,7 @@ void test_and(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0) @@ -117,8 +110,7 @@ void test_or(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0) @@ -134,8 +126,7 @@ void test_xor(void) ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number); - number* right = (number*)_stack->back(); - _stack->pop_back(); + number* right = (number*)_stack->pop_back(); number* left = (number*)_stack->back(); if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0) diff --git a/src/stack.h b/src/stack.h index bd628a7..efab231 100644 --- a/src/stack.h +++ b/src/stack.h @@ -53,8 +53,10 @@ public: _current += size; } - void pop_back() + void* pop_back() { + void* back = NULL; + if (_count > 0) { _current = _vpointer[_count - 1]; @@ -62,7 +64,11 @@ public: _vpointer.pop_back(); _vtype.pop_back(); _count--; + + back = _current; } + + return back; } unsigned int size()