#73: tests on store commands, debugged rcl

This commit is contained in:
Louis Rubet 2017-05-25 12:33:51 +02:00
parent 8df5919bcb
commit e95c948660
4 changed files with 58 additions and 32 deletions

View file

@ -93,7 +93,7 @@ program::keyword_t program::_keywords[] =
{ cmd_keyword, "rcl", &program::rcl, "recall a variable. ex: 'name' rcl" },
{ cmd_keyword, "purge", &program::purge, "delete a variable. ex: 'name' purge" },
{ cmd_keyword, "vars", &program::vars, "list all variables" },
{ cmd_keyword, "edit", &program::edit, "edit a vriable content" },
{ cmd_keyword, "edit", &program::edit, "edit a variable content" },
//PROGRAM
{ cmd_undef, "", NULL, "\nPROGRAM"},

View file

@ -5,7 +5,7 @@ void sto(void)
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
string name(((symbol*)_stack->pop_back())->_value);
_global_heap->add(name, _stack->get_obj(0), _stack->get_len(0));
_heap->add(name, _stack->get_obj(0), _stack->get_len(0));
(void)_stack->pop_back();
}
@ -17,16 +17,12 @@ void rcl(void)
// recall a variable
object* obj;
unsigned int size;
int type;
string variable(((symbol*)_stack->back())->_value);
// mind the order of heaps
if (_local_heap.get(variable, obj, size)
|| ((_parent_local_heap != NULL) && (_parent_local_heap->get(variable, obj, size)))
|| _global_heap->get(variable, obj, size))
if (find_variable(variable, obj, size))
{
(void)_stack->pop_back();
object* new_obj = _stack->allocate_back(size, obj->_type);
stack::copy_and_push_back(obj, *_stack, size);
}
else
@ -68,16 +64,13 @@ void auto_rcl(symbol* symb)
{
object* obj;
unsigned int size;
int type;
string variable(symb->_value);
// mind the order of heaps
if (_local_heap.get(variable, obj, size)
|| ((_parent_local_heap != NULL) && (_parent_local_heap->get(variable, obj, size)))
|| _global_heap->get(variable, obj, size))
if (find_variable(variable, obj, size))
{
stack::copy_and_push_back(obj, *_stack, size);
if (type == cmd_program)
if (obj->_type == cmd_program)
eval();
}
else
@ -97,20 +90,7 @@ void purge(void)
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
string name(((symbol*)_stack->pop_back())->_value);
// mind the order of heaps
bool done = false;
if (!_local_heap.erase(name))
{
if ((_parent_local_heap != NULL) && (_parent_local_heap->erase(name)))
done = true;
else if (_global_heap->erase(name))
done = true;
}
else
done = true;
if (!done)
if (!_heap->erase(name))
ERR_CONTEXT(ret_unknown_variable);
}
@ -118,26 +98,32 @@ void vars(void)
{
object* obj;
unsigned int size;
int type;
program* parent = _parent_prog;
string name;
for (int i=0; i<(int)_global_heap->size(); i++)
// heap variables
for (int i=0; i<(int)_heap->size(); i++)
{
(void)_global_heap->get_by_index(i, name, obj, size);
(void)_heap->get_by_index(i, name, obj, size);
printf("var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]);
obj->show();
printf("\n");
}
if (_parent_local_heap != NULL)
// parents local variables
while (parent != NULL)
{
for (int i=0; i<(int)_parent_local_heap->size(); i++)
for (int i=0; i<(int)parent->_local_heap.size(); i++)
{
(void)_parent_local_heap->get_by_index(i, name, obj, size);
(void)parent->_local_heap.get_by_index(i, name, obj, size);
printf("local var %d: name '%s', type %s, value ", i+1, name.c_str(), cmd_type_string[obj->_type]);
obj->show();
printf("\n");
}
parent = parent->_parent_prog;
}
// local variables
for (int i=0; i<(int)_local_heap.size(); i++)
{
(void)_local_heap.get_by_index(i, name, obj, size);

View file

@ -5,3 +5,4 @@
#include test/06-real.txt
#include test/07-string.txt
#include test/08-test.txt
#include test/09-store.txt

39
test/09-store.txt Normal file
View file

@ -0,0 +1,39 @@
## VARS
# sto (1)
1 'a' sto
-> stack size should be 0
'a' a
-> stack should be 'a', 1
erase
# sto (2)
2 'a' sto a
-> stack should be 2
erase
# rcl (1)
'a' rcl
-> stack should be 2
erase
# rcl (2)
'b' rcl
-> error should be 5
-> stack should be 'b'
erase
# sto in prog then rcl
3 << 'r' sto >> eval r 'r' rcl
-> stack should be 3, 3
erase
# purge (1)
a 'a' purge a
-> stack should be 2, 'a'
erase
# purge (2)
'a' purge
-> error should be 5
-> stack size should be 0