rpn/src/rpn-branch.h

208 lines
5.6 KiB
C
Raw Normal View History

2014-02-12 11:26:26 +01:00
//
int rpn_if(branch& myobj)
{
2015-05-19 17:51:03 +02:00
// myobj.arg1 = 'if' condition evaluation value
MIN_ARGUMENTS_RET(1, -1);
ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, -1);
2017-04-17 23:10:53 +02:00
if (mpfr_cmp_si(((number*)_stack->get_obj(0))->_value.mpfr, 0UL) != 0)
2017-04-17 23:10:53 +02:00
myobj.arg1 = 1;
else
myobj.arg1 = 0;
(void)_stack->pop_back();
2015-05-19 17:51:03 +02:00
return -1;
}
int rpn_then(branch& myobj)
{
2015-05-19 17:51:03 +02:00
// 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*)seq_obj(myobj.arg3);
if (if_cmd->arg1 == 1)
return myobj.arg1;
else
return myobj.arg2;
}
int rpn_else(branch& myobj)
{
2015-05-19 17:51:03 +02:00
// 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*)seq_obj(myobj.arg3);
if (if_cmd->arg1 == 1)
return myobj.arg2;
else
return myobj.arg1;
}
void rpn_end(void)
{
2015-05-19 17:51:03 +02:00
// nothing
}
int rpn_start(branch& myobj)
{
int ret = -1;
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS_RET(2, 1);
ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, -1);
ARG_MUST_BE_OF_TYPE_RET(1, cmd_number, -1);
// farg2 = last value of start command
2017-05-15 17:17:49 +02:00
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
myobj.farg2 = (number*)_branch_stack.back();
2017-04-29 15:42:06 +02:00
_stack->pop_back();
2017-05-15 17:17:49 +02:00
// farg1 = first value of start command
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
myobj.farg1 = (number*)_branch_stack.back();
2017-04-29 15:42:06 +02:00
_stack->pop_back();
2017-05-15 17:17:49 +02:00
// test value
if (myobj.farg1->_value > myobj.farg2->_value)
// last boundary lower than first boundary
// -> next command shall be after 'next'
// arg2 holds index of 'next'
ret = myobj.arg2 + 1;
return ret;
}
int rpn_for(branch& myobj)
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS_RET(2, 1);
ARG_MUST_BE_OF_TYPE_RET(0, cmd_number, -1);
ARG_MUST_BE_OF_TYPE_RET(1, cmd_number, -1);
2015-05-19 17:51:03 +02:00
symbol* sym = ((symbol*)seq_obj(myobj.arg1));
2015-05-19 17:51:03 +02:00
// farg1 = first value of for command
// farg2 = last value of for command
// arg1 = index of symbol to increase
2017-04-29 15:42:06 +02:00
stack::copy_and_push_back(*_stack, 0, _branch_stack);
2017-05-02 10:47:03 +02:00
//myobj.farg2 = (number*)_branch_stack.back();
//myobj.farg2->init(_branch_stack.back_blob());
2017-04-29 15:42:06 +02:00
stack::copy_and_push_back(*_stack, 0, _branch_stack);
2017-05-02 10:47:03 +02:00
//myobj.farg1 = (number*)_branch_stack.back();
//myobj.farg1->init(_branch_stack.back_blob());
2014-03-10 17:13:18 +01:00
2015-05-19 17:51:03 +02:00
// store symbol with first value
2017-04-29 15:42:06 +02:00
//TODO
2017-05-02 10:47:03 +02:00
//number* num = (number*)_local_heap.add(string(sym->_value), NULL, number::calc_size(), cmd_number);
2017-04-29 15:42:06 +02:00
//num->set(myobj.farg1);
2015-05-19 17:51:03 +02:00
return myobj.arg1 + 1;
}
int rpn_next(branch& myobj)
{
2015-05-19 17:51:03 +02:00
// arg1 = index of start or for command in program
// farg1 = current count
branch* start_or_for = (branch*)seq_obj(myobj.arg1);
if (! myobj.arg_bool)
{
myobj.arg_bool = true;
myobj.farg1 = start_or_for->farg1;
}
// increment then test
mpfr_add_si(myobj.farg1->_value.mpfr, myobj.farg1->_value.mpfr, 1UL, MPFR_DEF_RND);
2015-05-19 17:51:03 +02:00
// for command: increment symbol too
if (start_or_for->arg1 != -1)
{
void* obj;
unsigned int size;
int type;
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
2015-05-19 17:51:03 +02:00
// check symbol variable is a number, then increase
2017-04-29 15:42:06 +02:00
// if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
// {
// ((number*)obj)->set(myobj.farg1);
// ((number*)obj)->ensure_significand();
// }
2015-05-19 17:51:03 +02:00
}
//test value
2017-04-29 15:42:06 +02:00
if (myobj.farg1->_value > start_or_for->farg2->_value)
2015-05-19 17:51:03 +02:00
{
// end of loop
myobj.arg_bool = false;// init again next time
2017-05-15 17:17:49 +02:00
_branch_stack.pop_back();
_branch_stack.pop_back();
2015-05-19 17:51:03 +02:00
return -1;
}
else
{
// for command: next instruction will be after symbol variable
if (start_or_for->arg1 != -1)
return start_or_for->arg1 + 1;
// start command: next instruction will be after start command
else
return myobj.arg1 + 1;
}
}
int rpn_step(branch& myobj)
{
2017-04-29 15:42:06 +02:00
//TODO
#if 0
2015-05-19 17:51:03 +02:00
// arg1 = index of start or for command in program
// farg1 = current count
2017-04-29 15:42:06 +02:00
number* step = _stack.pop_back();
2015-05-19 17:51:03 +02:00
branch* start_or_for = (branch*)seq_obj(myobj.arg1);
if (! myobj.arg_bool)
{
myobj.arg_bool = true;
myobj.farg1 = start_or_for->farg1;
}
// increment then test
2017-04-17 23:10:53 +02:00
mpfr_add(&myobj.farg1.mpfr, &myobj.farg1.mpfr, &step.mpfr, MPFR_DEF_RND);
2015-05-19 17:51:03 +02:00
// for command: increment symbol too
if (start_or_for->arg1 != -1)
{
void* obj;
unsigned int size;
int type;
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
2015-05-19 17:51:03 +02:00
// check symbol variable is a number, then increase
2015-03-04 17:01:30 +01:00
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
{
2015-05-19 17:51:03 +02:00
((number*)obj)->_value = myobj.farg1;
}
2015-05-19 17:51:03 +02:00
}
//test value
if (((step>0) && (myobj.farg1 > start_or_for->farg2))
|| ((step<0) && (myobj.farg1 < start_or_for->farg2)))
{
// end of loop
myobj.arg_bool = false;// init again next time
return -1;
}
else
{
// for command: next instruction will be after symbol variable
if (start_or_for->arg1 != -1)
return start_or_for->arg1 + 1;
// start command: next instruction will be after start command
else
return myobj.arg1 + 1;
}
2017-04-29 15:42:06 +02:00
#endif
return -1;
}