Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added implementation for plaintoshadow/shadowtoplain swap methods #61

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration de
case IReferenceTypeDeclaration referenceTypeDeclaration:
break;
case IEnumTypeDeclaration enumTypeDeclaration:
AddToSource($" plain.{declaration.Name} = ({declaration.Type.FullyQualifiedName}){declaration.Name}.LastValue;;");
AddToSource($" plain.{declaration.Name} = ({declaration.Type.FullyQualifiedName}){declaration.Name}.LastValue;");
break;
case INamedValueTypeDeclaration namedValueTypeDeclaration:
AddToSource($" plain.{declaration.Name} = {declaration.Name}.LastValue;");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using AX.ST.Semantic.Model.Declarations.Types;
using AX.ST.Semantic.Model.Declarations;
using Ix.Compiler.Cs.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AX.ST.Semantic;
using Ix.Compiler.Core;
using IX.Compiler.Core;
using Ix.Connector;

namespace Ix.Compiler.Cs.Onliner
{
internal class CsOnlinerPlainerPlainToShadowBuilder : ICombinedThreeVisitor
{
private readonly StringBuilder _memberDeclarations = new();

protected CsOnlinerPlainerPlainToShadowBuilder(Compilation compilation)
{
Compilation = compilation;
}

private Compilation Compilation { get; }

public string Output => _memberDeclarations.ToString().FormatCode();


public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVisitor visitor)
{
if (fieldDeclaration.IsMemberEligibleForTranspile(Compilation))
{
CreateAssignment(fieldDeclaration.Type, fieldDeclaration);
}
}

public void CreateInterfaceDeclaration(IInterfaceDeclaration interfaceDeclaration, IxNodeVisitor visitor)
{
//
}

public void CreateVariableDeclaration(IVariableDeclaration variableDeclaration, IxNodeVisitor visitor)
{
if (variableDeclaration.IsMemberEligibleForTranspile(Compilation))
{
CreateAssignment(variableDeclaration.Type, variableDeclaration);
}
}

private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration declaration)
{
switch (typeDeclaration)
{
case IInterfaceDeclaration interfaceDeclaration:
case IClassDeclaration classDeclaration:
//case IAnonymousTypeDeclaration anonymousTypeDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($" await this.{declaration.Name}.{MethodName}(plain.{declaration.Name});");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:


switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
//ArrayOfBytes.Select(p => p.Cyclic = plain.ArrayOfBytes[index++]).ToArray();
//AddToSource($"Ix.Connector.BuilderHelpers.Arrays.CopyPlainToOnline<Pocos.{arrayTypeDeclaration.ElementTypeAccess.Type.FullyQualifiedName}, {arrayTypeDeclaration.ElementTypeAccess.Type.FullyQualifiedName}>(plain.{declaration.Name}, {declaration.Name});");
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"{declaration.Name}.Select(p => p.{MethodName}(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
//ArrayOfDrives.Select(p => p.PlainToOnlineAsync(plain.ArrayOfDrives[index++])).ToArray();
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"{declaration.Name}.Select(p => p.Shadow = plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++]).ToArray();");
//AddToSource($"Ix.Connector.BuilderHelpers.Arrays.CopyPlainToOnline<{IecToClrConverter.TransformType(arrayTypeDeclaration.ElementTypeAccess.Type)}, {IecToOnlinerConverter.TransformType(arrayTypeDeclaration.ElementTypeAccess.Type)}>(plain.{declaration.Name}, {declaration.Name});");
break;
}
break;
break;
case IReferenceTypeDeclaration referenceTypeDeclaration:
break;
case IEnumTypeDeclaration enumTypeDeclaration:
AddToSource($" {declaration.Name}.Shadow = (short)plain.{declaration.Name};");
break;
case INamedValueTypeDeclaration namedValueTypeDeclaration:
AddToSource($" {declaration.Name}.Shadow = plain.{declaration.Name};");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($" {declaration.Name}.Shadow = plain.{declaration.Name};");
break;
}
}

public void CreateArrayTypeDeclaration(IArrayTypeDeclaration arrayTypeDeclaration, IxNodeVisitor visitor)
{
//
}


protected void AddToSource(string token, string separator = " ")
{
_memberDeclarations.Append($"{token}{separator}");
}

public void AddTypeConstructionParameters(string parametersString)
{
AddToSource(parametersString);
}

private static readonly string MethodName = TwinObjectExtensions.PlainToShadowMethodName;

