mirror of
https://github.com/louisrubet/rpn
synced 2025-01-01 18:20:06 +01:00
Programs have parent heap
This commit is contained in:
parent
50d396fbf3
commit
f27efe58d8
5 changed files with 62 additions and 37 deletions
|
@ -72,7 +72,7 @@ int rpn_for(branch& myobj)
|
|||
// store symbol with first value
|
||||
number num;
|
||||
num.set(myobj.farg1);
|
||||
_local_heap->add(string(sym->_value), &num, num.size(), cmd_number);
|
||||
_local_heap.add(string(sym->_value), &num, num.size(), cmd_number);
|
||||
|
||||
return myobj.arg1 + 1;
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ int rpn_next(branch& myobj)
|
|||
int type;
|
||||
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ int rpn_step(branch& myobj)
|
|||
int type;
|
||||
symbol* var = (symbol*)seq_obj(start_or_for->arg1);
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,11 @@ void eval(void)
|
|||
unsigned int size;
|
||||
int type;
|
||||
string variable(((symbol*)_stack->back())->_value);
|
||||
if (_local_heap->get(variable, obj, size, type) || _global_heap->get(variable, obj, size, type))
|
||||
|
||||
// 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))
|
||||
{
|
||||
// if variable holds a program, run this program
|
||||
if (type == cmd_program)
|
||||
|
@ -50,7 +54,8 @@ void eval(void)
|
|||
if (program::parse(prog_text.c_str(), prog) == ret_ok)
|
||||
{
|
||||
// run it
|
||||
prog.run(*_stack, *_global_heap);
|
||||
cout<<"eval: avant run: _local_heap.size = "<<_local_heap.size()<<endl;//lru
|
||||
prog.run(*_stack, *_global_heap, &_local_heap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,11 @@ void rcl(void)
|
|||
unsigned int size;
|
||||
int type;
|
||||
string variable(((symbol*)_stack->back())->_value);
|
||||
if (_local_heap->get(variable, obj, size, type) || _global_heap->get(variable, obj, size, type))
|
||||
|
||||
// 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))
|
||||
{
|
||||
_stack->pop_back();
|
||||
_stack->push_back(obj, size, type);
|
||||
|
@ -37,7 +41,12 @@ void auto_rcl(symbol* symb)
|
|||
void* obj;
|
||||
unsigned int size;
|
||||
int type;
|
||||
if (_local_heap->get(string(symb->_value), obj, size, type) || _global_heap->get(string(symb->_value), obj, size, type))
|
||||
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))
|
||||
{
|
||||
_stack->push_back(obj, size, type);
|
||||
if (type == cmd_program)
|
||||
|
@ -58,18 +67,22 @@ void purge(void)
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
|
||||
string name(((symbol*)_stack->back())->_value);
|
||||
if (_local_heap->erase(name))
|
||||
|
||||
// mind the order of heaps
|
||||
bool done = false;
|
||||
if (!_local_heap.erase(name))
|
||||
{
|
||||
//TODO another error
|
||||
ERR_CONTEXT(ret_bad_operand_type);
|
||||
if ((_parent_local_heap != NULL) && (_parent_local_heap->erase(name)))
|
||||
done = true;
|
||||
else if (_global_heap->erase(name))
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_global_heap->erase(name))
|
||||
_stack->pop_back();
|
||||
else
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
}
|
||||
done = true;
|
||||
|
||||
_stack->pop_back();
|
||||
if (!done)
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
}
|
||||
|
||||
void vars(void)
|
||||
|
@ -79,19 +92,28 @@ void vars(void)
|
|||
int type;
|
||||
string name;
|
||||
|
||||
for (int i=0; i<(int)_local_heap->size(); i++)
|
||||
for (int i=0; i<(int)_global_heap->size(); i++)
|
||||
{
|
||||
(void)_local_heap->get_by_index(i, name, (void*&)obj, size, type);
|
||||
(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++)
|
||||
{
|
||||
(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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,6 +29,6 @@ void strout()
|
|||
if (program::parse(entry.c_str(), prog) == ret_ok)
|
||||
{
|
||||
// run it
|
||||
prog.run(*_stack, *_global_heap);
|
||||
prog.run(*_stack, *_global_heap, &_local_heap);
|
||||
}
|
||||
}
|
||||
|
|
18
src/rpn.cpp
18
src/rpn.cpp
|
@ -355,9 +355,8 @@ public:
|
|||
program() { }
|
||||
|
||||
// run this program
|
||||
ret_value run(stack& stk, heap& hp, heap* local_hp = NULL)
|
||||
ret_value run(stack& stk, heap& hp, heap* parent_local_hp = NULL)
|
||||
{
|
||||
heap local_heap;
|
||||
bool go_out = false;
|
||||
ret_value ret = ret_ok;
|
||||
cmd_type_t type;
|
||||
|
@ -368,12 +367,8 @@ public:
|
|||
// global heap comes from outside
|
||||
_global_heap = &hp;
|
||||
|
||||
// local heap can come from outside
|
||||
// if not, set a new local heap
|
||||
if (local_hp != NULL)
|
||||
_local_heap = local_hp;
|
||||
else
|
||||
_local_heap = &local_heap;
|
||||
// parent local heap comes from outside
|
||||
_parent_local_heap = parent_local_hp;
|
||||
|
||||
_err = ret_ok;
|
||||
_err_context = "";
|
||||
|
@ -693,9 +688,12 @@ public:
|
|||
private:
|
||||
ret_value _err;
|
||||
string _err_context;
|
||||
stack* _stack;
|
||||
|
||||
stack* _stack;
|
||||
|
||||
heap* _global_heap;
|
||||
heap* _local_heap;
|
||||
heap _local_heap;
|
||||
heap* _parent_local_heap;
|
||||
|
||||
// helpers for keywords implementation
|
||||
floating_t getf()
|
||||
|
|
Loading…
Reference in a new issue