!54 feat: 禁止全局变量重复声明

Merge pull request !54 from Luke/bugfix/fix-global-redeclare-error
This commit is contained in:
Luke 2025-08-03 15:25:48 +00:00 committed by Gitee
commit d54a2c59b6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 41 additions and 1 deletions

10
.run/Bug6.run.xml Normal file
View File

@ -0,0 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Bug6" type="Application" factoryName="Application" folderName="BugFarm">
<option name="MAIN_CLASS_NAME" value="org.jcnc.snow.cli.SnowCLI" />
<module name="Snow" />
<option name="PROGRAM_PARAMETERS" value="compile run -d playground/BugFarm/Bug6 -o target/Bug6 --debug" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@ -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

View File

@ -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

View File

@ -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()
));
}
}
// 遍历模块中所有函数定义