Skip to content

Commit 550c10a

Browse files
committed
fix codegen for export
1 parent 0cbb30e commit 550c10a

File tree

5 files changed

+204
-35
lines changed

5 files changed

+204
-35
lines changed

TiaCodeGen.Tests/SampleTests.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,107 @@ public void SystemFunctionDPXX_DAT()
436436

437437
//Assert.AreEqual("<Access Scope=\"LocalVariable\" UId=\"1\">\r\n<Symbol>\r\n<Component Name=\"A\">\r\n</Component>\r\n<Component Name=\"B\">\r\n</Component>\r\n<Component Name=\"C\">\r\n<Access Scope=\"LocalVariable\">\r\n<Symbol>\r\n<Component Name=\"D\" />\r\n<Component Name=\"E\" />\r\n<Component Name=\"F\" />\r\n</Symbol>\r\n</Access>\r\n<Access Scope=\"LiteralConstant\">\r\n<Constant>\r\n<ConstantType>DInt</ConstantType>\r\n<ConstantValue>1</ConstantValue>\r\n</Constant>\r\n</Access>\r\n</Component>\r\n</Symbol>\r\n</Access>\r\n".Replace("\n", "").Replace("\r", ""), xml.Replace("\n", "").Replace("\r", ""));
438438
}
439+
440+
[Test]
441+
public void ComplexOr()
442+
{
443+
var codeblock = new CodeBlock() { Safety = false };
444+
445+
var nw = new Network("T1", "T1");
446+
447+
nw.Add(
448+
new Coil(
449+
new Signal("Test11"),
450+
new Or(
451+
new And(
452+
new Signal("Test1"),
453+
new Or(
454+
new And(
455+
new Signal("Test2"),
456+
new Or(
457+
new Signal("Test3"),
458+
new Signal("Test4")
459+
)
460+
{ DebugInfo="aaa" }
461+
),
462+
new Signal("Test5")
463+
)
464+
{ DebugInfo = "bbb" }
465+
),
466+
new And(
467+
new Signal("Test6"),
468+
new Or(
469+
new And(
470+
new Signal("Test7"),
471+
new Or(
472+
new Signal("Test8"),
473+
new Signal("Test9")
474+
)
475+
{ DebugInfo = "ccc" }
476+
),
477+
new Signal("Test10")
478+
)
479+
{ DebugInfo = "ddd" }
480+
)
481+
)
482+
{ DebugInfo = "eee" }
483+
)
484+
);
485+
codeblock.Add(nw);
486+
487+
var block = new Block("Test", "blabla", codeblock);
488+
block.Interface = TestInterface;
489+
var xml = block.GetCode();
490+
}
491+
492+
[Test]
493+
public void ComplexOr2()
494+
{
495+
var codeblock = new CodeBlock() { Safety = false };
496+
497+
var nw = new Network("T1", "T1");
498+
499+
nw.Add(
500+
new Coil(
501+
new Signal("Test11"),
502+
new Or(
503+
new Signal("Test12"),
504+
new Or(
505+
new And(
506+
new Signal("Test1"),
507+
new Or(
508+
new And(
509+
new Signal("Test2"),
510+
new Or(
511+
new Signal("Test3"),
512+
new Signal("Test4")
513+
)
514+
),
515+
new Signal("Test5")
516+
)
517+
),
518+
new And(
519+
new Signal("Test6"),
520+
new Or(
521+
new And(
522+
new Signal("Test7"),
523+
new Or(
524+
new Signal("Test8"),
525+
new Signal("Test9")
526+
)
527+
),
528+
new Signal("Test10")
529+
)
530+
)
531+
)
532+
)
533+
)
534+
);
535+
codeblock.Add(nw);
536+
537+
var block = new Block("Test", "blabla", codeblock);
538+
block.Interface = TestInterface;
539+
var xml = block.GetCode();
540+
}
439541
}
440542
}

TiaCodegen/Blocks/Block.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public virtual string GetBlockHeader(ref int id)
5656
<MemoryLayout>Optimized</MemoryLayout>
5757
<Name>{Name}</Name>
5858
{(Number != 0 ? "<Number>" + Number + "</Number>" : "")}
59+
<Namespace />
5960
<ProgrammingLanguage>LAD</ProgrammingLanguage>
6061
</AttributeList>
6162
<ObjectList>";

TiaCodegen/CodeGen/KopCodeHelper.cs

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public KopCodeHelper(CodeBlock block)
4545

