Skip to content

Commit

Permalink
[wip] taking s7 pragmas into account when compiling twins
Browse files Browse the repository at this point in the history
  • Loading branch information
PTKu committed Nov 23, 2023
1 parent 14e7127 commit d80a858
Show file tree
Hide file tree
Showing 77 changed files with 1,990 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ public interface ISourceBuilder
/// Get the semantic compilation for this builder.
/// </summary>
public Compilation Compilation { get; }

eCommAccessibility TypeCommAccessibility { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AXSharp.Compiler.Core
{
public enum eCommAccessibility
{
None,
ReadWrite,
ReadOnly,
}
}
129 changes: 109 additions & 20 deletions src/AXSharp.compiler/src/AXSharp.Cs.Compiler/Helpers/SemanticsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class SemanticsHelpers
public static bool IsMemberEligibleForTranspile(this IFieldDeclaration field, ISourceBuilder sourceBuilder, string coBuilder = "")
{
return field.AccessModifier == AccessModifier.Public
&& field.Type.IsTypeEligibleForTranspile(sourceBuilder)
&& field.IsEligibleForTranspile(sourceBuilder)
&& !IsToBeOmitted(field, sourceBuilder, coBuilder);
}

Expand Down Expand Up @@ -75,28 +75,72 @@ private static bool IsToBeOmitted(this IStorageDeclaration fieldDeclaration, ISo
/// <summary>
/// Determines whether the member or type is eligible for generation.
/// </summary>
/// <param name="typeDeclaration"></param>
/// <param name="fieldDeclaration"></param>
/// <param name="sourceBuilder"></param>
/// <returns></returns>
public static bool IsTypeEligibleForTranspile(this ITypeDeclaration typeDeclaration, ISourceBuilder sourceBuilder)
/// <returns>True when the type is eligible</returns>
public static bool IsEligibleForTranspile(this IFieldDeclaration fieldDeclaration, ISourceBuilder sourceBuilder)
{
var type = fieldDeclaration.Type;
return !(type is IReferenceTypeDeclaration)
&&
fieldDeclaration.IsAvailableForComm(sourceBuilder)
&&
(type is IScalarTypeDeclaration ||
type is IStringTypeDeclaration ||
type is IStructuredTypeDeclaration ||
type is INamedValueTypeDeclaration ||
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
p.FullyQualifiedName == type.FullyQualifiedName));
}

/// <summary>
/// Determines whether the member or type is eligible for generation.
/// </summary>
/// <param name="variableDeclaration"></param>
/// <param name="sourceBuilder"></param>
/// <returns>True when the type is eligible</returns>
public static bool IsEligibleForTranspile(this IVariableDeclaration variableDeclaration, ISourceBuilder sourceBuilder)
{
var asArray = typeDeclaration as IArrayTypeDeclaration;
var singleDimensionalArray = asArray is null || asArray.Dimensions.Count == 1;
var type = variableDeclaration.Type;
return !(type is IReferenceTypeDeclaration)
&&
variableDeclaration.IsAvailableForComm(sourceBuilder)
&&
(type is IScalarTypeDeclaration ||
type is IStringTypeDeclaration ||
type is IStructuredTypeDeclaration ||
type is INamedValueTypeDeclaration ||
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
p.FullyQualifiedName == type.FullyQualifiedName));
}


var isEligibleType = !(typeDeclaration is IReferenceTypeDeclaration)
/// <summary>
/// Determines whether the member is eligible for generation.
/// </summary>
/// <param name="arrayTypeDeclaration"></param>
/// <param name="sourceBuilder">Source builder</param>
/// <returns></returns>
public static bool IsEligibleForTranspile(this IArrayTypeDeclaration arrayTypeDeclaration, ISourceBuilder sourceBuilder)
{
var singleDimensionalArray = arrayTypeDeclaration.Dimensions.Count == 1;

var isEligibleType = !(arrayTypeDeclaration.ElementTypeAccess.Type is IReferenceTypeDeclaration)
&&
arrayTypeDeclaration.IsAvailableForComm(sourceBuilder)
&&
(typeDeclaration is IScalarTypeDeclaration ||
typeDeclaration is IStringTypeDeclaration ||
typeDeclaration is IStructuredTypeDeclaration ||
typeDeclaration is INamedValueTypeDeclaration ||
(arrayTypeDeclaration.ElementTypeAccess.Type is IScalarTypeDeclaration ||
arrayTypeDeclaration.ElementTypeAccess.Type is IStringTypeDeclaration ||
arrayTypeDeclaration.ElementTypeAccess.Type is IStructuredTypeDeclaration ||
arrayTypeDeclaration.ElementTypeAccess.Type is INamedValueTypeDeclaration ||
sourceBuilder.Compilation.GetSemanticTree().Types.Any(p =>
p.FullyQualifiedName == typeDeclaration.FullyQualifiedName));
p.FullyQualifiedName == arrayTypeDeclaration.ElementTypeAccess.Type.FullyQualifiedName));

