feat: 添加 Struct 节点的序列化支持

- 在 ModuleNode 中添加 structs 字段,支持结构体定义的序列化
- 新增 StructNode 处理逻辑,实现结构体节点的序列化
- 为结构体添加字段、初始化方法和成员方法的序列化支持
This commit is contained in:
Luke 2025-08-29 15:22:42 +08:00
parent 8fce695e8f
commit 3dd71ff742

View File

@ -83,7 +83,7 @@ public class ASTJsonSerializer {
return switch (n) {
// 模块节点
case ModuleNode(
String name, List<ImportNode> imports, List<DeclarationNode> globals, List<FunctionNode> functions, NodeContext _
String name, List<ImportNode> imports, List<DeclarationNode> globals,List<StructNode> structs, List<FunctionNode> functions, NodeContext _
) -> {
Map<String, Object> map = newNodeMap("Module");
map.put("name", name);
@ -105,9 +105,39 @@ public class ASTJsonSerializer {
for (FunctionNode f : functions) {
funcs.add(nodeToMap(f));
}
List<Object> lStructs = new ArrayList<>();
structs.forEach(s -> lStructs.add(nodeToMap(s)));
map.put("structs", lStructs);
map.put("functions", funcs);
yield map;
}
// Struct 节点
case StructNode(
String name, List<DeclarationNode> fields,
FunctionNode init, List<FunctionNode> methods, NodeContext _
) -> {
Map<String, Object> map = newNodeMap("Struct");
map.put("name", name);
List<Object> lFields = new ArrayList<>();
fields.forEach(d -> lFields.add(Map.of(
"type", "Field",
"name", d.getName(),
"varType", d.getType())));
map.put("fields", lFields);
map.put("init", init == null ? null : nodeToMap(init));
List<Object> lMethods = new ArrayList<>();
methods.forEach(f -> lMethods.add(nodeToMap(f)));
map.put("methods", lMethods);
yield map;
}
// 函数定义节点
case FunctionNode f -> {
Map<String, Object> map = newNodeMap("Function");