From f1c6537bb7d0d9e8dd534b0ec86c5122e841b754 Mon Sep 17 00:00:00 2001
From: Duc Long Hoang <duclonghoang.dlh@gmail.com>
Date: Mon, 25 Mar 2024 23:50:15 +0100
Subject: [PATCH 1/4] re #11172 @4.25 - create functions and commands

---
 Core/CodeNS/Code.cs                           |  31 ----
 Core/CodeNS/XMLParser.cs                      | 158 ++++++++++++++++++
 Core/CommandsNS/ACommand.cs                   | 102 +++++++++++
 .../{ComplexLogicNS => CommandsNS}/DoWhile.cs |   2 +-
 Core/{ComplexLogicNS => CommandsNS}/For.cs    |   2 +-
 .../IComplexLogic.cs                          |   2 +-
 Core/{ComplexLogicNS => CommandsNS}/If.cs     |   2 +-
 Core/{ComplexLogicNS => CommandsNS}/While.cs  |   2 +-
 Core/Core.csproj                              |   2 -
 Core/FunctionNS/Function.cs                   |  42 +++--
 Core/FunctionNS/IStepable.cs                  |   2 +-
 Core/Interpreter.cs                           |  61 +++++++
 Core/Program.cs                               |  52 ------
 13 files changed, 353 insertions(+), 107 deletions(-)
 delete mode 100644 Core/CodeNS/Code.cs
 create mode 100644 Core/CodeNS/XMLParser.cs
 create mode 100644 Core/CommandsNS/ACommand.cs
 rename Core/{ComplexLogicNS => CommandsNS}/DoWhile.cs (84%)
 rename Core/{ComplexLogicNS => CommandsNS}/For.cs (83%)
 rename Core/{ComplexLogicNS => CommandsNS}/IComplexLogic.cs (85%)
 rename Core/{ComplexLogicNS => CommandsNS}/If.cs (83%)
 rename Core/{ComplexLogicNS => CommandsNS}/While.cs (84%)
 create mode 100644 Core/Interpreter.cs
 delete mode 100644 Core/Program.cs

