From c7cfa2d2409bf46e78f42412ca75af4d4906bf33 Mon Sep 17 00:00:00 2001
From: Mattias Ajander <mattias@ajander.se>
Date: Wed, 9 Apr 2025 01:56:04 +0200
Subject: [PATCH] Destructor for scope, prevent functions with the same name as
 built in functions

---
 include/ast/declaration/FunctionNode.h | 1 +
 source/ast/declaration/FunctionNode.cc | 6 ++++++
 source/parser/Scope.cc                 | 8 +++++++-
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/ast/declaration/FunctionNode.h b/include/ast/declaration/FunctionNode.h
index 023254a..9711e82 100644
--- a/include/ast/declaration/FunctionNode.h
+++ b/include/ast/declaration/FunctionNode.h
@@ -3,6 +3,7 @@
 #include "ast/BlockNode.h"
 #include "ast/declaration/VariableNode.h"
 #include "ast/expression/ExpressionNode.h"
+#include "parser/BuiltIn.h"
 #include "parser/Registry.h"
 #include "parser/Scope.h"
 #include "token/Token.h"
diff --git a/source/ast/declaration/FunctionNode.cc b/source/ast/declaration/FunctionNode.cc
index 240c375..61977bc 100644
--- a/source/ast/declaration/FunctionNode.cc
+++ b/source/ast/declaration/FunctionNode.cc
@@ -28,6 +28,12 @@ FunctionNode::~FunctionNode()
 
 Node* FunctionNode::evaluate() const
 {
+    // Check if the function is built-in
+    if (BuiltIn::functions.find(identifier) != BuiltIn::functions.end())
+    {
+        throw RuntimeError(get_location(), "Cannot overwrite built-in function: " + identifier);
+    }
+
     // Register the function in the registry
     Registry::instance().add_function(const_cast<FunctionNode*>(this));
     // Add the function to the current scope
diff --git a/source/parser/Scope.cc b/source/parser/Scope.cc
index 8299717..d771d5c 100644
--- a/source/parser/Scope.cc
+++ b/source/parser/Scope.cc
@@ -4,7 +4,13 @@ namespace funk
 {
 
 Scope::Scope() {}
-Scope::~Scope() {}
+Scope::~Scope()
+{
+    for (auto& scope : scopes)
+    {
+        for (auto& [name, node] : scope) { delete node; }
+    }
+}
 
 Scope& Scope::instance()
 {
-- 
GitLab