prog8/compiler/test/ast/TestIntermediateAst.kt

58 lines
1.9 KiB
Kotlin
Raw Normal View History

package prog8tests.ast
2022-03-06 19:35:15 +00:00
import io.kotest.assertions.fail
import io.kotest.core.spec.style.FunSpec
2022-03-06 19:35:15 +00:00
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
2022-03-12 22:28:17 +00:00
import io.kotest.matchers.shouldNotBe
import prog8.code.ast.PtVariable
2022-03-12 22:28:17 +00:00
import prog8.code.core.CbmPrgLauncherType
import prog8.code.core.CompilationOptions
import prog8.code.core.OutputType
import prog8.code.core.ZeropageType
2022-03-11 19:35:25 +00:00
import prog8.code.target.C64Target
2022-03-10 20:28:35 +00:00
import prog8.compiler.IntermediateAstMaker
import prog8tests.helpers.compileText
class TestIntermediateAst: FunSpec({
test("creation") {
val text="""
%import textio
%import graphics
main {
sub start() {
ubyte cc
ubyte[] array = [1,2,3]
cc = 11 in array
cc = cc |> sin8u() |> cos8u()
}
}
"""
2022-03-07 20:44:50 +00:00
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
2022-03-12 22:28:17 +00:00
val options = CompilationOptions(
OutputType.PRG,
CbmPrgLauncherType.BASIC,
ZeropageType.BASICSAFE,
emptyList(),
floats = true,
noSysInit = false,
compTarget = C64Target()
)
val ast = IntermediateAstMaker(result.program, options).transform()
ast.name shouldBe result.program.name
2022-03-12 22:28:17 +00:00
ast.allBlocks().any() shouldBe true
2022-03-06 19:35:15 +00:00
val entry = ast.entrypoint() ?: fail("no main.start() found")
entry.name shouldBe "start"
2022-03-08 00:37:13 +00:00
entry.scopedName shouldBe listOf("main", "start")
2022-03-06 19:35:15 +00:00
val blocks = ast.allBlocks().toList()
blocks.size shouldBeGreaterThan 1
blocks[0].name shouldBe "main"
2022-03-08 00:37:13 +00:00
blocks[0].scopedName shouldBe listOf("main")
val ccdecl = entry.children[0] as PtVariable
ccdecl.name shouldBe "cc"
ccdecl.scopedName shouldBe listOf("main", "start", "cc")
ast.print()
}
})