duplicate label check

This commit is contained in:
Irmen de Jong 2018-08-13 11:34:14 +02:00
parent dcab0d1e98
commit 8974a9cc2d
2 changed files with 25 additions and 1 deletions

View File

@ -48,6 +48,7 @@ data class Position(val file: String, val line: Int, val startCol: Int, val endC
interface IAstProcessor { interface IAstProcessor {
// override the ones you want to act upon
fun process(module: Module) { fun process(module: Module) {
} }
fun process(expr: PrefixExpression): IExpression { fun process(expr: PrefixExpression): IExpression {

View File

@ -1,7 +1,6 @@
package il65.ast package il65.ast
import il65.ParsingFailedError import il65.ParsingFailedError
import javax.xml.crypto.Data
fun Module.checkValid() { fun Module.checkValid() {
@ -50,6 +49,18 @@ class AstChecker : IAstProcessor {
blockNames[block.name] = block.position blockNames[block.name] = block.position
} }
block.statements.forEach { it.process(this) } block.statements.forEach { it.process(this) }
// check if labels are unique
val labels = block.statements.filter { it is Label }.map { it as Label }
val labelnames = mutableMapOf<String, Position?>()
labels.forEach {
val existing = labelnames[it.name]
if(existing!=null) {
checkResult.add(SyntaxError("label name conflict, first defined on line ${existing.line}", it.position))
} else {
labelnames[it.name] = it.position
}
}
return block return block
} }
@ -72,6 +83,18 @@ class AstChecker : IAstProcessor {
subroutine.statements.forEach { it.process(this) } subroutine.statements.forEach { it.process(this) }
// check if labels are unique
val labels = subroutine.statements.filter { it is Label }.map { it as Label }
val labelnames = mutableMapOf<String, Position?>()
labels.forEach {
val existing = labelnames[it.name]
if(existing!=null) {
checkResult.add(SyntaxError("label name conflict, first defined on line ${existing.line}", it.position))
} else {
labelnames[it.name] = it.position
}
}
// subroutine must contain at least one 'return' or 'goto' // subroutine must contain at least one 'return' or 'goto'
// (or if it has an asm block, that must contain a 'rts' or 'jmp') // (or if it has an asm block, that must contain a 'rts' or 'jmp')
if(subroutine.statements.count { it is Return || it is Jump } == 0) { if(subroutine.statements.count { it is Return || it is Jump } == 0) {