#34: refactored stack::pop_back() for easier code

This commit is contained in:
Louis Rubet 2017-04-21 13:58:44 +02:00
parent c877cb20d8
commit 14adfd873e
10 changed files with 55 additions and 83 deletions

View file

@ -23,8 +23,7 @@ void rtob()
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
number* left = (number*)_stack->back(); number* left = (number*)_stack->pop_back();
_stack->pop_back();
binary bin; binary bin;
bin.set(mpfr_get_sj(&left->_value.mpfr, s_mpfr_rnd)); bin.set(mpfr_get_sj(&left->_value.mpfr, s_mpfr_rnd));

View file

@ -10,7 +10,7 @@ int rpn_if(branch& myobj)
myobj.arg1 = 1; myobj.arg1 = 1;
else else
myobj.arg1 = 0; myobj.arg1 = 0;
_stack->pop_back(); (void)_stack->pop_back();
return -1; return -1;
} }

View file

@ -16,9 +16,7 @@ void verbose()
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
int verbose = int(((number*)_stack->back())->_value); int verbose = int(((number*)_stack->pop_back())->_value);
_stack->pop_back();
g_verbose = verbose; g_verbose = verbose;
} }
@ -84,8 +82,7 @@ void std()
{ {
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
int precision = int(((number*)_stack->back())->_value); int precision = int(((number*)_stack->pop_back())->_value);
_stack->pop_back();
number::s_default_precision = int(precision); number::s_default_precision = int(precision);
} }
@ -103,8 +100,7 @@ void fix()
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
int precision = int(((number*)_stack->back())->_value); int precision = int(((number*)_stack->pop_back())->_value);
_stack->pop_back();
number::s_current_precision = int(precision); number::s_current_precision = int(precision);
number::s_mode = number::fix; number::s_mode = number::fix;
@ -120,8 +116,7 @@ void sci()
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
int precision = int(((number*)_stack->back())->_value); int precision = int(((number*)_stack->pop_back())->_value);
_stack->pop_back();
number::s_current_precision = int(precision); number::s_current_precision = int(precision);
number::s_mode = number::sci; number::s_mode = number::sci;

View file

@ -22,13 +22,13 @@ void eval(void)
if (type == cmd_program) if (type == cmd_program)
{ {
prog_text = ((oprogram*)obj)->_value; prog_text = ((oprogram*)obj)->_value;
_stack->pop_back(); (void)_stack->pop_back();
run_prog = true; run_prog = true;
} }
else else
{ {
// else recall variable (i.e. stack its content) // else recall variable (i.e. stack its content)
_stack->pop_back(); (void)_stack->pop_back();
_stack->push_back(obj, size, type); _stack->push_back(obj, size, type);
} }
} }
@ -38,8 +38,7 @@ void eval(void)
else if (IS_ARG_TYPE(0, cmd_program)) else if (IS_ARG_TYPE(0, cmd_program))
{ {
// eval a program // eval a program
prog_text = ((oprogram*)_stack->back())->_value; prog_text = ((oprogram*)_stack->pop_back())->_value;
_stack->pop_back();
run_prog = true; run_prog = true;
} }
else else
@ -125,7 +124,7 @@ int inprog(branch& myobj)
for (unsigned int i = myobj.arg1 + count_symbols; i > myobj.arg1; i--) 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)); 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 // run the program

View file

@ -7,8 +7,7 @@ void plus()
{ {
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_add(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); 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); ARG_MUST_BE_OF_TYPE(1, cmd_binary);
binary* right = (binary*)_stack->back(); binary* right = (binary*)_stack->pop_back();
_stack->pop_back();
binary* left = (binary*)_stack->back(); binary* left = (binary*)_stack->back();
left->_value += right->_value; left->_value += right->_value;
} }
@ -35,8 +33,7 @@ void minus()
{ {
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_sub(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); 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); ARG_MUST_BE_OF_TYPE(1, cmd_binary);
binary* right = (binary*)_stack->back(); binary* right = (binary*)_stack->pop_back();
_stack->pop_back();
binary* left = (binary*)_stack->back(); binary* left = (binary*)_stack->back();
left->_value -= right->_value; left->_value -= right->_value;
} }
@ -61,8 +57,7 @@ void mul()
{ {
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); 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); ARG_MUST_BE_OF_TYPE(1, cmd_binary);
binary* right = (binary*)_stack->back(); binary* right = (binary*)_stack->pop_back();
_stack->pop_back();
binary* left = (binary*)_stack->back(); binary* left = (binary*)_stack->back();
left->_value *= right->_value; left->_value *= right->_value;
} }
@ -89,8 +83,7 @@ void div()
{ {
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_div(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); CHECK_MPFR(mpfr_div(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));
} }
@ -104,8 +97,7 @@ void div()
} }
else else
{ {
binary* right = (binary*)_stack->back(); binary* right = (binary*)_stack->pop_back();
_stack->pop_back();
binary* left = (binary*)_stack->back(); binary* left = (binary*)_stack->back();
left->_value /= right->_value; left->_value /= right->_value;
} }
@ -146,8 +138,7 @@ void purcent()
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_mul(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_mul_si(&right->_value.mpfr, &right->_value.mpfr, 100L, s_mpfr_rnd)); 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_pow(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
CHECK_MPFR(mpfr_fmod(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd)); CHECK_MPFR(mpfr_fmod(&left->_value.mpfr, &left->_value.mpfr, &right->_value.mpfr, s_mpfr_rnd));

View file

@ -4,8 +4,8 @@ void swap(void)
MIN_ARGUMENTS(2); MIN_ARGUMENTS(2);
_stack->copy_obj_to_local(0, 0); _stack->copy_obj_to_local(0, 0);
_stack->copy_obj_to_local(1, 1); _stack->copy_obj_to_local(1, 1);
_stack->pop_back(); (void)_stack->pop_back();
_stack->pop_back(); (void)_stack->pop_back();
_stack->push_obj_from_local(0); _stack->push_obj_from_local(0);
_stack->push_obj_from_local(1); _stack->push_obj_from_local(1);
} }
@ -13,20 +13,20 @@ void swap(void)
void drop(void) void drop(void)
{ {
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
_stack->pop_back(); (void)_stack->pop_back();
} }
void drop2(void) void drop2(void)
{ {
MIN_ARGUMENTS(2); MIN_ARGUMENTS(2);
_stack->pop_back(); (void)_stack->pop_back();
_stack->pop_back(); (void)_stack->pop_back();
} }
void erase(void) void erase(void)
{ {
while(_stack->size()>0) while(_stack->size()>0)
_stack->pop_back(); (void)_stack->pop_back();
} }
void dup(void) void dup(void)
@ -50,8 +50,7 @@ void pick(void)
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
unsigned int to_pick = (unsigned int)int(((number*)_stack->back())->_value); unsigned int to_pick = (unsigned int)int(((number*)_stack->pop_back())->_value);
_stack->pop_back();
// treat stack depth errors // treat stack depth errors
if ((to_pick == 0) || (to_pick > _stack->size())) 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(0, 0);
_stack->copy_obj_to_local(1, 1); _stack->copy_obj_to_local(1, 1);
_stack->copy_obj_to_local(2, 2); _stack->copy_obj_to_local(2, 2);
_stack->pop_back(); (void)_stack->pop_back();
_stack->pop_back(); (void)_stack->pop_back();
_stack->pop_back(); (void)_stack->pop_back();
_stack->push_obj_from_local(1); _stack->push_obj_from_local(1);
_stack->push_obj_from_local(0); _stack->push_obj_from_local(0);
_stack->push_obj_from_local(2); _stack->push_obj_from_local(2);

