rpn/src/rpn-store.h

154 lines
4 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);
2015-03-01 16:02:23 +01:00
string name(((symbol*)_stack->get_obj(0))->_value);
_stack->pop_back();
_global_heap->add(name, _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0));
2015-05-19 17:51:03 +02:00
_stack->pop_back();
}
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
void* obj;
unsigned int size;
int type;
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 (_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))
2015-05-19 17:51:03 +02:00
{
_stack->pop_back();
_stack->push_back(obj, size, type);
}
else
ERR_CONTEXT(ret_unknown_variable);
}
2015-03-04 18:21:11 +01:00
void edit(void)
{
MIN_ARGUMENTS(1);
stringstream out;
2015-03-04 18:28:54 +01:00
if (((object*)_stack->back())->_type == cmd_number)
{
switch(number::s_mode)
{
case number::std:
out.precision(number::s_current_precision);
out.unsetf(ios_base::floatfield);
break;
case number::fix:
out << setprecision(number::s_current_precision) << fixed;
break;
case number::sci:
out << setprecision(number::s_current_precision) << scientific;
break;
}
}
// re-write stack objet in a stream
2015-03-04 18:21:11 +01:00
((object*)_stack->back())->show(out);
_stack->pop_back();
// edit: stuff chars using readline facility
string str = out.str();
for(int i=0;i<str.size();i++)
rl_stuff_char(str[i]);
}
// carefull : this is not a langage command
void auto_rcl(symbol* symb)
{
2015-05-19 17:51:03 +02:00
if (symb->_auto_eval)
{
void* obj;
unsigned int size;
int type;
2015-03-04 17:01:30 +01:00
string variable(symb->_value);
// mind the order of heaps
if (_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))
2015-03-03 22:07:48 +01:00
{
_stack->push_back(obj, size, type);
if (type == cmd_program)
eval();
else if (type == cmd_number)
((number*)_stack->back())->ensure_significand();
2015-03-03 22:07:48 +01:00
}
2015-05-19 17:51:03 +02:00
else
2015-03-01 16:02:23 +01:00
_stack->push_back(symb, symb->size(), cmd_symbol);
2015-05-19 17:51:03 +02:00
}
else
2015-03-01 16:02:23 +01:00
_stack->push_back(symb, symb->size(), cmd_symbol);
}
void purge(void)
{
2015-05-19 17:51:03 +02:00
MIN_ARGUMENTS(1);
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
2015-03-01 16:30:13 +01:00
string name(((symbol*)_stack->back())->_value);
2015-03-04 17:01:30 +01:00
// mind the order of heaps
bool done = false;
if (!_local_heap.erase(name))
{
2015-03-04 17:01:30 +01:00
if ((_parent_local_heap != NULL) && (_parent_local_heap->erase(name)))
done = true;
else if (_global_heap->erase(name))
done = true;
}
2015-03-01 16:30:13 +01:00
else
2015-03-04 17:01:30 +01:00
done = true;
_stack->pop_back();
if (!done)
ERR_CONTEXT(ret_unknown_variable);
}
void vars(void)
{
2015-05-19 17:51:03 +02:00
object* obj;
unsigned int size;
int type;
string name;
2015-03-04 17:01:30 +01:00
for (int i=0; i<(int)_global_heap->size(); i++)
{
(void)_global_heap->get_by_index(i, name, (void*&)obj, size, type);
cout<<"var "<<i+1<<": name '"<<name<<"', type "<<cmd_type_string[type]<<", value ";
obj->show();
cout<<endl;
}
if (_parent_local_heap != NULL)
{
for (int i=0; i<(int)_parent_local_heap->size(); i++)
{
(void)_parent_local_heap->get_by_index(i, name, (void*&)obj, size, type);
cout<<"local var "<<i+1<<": name '"<<name<<"', type "<<cmd_type_string[type]<<", value ";
obj->show();
cout<<endl;
}
}
for (int i=0; i<(int)_local_heap.size(); i++)
{
2015-03-04 17:01:30 +01:00
(void)_local_heap.get_by_index(i, name, (void*&)obj, size, type);
cout<<"local var "<<i+1<<": name '"<<name<<"', type "<<cmd_type_string[type]<<", value ";
obj->show();
cout<<endl;
}
}