From 7fab3cc6625910913c3100c6d678197a9a03d585 Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 3 Aug 2025 00:36:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E6=A3=80=E6=9F=A5=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=8F=98=E9=87=8F=E9=87=8D=E5=A4=8D=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 FunctionChecker 类中增加了对全局变量重复声明的检查 - 如果全局变量已经存在,则添加语义错误信息 --- .../jcnc/snow/compiler/semantic/core/FunctionChecker.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jcnc/snow/compiler/semantic/core/FunctionChecker.java b/src/main/java/org/jcnc/snow/compiler/semantic/core/FunctionChecker.java index cb54eb6..50f1ec6 100644 --- a/src/main/java/org/jcnc/snow/compiler/semantic/core/FunctionChecker.java +++ b/src/main/java/org/jcnc/snow/compiler/semantic/core/FunctionChecker.java @@ -59,7 +59,13 @@ public record FunctionChecker(Context ctx) { SymbolTable globalScope = new SymbolTable(null); for (DeclarationNode g : mod.globals()) { var t = ctx.parseType(g.getType()); - globalScope.define(new Symbol(g.getName(), t, SymbolKind.VARIABLE)); + // 检查全局变量是否重复声明 + if (!globalScope.define(new Symbol(g.getName(), t, SymbolKind.VARIABLE))) { + ctx.errors().add(new SemanticError( + g, + "全局变量重复声明: " + g.getName() + )); + } } // 遍历模块中所有函数定义 From 2188171b63cf6df92fe117fc6717ae318c3e96d0 Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 3 Aug 2025 00:39:12 +0800 Subject: [PATCH 2/3] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=20Bug6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- playground/BugFarm/Bug6/Main.snow | 13 +++++++++++++ playground/BugFarm/Bug6/OS.snow | 11 +++++++++++ 2 files changed, 24 insertions(+) create mode 100644 playground/BugFarm/Bug6/Main.snow create mode 100644 playground/BugFarm/Bug6/OS.snow diff --git a/playground/BugFarm/Bug6/Main.snow b/playground/BugFarm/Bug6/Main.snow new file mode 100644 index 0000000..7a5bdae --- /dev/null +++ b/playground/BugFarm/Bug6/Main.snow @@ -0,0 +1,13 @@ +module: Main + import: os + globals: + declare sum: int = 123 + function: main + parameter: + return_type: int + body: + os.print(sum) + return 0 + end body + end function +end module diff --git a/playground/BugFarm/Bug6/OS.snow b/playground/BugFarm/Bug6/OS.snow new file mode 100644 index 0000000..6026d43 --- /dev/null +++ b/playground/BugFarm/Bug6/OS.snow @@ -0,0 +1,11 @@ +module: os + import: os + function: print + parameter: + declare i1: int + return_type: void + body: + syscall("PRINT",i1) + end body + end function +end module \ No newline at end of file From f876f664149aaef6e2f339fcff80ec1fed268158 Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 3 Aug 2025 00:39:18 +0800 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=20Bug6=20?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .run/Bug6.run.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .run/Bug6.run.xml diff --git a/.run/Bug6.run.xml b/.run/Bug6.run.xml new file mode 100644 index 0000000..b4a248a --- /dev/null +++ b/.run/Bug6.run.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file