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