rpn/src/rpn-store.hpp

325 lines
8.8 KiB
C++
Raw Normal View History

//
void sto(void)
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS(2);
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
string name(((symbol*)_stack->pop_back())->_value);
_heap->add(name, _stack->get_obj(0), _stack->get_len(0));
(void)_stack->pop_back();
}
//
void stoadd(void)
{
MIN_ARGUMENTS(2);
if (_stack->get_type(0) == cmd_symbol && _stack->get_type(1) == cmd_number)
{
// get variable value on stack level 1, make op then modify variable
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
plus();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else if (_stack->get_type(1) == cmd_symbol && _stack->get_type(0) == cmd_number)
{
// copy value, get variable value on stack level 1,
// put back value on stack level 1, make op then modify variable
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
_stack->pop_back();
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack);
plus();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void stosub(void)
{
MIN_ARGUMENTS(2);
if (_stack->get_type(0) == cmd_symbol && _stack->get_type(1) == cmd_number)
{
// get variable value on stack level 1, make op then modify variable
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
minus();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else if (_stack->get_type(1) == cmd_symbol && _stack->get_type(0) == cmd_number)
{
// copy value, get variable value on stack level 1,
// put back value on stack level 1, make op then modify variable
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
_stack->pop_back();
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack);
minus();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void stomul(void)
{
MIN_ARGUMENTS(2);
if (_stack->get_type(0) == cmd_symbol && _stack->get_type(1) == cmd_number)
{
// get variable value on stack level 1, make op then modify variable
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
mul();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else if (_stack->get_type(1) == cmd_symbol && _stack->get_type(0) == cmd_number)
{
// copy value, get variable value on stack level 1,
// put back value on stack level 1, make op then modify variable
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
_stack->pop_back();
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack);
mul();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void stodiv(void)
{
MIN_ARGUMENTS(2);
if (_stack->get_type(0) == cmd_symbol && _stack->get_type(1) == cmd_number)
{
// get variable value on stack level 1, make op then modify variable
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
div();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else if (_stack->get_type(1) == cmd_symbol && _stack->get_type(0) == cmd_number)
{
// copy value, get variable value on stack level 1,
// put back value on stack level 1, make op then modify variable
stack::copy_and_push_back(*_stack, _stack->size()-1, _branch_stack);
_stack->pop_back();
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
stack::copy_and_push_back(_branch_stack, _branch_stack.size()-1, *_stack);
div();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void stoneg(void)
{
MIN_ARGUMENTS(1);
if (_stack->get_type(0) == cmd_symbol)
{
// get variable value on stack level 1, make op then modify variable
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
neg();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void stoinv(void)
{
MIN_ARGUMENTS(1);
if (_stack->get_type(0) == cmd_symbol)
{
// get variable value on stack level 1, make op then modify variable
string variable(((symbol*)_stack->back())->_value);
rcl();
if (_err == ret_ok)
{
inv();
_heap->add(variable, _stack->get_obj(0), _stack->get_len(0));
_stack->pop_back();
}
}
else
ERR_CONTEXT(ret_bad_operand_type);
}
void rcl(void)
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
2015-05-19 17:51:03 +02:00
// recall a variable
2017-05-02 10:47:03 +02:00
object* obj;
2015-05-19 17:51:03 +02:00
unsigned int size;
2015-02-26 22:33:28 +01:00
string variable(((symbol*)_stack->back())->_value);
2015-03-04 17:01:30 +01:00
// mind the order of heaps
if (find_variable(variable, obj, size))
2015-05-19 17:51:03 +02:00
{
(void)_stack->pop_back();
2017-05-02 10:47:03 +02:00
stack::copy_and_push_back(obj, *_stack, size);
2015-05-19 17:51:03 +02:00
}
else
ERR_CONTEXT(ret_unknown_variable);
}
2015-03-04 18:21:11 +01:00
void edit(void)
{
MIN_ARGUMENTS(1);
2017-05-24 14:09:52 +02:00
FILE* tmp = tmpfile();
if (tmp != NULL)
2015-03-04 18:28:54 +01:00
{
2017-05-24 14:09:52 +02:00
// re-write stack objet in a stream
((object*)_stack->pop_back())->show(tmp);
// edit: stuff chars using readline facility
int len = (int)ftell(tmp);
rewind(tmp);
for(int i=0;i<len;i++)
2015-03-04 18:28:54 +01:00
{
2017-05-24 14:09:52 +02:00
char readc;
if (fread(&readc, 1, 1, tmp) == 1)
rl_stuff_char(readc);
else
{
ERR_CONTEXT(ret_runtime_error);
break;
}
2015-03-04 18:28:54 +01:00
}
2017-05-24 14:09:52 +02:00
fclose(tmp);
2015-03-04 18:28:54 +01:00
}
2015-03-04 18:21:11 +01:00
}
// carefull : this is not a langage command
void auto_rcl(symbol* symb)
{
2017-05-02 11:33:37 +02:00
if (symb->_auto_eval)
2015-05-19 17:51:03 +02:00
{
2017-05-02 10:47:03 +02:00
object* obj;
2015-05-19 17:51:03 +02:00
unsigned int size;
2015-03-04 17:01:30 +01:00
string variable(symb->_value);
// mind the order of heaps
if (find_variable(variable, obj, size))
2015-03-03 22:07:48 +01:00
{
2017-05-02 10:47:03 +02:00
stack::copy_and_push_back(obj, *_stack, size);
if (obj->_type == cmd_program)
2015-03-03 22:07:48 +01:00
eval();
}
2015-05-19 17:51:03 +02:00
else
2017-05-02 10:47:03 +02:00
stack::copy_and_push_back(symb, *_stack, symb->size());
2015-05-19 17:51:03 +02:00
}
else
2017-05-02 10:47:03 +02:00
stack::copy_and_push_back(symb, *_stack, symb->size());
}
void purge(void)
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
string name(((symbol*)_stack->pop_back())->_value);
if (!_heap->erase(name))
2015-03-04 17:01:30 +01:00
ERR_CONTEXT(ret_unknown_variable);
}
void vars(void)
{
2015-05-19 17:51:03 +02:00
object* obj;
unsigned int size;
program* parent = _parent_prog;
2015-05-19 17:51:03 +02:00
string name;
// heap variables
for (int i=0; i<(int)_heap->count_vars(); i++)
2015-03-04 17:01:30 +01:00
{
(void)_heap->get_by_index(i, name, obj, size);
printf("var %d: name '%s', type %s, value ", i+1, name.c_str(), object::s_cmd_type_string[obj->_type]);
2015-03-04 17:01:30 +01:00
obj->show();
2017-05-24 14:09:52 +02:00
printf("\n");
2015-03-04 17:01:30 +01:00
}
// parents local variables
while (parent != NULL)
2015-03-04 17:01:30 +01:00
{
for (int i=0; i<(int)parent->_local_heap.size(); i++)
2015-03-04 17:01:30 +01:00
{
(void)parent->_local_heap.get_by_index(i, name, obj, size);
printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), object::s_cmd_type_string[obj->_type]);
2015-03-04 17:01:30 +01:00
obj->show();
2017-05-24 14:09:52 +02:00
printf("\n");
2015-03-04 17:01:30 +01:00
}
parent = parent->_parent_prog;
2015-03-04 17:01:30 +01:00
}
// local variables
2015-03-04 17:01:30 +01:00
for (int i=0; i<(int)_local_heap.size(); i++)
{
2017-05-02 10:47:03 +02:00
(void)_local_heap.get_by_index(i, name, obj, size);
printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), object::s_cmd_type_string[obj->_type]);
obj->show();
2017-05-24 14:09:52 +02:00
printf("\n");
}
}
2017-05-31 22:39:14 +02:00
void clusr(void)
{
_heap->erase_all();
}