From d2d08bf143c6e7963ef60b154cf11fb26ee9ad8c Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 8 Jul 2022 22:03:05 +0200 Subject: [PATCH] fix compiler error about bool vs ubyte --- .../prog8/compiler/astprocessing/AstChecker.kt | 12 +++++++++++- examples/test.p8 | 16 ++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index fc4a023b3..9a046d891 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -79,8 +79,18 @@ internal class AstChecker(private val program: Program, errors.err("return value type mismatch or unknown symbol", returnStmt.value!!.position) } else { if (expectedReturnValues[0] != valueDt.getOr(DataType.UNDEFINED)) { - if(expectedReturnValues[0] != DataType.BOOL || valueDt.isnot(DataType.UBYTE)) + if(valueDt istype DataType.BOOL && expectedReturnValues[0] == DataType.UBYTE) { + // if the return value is a bool and the return type is ubyte, allow this. + } else if(valueDt istype DataType.UBYTE && expectedReturnValues[0] == DataType.BOOL) { + // if the return value is ubyte and the return type is bool, allow this only if value is 0 or 1 + val returnValue = returnStmt.value?.constValue(program) + if (returnValue == null || returnValue.type != DataType.UBYTE || (returnValue.number!=0.0 && returnValue.number!=1.0)) { + errors.err("type $valueDt of return value doesn't match subroutine's return type ${expectedReturnValues[0]}",returnStmt.value!!.position) + } + } + else { errors.err("type $valueDt of return value doesn't match subroutine's return type ${expectedReturnValues[0]}",returnStmt.value!!.position) + } } } } diff --git a/examples/test.p8 b/examples/test.p8 index 55bbb4f91..9b41f83aa 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,14 +4,18 @@ main { - sub noCollision(ubyte xpos, ubyte ypos) -> bool { - if xpos - return false - else - return true + ubyte key + + sub func() -> ubyte { + return key=='a' + } + + sub func2() -> bool { + return key=='z' } sub start() { - bool z=noCollision(1,2) + bool @shared z1=func() + bool @shared z2=func2() } }