mirror of
https://github.com/louisrubet/rpn
synced 2024-11-17 07:47:50 +01:00
Local heap mecanism for local variables
This commit is contained in:
parent
a801fc1c95
commit
5164c90925
5 changed files with 50 additions and 26 deletions
|
@ -72,7 +72,7 @@ int rpn_for(branch& myobj)
|
|||
// store symbol with first value
|
||||
number num;
|
||||
num.set(myobj.farg1);
|
||||
_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 (_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 (_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,7 @@ void eval(void)
|
|||
unsigned int size;
|
||||
int type;
|
||||
string variable(((symbol*)_stack->back())->_value);
|
||||
if (_heap->get(variable, obj, size, type))
|
||||
if (_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 +50,7 @@ void eval(void)
|
|||
if (program::parse(prog_text.c_str(), prog) == ret_ok)
|
||||
{
|
||||
// run it
|
||||
prog.run(*_stack, *_heap);
|
||||
prog.run(*_stack, *_global_heap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,9 +117,10 @@ int inprog(branch& myobj)
|
|||
}
|
||||
|
||||
// load variables
|
||||
heap local_heap;
|
||||
for (unsigned int i = myobj.arg1 + count_symbols; i > myobj.arg1; i--)
|
||||
{
|
||||
_heap->add(string(((symbol*)seq_obj(i))->_value), _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0));
|
||||
local_heap.add(string(((symbol*)seq_obj(i))->_value), _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0));
|
||||
_stack->pop_back();
|
||||
}
|
||||
|
||||
|
@ -131,7 +132,8 @@ int inprog(branch& myobj)
|
|||
if (program::parse(entry.c_str(), prog) == ret_ok)
|
||||
{
|
||||
// run it
|
||||
prog.run(*_stack, *_heap);
|
||||
prog.run(*_stack, *_global_heap, &local_heap);
|
||||
//TODO here: _local_heap free
|
||||
}
|
||||
|
||||
// point on next command
|
||||
|
|
|
@ -6,7 +6,7 @@ void sto(void)
|
|||
|
||||
string name(((symbol*)_stack->get_obj(0))->_value);
|
||||
_stack->pop_back();
|
||||
_heap->add(name, _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0));
|
||||
_global_heap->add(name, _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0));
|
||||
_stack->pop_back();
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ void rcl(void)
|
|||
unsigned int size;
|
||||
int type;
|
||||
string variable(((symbol*)_stack->back())->_value);
|
||||
if (_heap->get(variable, obj, size, type))
|
||||
if (_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 +37,7 @@ void auto_rcl(symbol* symb)
|
|||
void* obj;
|
||||
unsigned int size;
|
||||
int type;
|
||||
if (_heap->get(string(symb->_value), obj, size, type))
|
||||
if (_local_heap->get(string(symb->_value), obj, size, type) || _global_heap->get(string(symb->_value), obj, size, type))
|
||||
_stack->push_back(obj, size, type);
|
||||
else
|
||||
_stack->push_back(symb, symb->size(), cmd_symbol);
|
||||
|
@ -52,10 +52,18 @@ void purge(void)
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
|
||||
string name(((symbol*)_stack->back())->_value);
|
||||
if (!_heap->erase(name))
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
if (_local_heap->erase(name))
|
||||
{
|
||||
//TODO another error
|
||||
ERR_CONTEXT(ret_bad_operand_type);
|
||||
}
|
||||
else
|
||||
_stack->pop_back();
|
||||
{
|
||||
if (!_global_heap->erase(name))
|
||||
_stack->pop_back();
|
||||
else
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
}
|
||||
}
|
||||
|
||||
void vars(void)
|
||||
|
@ -65,9 +73,17 @@ void vars(void)
|
|||
int type;
|
||||
string name;
|
||||
|
||||
for (int i=0; i<(int)_heap->size(); i++)
|
||||
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)_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;
|
||||
|
|
|
@ -29,6 +29,6 @@ void strout()
|
|||
if (program::parse(entry.c_str(), prog) == ret_ok)
|
||||
{
|
||||
// run it
|
||||
prog.run(*_stack, *_heap);
|
||||
prog.run(*_stack, *_global_heap);
|
||||
}
|
||||
}
|
||||
|
|
26
src/rpn.cpp
26
src/rpn.cpp
|
@ -361,14 +361,19 @@ public:
|
|||
program() { }
|
||||
|
||||
// run this program
|
||||
ret_value run(stack& stk, heap& hp)
|
||||
ret_value run(stack& stk, heap& hp, heap* local_hp = NULL)
|
||||
{
|
||||
bool go_out = false;
|
||||
heap local_heap;
|
||||
bool go_out = false;
|
||||
ret_value ret = ret_ok;
|
||||
cmd_type_t type;
|
||||
|
||||
_stack = &stk;
|
||||
_heap = &hp;
|
||||
_global_heap = &hp;
|
||||
if (local_hp != NULL)
|
||||
_local_heap = local_hp;
|
||||
else
|
||||
_local_heap = &local_heap;
|
||||
_err = ret_ok;
|
||||
_err_context = "";
|
||||
|
||||
|
@ -688,7 +693,8 @@ private:
|
|||
ret_value _err;
|
||||
string _err_context;
|
||||
stack* _stack;
|
||||
heap* _heap;
|
||||
heap* _global_heap;
|
||||
heap* _local_heap;
|
||||
|
||||
// helpers for keywords implementation
|
||||
floating_t getf()
|
||||
|
@ -770,8 +776,8 @@ static void apply_default(void)
|
|||
//
|
||||
int _tmain(int argc, _TCHAR* argv[])
|
||||
{
|
||||
heap hp;
|
||||
stack st;
|
||||
heap global_heap;
|
||||
stack global_stack;
|
||||
int ret = 0;
|
||||
|
||||
// apply default configuration
|
||||
|
@ -790,10 +796,10 @@ int _tmain(int argc, _TCHAR* argv[])
|
|||
else
|
||||
{
|
||||
// run it
|
||||
if (prog.run(st, hp) == ret_good_bye)
|
||||
if (prog.run(global_stack, global_heap) == ret_good_bye)
|
||||
break;
|
||||
else
|
||||
program::show_stack(st);
|
||||
program::show_stack(global_stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -818,8 +824,8 @@ int _tmain(int argc, _TCHAR* argv[])
|
|||
string separator = "";
|
||||
|
||||
// run it
|
||||
ret = prog.run(st, hp);
|
||||
program::show_stack(st, separator);
|
||||
ret = prog.run(global_stack, global_heap);
|
||||
program::show_stack(global_stack, separator);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue