searching names in inlined assembly now ignores source comments

This commit is contained in:
Irmen de Jong 2022-02-13 13:41:12 +01:00
parent a3a6812608
commit af0e7f7187
4 changed files with 39 additions and 9 deletions

View File

@ -1 +1 @@
7.8
7.9-dev

View File

@ -68,11 +68,6 @@ class TestCompilerOnCharLit: FunSpec({
decl.type shouldBe VarDeclType.VAR
decl.datatype shouldBe DataType.UBYTE
// TODO: assertIs<CharLiteral>(decl.value,
// "char literals should be kept until code gen")
// val initializerValue = decl.value as CharLiteral
// assertEquals('\n', (initializerValue as CharLiteral).value)
withClue("initializer value should have been moved to separate assignment"){
decl.value shouldBe null
}

View File

@ -0,0 +1,29 @@
package prog8tests.ast
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import prog8.ast.base.Position
import prog8.ast.statements.InlineAssembly
class TestVarious: FunSpec({
test("symbol names in inline assembly blocks") {
val names1 = InlineAssembly("""
""", Position.DUMMY).names
names1 shouldBe emptySet()
val names2 = InlineAssembly("""
label: lda #<value
sta ${'$'}ea
sta 123
label2:
sta othervalue ; but not these in the comments
; also not these
;; ...or these
// valid words 123456
""", Position.DUMMY).names
names2 shouldBe setOf("label", "lda", "sta", "ea", "value", "label2", "othervalue", "valid", "words")
}
})

View File

@ -602,12 +602,18 @@ class InlineAssembly(val assembly: String, override val position: Position) : St
override fun accept(visitor: IAstVisitor) = visitor.visit(this)
override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent)
val names: Set<String> by lazy {
// A cache of all the words (identifiers) present in this block of assembly code
// this is used when checking if prog8 names are referenced from assembly code
// TODO: smarter pattern; don't include words in comments
val wordPattern = Regex("""\b([_a-zA-Z][_a-zA-Z0-9]+?)\b""", RegexOption.MULTILINE)
wordPattern.findAll(assembly).map { it.value }.toSet()
val wordPattern = Regex("""\b([_a-zA-Z][_a-zA-Z0-9]+?)\b""")
assembly.splitToSequence('\n')
.map {
val everythintBeforeComment = it.substringBefore(';')
wordPattern.findAll(everythintBeforeComment)
}
.flatMap { it.map { it.value } }
.toSet()
}
}