2022-03-06 17:29:30 +01:00
|
|
|
package prog8tests.ast
|
|
|
|
|
2022-03-06 20:35:15 +01:00
|
|
|
import io.kotest.assertions.fail
|
2022-03-06 17:29:30 +01:00
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2022-03-06 20:35:15 +01:00
|
|
|
import io.kotest.matchers.ints.shouldBeGreaterThan
|
2022-03-06 17:29:30 +01:00
|
|
|
import io.kotest.matchers.shouldBe
|
2022-03-21 01:01:21 +01:00
|
|
|
import prog8.code.ast.*
|
2022-03-13 12:52:12 +01:00
|
|
|
import prog8.code.core.DataType
|
2022-03-21 01:01:21 +01:00
|
|
|
import prog8.code.core.Position
|
2022-03-11 20:35:25 +01:00
|
|
|
import prog8.code.target.C64Target
|
2022-03-10 21:28:35 +01:00
|
|
|
import prog8.compiler.IntermediateAstMaker
|
2022-03-06 17:29:30 +01:00
|
|
|
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 21:44:50 +01:00
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
|
2022-03-13 12:52:12 +01:00
|
|
|
val ast = IntermediateAstMaker(result.program).transform()
|
2022-03-06 17:29:30 +01:00
|
|
|
ast.name shouldBe result.program.name
|
2022-03-12 23:28:17 +01:00
|
|
|
ast.allBlocks().any() shouldBe true
|
2022-03-06 20:35:15 +01:00
|
|
|
val entry = ast.entrypoint() ?: fail("no main.start() found")
|
2022-03-21 01:01:21 +01:00
|
|
|
entry.children.size shouldBe 4
|
2022-03-06 20:35:15 +01:00
|
|
|
entry.name shouldBe "start"
|
2022-03-08 01:37:13 +01:00
|
|
|
entry.scopedName shouldBe listOf("main", "start")
|
2022-03-06 20:35:15 +01:00
|
|
|
val blocks = ast.allBlocks().toList()
|
|
|
|
blocks.size shouldBeGreaterThan 1
|
|
|
|
blocks[0].name shouldBe "main"
|
2022-03-08 01:37:13 +01:00
|
|
|
blocks[0].scopedName shouldBe listOf("main")
|
2022-03-21 01:01:21 +01:00
|
|
|
|
|
|
|
val vars = entry.children[0] as PtScopeVarsDecls
|
2022-03-23 01:52:01 +01:00
|
|
|
val ccInit = entry.children[1] as PtAssignment
|
|
|
|
ccInit.target.identifier?.targetName shouldBe listOf("main","start","cc")
|
|
|
|
(ccInit.value as PtNumber).number shouldBe 0.0
|
2022-03-21 01:01:21 +01:00
|
|
|
|
|
|
|
val ccdecl = vars.children[0] as PtVariable
|
2022-03-08 01:37:13 +01:00
|
|
|
ccdecl.name shouldBe "cc"
|
|
|
|
ccdecl.scopedName shouldBe listOf("main", "start", "cc")
|
2022-03-13 12:52:12 +01:00
|
|
|
ccdecl.type shouldBe DataType.UBYTE
|
2022-03-21 01:01:21 +01:00
|
|
|
val arraydecl = vars.children[1] as PtVariable
|
2022-03-13 12:52:12 +01:00
|
|
|
arraydecl.name shouldBe "array"
|
|
|
|
arraydecl.type shouldBe DataType.ARRAY_UB
|
2022-03-21 01:01:21 +01:00
|
|
|
|
|
|
|
val containment = (entry.children[2] as PtAssignment).value as PtContainmentCheck
|
|
|
|
(containment.element as PtNumber).number shouldBe 11.0
|
|
|
|
val pipe = (entry.children[3] as PtAssignment).value as PtPipe
|
2022-03-13 12:52:12 +01:00
|
|
|
pipe.void shouldBe false
|
|
|
|
pipe.type shouldBe DataType.UBYTE
|
2022-03-06 17:29:30 +01:00
|
|
|
ast.print()
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|