void syntax check, fixes #135

This commit is contained in:
Irmen de Jong 2024-05-18 17:15:31 +02:00
parent 6e8a89e6f1
commit 62afd3342e
3 changed files with 36 additions and 6 deletions

View File

@ -570,6 +570,13 @@ internal class AstChecker(private val program: Program,
checkType(assignment.target, assignment.value, assignment.isAugmentable)
}
if(assignment.target.void && assignment.target.multi?.isNotEmpty()!=true) {
if(assignment.value is IFunctionCall)
errors.err("cannot assign to 'void', perhaps a void function call was intended", assignment.position)
else
errors.err("cannot assign to 'void'", assignment.position)
return
}
val fcall = assignment.value as? IFunctionCall
val fcallTarget = fcall?.target?.targetSubroutine(program)

View File

@ -4,6 +4,7 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldEndWith
import io.kotest.matchers.types.instanceOf
import prog8.ast.IFunctionCall
import prog8.ast.expressions.*
@ -612,5 +613,30 @@ main {
}"""
compileText(Cx16Target(), false, src) shouldNotBe null
}
test("void assignment is invalid") {
val src="""
main {
romsub $2000 = multi() -> ubyte @A, ubyte @Y
romsub $3000 = single() -> ubyte @A
sub start() {
void, void = multi() ; ok
cx16.r0L, void = multi() ; ok
void, cx16.r0L = multi() ; ok
void multi() ; ok
void single() ; ok
void = 3333 ; fail!
void = single() ; fail!
void = multi() ; fail!
}
}"""
val errors = ErrorReporterForTests()
compileText(C64Target(), optimize=false, src, writeAssembly=true, errors = errors) shouldBe null
errors.errors.size shouldBe 3
errors.errors[0] shouldEndWith "cannot assign to 'void'"
errors.errors[1] shouldEndWith "cannot assign to 'void', perhaps a void function call was intended"
errors.errors[2] shouldEndWith "cannot assign to 'void', perhaps a void function call was intended"
}
})

View File

@ -1,10 +1,7 @@
%import textio
%zeropage basicsafe
main {
sub start() {
uword workFunc=$2000
void = call(workFunc)
void = call($2000)
}
}