return isEligibleType && singleDimensionalArray;

}


/// <summary>
/// Determines whether the member is eligible for generation.
/// </summary>
Expand All @@ -107,7 +151,7 @@ typeDeclaration is INamedValueTypeDeclaration ||
public static bool IsMemberEligibleForTranspile(this IVariableDeclaration variable, ISourceBuilder sourceBuilder, string coBuilder = "")
{
return variable.IsInGlobalMemory
&& variable.Type.IsTypeEligibleForTranspile(sourceBuilder)
&& variable.IsEligibleForTranspile(sourceBuilder)
&& !IsToBeOmitted(variable, sourceBuilder, coBuilder);
}

Expand Down Expand Up @@ -135,15 +179,60 @@ public static bool IsMemberEligibleForConstructor(this IVariableDeclaration vari
return variable.IsMemberEligibleForTranspile(sourceBuilder, coBuilder);
}

/// <summary>
/// Determines whether the member is eligible for generation.
/// </summary>
/// <param name="arrayTypeDeclaration"></param>
/// <param name="sourceBuilder">Source builder</param>
/// <returns></returns>
private static bool IsAvailableForComm(this IDeclaration declaration, ISourceBuilder sourceBuilder)
{
var pragmaReadWrite = "S7.extern=ReadWrite".ToLower();
var pragmaRead = "S7.extern=ReadOnly".ToLower();
return declaration.Pragmas.Any(p =>
{
var prgma = p.Content.ToLower().Replace(" ", string.Empty, StringComparison.InvariantCulture);
return (prgma == pragmaReadWrite || prgma == pragmaRead);
}) || (sourceBuilder.TypeCommAccessibility == eCommAccessibility.ReadOnly || sourceBuilder.TypeCommAccessibility == eCommAccessibility.ReadWrite);
}
internal static bool IsAvailableReadOnlyForComm(this IDeclaration declaration)
{
var pargmaContent = "S7.extern=Read".ToLower();
return declaration.Pragmas.Any(p =>
{
var prgma = p.Content.ToLower().Replace(" ", string.Empty, StringComparison.InvariantCulture);
return (prgma == pargmaContent);
});
}
private static bool IsAvailableReadWriteForComm(this IDeclaration declaration)
{
var pargmaContent = "S7.extern=ReadWrite".ToLower();
return declaration.Pragmas.Any(p =>
{
var prgma = p.Content.ToLower().Replace(" ", string.Empty, StringComparison.InvariantCulture);
return (prgma == pargmaContent);
});
}

public static eCommAccessibility GetCommAccessibility(this IDeclaration declaration)
{

if (declaration.IsAvailableReadOnlyForComm())
{
return eCommAccessibility.ReadOnly;
}

if (declaration.IsAvailableReadWriteForComm())
{
return eCommAccessibility.ReadWrite;
}

return eCommAccessibility.None;
}