diff --git a/Core/CodeNS/Code.cs b/Core/CodeNS/Code.cs
deleted file mode 100644
index 776a5fe..0000000
--- a/Core/CodeNS/Code.cs
+++ /dev/null
@@ -1,31 +0,0 @@
-using Core.FunctionNS;
-using Core.MemoryNS;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Runtime.CompilerServices;
-using System.Text;
-using System.Threading.Tasks;
-using System.Xml.Linq;
-using System.Xml.XPath;
-
-namespace Core.CodeNS
-{
-    public static class Code
-    {
-        private static XElement? xElement = null;
-        public static void init(XElement element) { xElement = element; }
-
-        public static void createMainFunction() 
-        {
-            Function main = new Function();
-            main.Name = "Main";
-            // TODO
-            Memory.pushFunction(main);
-        }
-        public static XElement? getFunction(String name) 
-        {
-            if (xElement == null) { return null; }
-            return xElement.XPathSelectElement("./function[@name='" + name + "']"); }
-    }
-}
diff --git a/Core/CodeNS/XMLParser.cs b/Core/CodeNS/XMLParser.cs
new file mode 100644
index 0000000..e5babf0
--- /dev/null
+++ b/Core/CodeNS/XMLParser.cs
@@ -0,0 +1,158 @@
+using Core.CommandsNS;
+using Core.FunctionNS;
+using Core.MemoryNS;
+using System.Xml.Linq;
+using System.Xml.XPath;
+
+namespace Core.CodeNS
+{
+    public static class XMLParser
+    {
+        private static XElement? xElement = null;
+
+        public static void Init(XElement element) { xElement = element; }
+
+        public static void createMainFunction()
+        {
+            Function main = createFunction("Main");
+            // TODO - do something with main function here
+            // otherwise throw this function away and just call 'createFunction("Main");'
+        }
+
+        public static Function createFunction(string name)
+        {
+            Function f = new Function(name);
+            Memory.pushFunction(f);
+            parseFunction(name);
+            return f;
+        }
+
+        public static void parseFunction(string fnName)
+        {
+            XElement[] parameters = xElement?.XPathSelectElements("./function[@name='" + fnName + "']/parameters/*").ToArray();
+            XElement[] commands = xElement?.XPathSelectElements("./function[@name='" + fnName + "']/body/*").ToArray();
+
+            Function f = Memory.peekFunction();
+
+            if (f == null)
+            {
+                Console.WriteLine("Error: No function on stack");
+                return;
+            }
+
+            if (commands == null)
+            {
+                Console.WriteLine("Error: No commands in function");
+                return;
+            }
+
+            // !!! tohle cely by se dalo zabalit do jedne metody, pravdepodobne to vyuzijeme jeste jinde
+            foreach (XElement command in commands)
+            {
+                string commandName = command.Name.ToString();
+                Action action = commandName switch
+                {
+                    "declare" => () => HandleDeclare(command, f),
+                    "assign" => () => HandleAssign(command, f),
+                    "output" => () => HandleOutput(command, f),
+                    "input" => () => HandleInput(command, f),
+                    "call" => () => HandleCall(command, f),
+                    "if" => () => HandleIf(command, f),
+                    "while" => () => HandleWhile(command, f),
+                    "for" => () => HandleFor(command, f),
+                    "return" => () => HandleReturn(command, f),
+                    "break" => () => HandleBreak(command, f),
+                    "continue" => () => HandleContinue(command, f),
+                    // + dalsi chybejici commandy
+                    _ => () => HandleUnknown()
+                };
+                action.Invoke();
+            }
+
+        }
+
+        private static void HandleDeclare(XElement command, Function f)
+        {
+            string nameStr = command.Attribute("name")?.Value;
+            string[] names = nameStr.Split(", ");
+            string type = command.Attribute("type")?.Value;
+
+            foreach (var name in names)
+            {
+                ACommand cmd = new DeclareCommand(name, type);
+                f.Commands.Enqueue(cmd);
+            }
+        }
+
+        private static void HandleAssign(XElement command, Function f)
+        {
+            string name = command.Attribute("variable")?.Value;
+            string expression = command.Attribute("expression")?.Value;
+
+            ACommand cmd = new AssignCommand(name, expression);
+            f.Commands.Enqueue(cmd);
+        }
+
+        private static void HandleOutput(XElement command, Function f)
+        {
+            string expression = command.Attribute("expression")?.Value;
+            bool newLine = bool.Parse(command.Attribute("newline")?.Value);
+
+            ACommand cmd = new OutputCommand(expression, newLine);
+            f.Commands.Enqueue(cmd);
+        }
+
+        private static void HandleInput(XElement command, Function f)
+        {
+            string variableName = command.Attribute("variable")?.Value;
+            string prompt = command.Attribute("prompt")?.Value;
+
+            ACommand cmd = new InputCommand(variableName, prompt);
+            f.Commands.Enqueue(cmd);
+        }
+
+
+        private static void HandleCall(XElement command, Function f)
+        {
+            string expression = command.Attribute("expression")?.Value;
+
+            ACommand cmd = new CallCommand(expression);
+            f.Commands.Enqueue(cmd);
+        }
+
+        private static void HandleIf(XElement command, Function f)
+        {
+            // TODO
+        }
+
+        private static void HandleWhile(XElement command, Function f)
+        {
+            // TODO
+        }
+
+        private static void HandleFor(XElement command, Function f)
+        {
+            // TODO
+        }
+
+        private static void HandleReturn(XElement command, Function f)
+        {
+            // TODO
+        }
+
+        private static void HandleBreak(XElement command, Function f)
+        {
+            // TODO
+        }
+
+        private static void HandleContinue(XElement command, Function f)
+        {
+            // TODO
+        }
+
+        private static void HandleUnknown()
+        {
+            Console.WriteLine("Unknown command");
+        }
+    }
+}
diff --git a/Core/CommandsNS/ACommand.cs b/Core/CommandsNS/ACommand.cs
new file mode 100644
index 0000000..5564493
--- /dev/null
+++ b/Core/CommandsNS/ACommand.cs
@@ -0,0 +1,102 @@
+namespace Core.CommandsNS
+{
+    public abstract class ACommand
+    {
+        public abstract void execute();
+    }
+
+    public class AssignCommand : ACommand
+    {
+        public string VariableName { get; }
+        public string Expression { get; }
+
+        public AssignCommand(string variableName, string expression) =>
+            (VariableName, Expression) = (variableName, expression);
+
+        public override void execute()
+        {
+            Console.WriteLine("Execute assign command");
+            // TODO - blocked by Expression
+            // vyhodnoti se expression a vysledek se priradi do promenne 'VariableName'
+            // expression muze byt literal, promenna, volani funkce, nebo nejaka slozitejsi vec
+        }
+    }
+
+    public class DeclareCommand : ACommand
+    {
+        public string Name { get; }
+        public string Type { get; }
+
+        public DeclareCommand(string name, string type) =>
+            (Name, Type) = (name, type);
+
+        public override void execute()
+        {
+            Console.WriteLine("Execute declare command");
+            // TODO - blocked by StackObj a HeapObj
+            // vytvorit promennou, fci, strukturu, pole
+            // asi bude nejakej switch
+            // parse zda typ je jeden ze znamych enumĹŻ, jestli to neni nahodou pole
+            // pokud je to nezname klicove slovo, tak se jedna o struckturu
+            // !!! oprava: struktura, je klicove slovo viz EDataType - jeste uvidime
+        }
+    }
+
+    public class OutputCommand : ACommand
+    {
+        public string Expression { get; }
+
+        public bool NewLine { get; }
+
+        public OutputCommand(string expression, bool newline) =>
+            (Expression, NewLine) = (expression, newline);
+
+        public override void execute()
+        {
+            Console.WriteLine("Execute output command");
+            // TODO - blocked by Expression
+            // vykona se expression a hodnota se vypise na konzoli
+            // v nejjednodussim pripade je expression uz existujici promenna a ta se vypise
+            // jinak se musi vyhodnotit
+        }
+    }
+
+    public class CallCommand : ACommand
+    {
+        public string Expression { get; }
+        public CallCommand(string expression) =>
+            Expression = expression;
+
+        public override void execute()
+        {
+            Console.WriteLine("Execute call command");
+            // TODO - blocked by Expression
+            // vykona se expression, v tomto pripade to bude volani funkce
+            // navratova hodnota se neuklada -> na to slouzi assign
+        }
+    }
+
+    public class InputCommand : ACommand
+    {
+        public string VariableName { get; }
+
+        public string Prompt { get; }
+
+        public InputCommand(string variableName, string prompt) =>
+            (VariableName, Prompt) = (variableName, prompt);
+
+        public override void execute()
+        {
+            Console.WriteLine("Execute input command");
+            Console.WriteLine(Prompt);
+            string input = Console.ReadLine();
+
+            // TODO - nacte od uzivatele hodnotu a ulozi ji do promenne 'VariableName'
+            // to se najde v zasobniku -> blocked by stackObj a heapObj
+            // zatim vsechno do C# konzole, pozdeji se to bude resit pres GUI
+            // check na to jestli uzivatel nezadava blbosti
+        }
+    }
+
+    // TODO - dalsi prikazy jako if, for, while, return, break, continue, atd.
+}
diff --git a/Core/ComplexLogicNS/DoWhile.cs b/Core/CommandsNS/DoWhile.cs
similarity index 84%
rename from Core/ComplexLogicNS/DoWhile.cs
rename to Core/CommandsNS/DoWhile.cs
index 04fc346..2695b98 100644
--- a/Core/ComplexLogicNS/DoWhile.cs
+++ b/Core/CommandsNS/DoWhile.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Core.ComplexLogicNS
+namespace Core.CommandsNS
 {
     public class DoWhile
     {
diff --git a/Core/ComplexLogicNS/For.cs b/Core/CommandsNS/For.cs
similarity index 83%
rename from Core/ComplexLogicNS/For.cs
rename to Core/CommandsNS/For.cs
index 8b71eed..322d846 100644
--- a/Core/ComplexLogicNS/For.cs
+++ b/Core/CommandsNS/For.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Core.ComplexLogicNS
+namespace Core.CommandsNS
 {
     public class For
     {
diff --git a/Core/ComplexLogicNS/IComplexLogic.cs b/Core/CommandsNS/IComplexLogic.cs
similarity index 85%
rename from Core/ComplexLogicNS/IComplexLogic.cs
rename to Core/CommandsNS/IComplexLogic.cs
index f4e175e..f5fc4f2 100644
--- a/Core/ComplexLogicNS/IComplexLogic.cs
+++ b/Core/CommandsNS/IComplexLogic.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Core.ComplexLogicNS
+namespace Core.CommandsNS
 {
     public interface IComplexLogic
     {
diff --git a/Core/ComplexLogicNS/If.cs b/Core/CommandsNS/If.cs
similarity index 83%
rename from Core/ComplexLogicNS/If.cs
rename to Core/CommandsNS/If.cs
index 3b5d2ef..993d504 100644
--- a/Core/ComplexLogicNS/If.cs
+++ b/Core/CommandsNS/If.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Core.ComplexLogicNS
+namespace Core.CommandsNS
 {
     public class If
     {
diff --git a/Core/ComplexLogicNS/While.cs b/Core/CommandsNS/While.cs
similarity index 84%
rename from Core/ComplexLogicNS/While.cs
rename to Core/CommandsNS/While.cs
index 577f7ba..44cd879 100644
--- a/Core/ComplexLogicNS/While.cs
+++ b/Core/CommandsNS/While.cs
@@ -4,7 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
-namespace Core.ComplexLogicNS
+namespace Core.CommandsNS
 {
     public class While
     {
diff --git a/Core/Core.csproj b/Core/Core.csproj
index 87944dc..4c4eb47 100644
--- a/Core/Core.csproj
+++ b/Core/Core.csproj
@@ -13,8 +13,6 @@
 
   <ItemGroup>
 	<Folder Include="FunctionNS\" />
-	<Folder Include="CodeNS\" />
-	<Folder Include="ComplexLogicNS\" />
 	<Folder Include="DataTypeNS\" />
 	<Folder Include="MemoryNS\" />
   </ItemGroup>
diff --git a/Core/FunctionNS/Function.cs b/Core/FunctionNS/Function.cs
index 96391a3..4d8dc2d 100644
--- a/Core/FunctionNS/Function.cs
+++ b/Core/FunctionNS/Function.cs
@@ -1,7 +1,7 @@
+using Core.CommandsNS;
 using Core.DataTypeNS;
 using Core.MemoryNS;
 using System.Collections;
-using System.Transactions;
 
 namespace Core.FunctionNS
 {
@@ -10,33 +10,43 @@ namespace Core.FunctionNS
         /*
          * Atributtes
          */
-        private String name;
-        private Dictionary<String, StackObj> variables = new Dictionary<string, StackObj>();
+        public string Name { get; set; }
 
-        /*
-         * Getters, setters
-         */
-        public String Name
-        {
-            get { return name; }
-            set { name = value; }
-        }
+        public string Type { get; set; }
+
+        public Dictionary<string, StackObj> ParamsAndVars { get; set; }
+
+        public Queue<ACommand> Commands;
+
+        public Function(string name) => (Name, Commands) = (name, new Queue<ACommand>());
 
         /*
          * Code
          */
-        public bool step()
+        public void Step()
         {
-            Console.WriteLine("Step by function: " +  name);
-            Memory.popFunction(); // TODO - remove
-            return true;
+            Console.WriteLine("Step by function: " + Name);
+
+            if (Commands.Count == 0)
+            {
+                Memory.popFunction();
+                return;
+            }
+
+            ACommand a = Commands.Dequeue();
+            a.execute();
         }
 
-        public ArrayList getStackItems()
+        public ArrayList GetStackItems()
         {
             ArrayList list = new ArrayList();
             // TODO
             return list;
         }
+
+        public void AddVariable(string name, StackObj value)
+        {
+            //variables.Add(name, value);
+        }
     }
 }
\ No newline at end of file
diff --git a/Core/FunctionNS/IStepable.cs b/Core/FunctionNS/IStepable.cs
index 0d132ef..72421d1 100644
--- a/Core/FunctionNS/IStepable.cs
+++ b/Core/FunctionNS/IStepable.cs
@@ -2,6 +2,6 @@ namespace Core.FunctionNS
 {
     public interface IStepable
     {
-        public bool step();
+        public void Step();
     }
 }
\ No newline at end of file
diff --git a/Core/Interpreter.cs b/Core/Interpreter.cs
new file mode 100644
index 0000000..8abe48d
--- /dev/null
+++ b/Core/Interpreter.cs
@@ -0,0 +1,61 @@
+namespace Core
+{
+    using Core.CodeNS;
+    using Core.MemoryNS;
+    using FunctionNS;
+    using System.Xml.Linq;
+
+    internal class Interpreter
+    {
+        static void Main(string[] args)
+        {
+            string fileName = Directory.GetCurrentDirectory() + "\\..\\..\\..\\test.fprg";
+
+            if (!InitXMLFile(fileName))
+            {
+                Console.WriteLine("Error :(");
+            }
+
+            XMLParser.createMainFunction();
+
+            while (true)
+            {
+                Function f = Memory.peekFunction();
+                if (f == null)
+                {
+                    Console.WriteLine("END OF CODE");
+                    break;
+                }
+                f.Step();
+            }
+        }
+        private static bool InitXMLFile(string xmlFile)
+        {
+            if (!File.Exists(xmlFile))
+            {
+                Console.WriteLine("File: " + xmlFile + " does not exist.");
+                return false;
+            }
+
+            XDocument doc = XDocument.Load(xmlFile);
+
+            if (doc.Root == null)
+            {
+                Console.WriteLine("Cannot load file: " + xmlFile);
+                return false;
+            }
+
+            XMLParser.Init(doc.Root);
+            return true;
+        }
+
+        private void PrintHeap()
+        {
+            // TODO
+        }
+        private void PrintStack()
+        {
+            // TODO
+        }
+    }
+}
diff --git a/Core/Program.cs b/Core/Program.cs
deleted file mode 100644
index f6427b3..0000000
--- a/Core/Program.cs
+++ /dev/null
@@ -1,52 +0,0 @@
-namespace Core
-{
-    using Core.CodeNS;
-    using Core.MemoryNS;
-    using FunctionNS;
-    using System.Xml.Linq;
-
-    internal class Program
-    {
-        static void Main(string[] args)
-        {
-            String fileName = Directory.GetCurrentDirectory() + "\\..\\..\\..\\test.fprg";
-
-            if (initXMLFile(fileName))
-            {
-                Code.createMainFunction();
-                while (true) 
-                {
-                    Function f = Memory.peekFunction();
-                    if (f == null)
-                    {
-                        Console.WriteLine("END OF CODE");
-                        break;
-                    }
-                    f.step();
-                }
-            }
-        }
-
-        private static bool initXMLFile(string xmlFile)
-        {
-            if (!File.Exists(xmlFile)) 
-            {
-                Console.WriteLine("Cannot open file: " + xmlFile);
-                return false; 
-            }
-            String fileContents = File.ReadAllText(xmlFile);
-            XElement element = XElement.Parse(fileContents);
-            Code.init(element);
-            return true;
-        }
-
-        private void printHeap() 
-        {
-            // TODO
-        }
-        private void printStack()
-        {
-            // TODO
-        }
-}
-}
-- 
GitLab


From 99b00e06300b988dd552d9b1928d7fbd93935fa9 Mon Sep 17 00:00:00 2001
From: Duc Long Hoang <duclonghoang.dlh@gmail.com>
Date: Wed, 27 Mar 2024 17:20:00 +0100
Subject: [PATCH 2/4] re #11172 @3h - if for do while break continue

---
 Core/CodeNS/XMLParser.cs    |  65 ++++++++++++---
 Core/CommandsNS/ACommand.cs | 159 +++++++++++++++++++++++++++++++++---
 Core/FunctionNS/Function.cs |   2 +-
 3 files changed, 203 insertions(+), 23 deletions(-)

diff --git a/Core/CodeNS/XMLParser.cs b/Core/CodeNS/XMLParser.cs
index e5babf0..2f78173 100644
--- a/Core/CodeNS/XMLParser.cs
+++ b/Core/CodeNS/XMLParser.cs
@@ -58,12 +58,12 @@ namespace Core.CodeNS
                     "input" => () => HandleInput(command, f),
                     "call" => () => HandleCall(command, f),
                     "if" => () => HandleIf(command, f),
-                    "while" => () => HandleWhile(command, f),
                     "for" => () => HandleFor(command, f),
-                    "return" => () => HandleReturn(command, f),
+                    "while" => () => HandleWhile(command, f),
+                    "do" => () => HandleDo(command, f),
                     "break" => () => HandleBreak(command, f),
                     "continue" => () => HandleContinue(command, f),
-                    // + dalsi chybejici commandy
+                    // + dalsi chybejici commandy - to je asi vsechno
                     _ => () => HandleUnknown()
                 };
                 action.Invoke();
@@ -122,32 +122,73 @@ namespace Core.CodeNS
 
         private static void HandleIf(XElement command, Function f)
         {
-            // TODO
+            string expression = command.Attribute("expression")?.Value;
+            XElement[] ifCommands = command.XPathSelectElements("./then/*").ToArray();
+            XElement[] elseCommands = command.XPathSelectElements("./else/*").ToArray();
+
+            // TODO tady se musi rekurzivne zavolat ten switch a ukladat do jednotlivych comandu
+            // to se musi udelat pro vsechny blokove prikazy
+            Queue<ACommand> ifCmds = new Queue<ACommand>();
+            Queue<ACommand> elseCmds = new Queue<ACommand>();
+
+            ACommand cmd = new IfCommand(expression, ifCmds, elseCmds);
+            f.Commands.Enqueue(cmd);
         }
 
-        private static void HandleWhile(XElement command, Function f)
+        private static void HandleFor(XElement command, Function f)
         {
-            // TODO
+            string variable = command.Attribute("variable")?.Value;
+            string start = command.Attribute("start")?.Value;
+            string end = command.Attribute("end")?.Value;
+            string step = command.Attribute("step")?.Value;
+            string direction = command.Attribute("direction")?.Value;
+
+
+            // get all child nodes
+            XElement[] xCommands = command.XPathSelectElements("./*").ToArray();
+
+            // TODO - nejak rekuzivne naplnit commands XElementy
+            List<ACommand> commands = new List<ACommand>();
+
+            ACommand cmd = new ForCommand(variable, start, end, step, direction, commands);
+            f.Commands.Enqueue(cmd);
         }
 
-        private static void HandleFor(XElement command, Function f)
+        private static void HandleWhile(XElement command, Function f)
         {
-            // TODO
+            string expression = command.Attribute("expression")?.Value;
+            XElement[] xCommands = command.XPathSelectElements("./*").ToArray();
+
+            // TODO - nejak rekuzivne naplnit commands XElementy
+            List<ACommand> commands = new List<ACommand>();
+
+            ACommand cmd = new WhileCommand(expression, commands);
+            f.Commands.Enqueue(cmd);
         }
 
-        private static void HandleReturn(XElement command, Function f)
+        private static void HandleDo(XElement command, Function f)
         {
-            // TODO
+            string expression = command.Attribute("expression")?.Value;
+
+            XElement[] xCommands = command.XPathSelectElements("./*").ToArray();
+
+            // TODO - nejak rekuzivne naplnit commands XElementy
+            List<ACommand> commands = new List<ACommand>();
+
+            ACommand cmd = new DoCommand(expression, commands);
+            f.Commands.Enqueue(cmd);
         }
 
         private static void HandleBreak(XElement command, Function f)
         {
-            // TODO
+            ACommand cmd = new BreakCommand();
+            f.Commands.Enqueue(cmd);
         }
 
         private static void HandleContinue(XElement command, Function f)
         {
-            // TODO
+            ACommand cmd = new ContinueCommand();
+            f.Commands.Enqueue(cmd);
         }
 
         private static void HandleUnknown()
diff --git a/Core/CommandsNS/ACommand.cs b/Core/CommandsNS/ACommand.cs
index 5564493..7e45416 100644
--- a/Core/CommandsNS/ACommand.cs
+++ b/Core/CommandsNS/ACommand.cs
@@ -1,8 +1,11 @@
-namespace Core.CommandsNS
+using Core.FunctionNS;
+using System.Collections;
+
+namespace Core.CommandsNS
 {
     public abstract class ACommand
     {
-        public abstract void execute();
+        public abstract void Execute();
     }
 
     public class AssignCommand : ACommand
@@ -13,7 +16,7 @@
         public AssignCommand(string variableName, string expression) =>
             (VariableName, Expression) = (variableName, expression);
 
-        public override void execute()
+        public override void Execute()
         {
             Console.WriteLine("Execute assign command");
             // TODO - blocked by Expression
@@ -30,7 +33,7 @@
         public DeclareCommand(string name, string type) =>
             (Name, Type) = (name, type);
 
-        public override void execute()
+        public override void Execute()
         {
             Console.WriteLine("Execute declare command");
             // TODO - blocked by StackObj a HeapObj
@@ -45,13 +48,12 @@
     public class OutputCommand : ACommand
     {
         public string Expression { get; }
-
         public bool NewLine { get; }
 
         public OutputCommand(string expression, bool newline) =>
             (Expression, NewLine) = (expression, newline);
 
-        public override void execute()
+        public override void Execute()
         {
             Console.WriteLine("Execute output command");
             // TODO - blocked by Expression
@@ -67,7 +69,7 @@
         public CallCommand(string expression) =>
             Expression = expression;
 
-        public override void execute()
+        public override void Execute()
         {
             Console.WriteLine("Execute call command");
             // TODO - blocked by Expression
@@ -79,13 +81,12 @@
     public class InputCommand : ACommand
     {
         public string VariableName { get; }
-
         public string Prompt { get; }
 
         public InputCommand(string variableName, string prompt) =>
             (VariableName, Prompt) = (variableName, prompt);
 
-        public override void execute()
+        public override void Execute()
         {
             Console.WriteLine("Execute input command");
             Console.WriteLine(Prompt);
@@ -98,5 +99,143 @@
         }
     }
 
-    // TODO - dalsi prikazy jako if, for, while, return, break, continue, atd.
+
+    //!!! dalo by se uvazovat zda po vyhodnoceni expression by nestacilo prekopirovat vsechny spravne commandy do nadrazeneho bloku. Stoji na zamysleni
+    public class IfCommand : ACommand, IStepable
+    {
+        public string Expression { get; }
+        public Queue<ACommand> IfCommands { get; }
+        public Queue<ACommand> ElseCommands { get; }
+        private Queue<ACommand> UsedCommands;
+
+        public IfCommand(string expression, Queue<ACommand> ifCommands, Queue<ACommand> elseCommands) =>
+            (Expression, IfCommands, ElseCommands) = (expression, ifCommands, elseCommands);
+
+        public override void Execute()
+        {
+            Console.WriteLine("Execute if command");
+
+            // tohle bude inlined az bude vyhodnocovani expression hotovy
+            bool isTrue = Expression.Equals("Pokud se vyraz vyhodnoti jako spravne vykonat IfCommands");
+            UsedCommands = isTrue ? IfCommands : ElseCommands;
+
+            while (UsedCommands.Count > 0)
+            {
+                Step();
+            }
+
+            // TODO - mozna nejakej memory pop/peek
+
+        }
+
+        public void Step()
+        {
+            ACommand a = UsedCommands.Dequeue();
+            a.Execute();
+        }
+    }
+
+
+    public class ForCommand : ACommand, IStepable
+    {
+        public string VariableName { get; }
+        public string Start { get; }
+        public string End { get; }
+        public string _Step { get; }
+        public string Direction { get; }
+        public List<ACommand> Commands { get; }
+        private int Counter;
+
+        public ForCommand(string variableName, string start, string end, string step, string direction, List<ACommand> commands) =>
+            // pice vole tady je 'Step' jako member variable a jako metoda
+            (VariableName, Start, End, _Step, Direction, Commands, Counter) = (variableName, start, end, step, direction, commands, 0);
+
+        public override void Execute()
+        {
+            Console.WriteLine("Execute for command");
+            // TODO - logika na loop step
+            // Counter by se mel inkrementovat a modulovat
+            // tak se budou commandy vykonavat v otocce
+        }
+
+        public void Step()
+        {
+            // TODO - tady by mel byt asi loop na volani Execute() aby se dalo stepovat
+            ACommand a = Commands[Counter];
+            a.Execute();
+        }
+    }
+
+    public class WhileCommand : ACommand, IStepable
+    {
+        public string Expression { get; }
+        public List<ACommand> Commands { get; }
+        private int Counter;
+
+        public WhileCommand(string expression, List<ACommand> commands) =>
+            (Expression, Commands, Counter) = (expression, commands, Counter);
+
+        public override void Execute()
+        {
+            Console.WriteLine("Execute while command");
+            // TODO - logika na loop step
+            // Counter by se mel inkrementovat a modulovat
+            // tak se budou commandy vykonavat v otocce
+        }
+
+        public void Step()
+        {
+            // TODO - tady by mel byt asi loop na volani Execute() aby se dalo stepovat
+            ACommand a = Commands[Counter];
+            a.Execute();
+        }
+    }
+
+    public class DoCommand : ACommand, IStepable
+    {
+        public string Expression { get; }
+        public List<ACommand> Commands { get; }
+        private int Counter;
+
+        public DoCommand(string expression, List<ACommand> commands) =>
+            (Expression, Commands, Counter) = (expression, commands, 0);
+
+        public override void Execute()
+        {
+            Console.WriteLine("Execute do while command");
+            // TODO - logika na loop step
+            // Counter by se mel inkrementovat a modulovat
+            // tak se budou commandy vykonavat v otocce
+        }
+
+        public void Step()
+        {
+            // TODO - tady by mel byt asi loop na volani Execute() aby se dalo stepovat
+            ACommand a = Commands[Counter];
+            a.Execute();
+        }
+    }
+
+    public class BreakCommand : ACommand
+    {
+        public override void Execute()
+        {
+            Console.WriteLine("Execute break command");
+            // TODO - kompletne ukonci for/while/do blok
+            // asi se resi zahozenim funkce??? viz Memory.PopFunction()
+            // bacha pravdepodobne bude vnorenej jeste v nejakym if bloku
+        }
+    }
+
+    public class ContinueCommand : ACommand
+    {
+        public override void Execute()
+        {
+            Console.WriteLine("Execute continue command");
+            // TODO - mel by zmenit flow for/while/do cyklu.
+            // cykly si interne musi drzet nejaky counter na commandy
+            // a ten se timhle commandem resetuje
+            // bacha pravdepodobne bude vnorenej jeste v nejakym if bloku
+        }
+    }
 }
diff --git a/Core/FunctionNS/Function.cs b/Core/FunctionNS/Function.cs
index 4d8dc2d..5f38891 100644
--- a/Core/FunctionNS/Function.cs
+++ b/Core/FunctionNS/Function.cs
@@ -34,7 +34,7 @@ namespace Core.FunctionNS
             }
 
             ACommand a = Commands.Dequeue();
-            a.execute();
+            a.Execute();
         }
 
         public ArrayList GetStackItems()
-- 
GitLab


From 8a6b209e4f9f6307155ee27eae9b27731a8e438a Mon Sep 17 00:00:00 2001
From: Duc Long Hoang <duclong@students.zcu.cz>
Date: Wed, 27 Mar 2024 17:36:32 +0100
Subject: [PATCH 3/4] re #11172 @0.01 - test log time

---
 Core/CommandsNS/ACommand.cs | 2 --
 1 file changed, 2 deletions(-)

diff --git a/Core/CommandsNS/ACommand.cs b/Core/CommandsNS/ACommand.cs
index 7e45416..d78374e 100644
--- a/Core/CommandsNS/ACommand.cs
+++ b/Core/CommandsNS/ACommand.cs
@@ -99,7 +99,6 @@ namespace Core.CommandsNS
         }
     }
 
-
     //!!! dalo by se uvazovat zda po vyhodnoceni expression by nestacilo prekopirovat vsechny spravne commandy do nadrazeneho bloku. Stoji na zamysleni
     public class IfCommand : ACommand, IStepable
     {
@@ -135,7 +134,6 @@ namespace Core.CommandsNS
         }
     }
 
-
     public class ForCommand : ACommand, IStepable
     {
         public string VariableName { get; }
-- 
GitLab


From 3e3dcaf554f94901d23dad5c27e21728680c7971 Mon Sep 17 00:00:00 2001
From: vladimir_holy <vladimir.holy7@gmail.com>
Date: Thu, 28 Mar 2024 17:52:16 +0100
Subject: [PATCH 4/4] re #11172 - wrong method name in Memory

---
 Core/MemoryNS/Memory.cs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Core/MemoryNS/Memory.cs b/Core/MemoryNS/Memory.cs
index e9a91e2..b63af80 100644
--- a/Core/MemoryNS/Memory.cs
+++ b/Core/MemoryNS/Memory.cs
@@ -120,7 +120,7 @@ namespace Core.MemoryNS
             Dictionary<String, ArrayList> hashMap = new Dictionary<String, ArrayList>();
             foreach (Function function in functionStack)
             {
-                hashMap.Add(function.Name, function.getStackItems());
+                hashMap.Add(function.Name, function.GetStackItems());
             }
             return hashMap;
         }
-- 
GitLab