From fbecedaf41bd8ce18f8856b9fbe98c505d655ba6 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 11 Mar 2020 23:33:06 +0100 Subject: [PATCH] added error for unsupported sort(floatarray) --- compiler/src/prog8/ast/processing/AstChecker.kt | 10 +++++++++- .../target/c64/codegen/BuiltinFunctionsAsmGen.kt | 2 +- docs/source/programming.rst | 6 +++++- examples/test.p8 | 2 ++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index c3e99ebca..06e1febef 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -826,10 +826,18 @@ internal class AstChecker(private val program: Program, printWarning("result values of subroutine call are discarded (use void?)", functionCallStatement.position) } + if(functionCallStatement.target.nameInSource.last() == "sort") { + // sort is not supported on float arrays + val idref = functionCallStatement.args.singleOrNull() as? IdentifierReference + if(idref!=null && idref.inferType(program).istype(DataType.ARRAY_F)) { + checkResult.add(ExpressionError("sorting a floating point array is not supported", functionCallStatement.args.first().position)) + } + } + if(functionCallStatement.target.nameInSource.last() in setOf("lsl", "lsr", "rol", "ror", "rol2", "ror2", "swap", "sort", "reverse")) { // in-place modification, can't be done on literals if(functionCallStatement.args.any { it !is IdentifierReference && it !is RegisterExpr && it !is ArrayIndexedExpression && it !is DirectMemoryRead }) { - checkResult.add(ExpressionError("can't use that as argument to a in-place modifying function", functionCallStatement.position)) + checkResult.add(ExpressionError("can't use that as argument to a in-place modifying function", functionCallStatement.args.first().position)) } } super.visit(functionCallStatement) diff --git a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt index 80aa4d6b3..c376c9595 100644 --- a/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/codegen/BuiltinFunctionsAsmGen.kt @@ -512,7 +512,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val """) asmgen.out(if(decl.datatype==DataType.ARRAY_UW) " jsr prog8_lib.sort_uw" else " jsr prog8_lib.sort_w") } - DataType.ARRAY_F -> TODO("sort floats (consider another solution if possible - this will be very slow, if ever implemented)") + DataType.ARRAY_F -> throw AssemblyError("sorting of floating point array is not supported") else -> throw AssemblyError("weird type") } } diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 323974b37..17209b72a 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -713,9 +713,13 @@ sum(x) sort(array) Sort the array in ascending order (in-place) + Note: sorting a floating-point array is not supported right now, as a general sorting routine for this will + be extremely slow. Either build one yourself or find another solution that doesn't require sorting + floating point values. reverse(array) - Reverse the values in the array (in-place). Can be used after sort() to sort an array in descending order. + Reverse the values in the array (in-place). Supports all data types including floats. + Can be used after sort() to sort an array in descending order. len(x) Number of values in the array value x, or the number of characters in a string (excluding the size or 0-byte). diff --git a/examples/test.p8 b/examples/test.p8 index 3b031f50a..bc7ebb60a 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -117,6 +117,8 @@ main { float[] fa = [1.1, 2.2, 3.3, 4.4, 5.5] reverse(fa) + sort(uww3) + sort(fa) for ub in 0 to len(fa)-1 { c64flt.print_f(fa[ub]) c64scr.print(",")