public static CsOnlinerPlainerPlainToShadowBuilder Create(IxNodeVisitor visitor, IStructuredTypeDeclaration semantics,
Compilation compilation)
{
var builder = new CsOnlinerPlainerPlainToShadowBuilder(compilation);
builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));

builder.AddToSource("return this.RetrievePrimitives();");
builder.AddToSource($"}}");
return builder;
}

public static CsOnlinerPlainerPlainToShadowBuilder Create(IxNodeVisitor visitor, IClassDeclaration semantics,
Compilation compilation, bool isExtended)
{
var builder = new CsOnlinerPlainerPlainToShadowBuilder(compilation);
builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");


if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource("return this.RetrievePrimitives();");

builder.AddToSource($"}}");
return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using AX.ST.Semantic;
using AX.ST.Semantic.Model.Declarations;
using AX.ST.Semantic.Model.Declarations.Types;
using Ix.Compiler.Core;
using Ix.Compiler.Cs.Helpers;
using Ix.Connector;
using IX.Compiler.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ix.Compiler.Cs.Onliner
{
internal class CsOnlinerPlainerShadowToPlainBuilder : ICombinedThreeVisitor
{
private readonly StringBuilder _memberDeclarations = new();

protected CsOnlinerPlainerShadowToPlainBuilder(Compilation compilation)
{
Compilation = compilation;
}

private Compilation Compilation { get; }

public string Output => _memberDeclarations.ToString().FormatCode();


public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVisitor visitor)
{
if (fieldDeclaration.IsMemberEligibleForTranspile(Compilation))
{
CreateAssignment(fieldDeclaration.Type, fieldDeclaration);
}
}

public void CreateInterfaceDeclaration(IInterfaceDeclaration interfaceDeclaration, IxNodeVisitor visitor)
{
//
}

public void CreateVariableDeclaration(IVariableDeclaration variableDeclaration, IxNodeVisitor visitor)
{
if (variableDeclaration.IsMemberEligibleForTranspile(Compilation))
{
CreateAssignment(variableDeclaration.Type, variableDeclaration);
}
}

internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration declaration)
{
switch (typeDeclaration)
{
case IInterfaceDeclaration interfaceDeclaration:
case IClassDeclaration classDeclaration:
//case IAnonymousTypeDeclaration anonymousTypeDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($" plain.{declaration.Name} = await {declaration.Name}.{MethodName}();");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
//plain.ArrayOfDrives = ArrayOfDrives.Select(async p => await p.OnlineToPlainAsync()).Select(p => p.Result).ToArray();
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}()).Select(p => p.Result).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
//plain.ArrayOfBytes = ArrayOfBytes.Select(p => p.LastValue).ToArray();

AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(p => p.Shadow).ToArray();");
//AddToSource(
// $"Ix.Connector.BuilderHelpers.Arrays.CopyOnlineToPlain<{IecToOnlinerConverter.TransformType(arrayTypeDeclaration.ElementTypeAccess.Type)},{IecToClrConverter.TransformType(arrayTypeDeclaration.ElementTypeAccess.Type)}>({declaration.Name}, plain.{declaration.Name});");
break;
}
break;
case IReferenceTypeDeclaration referenceTypeDeclaration:
break;
case IEnumTypeDeclaration enumTypeDeclaration:
AddToSource($" plain.{declaration.Name} = ({declaration.Type.FullyQualifiedName}){declaration.Name}.Shadow;");
break;
case INamedValueTypeDeclaration namedValueTypeDeclaration:
AddToSource($" plain.{declaration.Name} = {declaration.Name}.Shadow;");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($" plain.{declaration.Name} = {declaration.Name}.Shadow;");
break;
}
}

public void CreateArrayTypeDeclaration(IArrayTypeDeclaration arrayTypeDeclaration, IxNodeVisitor visitor)
{
//
}


protected void AddToSource(string token, string separator = " ")
{
_memberDeclarations.Append($"{token}{separator}");
}

public void AddTypeConstructionParameters(string parametersString)
{
AddToSource(parametersString);
}

protected static readonly string MethodName = TwinObjectExtensions.ShadowToPlainMethodName;

public static CsOnlinerPlainerShadowToPlainBuilder Create(IxNodeVisitor visitor, IStructuredTypeDeclaration semantics,
Compilation compilation)
{
var builder = new CsOnlinerPlainerShadowToPlainBuilder(compilation);
builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(){{\n");
builder.AddToSource($"Pocos.{semantics.FullyQualifiedName} plain = new Pocos.{semantics.FullyQualifiedName}();");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));

builder.AddToSource($"return plain;");
builder.AddToSource($"}}");
return builder;
}

