Add missing concat. Fix error message location on not enough arguments.

This commit is contained in:
Christoffer Lerno
2024-09-15 15:56:08 +02:00
parent 1bfe9c568e
commit 9bb45cb6a3
3 changed files with 23 additions and 8 deletions

View File

@@ -55,7 +55,7 @@ macro rindex_of(array, element)
* @require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
* @ensure result.len == arr1.len + arr2.len
**/
macro concat_new(arr1, arr2, Allocator allocator = allocator::heap())
macro concat(arr1, arr2, Allocator allocator) @nodiscard
{
var $Type = $typeof(arr1[0]);
$Type[] result = allocator::alloc_array(allocator, $Type, arr1.len + arr2.len);
@@ -69,6 +69,21 @@ macro concat_new(arr1, arr2, Allocator allocator = allocator::heap())
}
return result;
}
/**
* Concatenate two arrays or slices, returning a slice containing the concatenation of them.
*
* @param [in] arr1
* @param [in] arr2
* @param [&inout] allocator "The allocator to use, default is the heap allocator"
* @require @typekind(arr1) == SLICE || @typekind(arr1) == ARRAY
* @require @typekind(arr2) == SLICE || @typekind(arr2) == ARRAY
* @require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
* @ensure result.len == arr1.len + arr2.len
**/
macro concat_new(arr1, arr2, Allocator allocator = allocator::heap()) @nodiscard
{
return concat(arr1, arr2, allocator);
}
/**
* Concatenate two arrays or slices, returning a slice containing the concatenation of them,
@@ -81,7 +96,7 @@ macro concat_new(arr1, arr2, Allocator allocator = allocator::heap())
* @require @typeis(arr1[0], $typeof(arr2[0])) "Arrays must have the same type"
* @ensure result.len == arr1.len + arr2.len
**/
macro tconcat(arr1, arr2) => concat(arr1, arr2, allocator::temp());
macro tconcat(arr1, arr2) @nodiscard => concat(arr1, arr2, allocator::temp());
module std::core::array::slice(<Type>);

View File

@@ -1723,7 +1723,7 @@ SPLAT_NORMAL:;
if (!sema_analyse_parameter(context, arg, params[i], callee->definition, optional, no_match_ref, callee->macro)) return false;
actual_args[i] = arg;
}
if (num_args) last = args[num_args - 1];
call->call_expr.arguments = args;
// 17. Set default values.
for (unsigned i = 0; i < func_param_count; i++)

View File

@@ -21,11 +21,11 @@ fn void! find_subarray()
fn void! concat()
{
int[3] a = { 1, 2, 3 };
array::concat_new(a, a);
array::concat_new(a[..], a[..]);
array::concat_new(a[:0], a[:0]);
array::concat_new(int[2] { 1, 2 }, a[:0]);
array::concat_new(a[:0], int[2] { 1, 2 });
(void)array::concat_new(a, a);
(void)array::concat_new(a[..], a[..]);
(void)array::concat_new(a[:0], a[:0]);
(void)array::concat_new(int[2] { 1, 2 }, a[:0]);
(void)array::concat_new(a[:0], int[2] { 1, 2 });
int[] c = array::concat_new(a[1..2], a);
assert (c == int[]{ 2, 3, 1, 2, 3 });
}