6502Android/app/src/main/kotlin/android/emu6502/Labels.kt

40 lines
1.1 KiB
Kotlin
Raw Normal View History

2015-06-09 17:21:49 +00:00
package android.emu6502
2015-06-15 05:51:31 +00:00
import android.emu6502.instructions.Symbols
import java.util.*
2017-03-08 05:30:12 +00:00
class Labels(private val assembler: Assembler,
private val symbols: Symbols) : HashMap<String, Int>() {
2015-06-15 05:51:31 +00:00
private val labelIndex: ArrayList<String> = ArrayList()
fun indexLines(lines: List<String>) {
lines.forEach { line ->
indexLine(line)
}
}
2017-03-08 05:30:12 +00:00
override fun get(label: String): Int {
2015-06-15 05:51:31 +00:00
return super.get(label) ?: -1
2015-06-09 17:21:49 +00:00
}
2015-06-15 05:51:31 +00:00
// Extract label if line contains one and calculate position in memory.
// Return false if label already exists.
private fun indexLine(input: String) {
// Figure out how many bytes this instruction takes
2017-03-08 05:30:12 +00:00
val currentPC = assembler.defaultCodePC
2015-06-15 05:51:31 +00:00
// TODO: find a better way for Labels to have access to assembler
assembler.assembleLine(input);
// Find command or label
if (input.matches("^\\w+:".toRegex())) {
2017-03-08 05:30:12 +00:00
val label = input.replace("(^\\w+):.*$".toRegex(), "$1")
2015-06-15 05:51:31 +00:00
if (symbols.get(label) != null) {
throw RuntimeException(
2017-03-08 05:30:12 +00:00
"**Label ${label}is already used as a symbol; please rename one of them**")
2015-06-15 05:51:31 +00:00
}
2017-03-08 05:30:12 +00:00
put(label, currentPC)
2015-06-15 05:51:31 +00:00
}
2015-06-09 17:21:49 +00:00
}
}