fix name redefinition check for multi-declarations

This commit is contained in:
Irmen de Jong 2024-09-20 19:55:32 +02:00
parent 585f6ffc9b
commit e29ff1c848
3 changed files with 64 additions and 23 deletions

View File

@ -48,22 +48,30 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter,
if(decl.name in BuiltinFunctions)
errors.err("builtin function cannot be redefined", decl.position)
if(decl.names.size<2) {
val existingInSameScope = decl.definingScope.lookup(listOf(decl.name))
fun checkNameForErrors(name: String) {
val existingInSameScope = decl.definingScope.lookup(listOf(name))
if (existingInSameScope != null && existingInSameScope !== decl)
nameError(decl.name, decl.position, existingInSameScope)
nameError(name, decl.position, existingInSameScope)
val existingOuter = decl.parent.definingScope.lookup(listOf(decl.name))
val existingOuter = decl.parent.definingScope.lookup(listOf(name))
if (existingOuter != null && existingOuter !== decl) {
if (existingOuter is VarDecl) {
if (existingOuter.parent !== decl.parent)
nameShadowWarning(decl.name, decl.position, existingOuter)
nameShadowWarning(name, decl.position, existingOuter)
else
nameError(decl.name, decl.position, existingOuter)
nameError(name, decl.position, existingOuter)
}
}
}
if(decl.names.size<2) {
checkNameForErrors(decl.name)
}
else {
for (name in decl.names)
checkNameForErrors(name)
}
if(decl.definingBlock.name==decl.name)
nameError(decl.name, decl.position, decl.definingBlock)
if(decl.definingSubroutine?.name==decl.name)
@ -72,6 +80,7 @@ internal class AstIdentifiersChecker(private val errors: IErrorReporter,
super.visit(decl)
}
override fun visit(subroutine: Subroutine) {
if(subroutine.name in BuiltinFunctions) {
// the builtin functions can't be redefined

View File

@ -193,4 +193,48 @@ main {
errors.errors.size shouldBe 1
errors.errors[0] shouldContain "statement"
}
test("redefined variable name in single declaration is reported") {
val src="""
main {
sub start() {
const ubyte count=11
cx16.r0++
ubyte count = 88 ; redefinition
cx16.r0 = count
}
}"""
val errors=ErrorReporterForTests()
compileText(C64Target(), false, src, writeAssembly = false, errors=errors) shouldBe null
errors.errors.size shouldBe 1
errors.errors[0] shouldContain "name conflict"
errors.clear()
compileText(C64Target(), true, src, writeAssembly = false, errors=errors) shouldBe null
errors.errors.size shouldBe 1
errors.errors[0] shouldContain "name conflict"
}
test("redefined variable name in multi declaration is reported") {
val src="""
main {
sub start() {
ubyte i
i++
ubyte i, j ; redefinition
i++
j++
}
}
"""
val errors=ErrorReporterForTests()
compileText(C64Target(), false, src, writeAssembly = false, errors=errors) shouldBe null
errors.errors.size shouldBe 1
errors.errors[0] shouldContain "name conflict"
errors.clear()
compileText(C64Target(), true, src, writeAssembly = false, errors=errors) shouldBe null
errors.errors.size shouldBe 1
errors.errors[0] shouldContain "name conflict"
}
})

View File

@ -1,22 +1,10 @@
%import floats
%import textio
%zeropage basicsafe
main {
sub start() {
cbm.SETTIM(0,0,0)
float xx = 1.234
floats.print(xx)
txt.nl()
xx= floats.time()
floats.print(xx)
txt.nl()
floats.print(floats.time())
txt.nl()
ubyte i
i++
txt.print("waiting 333 jiffies... ")
sys.wait(333)
floats.print(floats.time())
txt.nl()
ubyte i, j ; redefinition
i++
j++
}
}