!54 feat: 禁止全局变量重复声明
Merge pull request !54 from Luke/bugfix/fix-global-redeclare-error
This commit is contained in:
commit
d54a2c59b6
10
.run/Bug6.run.xml
Normal file
10
.run/Bug6.run.xml
Normal 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>
|
||||||
13
playground/BugFarm/Bug6/Main.snow
Normal file
13
playground/BugFarm/Bug6/Main.snow
Normal 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
|
||||||
11
playground/BugFarm/Bug6/OS.snow
Normal file
11
playground/BugFarm/Bug6/OS.snow
Normal 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
|
||||||
@ -59,7 +59,13 @@ public record FunctionChecker(Context ctx) {
|
|||||||
SymbolTable globalScope = new SymbolTable(null);
|
SymbolTable globalScope = new SymbolTable(null);
|
||||||
for (DeclarationNode g : mod.globals()) {
|
for (DeclarationNode g : mod.globals()) {
|
||||||
var t = ctx.parseType(g.getType());
|
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()
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 遍历模块中所有函数定义
|
// 遍历模块中所有函数定义
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user