From 496d52d1a1852e93f8a4204bb02a2c1cdcb22fd0 Mon Sep 17 00:00:00 2001
From: vladimir_holy <vladimir.holy7@gmail.com>
Date: Sun, 24 Mar 2024 16:05:06 +0100
Subject: [PATCH] re #11131 - implemented Memory class

---
 Core/CodeNS/Code.cs                  |   2 +-
 Core/ComplexLogicNS/DoWhile.cs       |   2 +-
 Core/ComplexLogicNS/For.cs           |   2 +-
 Core/ComplexLogicNS/IComplexLogic.cs |   2 +-
 Core/ComplexLogicNS/If.cs            |   2 +-
 Core/ComplexLogicNS/While.cs         |   2 +-
 Core/DataTypeNS/Array.cs             |   7 +-
 Core/DataTypeNS/EDataType.cs         |   2 +-
 Core/DataTypeNS/IHeapObj.cs          |   5 +-
 Core/DataTypeNS/StackObj.cs          |   4 +-
 Core/DataTypeNS/Structure.cs         |   7 +-
 Core/FunctionNS/ACommands.cs         |   7 --
 Core/FunctionNS/Function.cs          |  33 ++++++-
 Core/FunctionNS/IStepable.cs         |   2 +-
 Core/MemoryNS/Memory.cs              | 125 +++++++++++++++++++++++++++
 15 files changed, 181 insertions(+), 23 deletions(-)
 delete mode 100644 Core/FunctionNS/ACommands.cs

diff --git a/Core/CodeNS/Code.cs b/Core/CodeNS/Code.cs
index 80c8f83..915401d 100644
--- a/Core/CodeNS/Code.cs
+++ b/Core/CodeNS/Code.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.CodeNS
 {
-    internal class Code
+    public class Code
     {
     }
 }
diff --git a/Core/ComplexLogicNS/DoWhile.cs b/Core/ComplexLogicNS/DoWhile.cs
index 3c9a1f8..04fc346 100644
--- a/Core/ComplexLogicNS/DoWhile.cs
+++ b/Core/ComplexLogicNS/DoWhile.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.ComplexLogicNS
 {
-    internal class DoWhile
+    public class DoWhile
     {
     }
 }
diff --git a/Core/ComplexLogicNS/For.cs b/Core/ComplexLogicNS/For.cs
index 88f6ebe..8b71eed 100644
--- a/Core/ComplexLogicNS/For.cs
+++ b/Core/ComplexLogicNS/For.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.ComplexLogicNS
 {
-    internal class For
+    public class For
     {
     }
 }
diff --git a/Core/ComplexLogicNS/IComplexLogic.cs b/Core/ComplexLogicNS/IComplexLogic.cs
index 0257b47..f4e175e 100644
--- a/Core/ComplexLogicNS/IComplexLogic.cs
+++ b/Core/ComplexLogicNS/IComplexLogic.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.ComplexLogicNS
 {
-    internal interface IComplexLogic
+    public interface IComplexLogic
     {
     }
 }
diff --git a/Core/ComplexLogicNS/If.cs b/Core/ComplexLogicNS/If.cs
index 58b301e..3b5d2ef 100644
--- a/Core/ComplexLogicNS/If.cs
+++ b/Core/ComplexLogicNS/If.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.ComplexLogicNS
 {
-    internal class If
+    public class If
     {
     }
 }
diff --git a/Core/ComplexLogicNS/While.cs b/Core/ComplexLogicNS/While.cs
index 5102e4e..577f7ba 100644
--- a/Core/ComplexLogicNS/While.cs
+++ b/Core/ComplexLogicNS/While.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.ComplexLogicNS
 {
-    internal class While
+    public class While
     {
     }
 }
diff --git a/Core/DataTypeNS/Array.cs b/Core/DataTypeNS/Array.cs
index 980771f..0f68de4 100644
--- a/Core/DataTypeNS/Array.cs
+++ b/Core/DataTypeNS/Array.cs
@@ -6,7 +6,12 @@ using System.Threading.Tasks;
 
 namespace Core.DataTypeNS
 {
-    internal class Array
+    public class Array : IHeapObj
     {
+        public IHeapObj clone()
+        {
+            // TODO
+            return new Array();
+        }
     }
 }
