Skip to content
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
9 changes: 9 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpClassType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpClassType : CSharpType
{
public CSharpClassType(string name) : base(TypeKind.Class, name)
{
}
}
}
33 changes: 33 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpEnumType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpEnumType : CSharpType
{
public CSharpEnumType(string name) : base(TypeKind.Enum, name)
{
}

public void SetPrimitiveType(string type)
{
BaseTypes.Clear();
BaseTypes.Add(type);
}

public void MarkAsFlags()
{
Attributes.Add("[Flags]");
}

public void AddValues(Dictionary<string, string> values)
{
foreach (var value in values)
{
var field = new CSharpEnumValue(value.Key);
if (!string.IsNullOrEmpty(value.Value))
{
field.DefaultValue = value.Value;
}
AddMember(field);
}
}
}
}
23 changes: 23 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpEnumValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpEnumValue : CSharpTypeMember
{
public string? DefaultValue { get; set; }

public CSharpEnumValue(string name) : base(name, MemberKind.Field)
{
}

public override void Generate(CodeGenContext context)
{
if (DefaultValue != null)
{
context.WriteLine($"{Name} = {DefaultValue},");
}
else
{
context.WriteLine($"{Name},");
}
}
}
}
42 changes: 42 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpField : CSharpTypeMember
{
public string VariableType { get; private set; }
public string? DefaultValue { get; set; }
public bool IsStatic { get; set; }
public bool IsReadonly { get; set; }

public CSharpField(string variableType, string name) : base(name, MemberKind.Field)
{
VariableType = variableType;
}

public override void Generate(CodeGenContext context)
{
var sb = context.AcquireTempStringBuilder();
sb.Append(VisibilityModifier);
if (IsStatic)
{
sb.Append(" static");
}

if (IsReadonly)
{
sb.Append(" readonly");
}

sb.Append(' ');
sb.Append(VariableType);
sb.Append(' ');
sb.Append(Name);
if (DefaultValue != null)
{
sb.Append($" = {DefaultValue}");
}

context.WriteLine($"{sb.ToString().Trim()};");
context.ReleaseTempStringBuilder(sb);
}
}
}
68 changes: 68 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpFile : ICodeFragmentProvider
{
private readonly List<string> _usings = [];
private readonly List<CSharpNamespace> _namespaces = [];

public string Name { get; }
public string Directory { get; }
public string FilePath { get; }

public CSharpFile(string name)
{
Name = name;
Directory = "";
FilePath = $"{name}.cs";
}

public CSharpFile(string name, string directory)
{
Name = name;
Directory = directory;
FilePath = $"{directory}/{name}.cs";
}

public CSharpNamespace GetOrCreateNamespace(string @namespace)
{
foreach (var ns in _namespaces)
{
if (ns.Name == @namespace)
{
return ns;
}
}

var rv = new CSharpNamespace(@namespace);
_namespaces.Add(rv);
return rv;
}

public void Generate(CodeGenContext context)
{
if (_usings.Count > 0)
{
foreach (var @using in _usings)
{
context.WriteLine($"using {@using};");
}
context.WriteLine();
}

for (var index = 0; index < _namespaces.Count; index++)
{
var ns = _namespaces[index];
ns.Generate(context);
if (index != _namespaces.Count - 1)
{
context.WriteLine();
}
}
}

public void AddUsing(string @namespace)
{
_usings.Add(@namespace);
}
}
}
91 changes: 91 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpMethod : CSharpTypeMember
{
private readonly List<string> _bodyContents = [];
private readonly List<(string Type, string Name, string Attribute)> _parameterList;

public bool HasExpressionBody => PreferExpressionBody && _bodyContents.Count == 1;
public bool PreferExpressionBody { get; set; }
public string ReturnType { get; private set; }
public bool IsStatic { get; set; }
public bool IsPartial { get; set; }

public CSharpMethod(string name, string returnType) : base(name, MemberKind.Method)
{
ReturnType = returnType;
_parameterList = [];
}

public CSharpMethod(string name, string returnType, (string Type, string Name, string Attribute) parameter) : base(name, MemberKind.Method)
{
ReturnType = returnType;
_parameterList = [parameter];
}

public CSharpMethod(string name, string returnType, List<(string Type, string Name, string Attribute)> parameterList) : base(name, MemberKind.Method)
{
ReturnType = returnType;
_parameterList = parameterList;
}

public void AddBodyLine(string line)
{
_bodyContents.Add(line);
}

public override void Generate(CodeGenContext context)
{
foreach (var attribute in _attributes)
{
context.WriteLine(attribute);
}

var sb = context.AcquireTempStringBuilder();
sb.Append(VisibilityModifier);
if (IsStatic)
{
sb.Append(" static");
}
if (IsPartial)
{
sb.Append(" partial");
}
// Special formatting case for ctors that have empty return type
if (!string.IsNullOrEmpty(ReturnType))
{
sb.Append(' ');
sb.Append(ReturnType);
}
sb.Append(' ');
sb.Append(Name);
sb.Append('(');
sb.Append(string.Join(", ", _parameterList.Select(x => $"{x.Attribute} {x.Type} {x.Name}".Trim())));
sb.Append(')');
if (!IsPartial)
{
if (HasExpressionBody)
{
sb.Append($" => {_bodyContents[0]};");
context.WriteLine(sb.ToString());
}
else
{
context.WriteLine(sb.ToString());
context.EnterScope();
foreach (var line in _bodyContents)
{
context.WriteLine($"{line};");
}
context.LeaveScope();
}
}
else
{
sb.Append(';');
context.WriteLine(sb.ToString());
}
context.ReleaseTempStringBuilder(sb);
}
}
}
37 changes: 37 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpNamespace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpNamespace : ICodeFragmentProvider
{
private readonly List<CSharpType> _types = [];

public string Name { get; }

public CSharpNamespace(string name)
{
Name = name;
}

public void AddType(CSharpType csType)
{
_types.Add(csType);
}

public void Generate(CodeGenContext context)
{
context.WriteLine($"namespace {Name}");
context.EnterScope();

for (var index = 0; index < _types.Count; index++)
{
var type = _types[index];
type.Generate(context);
if (index != _types.Count - 1)
{
context.WriteLine();
}
}

context.LeaveScope();
}
}
}
50 changes: 50 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpProperty : CSharpTypeMember
{
public bool IsStatic { get; set; }
public string Type { get; private set; }
public string? Getter { get; set; }
public string? Setter { get; set; }

public CSharpProperty(string name, string type) : base(name, MemberKind.Property)
{
Type = type;
}

public override void Generate(CodeGenContext context)
{
foreach (var attribute in _attributes)
{
context.WriteLine(attribute);
}

var sb = context.AcquireTempStringBuilder();
sb.Append(VisibilityModifier);
if (IsStatic)
{
sb.Append(" static");
}
sb.Append(' ');
sb.Append(Type);
sb.Append(' ');
sb.Append(Name);
if (string.IsNullOrEmpty(Setter))
{
sb.Append(" => ");
sb.Append(Getter);
sb.Append(';');
context.WriteLine(sb.ToString());
}
else
{
context.WriteLine(sb.ToString());
context.EnterScope();
context.WriteLine($"get => {Getter};");
context.WriteLine($"set => {Setter};");
context.LeaveScope();
}
context.ReleaseTempStringBuilder(sb);
}
}
}
9 changes: 9 additions & 0 deletions src/SharpMetal.Generator/CSharpCodeGen/CSharpStructType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SharpMetal.Generator.CSharpCodeGen
{
public class CSharpStructType : CSharpType
{
public CSharpStructType(string name) : base(TypeKind.Struct, name)
{
}
}
}
Loading