4646
private void AddSignalDefinitions(Network network)
4747
{
48-
var signals = network.Children.Flatten(x => x.Children).OfType<Signal>();
48+
var signals = network.Children.Flatten(x => x.Children).OfType<Signal>().OrderBy(x => x.Name, new NaturalComparer());
4949

5050
foreach (var signal in signals)
5151
{
@@ -84,6 +84,24 @@ private void AddContactDefinitions(Network network, CodeBlock block)
8484
{
8585
var ops = FlattenOrdered(network.Children, x => x.Children).OfType<IOperationOrSignal>().ToList();
8686

87+
foreach (var op in ops)
88+
{
89+
if (op is Signal)
90+
{
91+
}
92+
else if (op is And || (op is Or && op.Children.Count == 1))
93+
{
94+
}
95+
else
96+
{
97+
if (op is Or && op.Children.Count > 1)
98+
{
99+
//We need to fill this before
100+
op.CreateContactAndFillCardinality(op);
101+
}
102+
}
103+
}
104+
87105
foreach (var op in ops)
88106
{
89107
if (op is Signal)
@@ -107,22 +125,22 @@ private void AddContactDefinitions(Network network, CodeBlock block)
107125
op.Children[0].OperationId = _currentId;
108126
}
109127

110-
if (!(op is Distributor))
111-
_currentId++;
112-
113128
if (op is Or && op.Children.Count > 1)
114129
{
115130
op.CreateContactAndFillCardinality(op);
116131
}
132+
133+
if (!(op is Distributor) && !op.DoNotCreateContact)
134+
_currentId++;
117135
}
118136
}
119137

120138
/*_sb.AppendLine("<!--");
121-
foreach (var op in ops)
139+
foreach (var op in ops)
122140
{
123-
_sb.AppendLine(op.ToString() + " -- " + op.OperationId);
124-
}
125-
_sb.AppendLine("-->");*/
141+
_sb.AppendLine(op.ToString() + " -- " + op.OperationId);
142+
}
143+
_sb.AppendLine("-->");*/
126144

