std::stringstream iss{post_string};
Token token{};
- std::stack<Node*> stack{};
- Node * new_node{nullptr};
+ std::stack<std::unique_ptr<Node>> stack{};
while (iss >> token)
{
if (token.is_integer())
{
- new_node = new Const_int{std::stoi(token)};
+ stack.push(std::make_unique<Const_int>(std::stoi(token)));
}
else if (token.is_decimal())
{
- new_node = new Const_double{std::stod(token)};
+ stack.push(std::make_unique<Const_double>(std::stod(token)));
}
else if ( isalpha(token.at(0)))
{
+ // Behövs ej pga smartpekare
+ /*
while(!stack.empty())
{
delete stack.top();
stack.pop();
- }
+ }*/
throw std::logic_error("Variables are not implemented");
+
}
else if (token.is_operator())
{
- Node * rhs{stack.top()};
+ std::unique_ptr<Node> rhs = std::move(stack.top());
stack.pop();
- Node * lhs{stack.top()};
+ std::unique_ptr<Node> lhs = std::move(stack.top());
stack.pop();
+
if (lhs == nullptr || rhs == nullptr)
{
+ // Behövs ej pga smartpekare
+ /*
while(!stack.empty())
{
delete stack.top();
stack.pop();
- }
+ }*/
throw std::logic_error("Missing operands");
+
}
if (token == "+")
{
- new_node = new Addition{lhs, rhs};
+ stack.push(std::make_unique<Addition>(lhs.release(), rhs.release()));
}
else if (token == "-")
{
- new_node = new Subtraction{lhs, rhs};
+ stack.push(std::make_unique<Subtraction>(lhs.release(), rhs.release()));
}
else if (token == "/")
{
- new_node = new Division{lhs, rhs};
+ stack.push(std::make_unique<Division>(lhs.release(), rhs.release()));
}
else if (token == "*")
{
- new_node = new Multiplication{lhs, rhs};
+ stack.push(std::make_unique<Multiplication>(lhs.release(), rhs.release()));
}
else if (token == "^")
{
- new_node = new Exponent{lhs, rhs};
+ stack.push(std::make_unique<Exponent>(lhs.release(), rhs.release()));
}
else
{
}
else
{
+ // Behövs ej pga smartpekare
+ /*
while(!stack.empty())
{
delete stack.top();
stack.pop();
- }
+ }*/
throw std::logic_error("Undefined characters in expression");
+
}
- stack.push(new_node);
}
if (stack.size() == 0)
{
throw std::logic_error("Empty expression");
}
- expression_root = stack.top();
+ expression_root = stack.top().release();
+
if (stack.size() > 1)
{
+ // Behövs ej pga smartpekare
+ /*
while(!stack.empty())
{
delete stack.top();
stack.pop();
}
+ */
throw std::logic_error("Missing operators");
}
}