diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-22 13:00:27 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-22 13:00:27 -0500 |
| commit | 663182abdf0cb34c1b4331bfe29cefe14e053f73 (patch) | |
| tree | b1b376f6d6d74dd9655786094db7f614f318c448 | |
| parent | 2ecd8e11fd9edc42f8593edf334dc54d3a2d6930 (diff) | |
Implement concatenation
| -rw-r--r-- | compile.c | 14 | ||||
| -rw-r--r-- | typecheck.c | 2 |
2 files changed, 15 insertions, 1 deletions
@@ -300,6 +300,20 @@ CORD compile(env_t *env, ast_t *ast) else code_err(ast, "Boolean operators are only supported for Bool and integer types"); } + case BINOP_CONCAT: { + switch (operand_t->tag) { + case StringType: { + return CORD_all("CORD_cat(", lhs, ", ", rhs, ")"); + } + case ArrayType: { + return CORD_all("({ array_t $joined = ", lhs, ", $rhs = ", rhs, ";\n" + "Array__insert_all(&$joined, $rhs, 0, ", compile_type_info(env, operand_t), ");\n" + "$joined; })"); + } + default: + code_err(ast, "Concatenation isn't supported for %T types", operand_t); + } + } default: break; } code_err(ast, "unimplemented binop"); diff --git a/typecheck.c b/typecheck.c index 781b6ffc..ad1062d1 100644 --- a/typecheck.c +++ b/typecheck.c @@ -447,7 +447,7 @@ type_t *get_type(env_t *env, ast_t *ast) if (!type_eq(lhs_t, rhs_t)) code_err(ast, "The type on the left side of this concatenation doesn't match the right side: %T vs. %T", lhs_t, rhs_t); - if (lhs_t->tag == ArrayType && lhs_t->tag == StringType) + if (lhs_t->tag == ArrayType || lhs_t->tag == StringType) return lhs_t; code_err(ast, "Only array/string value types support concatenation, not %T", lhs_t); |
