diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index 31992c0ca..10e3efba8 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -814,6 +814,14 @@ internal class AstChecker(private val program: Program, val targetStatement = checkFunctionOrLabelExists(functionCall.target, stmtOfExpression) if(targetStatement!=null) checkFunctionCall(targetStatement, functionCall.args, functionCall.position) + + // warn about sgn(unsigned) this is likely a mistake + if(functionCall.target.nameInSource.last()=="sgn") { + val sgnArgType = functionCall.args.first().inferType(program) + if(sgnArgType.istype(DataType.UBYTE) || sgnArgType.istype(DataType.UWORD)) + printWarning("sgn() of unsigned type is always 0 or 1, this is perhaps not what was intended", functionCall.args.first().position) + } + super.visit(functionCall) } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 3fab45f78..c146d23a5 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,6 +2,9 @@ TODO ==== +- remove statements after an exit() or return +- fix warnings about that unreachable code? + - option to load library files from a directory instead of the embedded ones diff --git a/examples/arithmetic/sgn.p8 b/examples/arithmetic/sgn.p8 new file mode 100644 index 000000000..3361dc7a0 --- /dev/null +++ b/examples/arithmetic/sgn.p8 @@ -0,0 +1,130 @@ +%import c64flt +%zeropage basicsafe + +main { + + sub start() { + byte b1 + byte b2 + ubyte ub1 + ubyte ub2 + word w1 + word w2 + uword uw1 + uword uw2 + float f1 + float f2 + + b1 = 10 + b2 = 10 + if sgn(b2-b1) != 0 + c64scr.print("sgn1 error1\n") + + b1 = -100 + b2 = -100 + if sgn(b2-b1) != 0 + c64scr.print("sgn1 error2\n") + + ub1 = 200 + ub2 = 200 + if sgn(ub2-ub1) != 0 + c64scr.print("sgn1 error3\n") + + w1 = 100 + w2 = 100 + if sgn(w2-w1) != 0 + c64scr.print("sgn1 error4\n") + + w1 = -2000 + w2 = -2000 + if sgn(w2-w1) != 0 + c64scr.print("sgn1 error5\n") + + uw1 = 999 + uw2 = 999 + if sgn(uw2-uw1) != 0 + c64scr.print("sgn1 error6\n") + + f1 = 3.45 + f2 = 3.45 + if sgn(f2-f1) != 0 + c64scr.print("sgn1 error7\n") + + + ; -1 + b1 = 11 + b2 = 10 + if sgn(b2-b1) != -1 + c64scr.print("sgn2 error1\n") + + b1 = -10 + b2 = -100 + if sgn(b2-b1) != -1 + c64scr.print("sgn2 error2\n") + + ub1 = 202 + ub2 = 200 + if sgn(ub2 as byte - ub1 as byte) != -1 + c64scr.print("sgn2 error3\n") + + w1 = 101 + w2 = 100 + if sgn(w2-w1) != -1 + c64scr.print("sgn2 error4\n") + + w1 = -200 + w2 = -2000 + if sgn(w2-w1) != -1 + c64scr.print("sgn2 error5\n") + + uw1 = 2222 + uw2 = 999 + if sgn(uw2 as word - uw1 as word) != -1 + c64scr.print("sgn2 error6\n") + if sgn(uw2 - uw1) != -1 + c64scr.print("sgn2 error6\n") + + f1 = 3.45 + f2 = 1.11 + if sgn(f2-f1) != -1 + c64scr.print("sgn2 error7\n") + + ; +1 + b1 = 11 + b2 = 20 + if sgn(b2-b1) != 1 + c64scr.print("sgn3 error1\n") + + b1 = -10 + b2 = -1 + if sgn(b2-b1) != 1 + c64scr.print("sgn3 error2\n") + + ub1 = 202 + ub2 = 205 + if sgn(ub2-ub1) != 1 + c64scr.print("sgn3 error3\n") + + w1 = 101 + w2 = 200 + if sgn(w2-w1) != 1 + c64scr.print("sgn3 error4\n") + + w1 = -200 + w2 = -20 + if sgn(w2-w1) != 1 + c64scr.print("sgn3 error5\n") + + uw1 = 2222 + uw2 = 9999 + if sgn(uw2-uw1) != 1 + c64scr.print("sgn3 error6\n") + + f1 = 3.45 + f2 = 5.11 + if sgn(f2-f1) != 1 + c64scr.print("sgn3 error7\n") + + c64scr.print("should see no sgn errors\n") + } + } diff --git a/examples/lines_circles.p8 b/examples/lines-circles.p8 similarity index 90% rename from examples/lines_circles.p8 rename to examples/lines-circles.p8 index 4b50144bb..28e05c718 100644 --- a/examples/lines_circles.p8 +++ b/examples/lines-circles.p8 @@ -14,6 +14,11 @@ main { for r in 3 to 12 step 3 { circle(r) } + line(5,3, 30,3) + line(5,3, 5, 24) + line(30,3,30,24) + line(5,24,30,24) + line(1, 10, 38, 24) line(1, 20, 38, 2) line(20, 4, 10, 24) @@ -27,23 +32,11 @@ main { ubyte dy = abs(y2 - y1) ubyte dx2 = 2 * dx ubyte dy2 = 2 * dy - byte ix = sgn(x2-x1) - byte iy = sgn(y2-y1) + byte ix = sgn(x2 as byte - x1 as byte) + byte iy = sgn(y2 as byte - y1 as byte) ubyte x = x1 ubyte y = y1 - ; TODO fix sgn - if x1= dy { ; TODO fix assembler problem when defining label here forever { diff --git a/examples/test.p8 b/examples/test.p8 index a8a43e714..a677335ae 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,4 +1,3 @@ -%import c64flt %zeropage basicsafe main {