2024-09-08 14:03:57 +00:00
|
|
|
package prog8tests.compiler
|
2022-01-07 21:12:13 +00:00
|
|
|
|
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2022-06-30 22:38:19 +00:00
|
|
|
import io.kotest.matchers.ints.shouldBeGreaterThan
|
2022-01-07 21:12:13 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
2022-03-07 20:41:12 +00:00
|
|
|
import io.kotest.matchers.shouldNotBe
|
2022-01-07 21:12:13 +00:00
|
|
|
import io.kotest.matchers.string.shouldContain
|
2022-07-02 21:55:32 +00:00
|
|
|
import io.kotest.matchers.types.instanceOf
|
|
|
|
import prog8.ast.IFunctionCall
|
2022-07-07 20:09:04 +00:00
|
|
|
import prog8.ast.expressions.*
|
2024-02-04 22:41:01 +00:00
|
|
|
import prog8.ast.printProgram
|
2022-07-07 20:09:04 +00:00
|
|
|
import prog8.ast.statements.Assignment
|
|
|
|
import prog8.ast.statements.IfElse
|
2024-03-10 22:45:44 +00:00
|
|
|
import prog8.code.ast.PtAsmSub
|
|
|
|
import prog8.code.ast.PtSub
|
2022-07-07 20:09:04 +00:00
|
|
|
import prog8.code.core.DataType
|
|
|
|
import prog8.code.core.Position
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.code.target.C64Target
|
2022-12-04 11:22:05 +00:00
|
|
|
import prog8.code.target.VMTarget
|
2022-01-07 21:12:13 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
|
|
|
import prog8tests.helpers.compileText
|
|
|
|
|
|
|
|
|
|
|
|
class TestTypecasts: FunSpec({
|
2022-07-12 15:14:33 +00:00
|
|
|
|
|
|
|
test("integer args for builtin funcs") {
|
|
|
|
val text="""
|
|
|
|
%import floats
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
float fl
|
2023-12-29 02:12:44 +00:00
|
|
|
floats.print(lsb(fl))
|
2022-07-12 15:14:33 +00:00
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2022-09-25 15:14:44 +00:00
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = false, errors=errors)
|
2022-07-12 15:14:33 +00:00
|
|
|
result shouldBe null
|
|
|
|
errors.errors.size shouldBe 1
|
2024-11-15 01:37:49 +00:00
|
|
|
errors.errors[0] shouldContain "type mismatch"
|
2022-07-12 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("not casting bool operands to logical operators") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
bool @shared bb2=true
|
2024-02-04 22:41:01 +00:00
|
|
|
bool @shared bb3=false
|
|
|
|
bool @shared bb = bb2 and bb3
|
2022-07-12 15:14:33 +00:00
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2024-02-04 22:41:01 +00:00
|
|
|
stmts.size shouldBe 6
|
|
|
|
val expr = (stmts[5] as Assignment).value as BinaryExpression
|
2022-07-12 15:14:33 +00:00
|
|
|
expr.operator shouldBe "and"
|
|
|
|
(expr.left as IdentifierReference).nameInSource shouldBe listOf("bb2") // no cast
|
2024-02-04 22:41:01 +00:00
|
|
|
(expr.right as IdentifierReference).nameInSource shouldBe listOf("bb3") // no cast
|
2022-07-12 15:14:33 +00:00
|
|
|
|
|
|
|
val result2 = compileText(C64Target(), true, text, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts2 = result2.compilerAst.entrypoint.statements
|
2024-02-04 22:41:01 +00:00
|
|
|
stmts2.size shouldBe 7
|
|
|
|
val expr2 = (stmts2[5] as Assignment).value as BinaryExpression
|
2023-11-15 22:10:20 +00:00
|
|
|
expr2.operator shouldBe "and"
|
2024-02-04 22:41:01 +00:00
|
|
|
(expr2.left as IdentifierReference).nameInSource shouldBe listOf("bb2") // no cast
|
|
|
|
(expr2.right as IdentifierReference).nameInSource shouldBe listOf("bb3") // no cast
|
2022-07-12 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 10:22:14 +00:00
|
|
|
test("bool expressions with functioncalls") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub ftrue(ubyte arg) -> ubyte {
|
|
|
|
arg++
|
2024-01-04 14:02:21 +00:00
|
|
|
return 42
|
|
|
|
}
|
|
|
|
|
|
|
|
sub btrue(ubyte arg) -> bool {
|
|
|
|
arg++
|
|
|
|
return true
|
2022-07-11 10:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
2023-12-28 04:17:15 +00:00
|
|
|
bool @shared ub1 = true
|
|
|
|
bool @shared ub2 = true
|
|
|
|
bool @shared ub3 = true
|
2022-07-11 10:22:14 +00:00
|
|
|
bool @shared bvalue
|
|
|
|
|
|
|
|
bvalue = ub1 xor ub2 xor ub3 xor true
|
2024-02-04 22:41:01 +00:00
|
|
|
bvalue = ub1 xor ub2 xor ub3 xor ftrue(99)!=0
|
|
|
|
bvalue = ub1 and ub2 and ftrue(99)!=0
|
2024-01-04 14:02:21 +00:00
|
|
|
bvalue = ub1 xor ub2 xor ub3 xor btrue(99)
|
|
|
|
bvalue = ub1 and ub2 and btrue(99)
|
2022-07-11 10:22:14 +00:00
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), true, text, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2024-02-04 22:41:01 +00:00
|
|
|
printProgram(result.compilerAst)
|
2022-07-11 10:22:14 +00:00
|
|
|
/*
|
2024-02-04 22:41:01 +00:00
|
|
|
bool @shared ub1
|
|
|
|
ub1 = true
|
|
|
|
bool @shared ub2
|
|
|
|
ub2 = true
|
|
|
|
bool @shared ub3
|
|
|
|
ub3 = true
|
|
|
|
bool @shared bvalue
|
|
|
|
bvalue = not ((ub1 xor ub2) xor ub3)
|
2024-01-04 14:02:21 +00:00
|
|
|
bvalue = (((ub1 xor ub2) xor ub3) xor (ftrue(99)!=0))
|
2023-11-15 22:10:20 +00:00
|
|
|
bvalue = ((ub1 and ub2) and (ftrue(99)!=0))
|
2024-01-04 14:02:21 +00:00
|
|
|
bvalue = (((ub1 xor ub2) xor ub3) xor btrue(99))
|
|
|
|
bvalue = ((ub1 and ub2) and btrue(99))
|
2022-07-11 10:22:14 +00:00
|
|
|
return
|
|
|
|
*/
|
2024-01-04 14:02:21 +00:00
|
|
|
stmts.size shouldBe 13
|
2024-02-04 22:41:01 +00:00
|
|
|
val assignValue1 = (stmts[7] as Assignment).value as PrefixExpression
|
2023-07-19 23:36:43 +00:00
|
|
|
val assignValue2 = (stmts[8] as Assignment).value as BinaryExpression
|
|
|
|
val assignValue3 = (stmts[9] as Assignment).value as BinaryExpression
|
2024-01-04 14:02:21 +00:00
|
|
|
val assignValue4 = (stmts[10] as Assignment).value as BinaryExpression
|
|
|
|
val assignValue5 = (stmts[11] as Assignment).value as BinaryExpression
|
2024-02-04 22:41:01 +00:00
|
|
|
assignValue1.operator shouldBe "not"
|
2023-11-15 22:10:20 +00:00
|
|
|
assignValue2.operator shouldBe "xor"
|
|
|
|
assignValue3.operator shouldBe "and"
|
2024-01-04 14:02:21 +00:00
|
|
|
assignValue4.operator shouldBe "xor"
|
|
|
|
assignValue5.operator shouldBe "and"
|
2022-07-11 10:22:14 +00:00
|
|
|
val right2 = assignValue2.right as BinaryExpression
|
|
|
|
val right3 = assignValue3.right as BinaryExpression
|
|
|
|
right2.operator shouldBe "!="
|
|
|
|
right3.operator shouldBe "!="
|
|
|
|
right2.left shouldBe instanceOf<IFunctionCall>()
|
|
|
|
right2.right shouldBe NumericLiteral(DataType.UBYTE, 0.0, Position.DUMMY)
|
|
|
|
right3.left shouldBe instanceOf<IFunctionCall>()
|
|
|
|
right3.right shouldBe NumericLiteral(DataType.UBYTE, 0.0, Position.DUMMY)
|
2024-01-04 14:02:21 +00:00
|
|
|
assignValue4.right shouldBe instanceOf<IFunctionCall>()
|
|
|
|
assignValue5.right shouldBe instanceOf<IFunctionCall>()
|
2022-07-11 10:22:14 +00:00
|
|
|
}
|
2022-01-07 21:12:13 +00:00
|
|
|
|
2022-07-07 20:09:04 +00:00
|
|
|
test("correct handling of bool parameters") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
|
|
|
|
sub thing(bool b1, bool b2) -> bool {
|
|
|
|
return (b1 and b2) or b1
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
bool boolvalue1 = true
|
|
|
|
bool boolvalue2 = false
|
|
|
|
|
|
|
|
boolvalue1 = thing(true, false)
|
2024-02-04 22:41:01 +00:00
|
|
|
boolvalue2 = thing(false, true)
|
2022-07-07 20:09:04 +00:00
|
|
|
|
|
|
|
if boolvalue1 and boolvalue2
|
|
|
|
boolvalue1=false
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2024-02-04 22:41:01 +00:00
|
|
|
stmts.size shouldBe 7
|
|
|
|
val fcall1 = ((stmts[4] as Assignment).value as IFunctionCall)
|
|
|
|
fcall1.args[0] shouldBe NumericLiteral(DataType.BOOL, 1.0, Position.DUMMY)
|
|
|
|
fcall1.args[1] shouldBe NumericLiteral(DataType.BOOL, 0.0, Position.DUMMY)
|
|
|
|
val fcall2 = ((stmts[5] as Assignment).value as IFunctionCall)
|
|
|
|
fcall2.args[0] shouldBe NumericLiteral(DataType.BOOL, 0.0, Position.DUMMY)
|
|
|
|
fcall2.args[1] shouldBe NumericLiteral(DataType.BOOL, 1.0, Position.DUMMY)
|
|
|
|
val ifCond = (stmts[6] as IfElse).condition as BinaryExpression
|
2022-07-11 10:22:14 +00:00
|
|
|
ifCond.operator shouldBe "and" // no asm writing so logical expressions haven't been replaced with bitwise equivalents yet
|
2022-07-07 20:09:04 +00:00
|
|
|
(ifCond.left as IdentifierReference).nameInSource shouldBe listOf("boolvalue1")
|
|
|
|
(ifCond.right as IdentifierReference).nameInSource shouldBe listOf("boolvalue2")
|
|
|
|
}
|
|
|
|
|
2022-07-05 22:49:57 +00:00
|
|
|
test("correct evaluation of words in boolean expressions") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword camg
|
2024-02-04 22:41:01 +00:00
|
|
|
bool @shared interlaced
|
2022-07-05 22:49:57 +00:00
|
|
|
interlaced = (camg & ${'$'}0004) != 0
|
2024-02-04 22:41:01 +00:00
|
|
|
cx16.r0L++
|
2022-07-05 22:49:57 +00:00
|
|
|
interlaced = (${'$'}0004 & camg) != 0
|
2024-02-04 22:41:01 +00:00
|
|
|
cx16.r0L++
|
2022-07-05 22:49:57 +00:00
|
|
|
uword @shared ww
|
|
|
|
ww = (camg & ${'$'}0004)
|
|
|
|
ww++
|
|
|
|
ww = (${'$'}0004 & camg)
|
2024-02-04 22:41:01 +00:00
|
|
|
ubyte @shared value
|
|
|
|
bool @shared collected = (value >= ${'$'}33) or (value >= ${'$'}66) or (value >= ${'$'}99) or (value >= ${'$'}CC)
|
2022-07-05 22:49:57 +00:00
|
|
|
}
|
|
|
|
}"""
|
2022-09-25 15:14:44 +00:00
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-09-25 15:14:44 +00:00
|
|
|
stmts.size shouldBeGreaterThan 10
|
2022-07-05 22:49:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 22:28:43 +00:00
|
|
|
test("word to byte casts") {
|
|
|
|
val text="""
|
|
|
|
%import textio
|
|
|
|
main {
|
|
|
|
sub func(ubyte arg) -> word {
|
|
|
|
return arg-99
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
txt.print_ub(func(0) as ubyte)
|
|
|
|
txt.print_uw(func(0) as ubyte)
|
|
|
|
txt.print_w(func(0) as ubyte)
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-07-05 22:28:43 +00:00
|
|
|
stmts.size shouldBe 3
|
|
|
|
}
|
|
|
|
|
2024-01-06 23:01:00 +00:00
|
|
|
test("ubyte to word casts") {
|
2024-06-03 23:00:46 +00:00
|
|
|
val src="""
|
2024-01-06 23:01:00 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte @shared bb = 255
|
|
|
|
cx16.r0s = (bb as byte) as word ; should result in -1 word value
|
|
|
|
cx16.r1s = (bb as word) ; should result in 255 word value
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
val result = compileText(C64Target(), true, src, writeAssembly = false)!!
|
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
|
|
|
stmts.size shouldBe 4
|
|
|
|
val assign1tc = (stmts[2] as Assignment).value as TypecastExpression
|
|
|
|
val assign2tc = (stmts[3] as Assignment).value as TypecastExpression
|
|
|
|
assign1tc.type shouldBe DataType.WORD
|
|
|
|
assign2tc.type shouldBe DataType.WORD
|
|
|
|
assign2tc.expression shouldBe instanceOf<IdentifierReference>()
|
|
|
|
val assign1subtc = (assign1tc.expression as TypecastExpression)
|
|
|
|
assign1subtc.type shouldBe DataType.BYTE
|
|
|
|
assign1subtc.expression shouldBe instanceOf<IdentifierReference>()
|
|
|
|
}
|
|
|
|
|
2022-07-02 21:55:32 +00:00
|
|
|
test("add missing & to function arguments") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
|
|
|
|
sub handler(uword fptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
uword variable
|
|
|
|
|
2023-12-26 13:47:31 +00:00
|
|
|
sys.pushw(variable)
|
|
|
|
sys.pushw(handler)
|
|
|
|
sys.pushw(&handler)
|
2022-07-02 21:55:32 +00:00
|
|
|
handler(variable)
|
|
|
|
handler(handler)
|
|
|
|
handler(&handler)
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = false)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val stmts = result.compilerAst.entrypoint.statements
|
2022-07-02 21:55:32 +00:00
|
|
|
stmts.size shouldBe 8
|
|
|
|
val arg1 = (stmts[2] as IFunctionCall).args.single()
|
|
|
|
val arg2 = (stmts[3] as IFunctionCall).args.single()
|
|
|
|
val arg3 = (stmts[4] as IFunctionCall).args.single()
|
|
|
|
val arg4 = (stmts[5] as IFunctionCall).args.single()
|
|
|
|
val arg5 = (stmts[6] as IFunctionCall).args.single()
|
|
|
|
val arg6 = (stmts[7] as IFunctionCall).args.single()
|
|
|
|
arg1 shouldBe instanceOf<IdentifierReference>()
|
|
|
|
arg2 shouldBe instanceOf<AddressOf>()
|
|
|
|
arg3 shouldBe instanceOf<AddressOf>()
|
|
|
|
arg4 shouldBe instanceOf<IdentifierReference>()
|
|
|
|
arg5 shouldBe instanceOf<AddressOf>()
|
|
|
|
arg6 shouldBe instanceOf<AddressOf>()
|
|
|
|
}
|
|
|
|
|
2022-01-07 21:12:13 +00:00
|
|
|
test("correct typecasts") {
|
|
|
|
val text = """
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
float @shared fl = 3.456
|
|
|
|
uword @shared uw = 5555
|
|
|
|
byte @shared bb = -44
|
|
|
|
|
|
|
|
bb = uw as byte
|
|
|
|
uw = bb as uword
|
|
|
|
fl = uw as float
|
|
|
|
fl = bb as float
|
|
|
|
bb = fl as byte
|
|
|
|
uw = fl as uword
|
2022-07-13 20:24:31 +00:00
|
|
|
uw = 8888 + (bb as ubyte)
|
2022-01-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)!!
|
2023-10-17 00:34:56 +00:00
|
|
|
result.compilerAst.entrypoint.statements.size shouldBe 14
|
2022-01-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("invalid typecasts of numbers") {
|
|
|
|
val text = """
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
ubyte @shared bb
|
|
|
|
|
|
|
|
bb = 5555 as ubyte
|
|
|
|
routine(5555 as ubyte)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub routine(ubyte bb) {
|
|
|
|
bb++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), false, text, writeAssembly = true, errors=errors) shouldBe null
|
2022-01-07 21:12:13 +00:00
|
|
|
errors.errors.size shouldBe 2
|
2023-12-15 21:05:57 +00:00
|
|
|
errors.errors[0] shouldContain "no cast"
|
|
|
|
errors.errors[1] shouldContain "no cast"
|
2022-01-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 21:05:57 +00:00
|
|
|
test("refuse to truncate float literal 1") {
|
2022-01-07 21:12:13 +00:00
|
|
|
val text = """
|
|
|
|
%option enable_floats
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
float @shared fl = 3.456 as uword
|
|
|
|
fl = 1.234 as uword
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), false, text, errors=errors) shouldBe null
|
2022-01-07 21:12:13 +00:00
|
|
|
errors.errors.size shouldBe 2
|
2023-12-15 21:05:57 +00:00
|
|
|
errors.errors[0] shouldContain "refused"
|
|
|
|
errors.errors[1] shouldContain "refused"
|
2022-01-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
|
2023-12-15 21:05:57 +00:00
|
|
|
test("refuse to truncate float literal 2") {
|
2022-01-07 21:12:13 +00:00
|
|
|
val text = """
|
|
|
|
%option enable_floats
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
float @shared fl = 3.456
|
|
|
|
fl++
|
|
|
|
fl = fl as uword
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), false, text, errors=errors) shouldBe null
|
2022-01-07 21:12:13 +00:00
|
|
|
errors.errors.size shouldBe 1
|
|
|
|
errors.errors[0] shouldContain "in-place makes no sense"
|
|
|
|
}
|
|
|
|
|
2023-12-15 21:05:57 +00:00
|
|
|
test("refuse to truncate float literal 3") {
|
2022-01-07 21:12:13 +00:00
|
|
|
val text = """
|
|
|
|
%option enable_floats
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword @shared ww = 3.456 as uword
|
|
|
|
ww++
|
|
|
|
ww = 3.456 as uword
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), false, text, errors=errors) shouldBe null
|
2022-01-07 21:12:13 +00:00
|
|
|
errors.errors.size shouldBe 2
|
2023-12-15 21:05:57 +00:00
|
|
|
errors.errors[0] shouldContain "refused"
|
|
|
|
errors.errors[1] shouldContain "refused"
|
2022-01-07 21:12:13 +00:00
|
|
|
}
|
2022-01-08 16:04:25 +00:00
|
|
|
|
|
|
|
test("correct implicit casts of signed number comparison and logical expressions") {
|
|
|
|
val text = """
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-27 20:34:58 +00:00
|
|
|
byte @shared bb = -10
|
|
|
|
word @shared ww = -1000
|
2022-01-08 16:04:25 +00:00
|
|
|
|
|
|
|
if bb>0 {
|
|
|
|
bb++
|
|
|
|
}
|
|
|
|
if bb < 0 {
|
|
|
|
bb ++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if bb & 1 !=0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
bb++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if bb & 128 !=0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
bb++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if bb & 255 !=0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
bb++
|
|
|
|
}
|
|
|
|
|
|
|
|
if ww>0 {
|
|
|
|
ww++
|
|
|
|
}
|
|
|
|
if ww < 0 {
|
|
|
|
ww ++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if ww & 1 !=0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
ww++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if ww & 32768 != 0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
ww++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if ww & 65535 != 0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
ww++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val statements = result.compilerAst.entrypoint.statements
|
2022-08-07 10:26:11 +00:00
|
|
|
statements.size shouldBeGreaterThan 10
|
2022-01-08 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("cast to unsigned in conditional") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-27 20:34:58 +00:00
|
|
|
byte @shared bb
|
|
|
|
word @shared ww
|
2022-01-08 16:04:25 +00:00
|
|
|
|
2024-02-27 20:34:58 +00:00
|
|
|
bool @shared iteration_in_progress
|
|
|
|
uword @shared num_bytes
|
2022-01-08 16:04:25 +00:00
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
if not iteration_in_progress or num_bytes==0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
num_bytes++
|
|
|
|
}
|
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
if bb as ubyte !=0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
bb++
|
|
|
|
}
|
2024-02-04 22:41:01 +00:00
|
|
|
if ww as uword !=0 {
|
2022-01-08 16:04:25 +00:00
|
|
|
ww++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileText(C64Target(), false, text, writeAssembly = true)!!
|
2023-02-09 00:46:23 +00:00
|
|
|
val statements = result.compilerAst.entrypoint.statements
|
2022-06-30 22:38:19 +00:00
|
|
|
statements.size shouldBeGreaterThan 10
|
2022-01-08 16:04:25 +00:00
|
|
|
}
|
2022-01-23 18:02:51 +00:00
|
|
|
|
|
|
|
test("no infinite typecast loop in assignment asmgen") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
word @shared qq = calculate(33)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub calculate(ubyte row) -> word {
|
|
|
|
return (8-(row as byte))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
2022-01-23 18:02:51 +00:00
|
|
|
}
|
2022-01-27 17:05:25 +00:00
|
|
|
|
|
|
|
test("(u)byte extend to word parameters") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-27 20:34:58 +00:00
|
|
|
byte @shared ub1 = -50
|
|
|
|
byte @shared ub2 = -51
|
|
|
|
byte @shared ub3 = -52
|
|
|
|
byte @shared ub4 = 100
|
2022-01-27 17:05:25 +00:00
|
|
|
word @shared ww = func(ub1, ub2, ub3, ub4)
|
|
|
|
ww = func(ub4, ub2, ub3, ub1)
|
|
|
|
ww=afunc(ub1, ub2, ub3, ub4)
|
|
|
|
ww=afunc(ub4, ub2, ub3, ub1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(word x1, word y1, word x2, word y2) -> word {
|
|
|
|
return x1
|
|
|
|
}
|
|
|
|
|
|
|
|
asmsub afunc(word x1 @R0, word y1 @R1, word x2 @R2, word y2 @R3) -> word @AY {
|
|
|
|
%asm {{
|
|
|
|
lda cx16.r0
|
|
|
|
ldy cx16.r0+1
|
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null
|
2022-01-27 17:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test("lsb msb used as args with word types") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword xx=${'$'}ea31
|
|
|
|
uword @shared ww = plot(lsb(xx), msb(xx))
|
|
|
|
}
|
|
|
|
|
|
|
|
inline asmsub plot(uword plotx @R0, uword ploty @R1) -> uword @AY{
|
|
|
|
%asm {{
|
|
|
|
lda cx16.r0
|
|
|
|
ldy cx16.r1
|
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}"""
|
2022-03-07 20:41:12 +00:00
|
|
|
compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null
|
2022-01-27 17:05:25 +00:00
|
|
|
}
|
2022-10-23 09:57:23 +00:00
|
|
|
|
|
|
|
test("memory reads byte into word variable") {
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword @shared ww
|
|
|
|
uword address = $1000
|
|
|
|
ww = @(address+100)
|
|
|
|
ww = @(address+1000)
|
|
|
|
cx16.r0 = @(address+100)
|
|
|
|
cx16.r0 = @(address+1000)
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2022-12-04 11:22:05 +00:00
|
|
|
|
|
|
|
test("various floating point casts don't crash the compiler") {
|
|
|
|
val text="""
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub score() -> ubyte {
|
|
|
|
cx16.r15++
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start() {
|
|
|
|
float @shared total = 0
|
|
|
|
ubyte bb = 5
|
|
|
|
|
|
|
|
cx16.r0 = 5
|
|
|
|
total += cx16.r0 as float
|
|
|
|
total += score() as float
|
|
|
|
uword ww = 5
|
|
|
|
total += ww as float
|
|
|
|
total += bb as float
|
|
|
|
float result = score() as float
|
|
|
|
total += result
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(C64Target(), true, text, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(VMTarget(), true, text, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2023-03-09 21:15:56 +00:00
|
|
|
|
|
|
|
test("byte when choices silently converted to word for convenience") {
|
2023-06-17 12:44:36 +00:00
|
|
|
val text="""
|
2023-03-09 21:15:56 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
uword z = 3
|
|
|
|
when z {
|
|
|
|
1-> z++
|
|
|
|
2-> z++
|
|
|
|
else -> z++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = false) shouldNotBe null
|
|
|
|
}
|
2023-06-17 12:44:36 +00:00
|
|
|
|
|
|
|
test("returning smaller dt than returndt is ok") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
void test()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test() -> uword {
|
|
|
|
return cx16.r0L
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = false) shouldNotBe null
|
|
|
|
}
|
|
|
|
|
|
|
|
test("returning bigger dt than returndt is not ok") {
|
|
|
|
val text="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
void test()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test() -> ubyte {
|
|
|
|
return cx16.r0
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors=ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = false, errors=errors) shouldBe null
|
|
|
|
errors.errors.single() shouldContain "doesn't match"
|
|
|
|
}
|
|
|
|
|
2023-12-03 00:48:01 +00:00
|
|
|
test("long type okay in const expr but otherwise overflow") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
const ubyte HEIGHT=240
|
|
|
|
uword large = 320*240/8/8
|
|
|
|
thing(large)
|
|
|
|
thing(320*240/8/8)
|
|
|
|
thing(320*HEIGHT/8/8)
|
2024-02-04 22:41:01 +00:00
|
|
|
thing(320*HEIGHT) ; overflow
|
|
|
|
large = 12345678 ; overflow
|
2023-12-03 00:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub thing(uword value) {
|
|
|
|
value++
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors=ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = false, errors=errors) shouldBe null
|
2024-02-04 22:41:01 +00:00
|
|
|
errors.errors.size shouldBe 3
|
|
|
|
errors.errors[0] shouldContain ":9:"
|
2023-12-15 21:05:57 +00:00
|
|
|
errors.errors[0] shouldContain "no cast"
|
2024-02-04 22:41:01 +00:00
|
|
|
errors.errors[1] shouldContain ":10:"
|
|
|
|
errors.errors[1] shouldContain "out of range"
|
|
|
|
errors.errors[2] shouldContain ":10:"
|
|
|
|
errors.errors[2] shouldContain "doesn't match"
|
2023-12-03 00:48:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
test("various bool typecasts and type mismatches") {
|
2024-01-25 20:14:20 +00:00
|
|
|
val src="""
|
2024-02-04 22:41:01 +00:00
|
|
|
%option enable_floats
|
|
|
|
|
2024-01-25 20:14:20 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-04 22:41:01 +00:00
|
|
|
|
|
|
|
float @shared fl
|
|
|
|
ubyte @shared flags
|
|
|
|
byte @shared flagss
|
|
|
|
uword @shared flagsw
|
|
|
|
word @shared flagssw
|
|
|
|
bool @shared bflags = 123
|
|
|
|
cx16.r0++
|
|
|
|
bflags = 123
|
|
|
|
cx16.r0++
|
|
|
|
bflags = 123 as bool
|
|
|
|
|
|
|
|
flags = bflags
|
|
|
|
flagss = bflags
|
|
|
|
flagsw = bflags
|
|
|
|
flagssw = bflags
|
|
|
|
fl = bflags
|
|
|
|
bflags = flags
|
|
|
|
bflags = flagss
|
|
|
|
bflags = flagsw
|
|
|
|
bflags = flagssw
|
|
|
|
bflags = fl
|
|
|
|
|
|
|
|
flags = bflags as ubyte
|
|
|
|
flagss = bflags as byte
|
|
|
|
flagsw = bflags as uword
|
|
|
|
flagssw = bflags as word
|
|
|
|
fl = bflags as float
|
|
|
|
bflags = flags as bool
|
|
|
|
bflags = flagss as bool
|
|
|
|
bflags = flagsw as bool
|
|
|
|
bflags = flagssw as bool
|
|
|
|
bflags = fl as bool
|
2024-01-25 20:14:20 +00:00
|
|
|
}
|
|
|
|
}"""
|
2024-02-04 22:41:01 +00:00
|
|
|
val errors=ErrorReporterForTests()
|
|
|
|
compileText(VMTarget(), false, src, writeAssembly = false, errors=errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 12
|
|
|
|
errors.errors.all { "type of value" in it } shouldBe true
|
|
|
|
errors.errors.all { "doesn't match" in it } shouldBe true
|
|
|
|
}
|
2024-01-25 20:54:40 +00:00
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
test("bool to byte cast in expression is not allowed") {
|
|
|
|
val text="""
|
2024-01-25 20:54:40 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-04 22:41:01 +00:00
|
|
|
ubyte[3] values
|
|
|
|
func1(22 in values)
|
|
|
|
func2(22 in values)
|
|
|
|
ubyte @shared qq = 22 in values
|
|
|
|
byte @shared ww = 22 in values
|
|
|
|
}
|
|
|
|
sub func1(ubyte arg) {
|
|
|
|
arg++
|
|
|
|
}
|
|
|
|
sub func2(byte arg) {
|
|
|
|
arg++
|
2024-01-25 20:54:40 +00:00
|
|
|
}
|
|
|
|
}"""
|
2024-02-04 22:41:01 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, text, writeAssembly = true, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 4
|
|
|
|
errors.errors[0] shouldContain("argument 1 type mismatch")
|
|
|
|
errors.errors[1] shouldContain("argument 1 type mismatch")
|
|
|
|
errors.errors[2] shouldContain("type of value BOOL doesn't match target")
|
|
|
|
errors.errors[3] shouldContain("type of value BOOL doesn't match target")
|
2023-12-03 00:48:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
test("bool function parameters correct typing") {
|
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool bb = func(true)
|
|
|
|
void func(true)
|
|
|
|
; all these should fail:
|
|
|
|
void func(0)
|
|
|
|
void func(1)
|
|
|
|
void func(42)
|
|
|
|
void func(65535)
|
|
|
|
void func(655.444)
|
|
|
|
cx16.r0L = func(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(bool draw) -> bool {
|
|
|
|
cx16.r0++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 6
|
|
|
|
errors.errors[0] shouldContain("type mismatch")
|
|
|
|
errors.errors[1] shouldContain("type mismatch")
|
|
|
|
errors.errors[2] shouldContain("type mismatch")
|
|
|
|
errors.errors[3] shouldContain("type mismatch")
|
|
|
|
errors.errors[4] shouldContain("type mismatch")
|
|
|
|
errors.errors[5] shouldContain("type of value BOOL doesn't match target")
|
|
|
|
}
|
|
|
|
|
|
|
|
test("no implicit bool-to-int cast") {
|
2024-01-25 20:14:20 +00:00
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-04 22:41:01 +00:00
|
|
|
func(true)
|
|
|
|
func(true as ubyte)
|
|
|
|
cx16.r0L = true
|
|
|
|
cx16.r0L = true as ubyte
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(bool b) {
|
|
|
|
cx16.r0++
|
2024-01-25 20:14:20 +00:00
|
|
|
}
|
|
|
|
}"""
|
2024-02-04 22:41:01 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 2
|
|
|
|
errors.errors[0] shouldContain(":5:14: argument 1 type mismatch")
|
|
|
|
errors.errors[1] shouldContain(":6:20: type of value BOOL doesn't match target")
|
|
|
|
}
|
2024-01-25 20:54:40 +00:00
|
|
|
|
2024-02-04 22:41:01 +00:00
|
|
|
test("no implicit int-to-bool cast") {
|
|
|
|
val src="""
|
2024-01-25 20:54:40 +00:00
|
|
|
main {
|
|
|
|
sub start() {
|
2024-02-04 22:41:01 +00:00
|
|
|
func1(true)
|
|
|
|
func2(true)
|
|
|
|
func1(true as ubyte)
|
|
|
|
func2(true as uword)
|
|
|
|
bool @shared bb1 = 1
|
|
|
|
bool @shared bb2 = 12345
|
|
|
|
bool @shared bb3 = 1 as bool
|
|
|
|
bool @shared bb4 = 12345 as bool
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func1(ubyte ub) {
|
|
|
|
cx16.r0++
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func2(uword uw) {
|
|
|
|
cx16.r0++
|
2024-01-25 20:54:40 +00:00
|
|
|
}
|
|
|
|
}"""
|
2024-02-04 22:41:01 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 4
|
|
|
|
errors.errors[0] shouldContain(":4:15: no implicit cast")
|
|
|
|
errors.errors[1] shouldContain(":5:15: no implicit cast")
|
|
|
|
errors.errors[2] shouldContain(":8:28: type of value UBYTE doesn't match target")
|
|
|
|
errors.errors[3] shouldContain(":9:28: type of value UWORD doesn't match target")
|
2024-01-25 20:14:20 +00:00
|
|
|
}
|
2024-03-10 22:45:44 +00:00
|
|
|
|
|
|
|
test("str replaced with uword in subroutine params and return types") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
derp("hello")
|
|
|
|
mult3("hello")
|
|
|
|
}
|
|
|
|
|
|
|
|
sub derp(str arg) -> str {
|
|
|
|
cx16.r0++
|
|
|
|
return arg
|
|
|
|
}
|
|
|
|
|
|
|
|
asmsub mult3(str input @XY) -> str @XY {
|
|
|
|
%asm {{
|
|
|
|
ldx #100
|
|
|
|
ldy #101
|
|
|
|
rts
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
val result = compileText(VMTarget(), true, src, writeAssembly = true)!!
|
|
|
|
val main = result.codegenAst!!.allBlocks().first()
|
|
|
|
val derp = main.children.single { it is PtSub && it.name=="main.derp"} as PtSub
|
|
|
|
derp.returntype shouldBe DataType.UWORD
|
|
|
|
derp.parameters.single().type shouldBe DataType.UWORD
|
|
|
|
val mult3 = main.children.single { it is PtAsmSub && it.name=="main.mult3"} as PtAsmSub
|
|
|
|
mult3.parameters.single().second.type shouldBe DataType.UWORD
|
|
|
|
mult3.returns.single().second shouldBe DataType.UWORD
|
|
|
|
}
|
|
|
|
|
|
|
|
test("return 0 for str converted to uword") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
cx16.r0 = test()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub test() -> str {
|
|
|
|
cx16.r0++
|
|
|
|
if cx16.r0L==255
|
|
|
|
return 0
|
|
|
|
|
|
|
|
return 42
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
compileText(C64Target(), true, src, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2024-04-10 20:33:22 +00:00
|
|
|
|
|
|
|
test("bool to word cast") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
bool @shared flag, flag2
|
|
|
|
cx16.r0L = (flag and flag2) as ubyte
|
|
|
|
cx16.r0 = (flag and flag2) as uword
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
compileText(VMTarget(), false, src, writeAssembly = true) shouldNotBe null
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = true) shouldNotBe null
|
|
|
|
}
|
2024-09-18 21:00:03 +00:00
|
|
|
|
|
|
|
test("undefined symbol error instead of type cast error") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
const ubyte foo = 0
|
|
|
|
ubyte bar = 0
|
|
|
|
sub start() {
|
|
|
|
when foo {
|
|
|
|
notdefined -> bar = 1
|
|
|
|
else -> bar = 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 2
|
|
|
|
errors.errors[1] shouldContain "undefined symbol"
|
|
|
|
}
|
2024-10-08 22:54:17 +00:00
|
|
|
|
|
|
|
test("return unsigned values for signed results ok if value fits") {
|
|
|
|
val src = """
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
void foo()
|
|
|
|
void bar()
|
|
|
|
void overflow1()
|
|
|
|
void overflow2()
|
|
|
|
}
|
|
|
|
|
|
|
|
sub foo() -> byte {
|
|
|
|
return 42
|
|
|
|
}
|
|
|
|
sub bar() -> word {
|
|
|
|
return 12345
|
|
|
|
}
|
|
|
|
sub overflow1() -> byte {
|
|
|
|
return 200
|
|
|
|
}
|
|
|
|
sub overflow2() -> word {
|
|
|
|
return 44444
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
compileText(C64Target(), false, src, writeAssembly = false, errors = errors) shouldBe null
|
|
|
|
errors.errors.size shouldBe 2
|
|
|
|
errors.errors[0] shouldContain "17:16: type UBYTE of return value doesn't match subroutine's return type BYTE"
|
|
|
|
errors.errors[1] shouldContain "20:16: type UWORD of return value doesn't match subroutine's return type WORD"
|
|
|
|
}
|
2024-11-19 22:46:25 +00:00
|
|
|
|
|
|
|
test("if-expression adjusts different value types to common type") {
|
|
|
|
val src="""
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
cx16.r0sL = if cx16.r0L < cx16.r1L -1 else 1
|
|
|
|
}
|
|
|
|
}"""
|
|
|
|
|
|
|
|
val errors = ErrorReporterForTests()
|
|
|
|
val result = compileText(C64Target(), false, src, writeAssembly = false, errors = errors)!!
|
|
|
|
val program = result.compilerAst
|
|
|
|
val st = program.entrypoint.statements
|
|
|
|
st.size shouldBe 1
|
|
|
|
val assign = st[0] as Assignment
|
|
|
|
assign.target.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.BYTE
|
|
|
|
val ifexpr = assign.value as IfExpression
|
|
|
|
ifexpr.truevalue.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.BYTE
|
|
|
|
ifexpr.falsevalue.inferType(program).getOr(DataType.UNDEFINED) shouldBe DataType.BYTE
|
|
|
|
ifexpr.truevalue shouldBe instanceOf<NumericLiteral>()
|
|
|
|
ifexpr.falsevalue shouldBe instanceOf<NumericLiteral>()
|
|
|
|
}
|
2022-01-07 21:12:13 +00:00
|
|
|
})
|