db536cfb2c
核心修复: - 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败) - 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements) - 修复 PropertyConverter init-only 属性组索引 新增转换器 (C# 8-13 特性): - NullCoalescingConverter: ??、?.、??= 运算符转换 - SwitchExpressionConverter: switch 表达式→if-else 链 - PrimaryConstructorConverter: 主构造函数→传统构造函数 增强: - LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等 - AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误 - NamingConverter: PascalCase→camelCase 命名转换 - DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录 - XML Doc→JavaDoc 格式转换与注释保留 新增测试: - CSharpToJavaEdgeCaseTests: 16 个边界测试 - CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试 - 从 164 增加到 179 总测试 (168 通过, 0 失败) 新增文件: - Pipeline/Converters/NullCoalescingConverter.cs - Pipeline/Converters/SwitchExpressionConverter.cs - Pipeline/Converters/PrimaryConstructorConverter.cs - Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs - Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs - Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
90 lines
3.3 KiB
C#
90 lines
3.3 KiB
C#
using System.Text;
|
|
using CodePlay.Core.Interfaces;
|
|
using CodePlay.Core.Common;
|
|
|
|
namespace CodePlay.Core.Converters;
|
|
|
|
public class CppCodeGenerator : ICodeGenerator
|
|
{
|
|
private StringBuilder _out = new();
|
|
private int _indent;
|
|
|
|
public string Generate(SyntaxTree tree)
|
|
{
|
|
_out.Clear(); _indent = 0;
|
|
|
|
_out.AppendLine("#include <iostream>");
|
|
_out.AppendLine("#include <string>");
|
|
_out.AppendLine("#include <vector>");
|
|
_out.AppendLine("#include <map>");
|
|
_out.AppendLine("#include <memory>");
|
|
_out.AppendLine("#include <future>");
|
|
_out.AppendLine();
|
|
|
|
GenNode(tree.Root);
|
|
return _out.ToString();
|
|
}
|
|
|
|
void GenNode(SyntaxNode n)
|
|
{
|
|
if (n.Type == SyntaxNodeType.Unknown && !string.IsNullOrWhiteSpace(n.Text))
|
|
{
|
|
foreach (var line in n.Text.Split('\n'))
|
|
{
|
|
var t = line.Trim();
|
|
if (!string.IsNullOrEmpty(t) && !t.StartsWith("{") && !t.StartsWith("}"))
|
|
_out.AppendLine(IndentLine(t));
|
|
}
|
|
return;
|
|
}
|
|
|
|
switch (n.Type)
|
|
{
|
|
case SyntaxNodeType.CompilationUnit:
|
|
foreach (var c in n.Children) GenNode(c);
|
|
break;
|
|
case SyntaxNodeType.Namespace:
|
|
var ns = System.Text.RegularExpressions.Regex.Match(n.Text, @"namespace\s+([\w.]+)").Groups[1].Value;
|
|
_out.AppendLine($"namespace {ns} {{"); _indent++;
|
|
foreach (var c in n.Children) GenNode(c);
|
|
_indent--; _out.AppendLine("}");
|
|
break;
|
|
case SyntaxNodeType.Class:
|
|
var cls = n.Text.Trim();
|
|
var brace = cls.IndexOf('{'); if (brace > 0) cls = cls.Substring(0, brace);
|
|
_out.AppendLine(IndentLine($"class {cls.Replace("public ", "")} {{"));
|
|
_out.AppendLine(IndentLine("public:"));
|
|
_indent++;
|
|
foreach (var c in n.Children) GenNode(c);
|
|
_indent--;
|
|
_out.AppendLine(IndentLine("};")); _out.AppendLine();
|
|
break;
|
|
case SyntaxNodeType.Method:
|
|
var sig = n.Text.Split('\n').First().Trim();
|
|
if (sig.EndsWith("{")) sig = sig[..^1].Trim();
|
|
_out.AppendLine(IndentLine($"{sig} {{"));
|
|
_indent++;
|
|
var body = string.Join('\n', n.Text.Split('{', '}').Skip(1)).Trim();
|
|
foreach (var l in body.Split('\n')) if (!string.IsNullOrWhiteSpace(l)) _out.AppendLine(IndentLine(l.Trim()));
|
|
_indent--;
|
|
_out.AppendLine(IndentLine("}")); _out.AppendLine();
|
|
break;
|
|
case SyntaxNodeType.Field:
|
|
case SyntaxNodeType.Property:
|
|
if (!string.IsNullOrWhiteSpace(n.Text)) _out.AppendLine(IndentLine(n.Text.Trim()));
|
|
break;
|
|
default:
|
|
if (!string.IsNullOrWhiteSpace(n.Text))
|
|
_out.AppendLine(IndentLine(n.Text.Trim()));
|
|
foreach (var c in n.Children) GenNode(c);
|
|
break;
|
|
}
|
|
}
|
|
|
|
string IndentLine(string t)
|
|
{
|
|
var spaces = string.Concat(Enumerable.Repeat(" ", _indent * 2));
|
|
return spaces + t;
|
|
}
|
|
}
|