Fixed bug related to register
dc / build (push) Successful in 15s
Details
dc / build (push) Successful in 15s
Details
This commit is contained in:
parent
5997f06bd0
commit
5de860a089
2
man.md
2
man.md
|
@ -355,6 +355,8 @@ This overwrites the top of the stack and does **NOT** follow the LIFO policy.
|
|||
Copy the value in top of the stack of register _r_ and push it onto the main stack.
|
||||
The value 0 is retrieved if the register is uninitialized. This does not alter the contents of _r_.
|
||||
|
||||
The current register value is the top of the register's stack.
|
||||
|
||||
**S**`r`
|
||||
|
||||
Pop the value off the top of the (main) stack and push it onto the stack of register _r_.
|
||||
|
|
25
src/eval.cpp
25
src/eval.cpp
|
@ -262,18 +262,20 @@ std::optional<std::string> Evaluate::parse_register_command(std::string token) {
|
|||
auto reg_name = token.at(1);
|
||||
auto head = this->stack.pop(true);
|
||||
|
||||
// Always discard previous instance of the register
|
||||
// i.e., initialize a new instance of register 'reg_name'
|
||||
this->regs.erase(reg_name);
|
||||
this->regs.insert(
|
||||
std::make_pair(reg_name, dc::Register{
|
||||
dc::Stack<std::string>(),
|
||||
std::unordered_map<int, std::string>()
|
||||
})
|
||||
);
|
||||
|
||||
// Populate register's 'reg_name' stack with top of main stack
|
||||
this->regs[reg_name].stack.push(head);
|
||||
// if register's stack exist, overwrite top of the stack
|
||||
// Otherwise allocate a new instance of the register
|
||||
auto it = this->regs.find(reg_name);
|
||||
if(it != this->regs.end()) { // Register exist
|
||||
auto head_idx = it->second.stack.size()-1;
|
||||
it->second.stack[head_idx] = head;
|
||||
} else { // Register does not exist
|
||||
this->regs[reg_name] = dc::Register{
|
||||
dc::Stack<std::string>(),
|
||||
std::unordered_map<int, std::string>()
|
||||
};
|
||||
this->regs[reg_name].stack.push(head);
|
||||
}
|
||||
} else if(token.at(0) == 'S') {
|
||||
// An uppercase 'S' pops the top of the main stack and
|
||||
// pushes it onto the stack of selected register.
|
||||
|
@ -299,6 +301,7 @@ std::optional<std::string> Evaluate::parse_register_command(std::string token) {
|
|||
dc::Stack<std::string>(),
|
||||
std::unordered_map<int, std::string>()
|
||||
};
|
||||
this->regs[reg_name].stack.push(head);
|
||||
}
|
||||
} else if(token.at(0) == 'L') {
|
||||
// An uppercase 'L' pops the top of the register's stack
|
||||
|
|
Loading…
Reference in New Issue