feat: 支持类继承和多构造函数的序列化
- 在 StructNode 中添加 parent 属性,用于表示父类- 支持多构造函数的序列化,将 inits 参数替换单个 init 参数 - 优化 methods 参数的序列化逻辑,增加空值检查 - 重构部分代码,提高可读性和维护性
This commit is contained in:
parent
5c48f4038b
commit
808403e8e8
@ -5,7 +5,10 @@ import org.jcnc.snow.compiler.parser.ast.base.ExpressionNode;
|
||||
import org.jcnc.snow.compiler.parser.ast.base.Node;
|
||||
import org.jcnc.snow.compiler.parser.ast.base.NodeContext;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@code ASTJsonSerializer} 是抽象语法树(AST)序列化工具类。
|
||||
@ -83,7 +86,8 @@ public class ASTJsonSerializer {
|
||||
return switch (n) {
|
||||
// 模块节点
|
||||
case ModuleNode(
|
||||
String name, List<ImportNode> imports, List<DeclarationNode> globals,List<StructNode> structs, 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,22 +109,21 @@ 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 节点
|
||||
// Struct 节点(多构造支持)
|
||||
case StructNode(
|
||||
String name, List<DeclarationNode> fields,
|
||||
FunctionNode init, List<FunctionNode> methods, NodeContext _
|
||||
String name, String parent, List<DeclarationNode> fields, List<FunctionNode> inits,
|
||||
List<FunctionNode> methods, NodeContext _
|
||||
) -> {
|
||||
Map<String, Object> map = newNodeMap("Struct");
|
||||
map.put("name", name);
|
||||
map.put("parent", parent);
|
||||
|
||||
List<Object> lFields = new ArrayList<>();
|
||||
fields.forEach(d -> lFields.add(Map.of(
|
||||
@ -129,10 +132,19 @@ public class ASTJsonSerializer {
|
||||
"varType", d.getType())));
|
||||
map.put("fields", lFields);
|
||||
|
||||
map.put("init", init == null ? null : nodeToMap(init));
|
||||
// 多构造函数序列化为 inits 数组
|
||||
List<Object> lInits = new ArrayList<>();
|
||||
if (inits != null) {
|
||||
for (FunctionNode ctor : inits) {
|
||||
lInits.add(nodeToMap(ctor));
|
||||
}
|
||||
}
|
||||
map.put("inits", lInits);
|
||||
|
||||
List<Object> lMethods = new ArrayList<>();
|
||||
methods.forEach(f -> lMethods.add(nodeToMap(f)));
|
||||
if (methods != null) {
|
||||
for (FunctionNode f : methods) lMethods.add(nodeToMap(f));
|
||||
}
|
||||
map.put("methods", lMethods);
|
||||
yield map;
|
||||
}
|
||||
@ -219,9 +231,8 @@ public class ASTJsonSerializer {
|
||||
private static Object exprToMap(ExpressionNode expr) {
|
||||
return switch (expr) {
|
||||
// 二元表达式
|
||||
case BinaryExpressionNode(
|
||||
ExpressionNode left, String operator, ExpressionNode right, NodeContext _
|
||||
) -> exprMap("BinaryExpression",
|
||||
case BinaryExpressionNode(ExpressionNode left, String operator, ExpressionNode right, NodeContext _) ->
|
||||
exprMap("BinaryExpression",
|
||||
"left", exprToMap(left),
|
||||
"operator", operator,
|
||||
"right", exprToMap(right)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user