mirror of
https://github.com/louisrubet/rpn
synced 2025-02-07 08:45:48 +01:00
heap by reference
This commit is contained in:
parent
06a103fa8f
commit
bff2a3ec0c
5 changed files with 22 additions and 22 deletions
|
@ -115,7 +115,7 @@ int main(int argc, char* argv[]) {
|
|||
// entry loop
|
||||
while (go_on) {
|
||||
// make program from interactive entry
|
||||
program prog(&_global_stack, &_global_heap);
|
||||
program prog(&_global_stack, _global_heap);
|
||||
switch (program::entry(prog)) {
|
||||
case ret_good_bye:
|
||||
go_on = false;
|
||||
|
@ -140,7 +140,7 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
// run with cmd line arguments
|
||||
else {
|
||||
program prog(&_global_stack, &_global_heap);
|
||||
program prog(&_global_stack, _global_heap);
|
||||
string entry;
|
||||
int i;
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ struct if_layout_t {
|
|||
//< program class: the class containing a string parser, all the programs keywords, a stack for running the program
|
||||
class program : public deque<object*> {
|
||||
public:
|
||||
program(rpnstack* stk, heap* hp, program* parent = nullptr):_stack(stk),_heap(hp),_parent(parent) {
|
||||
program(rpnstack* stk, heap& hp, program* parent = nullptr):_stack(stk),_heap(hp),_parent(parent) {
|
||||
interrupt_now = false;
|
||||
}
|
||||
virtual ~program() {
|
||||
|
@ -85,7 +85,7 @@ class program : public deque<object*> {
|
|||
rpnstack* _stack;
|
||||
|
||||
// global heap (sto, rcl)
|
||||
heap* _heap;
|
||||
heap& _heap;
|
||||
|
||||
// local heap for local loop variables (for..next)
|
||||
heap _local_heap;
|
||||
|
|
|
@ -22,7 +22,7 @@ bool program::find_variable(string& variable, object*& obj) {
|
|||
parent = parent->_parent;
|
||||
}
|
||||
if (!found) {
|
||||
if (_heap->get(variable, obj)) found = true;
|
||||
if (_heap.get(variable, obj)) found = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,12 +7,12 @@ void program::rpn_sto(void) {
|
|||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
|
||||
// store symbol with first value
|
||||
const auto it = _heap->find(_stack->value<ostring>(0));
|
||||
if (it != _heap->end()) {
|
||||
const auto it = _heap.find(_stack->value<ostring>(0));
|
||||
if (it != _heap.end()) {
|
||||
delete it->second;
|
||||
_heap->erase(it);
|
||||
_heap.erase(it);
|
||||
}
|
||||
(*_heap)[_stack->value<ostring>(0)] = _stack->at(1)->clone();
|
||||
_heap[_stack->value<ostring>(0)] = _stack->at(1)->clone();
|
||||
_stack->pop_front(2);
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ void program::rpn_sto(void) {
|
|||
void program::rpn_stoadd(void) {
|
||||
MIN_ARGUMENTS(2);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
if (_heap->find(_stack->value<ostring>(0)) == _heap->end()) {
|
||||
if (_heap.find(_stack->value<ostring>(0)) == _heap.end()) {
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
return;
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ void program::rpn_stoadd(void) {
|
|||
void program::rpn_stosub(void) {
|
||||
MIN_ARGUMENTS(2);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
if (_heap->find(_stack->value<ostring>(0)) == _heap->end()) {
|
||||
if (_heap.find(_stack->value<ostring>(0)) == _heap.end()) {
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
return;
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ void program::rpn_stosub(void) {
|
|||
void program::rpn_stomul(void) {
|
||||
MIN_ARGUMENTS(2);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
if (_heap->find(_stack->value<ostring>(0)) == _heap->end()) {
|
||||
if (_heap.find(_stack->value<ostring>(0)) == _heap.end()) {
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
return;
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ void program::rpn_stomul(void) {
|
|||
void program::rpn_stodiv(void) {
|
||||
MIN_ARGUMENTS(2);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
if (_heap->find(_stack->value<ostring>(0)) == _heap->end()) {
|
||||
if (_heap.find(_stack->value<ostring>(0)) == _heap.end()) {
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
return;
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ void program::rpn_stodiv(void) {
|
|||
void program::rpn_stoneg(void) {
|
||||
MIN_ARGUMENTS(1);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
if (_heap->find(_stack->value<ostring>(0)) == _heap->end()) {
|
||||
if (_heap.find(_stack->value<ostring>(0)) == _heap.end()) {
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
return;
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ void program::rpn_stoneg(void) {
|
|||
void program::rpn_stoinv(void) {
|
||||
MIN_ARGUMENTS(1);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
if (_heap->find(_stack->value<ostring>(0)) == _heap->end()) {
|
||||
if (_heap.find(_stack->value<ostring>(0)) == _heap.end()) {
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
return;
|
||||
}
|
||||
|
@ -174,10 +174,10 @@ void program::rpn_purge(void) {
|
|||
MIN_ARGUMENTS(1);
|
||||
ARG_MUST_BE_OF_TYPE(0, cmd_symbol);
|
||||
|
||||
const auto i = _heap->find(_stack->value<symbol>(0));
|
||||
if (i != _heap->end()) {
|
||||
const auto i = _heap.find(_stack->value<symbol>(0));
|
||||
if (i != _heap.end()) {
|
||||
delete i->second;
|
||||
_heap->erase(i);
|
||||
_heap.erase(i);
|
||||
} else
|
||||
ERR_CONTEXT(ret_unknown_variable);
|
||||
_stack->pop();
|
||||
|
@ -191,8 +191,8 @@ void program::rpn_vars(void) {
|
|||
string name;
|
||||
|
||||
// heap variables
|
||||
for (int i = 0; i < (int)_heap->size(); i++) {
|
||||
(void)_heap->get_by_index(i, name, obj);
|
||||
for (int i = 0; i < (int)_heap.size(); i++) {
|
||||
(void)_heap.get_by_index(i, name, obj);
|
||||
cout<<"var "<<i+1<<": name '"<<name<<"', type "<<obj->name()<<", value ";
|
||||
obj->show(cout);
|
||||
cout<<endl;
|
||||
|
@ -220,4 +220,4 @@ void program::rpn_vars(void) {
|
|||
|
||||
/// @brief clusr keyword implementation
|
||||
///
|
||||
void program::rpn_clusr(void) { _heap->clear(); }
|
||||
void program::rpn_clusr(void) { _heap.clear(); }
|
||||
|
|
|
@ -227,7 +227,7 @@ void program::test(string test_filename, int& total_tests, int& total_tests_fail
|
|||
// parse entry and run line
|
||||
entry = regex_replace(entry, regex("`"), "");
|
||||
if (!entry.empty()) {
|
||||
program prog(&stk, &hp);
|
||||
program prog(&stk, hp);
|
||||
ret = program::parse(entry, prog);
|
||||
if (ret == ret_ok) {
|
||||
// run it
|
||||
|
|
Loading…
Add table
Reference in a new issue