prog8/compiler/test/ast/TestIntermediateAst.kt

77 lines
2.8 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-21 00:01:21 +00:00
import prog8.code.ast.*
2022-12-30 16:41:38 +00:00
import prog8.code.core.*
2022-03-11 19:35:25 +00:00
import prog8.code.target.C64Target
2022-08-14 11:06:11 +00:00
import prog8.compiler.astprocessing.IntermediateAstMaker
2022-12-30 16:41:38 +00:00
import prog8.compiler.astprocessing.SymbolTableMaker
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 = sqrt16(lsb(cc))
}
}
"""
2022-12-30 16:41:38 +00:00
val target = C64Target()
val options = CompilationOptions(
OutputType.RAW,
CbmPrgLauncherType.NONE,
ZeropageType.DONTUSE,
emptyList(),
floats = false,
noSysInit = true,
compTarget = target,
loadAddress = target.machine.PROGRAM_LOAD_ADDRESS
)
val result = compileText(target, false, text, writeAssembly = false)!!
val st = SymbolTableMaker(result.program, options).make()
2022-12-30 16:41:38 +00:00
val ast = IntermediateAstMaker(result.program, st, 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")
2022-03-21 00:01:21 +00:00
entry.children.size shouldBe 4
2022-03-06 19:35:15 +00:00
entry.name shouldBe "start"
2022-12-30 17:07:53 +00:00
entry.scopedName shouldBe "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-12-30 17:07:53 +00:00
blocks[0].scopedName shouldBe "main"
2022-03-21 00:01:21 +00:00
val vars = entry.children[0] as PtScopeVarsDecls
2022-03-23 00:52:01 +00:00
val ccInit = entry.children[1] as PtAssignment
2022-12-30 17:07:53 +00:00
ccInit.target.identifier?.name shouldBe "main.start.cc"
2022-03-23 00:52:01 +00:00
(ccInit.value as PtNumber).number shouldBe 0.0
2022-03-21 00:01:21 +00:00
val ccdecl = vars.children[0] as PtVariable
2022-03-08 00:37:13 +00:00
ccdecl.name shouldBe "cc"
2022-12-30 17:07:53 +00:00
ccdecl.scopedName shouldBe "main.start.cc"
2022-03-13 11:52:12 +00:00
ccdecl.type shouldBe DataType.UBYTE
2022-03-21 00:01:21 +00:00
val arraydecl = vars.children[1] as PtVariable
2022-03-13 11:52:12 +00:00
arraydecl.name shouldBe "array"
arraydecl.type shouldBe DataType.ARRAY_UB
2022-03-21 00:01:21 +00:00
2022-07-11 10:22:14 +00:00
val containmentCast = (entry.children[2] as PtAssignment).value as PtTypeCast
containmentCast.type shouldBe DataType.UBYTE
val containment = containmentCast.value as PtContainmentCheck
2022-03-21 00:01:21 +00:00
(containment.element as PtNumber).number shouldBe 11.0
val fcall = (entry.children[3] as PtAssignment).value as PtFunctionCall
fcall.void shouldBe false
fcall.type shouldBe DataType.UBYTE
ast.print()
}
})