Files
RScript/PublicTypes/InjectVariable/CStyle.cs
2025-11-25 17:03:43 +08:00

128 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Convention.RScript.Variable.CStyle
{
public class CScriptRScriptVariableGenerater : RScriptInjectVariableGenerater
{
public static string GetTypename(Type type)
{
if (type == typeof(int))
return "int";
if (type == typeof(float))
return "float";
if (type == typeof(double))
return "double";
if (type == typeof(bool))
return "bool";
if (type == typeof(void))
return "void";
if (type.IsEnum)
return type.FullName.Replace('`', '_');
if (type.IsClass)
return type.FullName.Replace('`', '_');
var name = type.Name.Replace('`', '_');
return name;
}
private int m_layer = 0;
private int layer
{
get=>m_layer;
set
{
m_layer = value;
Prefix = new('\t', layer);
}
}
private string Prefix = "";
public CScriptRScriptVariableGenerater(Type targetType, Generater generater, Destorier destorier, string name) : base(targetType, generater, destorier, name)
{
}
protected override string WriteClassBodyEnter(Type currentType)
{
string result = $"{Prefix}{"{"}\n{Prefix}public:";
layer++;
return result;
}
protected override string WriteClassBodyExit(Type currentType)
{
layer--;
return $"{Prefix}{"};"}";
}
protected override string WriteClassHead(Type currentType)
{
string description = string.IsNullOrEmpty(this.description) ? "" : string.Join("", from item in this.description.Split('\n') where string.IsNullOrEmpty(item) == false select $"{Prefix}{item}\n");
string suffix = currentType.BaseType == typeof(object) ? string.Empty : $" : public {GetTypename(currentType.BaseType)}";
string result = $"{Prefix}\\*{description}*\\\n{Prefix}{Prefix}class {GetTypename(currentType)}{suffix}";
return result;
}
protected override string WriteClassMethod(Type returnType, string methodName, string[] parameterNames, Type[] parameterTypes)
{
List<string> parameters = new();
for (int i = 0, e = parameterNames.Length; i < e; i++)
{
parameters.Add($"{GetTypename(parameterTypes[i])} {parameterNames[i]}");
}
return $"{Prefix}{GetTypename(returnType)} {methodName}({string.Join(", ", parameters)});";
}
protected override string WriteClassTail(Type currentType)
{
return "";
}
protected override string WriteEnumBodyEnter(Type currentType)
{
string result = $"{Prefix}{"{"}";
layer++;
return result;
}
protected override string WriteEnumBodyExit(Type currentType)
{
layer--;
return $"{Prefix}{"};"}";
}
protected override string WriteEnumHead(Type currentEnum)
{
return $"{Prefix}enum {GetTypename(currentEnum)}";
}
protected override string WriteEnumName(string enumName)
{
return $"{Prefix}{enumName},";
}
protected override string WriteEnumTail(Type currentType)
{
return "";
}
protected override string WritePageEnd(Type currentType)
{
return "";
}
protected override string WritePageHead(Type currentType)
{
return $"#include\"{GetFilename(currentType.BaseType)}\"";
}
public override string GetFilename(Type currentType)
{
return GetTypename(currentType);
}
}
}