diff --git a/src/main/java/org/jcnc/snow/compiler/parser/ast/DeclarationNode.java b/src/main/java/org/jcnc/snow/compiler/parser/ast/DeclarationNode.java index dd5bea6..af2cdae 100644 --- a/src/main/java/org/jcnc/snow/compiler/parser/ast/DeclarationNode.java +++ b/src/main/java/org/jcnc/snow/compiler/parser/ast/DeclarationNode.java @@ -10,21 +10,26 @@ import java.util.Optional; * {@code DeclarationNode} 表示抽象语法树(AST)中的变量声明语句节点。 *

* 变量声明用于在语法层引入新的标识符及其类型信息, - * 通常格式为 {@code type name = initializer;},其中初始化表达式可省略。 *

*/ 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 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; + } + /** * 获取可选的初始化表达式。 *