From fd4d72d8974f9069f8458896656b9be1cac3c84f Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:45:06 +0000 Subject: [PATCH 1/2] Fix: Add support for concatenating arrays #192 --- src/interpreter/interpreter.c | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/interpreter/interpreter.c b/src/interpreter/interpreter.c index ac01ff8..c1e7834 100644 --- a/src/interpreter/interpreter.c +++ b/src/interpreter/interpreter.c @@ -408,6 +408,59 @@ InterpretResult handle_string_concatenation(InterpretResult left, return make_result(lv_result, false, false); } +/** + * @brief Concatenates two arrays and returns the resulting array. + * + * @param left_res The left-hand side array. + * @param right_res The right-hand side array. + * @return InterpretResult The result of the concatenation or an error. + */ +InterpretResult handle_array_concatenation(InterpretResult left_res, + InterpretResult right_res) { + // Ensure both operands are arrays + if (left_res.value.type != TYPE_ARRAY || + right_res.value.type != TYPE_ARRAY) { + return raise_error( + "Array concatenation requires both operands to be arrays.\n"); + } + + ArrayValue *left_array = &left_res.value.data.array; + ArrayValue *right_array = &right_res.value.data.array; + + // Calculate the new size + size_t new_count = left_array->count + right_array->count; + size_t new_capacity = left_array->capacity + right_array->capacity; + + // Allocate memory for the new array elements + LiteralValue *new_elements = malloc(new_capacity * sizeof(LiteralValue)); + if (!new_elements) { + return raise_error( + "Memory allocation failed during array concatenation.\n"); + } + + // Copy elements from the left array + for (size_t i = 0; i < left_array->count; i++) { + new_elements[i] = left_array->elements[i]; + } + + // Copy elements from the right array + for (size_t i = 0; i < right_array->count; i++) { + new_elements[left_array->count + i] = right_array->elements[i]; + } + + // Create the new concatenated array + ArrayValue concatenated_array; + concatenated_array.count = new_count; + concatenated_array.capacity = new_capacity; + concatenated_array.elements = new_elements; + + LiteralValue result; + result.type = TYPE_ARRAY; + result.data.array = concatenated_array; + + return make_result(result, false, false); +} + // Helper function to handle numeric operations and comparisons InterpretResult handle_numeric_operator(const char *op, InterpretResult left_res, @@ -530,6 +583,14 @@ InterpretResult evaluate_operator(const char *op, InterpretResult left_res, InterpretResult right_res) { debug_print_int("Operator: `%s`\n", op); + // Handle array concatenation with "+" operator + if (strcmp(op, "+") == 0) { + if (left_res.value.type == TYPE_ARRAY && + right_res.value.type == TYPE_ARRAY) { + return handle_array_concatenation(left_res, right_res); + } + } + // Handle string concatenation with "+" operator if (strcmp(op, "+") == 0 && (left_res.value.type == TYPE_STRING || right_res.value.type == TYPE_STRING)) { From f1cbe8cf5045c32f263a2a6346ea0d987fb389f0 Mon Sep 17 00:00:00 2001 From: Kenny <70860732+KennyOliver@users.noreply.github.com> Date: Thu, 9 Jan 2025 22:45:42 +0000 Subject: [PATCH 2/2] Update: `tests/2_concatenation.flv` and test 2 in `all.flv` #192 --- src/tests/2_concatenation.flv | 5 +++++ src/tests/all.flv | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/tests/2_concatenation.flv b/src/tests/2_concatenation.flv index 5a5e41e..0152d8d 100644 --- a/src/tests/2_concatenation.flv +++ b/src/tests/2_concatenation.flv @@ -17,3 +17,8 @@ serve(int_str); const int_int = 1 + 2; serve(int_int); + +const array1 = [1, 2, 3]; +const array2 = [4, 5, 6]; +const array_concatenation_result = array1 + array2; +serve(array_concatenation_result); # [1, 2, 3, 4, 5, 6] diff --git a/src/tests/all.flv b/src/tests/all.flv index 564c630..800928b 100644 --- a/src/tests/all.flv +++ b/src/tests/all.flv @@ -14,6 +14,25 @@ serve(name); serve("Age:", age); +# Extra tests +const str_str = "X" + " Y"; +serve(str_str); + +const str_int = "X" + 1; +serve(str_int); + +const int_str = 1 + "Y"; +serve(int_str); + +const int_int = 1 + 2; +serve(int_int); + +const array1 = [1, 2, 3]; +const array2 = [4, 5, 6]; +const array_concatenation_result = array1 + array2; +serve(array_concatenation_result); # [1, 2, 3, 4, 5, 6] + + # ================================================== # 3 # ==================================================