check for missing '&' in string + value expressions (can't just add a value to a string)

This commit is contained in:
Irmen de Jong 2022-01-24 23:30:15 +01:00
parent 14407bd1aa
commit e2b8c069d7
4 changed files with 60 additions and 38 deletions

View File

@ -835,6 +835,13 @@ internal class AstChecker(private val program: Program,
val leftDt = leftIDt.getOr(DataType.UNDEFINED)
val rightDt = rightIDt.getOr(DataType.UNDEFINED)
if(expr.operator=="+" || expr.operator=="-") {
if(leftDt == DataType.STR || rightDt == DataType.STR || leftDt in ArrayDatatypes || rightDt in ArrayDatatypes) {
errors.err("missing & (address-of) on the string operand", expr.position)
return
}
}
when(expr.operator){
"/", "%" -> {
val constvalRight = expr.right.constValue(program)

View File

@ -56,4 +56,45 @@ class TestAstChecks: FunSpec({
errors.errors[0] shouldContain ":7:28) assignment value is invalid"
errors.errors[1] shouldContain ":8:28) assignment value is invalid"
}
test("can't do str or array expression without using address-of") {
val text = """
%import textio
main {
sub start() {
ubyte[] array = [1,2,3,4]
str s1 = "test"
ubyte ff = 1
txt.print(s1+ff)
txt.print(array+ff)
txt.print_uwhex(s1+ff, true)
txt.print_uwhex(array+ff, true)
}
}
"""
val errors = ErrorReporterForTests()
compileText(C64Target, false, text, writeAssembly = false, errors=errors).assertFailure()
errors.errors.filter { it.contains("missing &") }.size shouldBe 4
}
test("str or array expression with address-of") {
val text = """
%import textio
main {
sub start() {
ubyte[] array = [1,2,3,4]
str s1 = "test"
ubyte ff = 1
txt.print(&s1+ff)
txt.print(&array+ff)
txt.print_uwhex(&s1+ff, true)
txt.print_uwhex(&array+ff, true)
; also good:
ff = (s1 == "derp")
ff = (s1 != "derp")
}
}
"""
compileText(C64Target, false, text, writeAssembly = false).assertSuccess()
}
})

View File

@ -3,8 +3,6 @@ TODO
For next release
^^^^^^^^^^^^^^^^
- str s1, ubyte ff, txt.print_uwhex(s1+ff, true) ; TODO fix compiler crash on s1+ff. why no crash when using 1-argument functioncall?
...

View File

@ -4,43 +4,19 @@
main {
sub start() {
str s1 = "irmen@razorvine.net"
str s1 = "irmen"
ubyte ff = 1
txt.print(&s1+ff)
txt.nl()
txt.print(&s1+ff)
txt.nl()
txt.print_uwhex(&s1+ff, true)
ubyte qq = '@'
if qq in s1
txt.print("good1\n")
else
txt.print("error1\n")
if 'r' in s1
txt.print("good2\n")
else
txt.print("error2\n")
if qq in "irmen22@razorvine.netirmen22@razorvine.netirmen22@razorvine.netirmen22@razorvine.net"
txt.print("good3\n")
else
txt.print("error3\n")
qq = 2
if qq in [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
txt.print("good4\n")
else
txt.print("error4\n")
qq='z'
if qq in "irm@razo"
txt.print("good5\n")
else
txt.print("error5\n")
uword zz = 2222
if zz in [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2222,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
txt.print("good6\n")
else
txt.print("error6\n")
; str s1, ubyte ff, txt.print_uwhex(s1+ff, true) ; TODO fix compiler crash on s1+ff. why no crash when using 1-argument functioncall?
ubyte[] array = [1,2,3,4]
txt.print_uwhex(&array+ff, true)
txt.nl()
txt.print_uwhex(&array+ff, true)
txt.nl()
}
}