added error for unsupported sort(floatarray)

This commit is contained in:
Irmen de Jong 2020-03-11 23:33:06 +01:00
parent aa36acd65a
commit fbecedaf41
4 changed files with 17 additions and 3 deletions

View File

@ -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)

View File

@ -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")
}
}

View File

@ -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).

View File

@ -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(",")