From a9f4ec6caccbf149aba26cf33a6bcf7a8444b139 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Sun, 22 Feb 2026 08:26:43 +0000
Subject: [PATCH 1/2] Initial plan
From 279faed6669a21328537252f4a628ad127f956eb Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Sun, 22 Feb 2026 08:34:25 +0000
Subject: [PATCH 2/2] Fix bugs: array bounds, empty string checks, swapped
class files
Co-authored-by: jogibear9988 <364896+jogibear9988@users.noreply.github.com>
---
TiaCodegen/CodeGen/KopCodeHelper.cs | 6 ++---
TiaCodegen/Commands/BaseOperationOrSignal.cs | 2 +-
.../Commands/Functions/Arithmetic/AddCall.cs | 6 ++---
.../Commands/Functions/Arithmetic/MulCall.cs | 6 ++---
TiaCodegen/Commands/Move.cs | 2 +-
TiaCodegen/Commands/Signals/Signal.cs | 24 ++++++++++++-------
6 files changed, 26 insertions(+), 20 deletions(-)
diff --git a/TiaCodegen/CodeGen/KopCodeHelper.cs b/TiaCodegen/CodeGen/KopCodeHelper.cs
index e8c2d46..2957ae6 100644
--- a/TiaCodegen/CodeGen/KopCodeHelper.cs
+++ b/TiaCodegen/CodeGen/KopCodeHelper.cs
@@ -516,7 +516,7 @@ private void AddWires(IOperationOrSignal op)
}
else
{
- _sb.AppendLine("");
+ _sb.AppendLine("");
}
}
_sb.AppendLine("");
@@ -527,7 +527,7 @@ private void AddWires(IOperationOrSignal op)
{
_sb.AppendLine("" + (debug ?? ("")));
_sb.AppendLine("");
- _sb.AppendLine("");
+ _sb.AppendLine("");
_sb.AppendLine("");
_currentId++;
}
@@ -781,7 +781,7 @@ void ImportChildOrs (Or childOr)
if (op.Children[1] is Signal)
ch = op.Children[1];
else
- ch = op.Children[1].Children.Last();
+ ch = op.Children[1].Children.Any() ? op.Children[1].Children.Last() : op.Children[1];
if (op.Children[1] is Or || op.Children[1] is CompareOperator || op.Children[1] is Move || op.Children[1] is Convert || op.Children[1] is S_Move || op.Children[1] is ICoil)
{
diff --git a/TiaCodegen/Commands/BaseOperationOrSignal.cs b/TiaCodegen/Commands/BaseOperationOrSignal.cs
index 5075b79..5a8b48d 100644
--- a/TiaCodegen/Commands/BaseOperationOrSignal.cs
+++ b/TiaCodegen/Commands/BaseOperationOrSignal.cs
@@ -59,7 +59,7 @@ public override string ToString()
public IOperationOrSignal GetFirstChildNotAnd()
{
- if (this is And)
+ if (this is And && Children.Count > 0)
{
var ch1 = ((And)this).Children[0];
if (ch1 is BaseOperationOrSignal)
diff --git a/TiaCodegen/Commands/Functions/Arithmetic/AddCall.cs b/TiaCodegen/Commands/Functions/Arithmetic/AddCall.cs
index 101e686..b56d6f6 100644
--- a/TiaCodegen/Commands/Functions/Arithmetic/AddCall.cs
+++ b/TiaCodegen/Commands/Functions/Arithmetic/AddCall.cs
@@ -4,9 +4,9 @@
namespace TiaCodegen.Commands.Functions.Arithmetic
{
- public class MulCall : VariableArithmeticCall
+ public class AddCall : VariableArithmeticCall
{
- public MulCall(string type,
+ public AddCall(string type,
IOperationOrSignal in1,
IOperationOrSignal in2,
IOperationOrSignal in3 = null,
@@ -58,7 +58,7 @@ public MulCall(string type,
IOperationOrSignal in49 = null,
IOperationOrSignal in50 = null,
IOperationOrSignal out1 = null,
- IOperationOrSignal eno = null) : base("Mul", eno)
+ IOperationOrSignal eno = null) : base("Add", eno)
{
DisableEno = true;
Type = type;
diff --git a/TiaCodegen/Commands/Functions/Arithmetic/MulCall.cs b/TiaCodegen/Commands/Functions/Arithmetic/MulCall.cs
index b56d6f6..101e686 100644
--- a/TiaCodegen/Commands/Functions/Arithmetic/MulCall.cs
+++ b/TiaCodegen/Commands/Functions/Arithmetic/MulCall.cs
@@ -4,9 +4,9 @@
namespace TiaCodegen.Commands.Functions.Arithmetic
{
- public class AddCall : VariableArithmeticCall
+ public class MulCall : VariableArithmeticCall
{
- public AddCall(string type,
+ public MulCall(string type,
IOperationOrSignal in1,
IOperationOrSignal in2,
IOperationOrSignal in3 = null,
@@ -58,7 +58,7 @@ public AddCall(string type,
IOperationOrSignal in49 = null,
IOperationOrSignal in50 = null,
IOperationOrSignal out1 = null,
- IOperationOrSignal eno = null) : base("Add", eno)
+ IOperationOrSignal eno = null) : base("Mul", eno)
{
DisableEno = true;
Type = type;
diff --git a/TiaCodegen/Commands/Move.cs b/TiaCodegen/Commands/Move.cs
index e18eed4..cb0c8e8 100644
--- a/TiaCodegen/Commands/Move.cs
+++ b/TiaCodegen/Commands/Move.cs
@@ -9,6 +9,6 @@ public Move(params IOperationOrSignal[] operationOrSignals)
: base(operationOrSignals)
{ }
- public override int Cardinality { get { return Children.Count() - 1; } }
+ public override int Cardinality { get { return Children.Any() ? Children.Count() - 1 : 0; } }
}
}
diff --git a/TiaCodegen/Commands/Signals/Signal.cs b/TiaCodegen/Commands/Signals/Signal.cs
index 159462e..9594d1c 100644
--- a/TiaCodegen/Commands/Signals/Signal.cs
+++ b/TiaCodegen/Commands/Signals/Signal.cs
@@ -192,9 +192,10 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
{
sb.AppendLine("" + " ");
sb.AppendLine("");
- for (int i = 0; i < Escape(Name).Split('.').Length; i++) // Aufsplitten von a.b.c in einzel Elemente
+ var nameParts = Escape(Name).Split('.');
+ for (int i = 0; i < nameParts.Length; i++) // Aufsplitten von a.b.c in einzel Elemente
{
- var part = Unescape(Escape(Name).Split('.')[i]);
+ var part = Unescape(nameParts[i]);
var idx = part.IndexOf("[") + 1; // Aufsplitten von Arrays
if (idx > 0)
@@ -213,7 +214,7 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
{
var p1 = part.Substring(idx, part.Length - idx);
var accessType = "GlobalVariable";
- if (p1[0] == '#')
+ if (p1.Length > 0 && p1[0] == '#')
{
p1 = p1.Substring(1);
accessType = "LocalVariable";
@@ -224,7 +225,9 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
while (p1 != null)
{
i++;
- p1 = Unescape(Escape(Name).Split('.')[i]);
+ if (i >= nameParts.Length)
+ break;
+ p1 = Unescape(nameParts[i]);
if (p1.Contains("]"))
{
sb.AppendLine("");
@@ -328,12 +331,13 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
sb.AppendLine("" + " ");
}
sb.AppendLine("");
- var cnt = Escape(LocalName).Split('.').Length;
+ var localNameParts = Escape(LocalName).Split('.');
+ var cnt = localNameParts.Length;
if (this is FixedSignal)
cnt = 1;
for (int i = 0; i < cnt; i++) // Aufsplitten von a.b.c in einzel Elemente
{
- var part = Unescape(Escape(LocalName).Split('.')[i]);
+ var part = Unescape(localNameParts[i]);
if (this is FixedSignal)
part = LocalName;
@@ -354,7 +358,7 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
{
var p1 = part.Substring(idx, part.Length - idx);
var accessType = "GlobalVariable";
- if (p1[0] == '#')
+ if (p1.Length > 0 && p1[0] == '#')
{
p1 = p1.Substring(1);
accessType = "LocalVariable";
@@ -366,7 +370,9 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
while (p1 != null)
{
i++;
- p1 = Unescape(Escape(LocalName).Split('.')[i]);
+ if (i >= localNameParts.Length)
+ break;
+ p1 = Unescape(localNameParts[i]);
if (p1.Contains(","))
{
var innerParts = p1.Split(',');
@@ -397,7 +403,7 @@ public void AddXmlToStringBuilder(ulong id, StringBuilder sb)
sb.AppendLine("");
var accessTypePne = "GlobalVariable";
- if (pNe[0] == '#')
+ if (pNe.Length > 0 && pNe[0] == '#')
{
pNe = pNe.Substring(1);
accessTypePne = "LocalVariable";