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

This commit is contained in:
RUBET Louis 2017-04-21 13:58:44 +02:00
parent 980a282fbd
commit 2bdc1c4f72
10 changed files with 55 additions and 83 deletions

View file

@ -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));

View file

@ -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;
}

View file

@ -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;

View file

@ -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

View file

@ -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));

View file

@ -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);

View file

@ -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);
}

View file

@ -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;

View file

@ -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)

View file

@ -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()