feat: 增加常量声明支持并优化变量声明节点
- 在 DeclarationNode 类中添加 isConst 字段,用于表示是否为常量声明 -增加 isConst() 方法以判断声明是否为常量 - 优化类注释,明确变量和方法的作用 - 更新构造函数以支持常量声明
This commit is contained in:
parent
310e909359
commit
18c9cbad7b
@ -10,21 +10,26 @@ import java.util.Optional;
|
||||
* {@code DeclarationNode} 表示抽象语法树(AST)中的变量声明语句节点。
|
||||
* <p>
|
||||
* 变量声明用于在语法层引入新的标识符及其类型信息,
|
||||
* 通常格式为 {@code type name = initializer;},其中初始化表达式可省略。
|
||||
* </p>
|
||||
*/
|
||||
public class DeclarationNode implements StatementNode {
|
||||
|
||||
/** 声明的变量名称 */
|
||||
/** 声明的变量名称。 */
|
||||
private final String name;
|
||||
|
||||
/** 变量的数据类型(如 "int", "string") */
|
||||
/** 变量的数据类型(如 "int", "string")。 */
|
||||
private final String type;
|
||||
|
||||
/** 可选的初始化表达式 */
|
||||
/** 是否为常量声明(true 表示 const 变量,false 表示普通变量)。 */
|
||||
private final boolean isConst;
|
||||
|
||||
/**
|
||||
* 可选的初始化表达式。
|
||||
* 如果未指定初始化表达式,则为 Optional.empty()。
|
||||
*/
|
||||
private final Optional<ExpressionNode> initializer;
|
||||
|
||||
/** 节点上下文信息(包含行号、列号等) */
|
||||
/** 节点上下文信息(如源码中的行号、列号等)。 */
|
||||
private final NodeContext context;
|
||||
|
||||
/**
|
||||
@ -32,12 +37,14 @@ public class DeclarationNode implements StatementNode {
|
||||
*
|
||||
* @param name 变量名称
|
||||
* @param type 变量类型字符串(如 "int"、"string")
|
||||
* @param isConst 是否为常量声明
|
||||
* @param initializer 可选初始化表达式,若为 {@code null} 表示未初始化
|
||||
* @param context 节点上下文信息(包含行号、列号等)
|
||||
*/
|
||||
public DeclarationNode(String name, String type, ExpressionNode initializer, NodeContext context) {
|
||||
public DeclarationNode(String name, String type, boolean isConst, ExpressionNode initializer, NodeContext context) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.isConst = isConst;
|
||||
this.initializer = Optional.ofNullable(initializer);
|
||||
this.context = context;
|
||||
}
|
||||
@ -60,6 +67,15 @@ public class DeclarationNode implements StatementNode {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断该声明是否为常量(const)。
|
||||
*
|
||||
* @return 如果为常量声明则返回 true,否则返回 false
|
||||
*/
|
||||
public boolean isConst() {
|
||||
return isConst;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取可选的初始化表达式。
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user