diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-23 13:47:06 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-23 13:47:06 -0500 |
| commit | 8bee541c76ebea46ea1c809805270bb69595a56a (patch) | |
| tree | be8fbe4b4ecc9147fcfbc8a627334bafeabb46cc | |
| parent | c04b373cb49df5d3505247ae141d77cd84adf1ff (diff) | |
Support ++= item
| -rw-r--r-- | compile.c | 19 | ||||
| -rw-r--r-- | tests/arrays.nl | 3 |
2 files changed, 18 insertions, 4 deletions
@@ -336,6 +336,8 @@ CORD compile(env_t *env, ast_t *ast) operand_t = lhs_t; else if (can_promote(lhs_t, rhs_t)) operand_t = rhs_t; + else if (lhs_t->tag == ArrayType && can_promote(rhs_t, Match(lhs_t, ArrayType)->item_type)) + operand_t = lhs_t; else code_err(ast, "I can't do operations between %T and %T", lhs_t, rhs_t); @@ -377,10 +379,19 @@ CORD compile(env_t *env, ast_t *ast) if (operand_t->tag == StringType) { return CORD_asprintf("%r = CORD_cat(%r, %r);", lhs, lhs, rhs); } else if (operand_t->tag == ArrayType) { - if (update->lhs->tag == Var) - return CORD_all("Array__insert_all(&", lhs, ", ", rhs, ", 0, ", compile_type_info(env, operand_t), ")"); - else - return CORD_all(lhs, "Array__concat(", lhs, ", ", rhs, ", ", compile_type_info(env, operand_t), ")"); + if (can_promote(rhs_t, Match(lhs_t, ArrayType)->item_type)) { + // arr ++= item + if (update->lhs->tag == Var) + return CORD_all("Array__insert(&", lhs, ", $stack(", rhs, "), 0, ", compile_type_info(env, operand_t), ")"); + else + return CORD_all(lhs, "Array__concat(", lhs, ", $Array(", rhs, "), ", compile_type_info(env, operand_t), ")"); + } else { + // arr ++= [...] + if (update->lhs->tag == Var) + return CORD_all("Array__insert_all(&", lhs, ", ", rhs, ", 0, ", compile_type_info(env, operand_t), ")"); + else + return CORD_all(lhs, "Array__concat(", lhs, ", ", rhs, ", ", compile_type_info(env, operand_t), ")"); + } } else { code_err(ast, "'++=' is not implemented for %T types", operand_t); } diff --git a/tests/arrays.nl b/tests/arrays.nl index 0c118364..5484d5e2 100644 --- a/tests/arrays.nl +++ b/tests/arrays.nl @@ -28,3 +28,6 @@ for i,x in arr >> arr2 = [10, 20, 30, 40, 50, 60] +>> arr2 ++= 70 +>> arr2 += [10, 20, 30, 40, 50, 60, 70] |
