From 0e17a0474a1c24110a7199e0c26c7d6706ac6337 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 20 Feb 2024 22:53:15 +0100 Subject: [PATCH] added type check to catch invalid comparisons, fix maze example --- .../src/prog8/compiler/astprocessing/AstChecker.kt | 11 +++++++++++ examples/maze.p8 | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index ae2197298..0b73bf2a2 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -1055,6 +1055,17 @@ internal class AstChecker(private val program: Program, // str+str and str*number have already been const evaluated before we get here. errors.err("no computational or logical expressions with strings or arrays are possible", expr.position) } + } else { + if(expr.left is TypecastExpression && expr.right is NumericLiteral && !expr.right.inferType(program).istype(DataType.FLOAT)) { + val origLeftDt = (expr.left as TypecastExpression).expression.inferType(program).getOr(DataType.UNDEFINED) + if(!origLeftDt.equalsSize(rightDt)) + errors.err("operands are not the same type", expr.right.position) + } + if(expr.right is TypecastExpression && expr.left is NumericLiteral && !expr.left.inferType(program).istype(DataType.FLOAT)) { + val origRightDt = (expr.right as TypecastExpression).expression.inferType(program).getOr(DataType.UNDEFINED) + if(!origRightDt.equalsSize(leftDt)) + errors.err("operands are not the same type", expr.right.position) + } } if(leftDt==DataType.BOOL || rightDt==DataType.BOOL || diff --git a/examples/maze.p8 b/examples/maze.p8 index d28f5eac4..6cd8d9445 100644 --- a/examples/maze.p8 +++ b/examples/maze.p8 @@ -267,7 +267,7 @@ solve_loop: else { ; dead end, pop stack pathstackptr-- - if stackptr==65535 { + if pathstackptr==65535 { txt.print("no solution?!") return }