View file

@ -4,10 +4,9 @@ void sto(void)
MIN_ARGUMENTS(2); MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_symbol); ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
string name(((symbol*)_stack->get_obj(0))->_value); string name(((symbol*)_stack->pop_back())->_value);
_stack->pop_back();
_global_heap->add(name, _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0)); _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) void rcl(void)
@ -26,7 +25,7 @@ void rcl(void)
|| ((_parent_local_heap != NULL) && (_parent_local_heap->get(variable, obj, size, type))) || ((_parent_local_heap != NULL) && (_parent_local_heap->get(variable, obj, size, type)))
|| _global_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); _stack->push_back(obj, size, type);
} }
else else
@ -57,8 +56,7 @@ void edit(void)
} }
// re-write stack objet in a stream // re-write stack objet in a stream
((object*)_stack->back())->show(out); ((object*)_stack->pop_back())->show(out);
_stack->pop_back();
// edit: stuff chars using readline facility // edit: stuff chars using readline facility
string str = out.str(); string str = out.str();
@ -100,7 +98,7 @@ void purge(void)
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_symbol); 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 // mind the order of heaps
bool done = false; bool done = false;
@ -114,7 +112,6 @@ void purge(void)
else else
done = true; done = true;
_stack->pop_back();
if (!done) if (!done)
ERR_CONTEXT(ret_unknown_variable); ERR_CONTEXT(ret_unknown_variable);
} }

View file

@ -7,8 +7,7 @@ void instr()
{ {
// write the object in stack(0) in a string and remove this obj // write the object in stack(0) in a string and remove this obj
stringstream out; stringstream out;
((object*)_stack->back())->show(out); ((object*)_stack->pop_back())->show(out);
_stack->pop_back();
// reserve the correct size in stack // reserve the correct size in stack
_stack->push_back(NULL, out.str().size(), cmd_string, true); _stack->push_back(NULL, out.str().size(), cmd_string, true);
@ -24,8 +23,7 @@ void strout()
MIN_ARGUMENTS(1); MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_string); ARG_MUST_BE_OF_TYPE(0, cmd_string);
string entry(((ostring*)_stack->back())->_value); string entry(((ostring*)_stack->pop_back())->_value);
_stack->pop_back();
program prog; program prog;

View file

@ -4,8 +4,7 @@ void sup(void)
ARG_MUST_BE_OF_TYPE(0, cmd_number); ARG_MUST_BE_OF_TYPE(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) > 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) >= 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) < 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) <= 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) != 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp(&left->_value.mpfr, &right->_value.mpfr) == 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if ((mpfr_cmp_si(&left->_value.mpfr, 0) == 0) 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(0, cmd_number);
ARG_MUST_BE_OF_TYPE(1, cmd_number); ARG_MUST_BE_OF_TYPE(1, cmd_number);
number* right = (number*)_stack->back(); number* right = (number*)_stack->pop_back();
_stack->pop_back();
number* left = (number*)_stack->back(); number* left = (number*)_stack->back();
if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0) if (mpfr_cmp_si(&left->_value.mpfr, 0) == 0)

View file

@ -53,8 +53,10 @@ public:
_current += size; _current += size;
} }
void pop_back() void* pop_back()
{ {
void* back = NULL;
if (_count > 0) if (_count > 0)
{ {
_current = _vpointer[_count - 1]; _current = _vpointer[_count - 1];
@ -62,7 +64,11 @@ public:
_vpointer.pop_back(); _vpointer.pop_back();
_vtype.pop_back(); _vtype.pop_back();
_count--; _count--;
back = _current;
} }
return back;
} }
unsigned int size() unsigned int size()