Skip to content

Commit

Permalink
fix name redefinition check for multi-declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
irmen committed Sep 20, 2024
1 parent 585f6ff commit e29ff1c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
21 changes: 15 additions & 6 deletions compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions compiler/test/ast/TestAstChecks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
})
22 changes: 5 additions & 17 deletions examples/test.p8
Original file line number Diff line number Diff line change
@@ -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++
}
}

0 comments on commit e29ff1c

Please sign in to comment.