From 81b3d2db4f78ae7cadfef0df9484874b80b18937 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 8 Jul 2022 21:09:02 +0200 Subject: [PATCH] fix compiler crash --- .../prog8/compiler/astprocessing/AstChecker.kt | 6 ++++-- compilerAst/src/prog8/ast/Extensions.kt | 1 + .../prog8/ast/expressions/AstExpressions.kt | 2 +- .../src/prog8/compiler/BuiltinFunctions.kt | 2 +- examples/tehtriz.p8 | 18 +++++++++--------- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 1dd9f515c..fc4a023b3 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -78,8 +78,10 @@ internal class AstChecker(private val program: Program, if(!valueDt.isKnown) { errors.err("return value type mismatch or unknown symbol", returnStmt.value!!.position) } else { - if (expectedReturnValues[0] != valueDt.getOr(DataType.UNDEFINED)) - errors.err("type $valueDt of return value doesn't match subroutine's return type ${expectedReturnValues[0]}", returnStmt.value!!.position) + if (expectedReturnValues[0] != valueDt.getOr(DataType.UNDEFINED)) { + if(expectedReturnValues[0] != DataType.BOOL || valueDt.isnot(DataType.UBYTE)) + errors.err("type $valueDt of return value doesn't match subroutine's return type ${expectedReturnValues[0]}",returnStmt.value!!.position) + } } } super.visit(returnStmt) diff --git a/compilerAst/src/prog8/ast/Extensions.kt b/compilerAst/src/prog8/ast/Extensions.kt index 8c451fe61..ce83051d1 100644 --- a/compilerAst/src/prog8/ast/Extensions.kt +++ b/compilerAst/src/prog8/ast/Extensions.kt @@ -51,6 +51,7 @@ fun getTempRegisterName(dt: InferredTypes.InferredType): List { return when { // TODO assume (hope) cx16.r9 isn't used for anything else during the use of this temporary variable... dt istype DataType.UBYTE -> listOf("cx16", "r9L") + dt istype DataType.BOOL -> listOf("cx16", "r9L") dt istype DataType.BYTE -> listOf("cx16", "r9sL") dt istype DataType.UWORD -> listOf("cx16", "r9") dt istype DataType.WORD -> listOf("cx16", "r9s") diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 4e0d54d81..dd06fa4cf 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -191,7 +191,7 @@ class BinaryExpression(var left: Expression, var operator: String, var right: Ex return when (parent) { is TypecastExpression -> InferredTypes.InferredType.known((parent as TypecastExpression).type) is Assignment -> (parent as Assignment).target.inferType(program) - else -> InferredTypes.InferredType.known(DataType.UBYTE) // note: don't use BOOL type here to avoid type errors later! Will be replaced anyway. + else -> InferredTypes.InferredType.known(DataType.BOOL) } } diff --git a/compilerAst/src/prog8/compiler/BuiltinFunctions.kt b/compilerAst/src/prog8/compiler/BuiltinFunctions.kt index 4426c8c47..2d64af559 100644 --- a/compilerAst/src/prog8/compiler/BuiltinFunctions.kt +++ b/compilerAst/src/prog8/compiler/BuiltinFunctions.kt @@ -226,7 +226,7 @@ private fun builtinLen(args: List, position: Position, program: Prog ?: throw CannotEvaluateException("len", "no target vardecl") return when(target.datatype) { - DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W, DataType.ARRAY_F -> { + in ArrayDatatypes -> { arraySize = target.arraysize?.constIndex() if(arraySize==null) throw CannotEvaluateException("len", "arraysize unknown") diff --git a/examples/tehtriz.p8 b/examples/tehtriz.p8 index b975feb80..65c0b2b3a 100644 --- a/examples/tehtriz.p8 +++ b/examples/tehtriz.p8 @@ -28,7 +28,7 @@ main { ubyte nextBlock ubyte speedlevel ubyte holding - ubyte holdingAllowed + bool holdingAllowed sub start() { @@ -520,21 +520,21 @@ blocklogic { ; The full play area is bordered by (in)visible characters that will collide. ; Collision is determined by reading the screen data directly. - sub canRotateCW(ubyte xpos, ubyte ypos) -> ubyte { + sub canRotateCW(ubyte xpos, ubyte ypos) -> bool { rotateCW() - ubyte nocollision = noCollision(xpos, ypos) + bool nocollision = noCollision(xpos, ypos) rotateCCW() return nocollision } - sub canRotateCCW(ubyte xpos, ubyte ypos) -> ubyte { + sub canRotateCCW(ubyte xpos, ubyte ypos) -> bool { rotateCCW() - ubyte nocollision = noCollision(xpos, ypos) + bool nocollision = noCollision(xpos, ypos) rotateCW() return nocollision } - sub noCollision(ubyte xpos, ubyte ypos) -> ubyte { + sub noCollision(ubyte xpos, ubyte ypos) -> bool { ubyte @zp i for i in 15 downto 0 { if currentBlock[i] and txt.getchr(xpos + (i&3), ypos+i/4)!=32 @@ -543,14 +543,14 @@ blocklogic { return true } - sub isGameOver(ubyte xpos, ubyte ypos) -> ubyte { + sub isGameOver(ubyte xpos, ubyte ypos) -> bool { main.drawBlock(xpos, ypos, 32) - ubyte result = ypos==main.startYpos and not noCollision(xpos, ypos+1) + bool result = ypos==main.startYpos and not noCollision(xpos, ypos+1) main.drawBlock(xpos, ypos, 160) return result } - sub isLineFull(ubyte ypos) -> ubyte { + sub isLineFull(ubyte ypos) -> bool { ubyte x for x in main.boardOffsetX to main.boardOffsetX+main.boardWidth-1 { if txt.getchr(x, ypos)==32