mirror of
https://github.com/irmen/prog8.git
synced 2024-11-27 03:50:27 +00:00
added check for duplicate label definitions
This commit is contained in:
parent
6bd99d63b4
commit
a995867deb
@ -158,6 +158,30 @@ interface INameScope {
|
||||
if(!statements.remove(stmt))
|
||||
throw FatalAstException("stmt to remove wasn't found in scope")
|
||||
}
|
||||
|
||||
fun getAllLabels(label: String): List<Label> {
|
||||
val result = mutableListOf<Label>()
|
||||
|
||||
fun find(scope: INameScope) {
|
||||
scope.statements.forEach {
|
||||
when(it) {
|
||||
is Label -> result.add(it)
|
||||
is INameScope -> find(it)
|
||||
is IfStatement -> {
|
||||
find(it.truepart)
|
||||
find(it.elsepart)
|
||||
}
|
||||
is RepeatLoop -> find(it.body)
|
||||
is ForeverLoop -> find(it.body)
|
||||
is WhileLoop -> find(it.body)
|
||||
is WhenStatement -> it.choices.forEach { choice->find(choice.statements) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
find(this)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
interface IAssignable {
|
||||
|
@ -165,9 +165,15 @@ internal class AstIdentifiersChecker(private val program: Program,
|
||||
// the builtin functions can't be redefined
|
||||
errors.err("builtin function cannot be redefined", label.position)
|
||||
} else {
|
||||
val existing = program.namespace.lookup(listOf(label.name), label)
|
||||
if (existing != null && existing !== label)
|
||||
nameError(label.name, label.position, existing)
|
||||
val existing = label.definingSubroutine()?.getAllLabels(label.name) ?: emptyList()
|
||||
for(el in existing) {
|
||||
if(el === label || el.name != label.name)
|
||||
continue
|
||||
else {
|
||||
nameError(label.name, label.position, el)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.visit(label)
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import kotlin.math.floor
|
||||
|
||||
|
||||
/*
|
||||
TODO: remove unreachable code?
|
||||
TODO: remove unreachable code after return and exit()
|
||||
TODO: proper inlining of tiny subroutines (at first, restrict to subs without parameters and variables in them, and build it up from there: correctly renaming/relocating all variables in them and refs to those as well)
|
||||
*/
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user