Skip to content

Commit

Permalink
Merge pull request #193 from KennyOliver/issue-192
Browse files Browse the repository at this point in the history
Issue 192: Add Array Concatenation with `+` Operator
  • Loading branch information
KennyOliver authored Jan 9, 2025
2 parents 23e9cb8 + f1cbe8c commit c5d9b57
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/interpreter/interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Expand Down
5 changes: 5 additions & 0 deletions src/tests/2_concatenation.flv
Original file line number Diff line number Diff line change
Expand Up @@ -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]
19 changes: 19 additions & 0 deletions src/tests/all.flv
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ==================================================
Expand Down

0 comments on commit c5d9b57

Please sign in to comment.