public static bool IsMemberEligibleForConstructor(this IArrayTypeDeclaration arrayTypeDeclaration, ISourceBuilder sourceBuilder)
{
return arrayTypeDeclaration.ElementTypeAccess.Type.IsTypeEligibleForTranspile(sourceBuilder);
return IsEligibleForTranspile(arrayTypeDeclaration, sourceBuilder);

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVis
AddToSource("{get;}");
break;
case IArrayTypeDeclaration array:
if (array.ElementTypeAccess.Type.IsTypeEligibleForTranspile(SourceBuilder))
if (array.IsEligibleForTranspile(SourceBuilder))
{
AddToSource($"{fieldDeclaration.AccessModifier.Transform()} ");
fieldDeclaration.Type.Accept(visitor, this);
Expand Down Expand Up @@ -161,7 +161,7 @@ public void CreateVariableDeclaration(IVariableDeclaration semantics, IxNodeVisi
AddToSource("{get;}");
break;
case IArrayTypeDeclaration array:
if (array.ElementTypeAccess.Type.IsTypeEligibleForTranspile(SourceBuilder))
if (array.IsEligibleForTranspile(SourceBuilder))
{
AddToSource($"public");
semantics.Type.Accept(visitor, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void CreateVariableDeclaration(IVariableDeclaration variableDeclaration,
}
}


internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration declaration)
{
switch (typeDeclaration)
Expand All @@ -68,18 +69,23 @@ internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration de
AddToSource($"#pragma warning restore CS0612\n");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
if (arrayTypeDeclaration.IsMemberEligibleForConstructor(SourceBuilder))
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"#pragma warning disable CS0612\n");
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodNameNoac}Async()).Select(p => p.Result).ToArray();");
AddToSource($"#pragma warning restore CS0612\n");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(p => p.LastValue).ToArray();");
break;
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"#pragma warning disable CS0612\n");
AddToSource(
$"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodNameNoac}Async()).Select(p => p.Result).ToArray();");
AddToSource($"#pragma warning restore CS0612\n");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource(
$"plain.{declaration.Name} = {declaration.Name}.Select(p => p.LastValue).ToArray();");
break;
}
}
break;
case IReferenceTypeDeclaration referenceTypeDeclaration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,30 @@ private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration dec
AddToSource($"#pragma warning restore CS0612\n");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:


switch (arrayTypeDeclaration.ElementTypeAccess.Type)
if (arrayTypeDeclaration.IsMemberEligibleForConstructor(SourceBuilder))
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"#pragma warning disable CS0612\n");
AddToSource($"{declaration.Name}.Select(p => p.{MethodNameNoac}Async(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
AddToSource($"#pragma warning restore CS0612\n");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"#pragma warning disable CS0612\n");
AddToSource($"{declaration.Name}.Select(p => p.LethargicWrite(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
AddToSource($"#pragma warning restore CS0612\n");
break;
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"#pragma warning disable CS0612\n");
AddToSource(
$"{declaration.Name}.Select(p => p.{MethodNameNoac}Async(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
AddToSource($"#pragma warning restore CS0612\n");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"#pragma warning disable CS0612\n");
AddToSource(
$"{declaration.Name}.Select(p => p.LethargicWrite(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
AddToSource($"#pragma warning restore CS0612\n");
break;
}
}

break;
case IReferenceTypeDeclaration referenceTypeDeclaration:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,25 @@ private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration dec
break;
case IArrayTypeDeclaration arrayTypeDeclaration:


switch (arrayTypeDeclaration.ElementTypeAccess.Type)
if (arrayTypeDeclaration.IsMemberEligibleForConstructor(SourceBuilder))
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"{declaration.Name}.Select(p => p.{MethodName}Async(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"{declaration.Name}.Select(p => p.Shadow = plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++]).ToArray();");
break;
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource(
$"{declaration.Name}.Select(p => p.{MethodName}Async(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource(
$"{declaration.Name}.Select(p => p.Shadow = plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++]).ToArray();");
break;
}
}

break;
case IReferenceTypeDeclaration referenceTypeDeclaration:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,24 @@ internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration de
AddToSource($" plain.{declaration.Name} = await {declaration.Name}.{MethodName}Async();");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
if (arrayTypeDeclaration.IsMemberEligibleForConstructor(SourceBuilder))
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}Async()).Select(p => p.Result).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:

AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(p => p.Shadow).ToArray();");
break;
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource(
$"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}Async()).Select(p => p.Result).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:

AddToSource(
$"plain.{declaration.Name} = {declaration.Name}.Select(p => p.Shadow).ToArray();");
break;
}
}

break;
case IReferenceTypeDeclaration referenceTypeDeclaration:
break;
Expand Down
Loading

0 comments on commit d80a858

Please sign in to comment.