diff --git a/Core/DataTypeNS/EDataType.cs b/Core/DataTypeNS/EDataType.cs
index 3d30a40..bea7cc6 100644
--- a/Core/DataTypeNS/EDataType.cs
+++ b/Core/DataTypeNS/EDataType.cs
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace Core.DataTypeNS
 {
-    enum EDataType
+    public enum EDataType
     {
         INTEGER, REAL, CHAR, STRING, BOOLEAN, STRUCTURE
     }
diff --git a/Core/DataTypeNS/IHeapObj.cs b/Core/DataTypeNS/IHeapObj.cs
index e61c064..b74af04 100644
--- a/Core/DataTypeNS/IHeapObj.cs
+++ b/Core/DataTypeNS/IHeapObj.cs
@@ -4,9 +4,10 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Coree.DataTypeNS
+namespace Core.DataTypeNS
 {
-    internal interface IHeapObj
+    public interface IHeapObj
     {
+        public IHeapObj clone();
     }
 }
diff --git a/Core/DataTypeNS/StackObj.cs b/Core/DataTypeNS/StackObj.cs
index 0c542f0..71d3401 100644
--- a/Core/DataTypeNS/StackObj.cs
+++ b/Core/DataTypeNS/StackObj.cs
@@ -4,9 +4,9 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Coree.DataTypeNS
+namespace Core.DataTypeNS
 {
-    internal class StackObj
+    public class StackObj
     {
     }
 }
diff --git a/Core/DataTypeNS/Structure.cs b/Core/DataTypeNS/Structure.cs
index 401a8ee..f6048b8 100644
--- a/Core/DataTypeNS/Structure.cs
+++ b/Core/DataTypeNS/Structure.cs
@@ -6,7 +6,12 @@ using System.Threading.Tasks;
 
 namespace Core.DataTypeNS
 {
-    internal class Structure
+    public class Structure : IHeapObj
     {
+        public IHeapObj clone()
+        {
+            // TODO
+            return new Structure();
+        }
     }
 }
diff --git a/Core/FunctionNS/ACommands.cs b/Core/FunctionNS/ACommands.cs
deleted file mode 100644
index e8f9c2a..0000000
--- a/Core/FunctionNS/ACommands.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace Core.FunctionNS
-{
-    internal abstract class ACommands : IStepable
-    {
-        abstract public bool step();
-    }
-}
\ No newline at end of file
diff --git a/Core/FunctionNS/Function.cs b/Core/FunctionNS/Function.cs
index ceea960..875f189 100644
--- a/Core/FunctionNS/Function.cs
+++ b/Core/FunctionNS/Function.cs
@@ -1,10 +1,39 @@
+using Core.DataTypeNS;
+using System.Collections;
+using System.Transactions;
+
 namespace Core.FunctionNS
 {
-    internal class Function : ACommands
+    public class Function : IStepable
     {
-        public override bool step()
+        /*
+         * Atributtes
+         */
+        private String? name;
+        private Dictionary<String, StackObj> variables = new Dictionary<string, StackObj>();
+
+        /*
+         * Getters, setters
+         */
+        public string Name
+        {
+            get { return name; }
+            set { name = value; }
+        }
+
+        /*
+         * Code
+         */
+        public bool step()
         {
             return true;
         }
+
+        public ArrayList getStackItems()
+        {
+            ArrayList list = new ArrayList();
+            // TODO
+            return list;
+        }
     }
 }
\ No newline at end of file
diff --git a/Core/FunctionNS/IStepable.cs b/Core/FunctionNS/IStepable.cs
index e9ef723..0d132ef 100644
--- a/Core/FunctionNS/IStepable.cs
+++ b/Core/FunctionNS/IStepable.cs
@@ -1,6 +1,6 @@
 namespace Core.FunctionNS
 {
-    internal interface IStepable
+    public interface IStepable
     {
         public bool step();
     }
diff --git a/Core/MemoryNS/Memory.cs b/Core/MemoryNS/Memory.cs
index 7994e63..e9a91e2 100644
--- a/Core/MemoryNS/Memory.cs
+++ b/Core/MemoryNS/Memory.cs
@@ -6,8 +6,133 @@ using System.Threading.Tasks;
 
 namespace Core.MemoryNS
 {
+    using FunctionNS;
+    using DataTypeNS;
+    using System.Collections;
+
     public static class Memory
     {
+        private static Stack<Function> functionStack = new Stack<Function>();
+        private static ArrayList heap = new ArrayList();
+        private static Stack<StackObj> returnStack = new Stack<StackObj>();
+
+        /*
+         * FuntionStack methods
+         */
+        public static void pushFunction(Function function) { functionStack.Push(function); }
+
+        public static Function? popFunction() 
+        {
+            try { return functionStack.Pop(); } 
+            catch { return null; }
+        }
+
+        public static Function? peekFunction() 
+        {
+            try { return functionStack.Peek(); }
+            catch { return null; }
+        }
+
+        /*
+         * Heap methods
+         */
+        public static int addHeapObj(IHeapObj heapObj) 
+        {
+            for (int i = 0; i < heap.Count + 1; i++)
+            {
+                try
+                {
+                    if (heap[i] == null)
+                    {
+                        heap.Insert(i, heapObj);
+                        return i;
+                    };
+                } 
+                catch
+                {
+                    heap.Insert(i, heapObj);
+                    return i;
+                };
+            }
+            return heap.Count;
+        }
+
+        public static int copyHeapObj(int index)
+        {
+            try
+            {
+                IHeapObj heapObj = (IHeapObj)heap[index];
+                if (heapObj == null)
+                {
+                    return -1;
+                } else
+                {
+                    return addHeapObj(heapObj.clone());
+                }
+            } catch
+            {
+                return -1;
+            }
+        }
+
+        public static IHeapObj? getHeapObj(int index)
+        {
+            try
+            {
+                return (IHeapObj)heap[index];
+            }
+            catch
+            {
+                return null;
+            }
+        }
+
+        public static void removeHeapObj(int index)
+        {
+            try
+            {
+                heap[index] = null;
+            } catch { }
+        }
+
+        /*
+        * ReturnStack methods
+        */
+        public static void pushReturnValue(StackObj stackObj) { returnStack.Push(stackObj); }
+
+        public static StackObj? popReturnValue()
+        {
+            try { return returnStack.Pop(); }
+            catch { return null; }
+        }
+
+        /*
+         * Methods to return content of Stack and Heap
+         */
+
+        public static ArrayList getHeapItems()
+        {
+            return heap;
+        }
+
+        public static Dictionary<String, ArrayList> getStackItems()
+        {
+            Dictionary<String, ArrayList> hashMap = new Dictionary<String, ArrayList>();
+            foreach (Function function in functionStack)
+            {
+                hashMap.Add(function.Name, function.getStackItems());
+            }
+            return hashMap;
+        }
+
+
+        /*
+        * Garbage Collector Class
+        */
 
+        private class GarbageCollector
+        {
+            // TODO
+        }
     }
 }
-- 
GitLab