Added smart pointers
authorAlma Dejus <almde515@student.liu.se>
Sat, 28 Oct 2023 13:42:38 +0000 (15:42 +0200)
committerAlma Dejus <almde515@student.liu.se>
Sat, 28 Oct 2023 13:42:38 +0000 (15:42 +0200)
src/expression.cc
src/operators.h

index 67378b828bcc8c5202cf1b0c70fdc038da6f78e3..b5b3463816aff24ff9f7141d4c5f1e8a472a423c 100644 (file)
@@ -8,64 +8,70 @@ Expression::Expression(const std::string & expression)
     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
             {
@@ -74,29 +80,35 @@ Expression::Expression(const std::string & expression)
         }
         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");
     }
 }
index bfe5bb655501b4b1b5d041f60f178a9ef9c718ac..fb7f92913b9a326b7412ec79d878f6d56bd359cf 100644 (file)
@@ -5,6 +5,7 @@
 #include <sstream>
 #include <stdexcept>
 #include <cmath>
+#include <memory>
 
 #include "node.h"