127145
foreach (var op in ops)
128146
{
@@ -528,7 +546,7 @@ private void AddWires(IOperationOrSignal op)
528546
}
529547
else if (c is And && c.Children.FirstOrDefault() is Or)
530548
{
531-
foreach(var chIn in c.Children.FirstOrDefault().Children)
549+
foreach (var chIn in c.Children.FirstOrDefault().Children)
532550
{
533551
foreach (var ch in GetAllOrSignals(chIn))
534552
{
@@ -545,7 +563,7 @@ private void AddWires(IOperationOrSignal op)
545563
}
546564
}
547565
else
548-
{
566+
{
549567
if (sng is Signal)
550568
{
551569
_sb.AppendLine("<IdentCon UId=\"" + ((Signal)sng).SignalId + "\" />" + " <!-- " + ((Signal)sng).Name + " -->");
@@ -573,35 +591,32 @@ private void AddWires(IOperationOrSignal op)
573591
_sb.AppendLine("</Wire>");
574592
_currentId++;
575593
}
576-
else if (op is Or && op.Children.Count > 1)
577-
{
578-
int i = 1;
579-
foreach (var ch in op.Children)
594+
else if (op is Or or && op.Children.Count > 1)
595+
{
596+
var orInputCounter = 1;
597+
void ImportChildOrs (Or childOr)
580598
{
581-
if (ch is And && ch.Children.Last() is Or) //Todo nur ein letztes or im and, könnte weiter verschachtelt sein
599+
var aaa = or;
600+
foreach (var ch in childOr.Children)
582601
{
583-
foreach (var ch2 in ch.Children.Last().Children)
602+
if (ch.Children.Count > 0 && ch.Children.Last() is Or subChildOr)
584603
{
585-
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ("<!-- Wire 1 Or -->")));
586-
_sb.AppendLine("<NameCon UId=\"" + ch2.OperationId + "\" Name=\"out\" />" + " <!-- " + ch2.ToString() + " -->");
587-
_sb.AppendLine("<NameCon UId=\"" + op.OperationId + "\" Name=\"in" + i + "\" />" + " <!-- " + op.ToString() + " -->");
588-
_sb.AppendLine("</Wire>");
589-
i++;
590-
_currentId++;
604+
ImportChildOrs(subChildOr);
591605
}
592-
}
593-
else
594-
{
595-
if (!op.DoNotCreateContact)
606+
else
596607
{
597-
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ("<!-- Wire 2 Or -->")));
608+
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ((op.DebugInfo != null ? " <!-- dbg: " + op.DebugInfo + "-->" : "") + "<!-- Wire 2 Or -->")));
598609
_sb.AppendLine("<NameCon UId=\"" + ch.OperationId + "\" Name=\"out\" />" + " <!-- " + ch.ToString() + " -->");
599-
_sb.AppendLine("<NameCon UId=\"" + op.OperationId + "\" Name=\"in" + i + "\" />" + " <!-- " + op.ToString() + " -->");
610+
_sb.AppendLine("<NameCon UId=\"" + op.OperationId + "\" Name=\"in" + orInputCounter + "\" />" + " <!-- " + op.ToString() + " -->");
600611
_sb.AppendLine("</Wire>");
601-
i++;
612+
orInputCounter++;
602613
_currentId++;
603614
}
604615
}
616+
};
617+
if (!op.DoNotCreateContact)
618+
{
619+
ImportChildOrs(or);
605620
}
606621
}
607622
else if (op is CompareOperator)
@@ -653,15 +668,15 @@ private void AddWires(IOperationOrSignal op)
653668
}
654669
}
655670
}
656-
else if (op is And || op is Or)
671+
else if (op is And /* || op is Or */ /* do we still get here? only with Or with one child */ )
657672
{
658673
for (int n = 0; n < op.Children.Count - 1; n++)
659674
{
660675
var ch = op.Children[n];
661676
var next = op.Children[n + 1];
662677
if (next is Or)
663678
{
664-
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ("<!-- Wire And next Or -->")));
679+
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ((op.DebugInfo != null ? " <!-- dbg: " + op.DebugInfo + "-->" : "") + "<!-- Wire And next Or -->")));
665680
_sb.AppendLine("<NameCon UId=\"" + ch.OperationId + "\" Name=\"out\" />" + (ch is Signal ? " <!-- " + ((Signal)ch).Name + " -->" : ""));
666681
foreach (var orSignal in next.Children)
667682
{
@@ -682,7 +697,7 @@ private void AddWires(IOperationOrSignal op)
682697
}
683698
else
684699
{
685-
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ("<!-- Wire And -->")));
700+
_sb.AppendLine("<Wire UId=\"" + _currentId + "\">" + (debug ?? ((op.DebugInfo != null ? " <!-- dbg: " + op.DebugInfo + "-->" : "") + "<!-- Wire And -->")));
686701
var outName = "out";
687702
if (ch is FunctionCall || ch is S_Move || ch is Move || ch is Convert)
688703
outName = "eno";
@@ -1060,9 +1075,9 @@ public string GetXml(ref int id)
10601075
}
10611076
}
10621077
/*_sb.AppendLine("<!--");
1063-
_sb.AppendLine("<!--");
1064-
PrintTree(_block, _sb);
1065-
_sb.AppendLine("-->");*/
1078+
_sb.AppendLine("<!--");
1079+
PrintTree(_block, _sb);
1080+
_sb.AppendLine("-->");*/
10661081
return _sb.ToString();
10671082
}
10681083

TiaCodegen/DotNetProjects.TiaCodegen.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Version>1.0.0.0</Version>
1010
<AssemblyVersion>1.0.0.0</AssemblyVersion>
1111
<FileVersion>1.0.0.0</FileVersion>
12+
<LangVersion>latest</LangVersion>
1213
</PropertyGroup>
1314

1415
</Project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
3+
namespace DotNetProjects.TiaCodegen.Extensions
4+
{
5+
public class NaturalComparer : IComparer<string>
6+
{
7+
public int Compare(string x, string y)
8+
{
9+
if (x == y) return 0;
10+
if (x == null) return -1;
11+
if (y == null) return 1;
12+
13+
int i = 0, j = 0;
14+
while (i < x.Length && j < y.Length)
15+
{
16+
char cx = x[i];
17+
char cy = y[j];
18+
19+
// If both are digits, compare numbers
20+
if (char.IsDigit(cx) && char.IsDigit(cy))
21+
{
22+
long vx = 0;
23+
while (i < x.Length && char.IsDigit(x[i]))
24+
vx = vx * 10 + (x[i++] - '0');
25+
26+
long vy = 0;
27+
while (j < y.Length && char.IsDigit(y[j]))
28+
vy = vy * 10 + (y[j++] - '0');
29+
30+
if (vx != vy)
31+
return vx < vy ? -1 : 1;
32+
}
33+
else
34+
{
35+
// Compare non-digits case-insensitively
36+
int cmp = char.ToUpperInvariant(cx).CompareTo(char.ToUpperInvariant(cy));
37+
if (cmp != 0)
38+
return cmp;
39+
40+
i++;
41+
j++;
42+
}
43+
}
44+
45+
// If one string is longer
46+
return x.Length - y.Length;
47+
}
48+
}
49+
50+
}

0 commit comments

Comments
 (0)