diff --git a/src/rpn-branch.h b/src/rpn-branch.h index 217cacb..c9e02f9 100644 --- a/src/rpn-branch.h +++ b/src/rpn-branch.h @@ -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; } diff --git a/src/rpn-program.h b/src/rpn-program.h index debcde2..4410f7a 100644 --- a/src/rpn-program.h +++ b/src/rpn-program.h @@ -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()<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 "<show(); + cout<size(); i++) + { + (void)_parent_local_heap->get_by_index(i, name, (void*&)obj, size, type); + cout<<"local var "<show(); + cout<show(); cout<size(); i++) - { - (void)_global_heap->get_by_index(i, name, (void*&)obj, size, type); - cout<<"var "<show(); - cout<