2022-03-06 16:29:30 +00:00
|
|
|
package prog8tests.ast
|
|
|
|
|
2022-03-06 19:35:15 +00:00
|
|
|
import io.kotest.assertions.fail
|
2022-03-06 16:29:30 +00:00
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2022-03-06 19:35:15 +00:00
|
|
|
import io.kotest.matchers.ints.shouldBeGreaterThan
|
2022-03-06 16:29:30 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
2024-02-04 22:41:01 +00:00
|
|
|
import io.kotest.matchers.types.instanceOf
|
2022-03-21 00:01:21 +00:00
|
|
|
import prog8.code.ast.*
|
2023-08-13 22:50:40 +00:00
|
|
|
import prog8.code.core.DataType
|
2024-01-01 13:55:29 +00:00
|
|
|
import prog8.code.core.Position
|
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
|
2023-10-03 20:54:28 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
2022-03-06 16:29:30 +00:00
|
|
|
import prog8tests.helpers.compileText
|
|
|
|
|
|
|
|
class TestIntermediateAst: FunSpec({
|
|
|
|
|
|
|
|
test("creation") {
|
|
|
|
val text="""
|
|
|
|
%import textio
|
|
|
|
%import graphics
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-04 22:41:01 +00:00
|
|
|
bool cc
|
|
|
|
ubyte dd
|
2022-03-06 16:29:30 +00:00
|
|
|
ubyte[] array = [1,2,3]
|
|
|
|
cc = 11 in array
|
2024-02-04 22:41:01 +00:00
|
|
|
dd = sqrt(lsb(dd))
|
2022-03-06 16:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-12-30 16:41:38 +00:00
|
|
|
val target = C64Target()
|
2023-10-03 20:54:28 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
2022-12-30 16:41:38 +00:00
|
|
|
val result = compileText(target, false, text, writeAssembly = false)!!
|
2023-10-03 20:54:28 +00:00
|
|
|
val ast = IntermediateAstMaker(result.compilerAst, errors).transform()
|
2023-02-09 00:46:23 +00:00
|
|
|
ast.name shouldBe result.compilerAst.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")
|
2024-02-04 22:41:01 +00:00
|
|
|
entry.children.size shouldBe 7
|
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
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
val ccInit = entry.children[3] as PtAssignment
|
2022-12-30 17:07:53 +00:00
|
|
|
ccInit.target.identifier?.name shouldBe "main.start.cc"
|
2024-02-04 22:41:01 +00:00
|
|
|
(ccInit.value as PtBool).value shouldBe false
|
|
|
|
val ddInit = entry.children[4] as PtAssignment
|
|
|
|
ddInit.target.identifier?.name shouldBe "main.start.dd"
|
|
|
|
(ddInit.value as PtNumber).number shouldBe 0.0
|
2022-03-21 00:01:21 +00:00
|
|
|
|
2023-01-29 12:25:15 +00:00
|
|
|
val ccdecl = entry.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"
|
2024-02-04 22:41:01 +00:00
|
|
|
ccdecl.type shouldBe DataType.BOOL
|
|
|
|
val dddecl = entry.children[1] as PtVariable
|
|
|
|
dddecl.name shouldBe "dd"
|
|
|
|
dddecl.scopedName shouldBe "main.start.dd"
|
|
|
|
dddecl.type shouldBe DataType.UBYTE
|
|
|
|
|
|
|
|
val arraydecl = entry.children[2] as IPtVariable
|
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
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
val ccAssignV = (entry.children[5] as PtAssignment).value
|
|
|
|
ccAssignV shouldBe instanceOf<PtContainmentCheck>()
|
|
|
|
val ddAssignV = (entry.children[6] as PtAssignment).value
|
|
|
|
ddAssignV shouldBe instanceOf<PtFunctionCall>()
|
2022-03-06 16:29:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 13:55:29 +00:00
|
|
|
test("isSame on binaryExpressions") {
|
|
|
|
val expr1 = PtBinaryExpression("/", DataType.UBYTE, Position.DUMMY)
|
|
|
|
expr1.add(PtNumber(DataType.UBYTE, 1.0, Position.DUMMY))
|
|
|
|
expr1.add(PtNumber(DataType.UBYTE, 2.0, Position.DUMMY))
|
|
|
|
val expr2 = PtBinaryExpression("/", DataType.UBYTE, Position.DUMMY)
|
|
|
|
expr2.add(PtNumber(DataType.UBYTE, 1.0, Position.DUMMY))
|
|
|
|
expr2.add(PtNumber(DataType.UBYTE, 2.0, Position.DUMMY))
|
|
|
|
(expr1 isSameAs expr2) shouldBe true
|
|
|
|
val expr3 = PtBinaryExpression("/", DataType.UBYTE, Position.DUMMY)
|
|
|
|
expr3.add(PtNumber(DataType.UBYTE, 2.0, Position.DUMMY))
|
|
|
|
expr3.add(PtNumber(DataType.UBYTE, 1.0, Position.DUMMY))
|
|
|
|
(expr1 isSameAs expr3) shouldBe false
|
|
|
|
}
|
|
|
|
|
|
|
|
test("isSame on binaryExpressions with associative operators") {
|
|
|
|
val expr1 = PtBinaryExpression("+", DataType.UBYTE, Position.DUMMY)
|
|
|
|
expr1.add(PtNumber(DataType.UBYTE, 1.0, Position.DUMMY))
|
|
|
|
expr1.add(PtNumber(DataType.UBYTE, 2.0, Position.DUMMY))
|
|
|
|
val expr2 = PtBinaryExpression("+", DataType.UBYTE, Position.DUMMY)
|
|
|
|
expr2.add(PtNumber(DataType.UBYTE, 1.0, Position.DUMMY))
|
|
|
|
expr2.add(PtNumber(DataType.UBYTE, 2.0, Position.DUMMY))
|
|
|
|
(expr1 isSameAs expr2) shouldBe true
|
|
|
|
val expr3 = PtBinaryExpression("+", DataType.UBYTE, Position.DUMMY)
|
|
|
|
expr3.add(PtNumber(DataType.UBYTE, 2.0, Position.DUMMY))
|
|
|
|
expr3.add(PtNumber(DataType.UBYTE, 1.0, Position.DUMMY))
|
|
|
|
(expr1 isSameAs expr3) shouldBe true
|
|
|
|
}
|
2022-03-06 16:29:30 +00:00
|
|
|
})
|