public static CsOnlinerPlainerShadowToPlainBuilder Create(IxNodeVisitor visitor, IClassDeclaration semantics,
Compilation compilation, bool isExtended)
{
var builder = new CsOnlinerPlainerShadowToPlainBuilder(compilation);
builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(){{\n");
builder.AddToSource($"Pocos.{semantics.FullyQualifiedName} plain = new Pocos.{semantics.FullyQualifiedName}();");

if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource($"return plain;");
builder.AddToSource($"}}");
return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using AX.ST.Semantic;
using AX.ST.Semantic.Model.Declarations.Types;
using AX.ST.Semantic.Model.Declarations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IX.Compiler.Core;

namespace Ix.Compiler.Cs.Onliner
{
internal class CsOnlinerPlainerShadowToPlainProtectedBuilder : CsOnlinerPlainerShadowToPlainBuilder
{
private readonly StringBuilder _memberDeclarations = new();

protected CsOnlinerPlainerShadowToPlainProtectedBuilder(Compilation compilation) : base(compilation)
{

}

public static CsOnlinerPlainerShadowToPlainProtectedBuilder Create(IxNodeVisitor visitor, IStructuredTypeDeclaration semantics,
Compilation compilation)
{
var builder = new CsOnlinerPlainerShadowToPlainProtectedBuilder(compilation);
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource($"return plain;");
builder.AddToSource($"}}");
return builder;
}

public static CsOnlinerPlainerShadowToPlainProtectedBuilder Create(IxNodeVisitor visitor, IClassDeclaration semantics,
Compilation compilation, bool isExtended)
{
var builder = new CsOnlinerPlainerShadowToPlainProtectedBuilder(compilation);
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");


if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource($"return plain;");
builder.AddToSource($"}}");
return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public void CreateClassDeclaration(IClassDeclarationSyntax classDeclarationSynta
AddToSource(CsOnlinerPlainerOnlineToPlainBuilder.Create(visitor, classDeclaration, Compilation, isExtended).Output);
AddToSource(CsOnlinerPlainerOnlineToPlainProtectedBuilder.Create(visitor, classDeclaration, Compilation, isExtended).Output);
AddToSource(CsOnlinerPlainerPlainToOnlineBuilder.Create(visitor, classDeclaration, Compilation, isExtended).Output);


AddToSource(CsOnlinerPlainerShadowToPlainBuilder.Create(visitor, classDeclaration, Compilation, isExtended).Output);
AddToSource(CsOnlinerPlainerShadowToPlainProtectedBuilder.Create(visitor, classDeclaration, Compilation, isExtended).Output);
AddToSource(CsOnlinerPlainerPlainToShadowBuilder.Create(visitor, classDeclaration, Compilation, isExtended).Output);

if (!isExtended) CreateITwinObjectImplementation();

AddToSource("}");
Expand Down
4 changes: 4 additions & 0 deletions src/ix.connectors/src/Ix.Connector/TwinObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static class TwinObjectExtensions

public static readonly string PlainToOnlineMethodName = "PlainToOnlineAsync";

public static readonly string ShadowToPlainMethodName = "ShadowToPlainAsync";

public static readonly string PlainToShadowMethodName = "PlainToShadowAsync";

/// <summary>
/// Makes <see cref="Ix.Connector.ITwinObject" /> readonly for this application.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ CONFIGURATION MyConfiguration
PlainToOnline_should_copy_entire_structure : MonsterData.Monster;
OnlineToShadowAsync_should_copy_entire_structure : MonsterData.Monster;
ShadowToOnlineAsync_should_copy_entire_structure : MonsterData.Monster;

ShadowToPlainAsync_should_copy_entire_structure : MonsterData.Monster;
PlainToShadowAsync_should_copy_entire_structure : MonsterData.Monster;
Pokus : Pokus;

RealMonster : RealMonsterData.RealMonster;
OnlineToShadow_should_copy : RealMonsterData.RealMonster;
ShadowToOnline_should_copy : RealMonsterData.RealMonster;
OnlineToPlain_should_copy: RealMonsterData.RealMonster;
PlainToOnline_should_copy: RealMonsterData.RealMonster;

END_VAR
END_CONFIGURATION

Expand Down
Loading