refactor: 重构 StructNode 类

- 将 init 参数改为 inits,支持多个构造函数
- 优化 toString 方法的实现,提高可读性和性能
This commit is contained in:
Luke 2025-09-01 16:53:16 +08:00
parent 6dc651d305
commit 94b4bf94cf

View File

@ -20,7 +20,7 @@ import java.util.StringJoiner;
* @param name 结构体名称 * @param name 结构体名称
* @param parent 父类名称无继承时为 {@code null} * @param parent 父类名称无继承时为 {@code null}
* @param fields 字段声明列表 * @param fields 字段声明列表
* @param init 构造函数可为 null * @param inits 构造函数可为 null
* @param methods 方法列表 * @param methods 方法列表
* @param context 源码位置信息 * @param context 源码位置信息
*/ */
@ -28,7 +28,7 @@ public record StructNode(
String name, String name,
String parent, String parent,
List<DeclarationNode> fields, List<DeclarationNode> fields,
FunctionNode init, List<FunctionNode> inits,
List<FunctionNode> methods, List<FunctionNode> methods,
NodeContext context NodeContext context
) implements Node { ) implements Node {
@ -40,19 +40,13 @@ public record StructNode(
*/ */
@Override @Override
public String toString() { public String toString() {
// 1) 构造字段声明的字符串类型+名称 return new StringJoiner(", ", "StructNode[", "]")
StringJoiner fj = new StringJoiner(", "); .add("name=" + name)
fields.forEach(d -> fj.add(d.getType() + " " + d.getName())); .add("parent=" + parent)
.add("fields=" + fields.size())
// 2) 构造方法名列表字符串 .add("ctors=" + (inits == null ? 0 : inits.size()))
StringJoiner mj = new StringJoiner(", "); .add("methods=" + methods.size())
methods.forEach(f -> mj.add(f.name())); .toString();
// 3) 合成最终输出
return "Struct(name=" + name +
", parent=" + (parent == null ? "null" : parent) +
", fields=[" + fj + "], init=" +
(init == null ? "null" : init.name()) +
", methods=[" + mj + "])";
} }
} }