From ced6c95d7f4a509ca25d82f12794d245006211ef Mon Sep 17 00:00:00 2001
From: Mattias Ajander <mattias@ajander.se>
Date: Tue, 1 Apr 2025 21:49:37 +0200
Subject: [PATCH] Removed eval caching since nodes will be evaluated in loops
 and such.

---
 include/ast/Node.h                    | 3 +--
 source/ast/BlockNode.cc               | 4 +---
 source/ast/expression/BinaryOpNode.cc | 4 +---
 source/ast/expression/LiteralNode.cc  | 3 +--
 source/ast/expression/UnaryOpNode.cc  | 4 +---
 5 files changed, 5 insertions(+), 13 deletions(-)

diff --git a/include/ast/Node.h b/include/ast/Node.h
index 0aace72..b6e901a 100644
--- a/include/ast/Node.h
+++ b/include/ast/Node.h
@@ -48,8 +48,7 @@ public:
     SourceLocation get_location() const;
 
 protected:
-    SourceLocation location;            ///< Source location where this node appears in the code
-    mutable Node* cached_eval{nullptr}; ///< Cached evaluation result
+    SourceLocation location; ///< Source location where this node appears in the code
 };
 
 } // namespace funk
diff --git a/source/ast/BlockNode.cc b/source/ast/BlockNode.cc
index 8893c99..c8cd698 100644
--- a/source/ast/BlockNode.cc
+++ b/source/ast/BlockNode.cc
@@ -21,11 +21,9 @@ Vector<Node*> BlockNode::get_statements() const
 
 Node* BlockNode::evaluate() const
 {
-    if (cached_eval) { return cached_eval; }
-
     Node* result{};
     for (Node* statement : statements) { result = statement->evaluate(); }
-    return cached_eval = result;
+    return result;
 }
 
 String BlockNode::to_s() const
diff --git a/source/ast/expression/BinaryOpNode.cc b/source/ast/expression/BinaryOpNode.cc
index abe6cd1..a19bdc4 100644
--- a/source/ast/expression/BinaryOpNode.cc
+++ b/source/ast/expression/BinaryOpNode.cc
@@ -16,8 +16,6 @@ BinaryOpNode::~BinaryOpNode()
 
 Node* BinaryOpNode::evaluate() const
 {
-    if (cached_eval) { return cached_eval; }
-
     NodeValue left_value{left->get_value()};
     NodeValue right_value{right->get_value()};
 
@@ -53,7 +51,7 @@ Node* BinaryOpNode::evaluate() const
         throw RuntimeError(location, e.what());
     }
 
-    return cached_eval = new LiteralNode(location, result);
+    return new LiteralNode(location, result);
 }
 
 String BinaryOpNode::to_s() const
diff --git a/source/ast/expression/LiteralNode.cc b/source/ast/expression/LiteralNode.cc
index f14a553..9a074de 100644
--- a/source/ast/expression/LiteralNode.cc
+++ b/source/ast/expression/LiteralNode.cc
@@ -7,8 +7,7 @@ LiteralNode::LiteralNode(const SourceLocation& loc, NodeValue value) : Expressio
 
 Node* LiteralNode::evaluate() const
 {
-    if (cached_eval) { return cached_eval; }
-    return cached_eval = const_cast<LiteralNode*>(this);
+    return const_cast<LiteralNode*>(this);
 }
 
 String LiteralNode::to_s() const
diff --git a/source/ast/expression/UnaryOpNode.cc b/source/ast/expression/UnaryOpNode.cc
index 60dcdb8..05d69df 100644
--- a/source/ast/expression/UnaryOpNode.cc
+++ b/source/ast/expression/UnaryOpNode.cc
@@ -15,8 +15,6 @@ UnaryOpNode::~UnaryOpNode()
 
 Node* UnaryOpNode::evaluate() const
 {
-    if (cached_eval) { return cached_eval; }
-
     NodeValue expr_value{expr->get_value()};
     NodeValue result{};
 
@@ -38,7 +36,7 @@ Node* UnaryOpNode::evaluate() const
         throw RuntimeError(location, e.what());
     }
 
-    return cached_eval = new LiteralNode(location, result);
+    return new LiteralNode(location, result);
 }
 
 String UnaryOpNode::to_s() const
-- 
GitLab