mirror of
https://github.com/louisrubet/rpn
synced 2025-01-19 10:26:22 +01:00
#34: implemented binary, branch, general, stack and store commands
This commit is contained in:
parent
67403c56ff
commit
0f8f76f283
7 changed files with 47 additions and 23 deletions
|
@ -26,10 +26,8 @@ void rtob()
|
||||||
number* left = (number*)_stack->back();
|
number* left = (number*)_stack->back();
|
||||||
_stack->pop_back();
|
_stack->pop_back();
|
||||||
|
|
||||||
cout<<"TODO"<<endl;
|
|
||||||
|
|
||||||
binary bin;
|
binary bin;
|
||||||
bin._value = mpfr_get_sj(&left->_value.mpfr, s_mpfr_rnd);
|
bin.set(mpfr_get_sj(&left->_value.mpfr, s_mpfr_rnd));
|
||||||
_stack->push_back(&bin, bin.size(), cmd_binary);
|
_stack->push_back(&bin, bin.size(), cmd_binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,10 +38,7 @@ void btor()
|
||||||
|
|
||||||
integer_t bin = getb();
|
integer_t bin = getb();
|
||||||
|
|
||||||
cout<<"TODO"<<endl;
|
|
||||||
|
|
||||||
_stack->push_back(NULL, sizeof(number), cmd_number, true);
|
_stack->push_back(NULL, sizeof(number), cmd_number, true);
|
||||||
number* left = (number*)_stack->back();
|
number* left = (number*)_stack->back();
|
||||||
left->_value = bin;
|
left->set(bin);
|
||||||
left->ensure_significand();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,9 +79,9 @@ int rpn_for(branch& myobj)
|
||||||
myobj.farg1.ensure_significand();
|
myobj.farg1.ensure_significand();
|
||||||
|
|
||||||
// store symbol with first value
|
// store symbol with first value
|
||||||
number num;
|
number* num = (number*)_local_heap.add(string(sym->_value), NULL, sizeof(number), cmd_number);
|
||||||
num.set(myobj.farg1);
|
num->set(myobj.farg1);
|
||||||
_local_heap.add(string(sym->_value), &num, num.size(), cmd_number);
|
num->ensure_significand();
|
||||||
|
|
||||||
return myobj.arg1 + 1;
|
return myobj.arg1 + 1;
|
||||||
}
|
}
|
||||||
|
@ -108,10 +108,11 @@ int rpn_next(branch& myobj)
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
int type;
|
int type;
|
||||||
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
||||||
|
|
||||||
// check symbol variable is a number, then increase
|
// check symbol variable is a number, then increase
|
||||||
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
||||||
{
|
{
|
||||||
((number*)obj)->_value = myobj.farg1;
|
((number*)obj)->set(myobj.farg1);
|
||||||
((number*)obj)->ensure_significand();
|
((number*)obj)->ensure_significand();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,6 +140,8 @@ int rpn_step(branch& myobj)
|
||||||
// arg1 = index of start or for command in program
|
// arg1 = index of start or for command in program
|
||||||
// farg1 = current count
|
// farg1 = current count
|
||||||
floating_t step = getf();
|
floating_t step = getf();
|
||||||
|
step.ensure_significand();
|
||||||
|
|
||||||
branch* start_or_for = (branch*)seq_obj(myobj.arg1);
|
branch* start_or_for = (branch*)seq_obj(myobj.arg1);
|
||||||
if (! myobj.arg_bool)
|
if (! myobj.arg_bool)
|
||||||
{
|
{
|
||||||
|
@ -157,6 +160,7 @@ int rpn_step(branch& myobj)
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
int type;
|
int type;
|
||||||
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
||||||
|
|
||||||
// check symbol variable is a number, then increase
|
// check symbol variable is a number, then increase
|
||||||
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
if (_local_heap.get(string(var->_value), obj, size, type) && (type == cmd_number))
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,7 +15,11 @@ void verbose()
|
||||||
{
|
{
|
||||||
MIN_ARGUMENTS(1);
|
MIN_ARGUMENTS(1);
|
||||||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
g_verbose = int(getf());
|
|
||||||
|
int verbose = int(((number*)_stack->back())->_value);
|
||||||
|
_stack->pop_back();
|
||||||
|
|
||||||
|
g_verbose = verbose;
|
||||||
}
|
}
|
||||||
|
|
||||||
void help()
|
void help()
|
||||||
|
@ -75,7 +79,10 @@ void std()
|
||||||
if (stack_size()>=1)
|
if (stack_size()>=1)
|
||||||
{
|
{
|
||||||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
number::s_default_precision = int(getf());
|
|
||||||
|
int precision = int(((number*)_stack->back())->_value);
|
||||||
|
_stack->pop_back();
|
||||||
|
number::s_default_precision = int(precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
number::s_current_precision = number::s_default_precision;
|
number::s_current_precision = number::s_default_precision;
|
||||||
|
@ -92,7 +99,10 @@ void fix()
|
||||||
MIN_ARGUMENTS(1);
|
MIN_ARGUMENTS(1);
|
||||||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
|
|
||||||
number::s_current_precision = int(getf());
|
int precision = int(((number*)_stack->back())->_value);
|
||||||
|
_stack->pop_back();
|
||||||
|
number::s_current_precision = int(precision);
|
||||||
|
|
||||||
number::s_mode = number::fix;
|
number::s_mode = number::fix;
|
||||||
|
|
||||||
// format for mpfr_printf
|
// format for mpfr_printf
|
||||||
|
@ -106,7 +116,10 @@ void sci()
|
||||||
MIN_ARGUMENTS(1);
|
MIN_ARGUMENTS(1);
|
||||||
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
|
|
||||||
number::s_current_precision = (int)getf();
|
int precision = int(((number*)_stack->back())->_value);
|
||||||
|
_stack->pop_back();
|
||||||
|
number::s_current_precision = int(precision);
|
||||||
|
|
||||||
number::s_mode = number::sci;
|
number::s_mode = number::sci;
|
||||||
|
|
||||||
// format for mpfr_printf
|
// format for mpfr_printf
|
||||||
|
|
|
@ -48,7 +48,10 @@ void dup2(void)
|
||||||
void pick(void)
|
void pick(void)
|
||||||
{
|
{
|
||||||
MIN_ARGUMENTS(1);
|
MIN_ARGUMENTS(1);
|
||||||
unsigned int to_pick = (unsigned int)getf();
|
ARG_MUST_BE_OF_TYPE(0, cmd_number);
|
||||||
|
|
||||||
|
unsigned int to_pick = (unsigned int)int(((number*)_stack->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()))
|
||||||
|
|
|
@ -84,9 +84,9 @@ void auto_rcl(symbol* symb)
|
||||||
{
|
{
|
||||||
_stack->push_back(obj, size, type);
|
_stack->push_back(obj, size, type);
|
||||||
if (type == cmd_program)
|
if (type == cmd_program)
|
||||||
{
|
|
||||||
eval();
|
eval();
|
||||||
}
|
else if (type == cmd_number)
|
||||||
|
((number*)_stack->back())->ensure_significand();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_stack->push_back(symb, symb->size(), cmd_symbol);
|
_stack->push_back(symb, symb->size(), cmd_symbol);
|
||||||
|
|
|
@ -185,7 +185,12 @@ struct number : public object
|
||||||
void set(long value)
|
void set(long value)
|
||||||
{
|
{
|
||||||
_type = cmd_number;
|
_type = cmd_number;
|
||||||
_value = (long)value;
|
_value = value;
|
||||||
|
}
|
||||||
|
void set(integer_t value)
|
||||||
|
{
|
||||||
|
_type = cmd_number;
|
||||||
|
_value = value;
|
||||||
}
|
}
|
||||||
unsigned int size() { return (unsigned int)sizeof(floating_t); }
|
unsigned int size() { return (unsigned int)sizeof(floating_t); }
|
||||||
|
|
||||||
|
@ -481,7 +486,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// could be an auto-evaluated symbol
|
// could be an auto-evaluated symbol
|
||||||
if (type == cmd_symbol)
|
else if (type == cmd_symbol)
|
||||||
{
|
{
|
||||||
auto_rcl((symbol*)seq_obj(i));
|
auto_rcl((symbol*)seq_obj(i));
|
||||||
i++;
|
i++;
|
||||||
|
|
10
src/stack.h
10
src/stack.h
|
@ -186,7 +186,7 @@ public:
|
||||||
free(i->second);
|
free(i->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(const string name, void* obj, unsigned int size, int type = 0)
|
void* add(const string name, void* obj, unsigned int size, int type = 0)
|
||||||
{
|
{
|
||||||
struct local_var* blob = _map[name];
|
struct local_var* blob = _map[name];
|
||||||
if (blob == NULL)
|
if (blob == NULL)
|
||||||
|
@ -203,9 +203,13 @@ public:
|
||||||
}
|
}
|
||||||
blob->length = size;
|
blob->length = size;
|
||||||
blob->type= type;
|
blob->type= type;
|
||||||
memcpy(&blob->blob, obj, size);
|
|
||||||
}
|
if (obj != NULL)
|
||||||
|
memcpy(&blob->blob, obj, size);
|
||||||
|
|
||||||
|
return (void*)&blob->blob;
|
||||||
|
}
|
||||||
|
|
||||||
bool get(const string name, void*& obj, unsigned int& size, int& type)
|
bool get(const string name, void*& obj, unsigned int& size, int& type)
|
||||||
{
|
{
|
||||||
map<string, struct local_var*>::iterator i = _map.find(name);
|
map<string, struct local_var*>::iterator i = _map.find(name);
|
||||||
|
|
Loading…
Reference in a new issue