From 18c9cbad7b8bc5ca6e9f100a06e547516f6ebf33 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 26 Aug 2025 01:44:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=B8=B8=E9=87=8F?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E6=94=AF=E6=8C=81=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=A3=B0=E6=98=8E=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 DeclarationNode 类中添加 isConst 字段,用于表示是否为常量声明 -增加 isConst() 方法以判断声明是否为常量 - 优化类注释,明确变量和方法的作用 - 更新构造函数以支持常量声明 --- .../compiler/parser/ast/DeclarationNode.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) 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; + } + /** * 获取可选的初始化表达式。 *