fix unittest

This commit is contained in:
Irmen de Jong 2024-01-01 16:11:35 +01:00
parent 0e086d788b
commit d446b57d05
2 changed files with 35 additions and 39 deletions

View File

@ -7,10 +7,7 @@ import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.types.instanceOf import io.kotest.matchers.types.instanceOf
import prog8.ast.IFunctionCall import prog8.ast.IFunctionCall
import prog8.ast.expressions.* import prog8.ast.expressions.*
import prog8.ast.statements.Assignment import prog8.ast.statements.*
import prog8.ast.statements.ForLoop
import prog8.ast.statements.InlineAssembly
import prog8.ast.statements.VarDecl
import prog8.code.core.DataType import prog8.code.core.DataType
import prog8.code.core.Position import prog8.code.core.Position
import prog8.code.target.C64Target import prog8.code.target.C64Target
@ -457,42 +454,38 @@ main {
compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null compileText(VMTarget(), optimize=false, src, writeAssembly=false) shouldNotBe null
} }
test("chained comparison") { test("no chained comparison modifying expression semantics") {
val src=""" val src="""
main { main {
sub start() { sub start() {
ubyte @shared x = 1 ubyte @shared n=20
ubyte @shared y = 2 ubyte @shared x=10
ubyte @shared z = 3
x = x==y==z if n < x {
y = 4<x<10 ; nothing here, conditional gets inverted
} else {
cx16.r0++
}
cx16.r0L = n<x == 0
cx16.r1L = not n<x
} }
}""" }"""
val result=compileText(VMTarget(), optimize=false, src, writeAssembly=false)!! val result=compileText(VMTarget(), optimize=true, src, writeAssembly=false)!!
val st = result.compilerAst.entrypoint.statements val st = result.compilerAst.entrypoint.statements
st.size shouldBe 8 st.size shouldBe 7
val comparison1 = (st[6] as Assignment).value as BinaryExpression val ifCond = (st[4] as IfElse).condition as BinaryExpression
comparison1.operator shouldBe "and" ifCond.operator shouldBe "=="
val left1 = comparison1.left as BinaryExpression (ifCond.right as NumericLiteral).number shouldBe 0.0
val right1 = comparison1.right as BinaryExpression (ifCond.left as BinaryExpression).operator shouldBe "<"
left1.operator shouldBe "==" val assign1 = (st[5] as Assignment).value as BinaryExpression
right1.operator shouldBe "==" val assign2 = (st[6] as Assignment).value as BinaryExpression
(left1.left as? IdentifierReference)?.nameInSource shouldBe listOf("x") assign1.operator shouldBe "=="
(left1.right as? IdentifierReference)?.nameInSource shouldBe listOf("y") (assign1.right as NumericLiteral).number shouldBe 0.0
(right1.left as? IdentifierReference)?.nameInSource shouldBe listOf("y") (assign1.left as BinaryExpression).operator shouldBe "<"
(right1.right as? IdentifierReference)?.nameInSource shouldBe listOf("z") assign2.operator shouldBe "=="
(assign2.right as NumericLiteral).number shouldBe 0.0
val comparison2 = (st[7] as Assignment).value as BinaryExpression (assign2.left as BinaryExpression).operator shouldBe "<"
comparison2.operator shouldBe "and"
val left2 = comparison2.left as BinaryExpression
val right2 = comparison2.right as BinaryExpression
left2.operator shouldBe ">"
right2.operator shouldBe "<"
(left2.left as? IdentifierReference)?.nameInSource shouldBe listOf("x")
(left2.right as? NumericLiteral)?.number shouldBe 4.0
(right2.left as? IdentifierReference)?.nameInSource shouldBe listOf("x")
(right2.right as? NumericLiteral)?.number shouldBe 10.0
} }
test("modulo is not directive") { test("modulo is not directive") {

View File

@ -3,12 +3,15 @@
main { main {
sub start() { sub start() {
ubyte[10] array1 ubyte @shared n=20
ubyte[10] array2 ubyte @shared x=10
ubyte @shared xx
cx16.r0 = (cx16.r1+cx16.r2) / (cx16.r2+cx16.r1) if n < x {
cx16.r1 = 4*(cx16.r1+cx16.r2) + 3*(cx16.r1+cx16.r2) ; nothing here, conditional gets inverted
cx16.r2 = array1[xx+20]==10 or array2[xx+20]==20 or array1[xx+20]==30 or array2[xx+20]==40 } else {
cx16.r0++
}
cx16.r0L = n<x == 0
cx16.r1L = not n<x
} }
} }