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