From eb8b501b95e4d8704245375eb5f532303cc8e2e7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 10 Mar 2025 12:55:01 -0400 Subject: [PATCH] For langs, do promotion to Text and text interpolation automatically and without adding quoting. --- compile.c | 11 +++++++++-- types.c | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compile.c b/compile.c index 2cd2869..068afb4 100644 --- a/compile.c +++ b/compile.c @@ -91,6 +91,10 @@ static bool promote(env_t *env, ast_t *ast, CORD *code, type_t *actual, type_t * return true; } + // Lang to Text: + if (actual->tag == TextType && needed->tag == TextType && streq(Match(needed, TextType)->lang, "Text")) + return true; + // Automatic optional checking for nums: if (needed->tag == NumType && actual->tag == OptionalType && Match(actual, OptionalType)->type->tag == NumType) { *code = CORD_all("({ ", compile_declaration(actual, "opt"), " = ", *code, "; ", @@ -2668,7 +2672,7 @@ CORD compile(env_t *env, ast_t *ast) case TextLiteral: { CORD literal = Match(ast, TextLiteral)->cord; if (literal == CORD_EMPTY) - return "Text(\"\")"; + return "EMPTY_TEXT"; if (string_literal_is_all_ascii(literal)) return CORD_all("Text(", compile_string_literal(literal), ")"); @@ -2712,7 +2716,10 @@ CORD compile(env_t *env, ast_t *ast) arg_ast_t *args = new(arg_ast_t, .value=chunk->ast); chunk_code = CORD_all(constructor->code, "(", compile_arguments(env, ast, arg_spec, args), ")"); } else if (type_eq(text_t, TEXT_TYPE)) { - chunk_code = compile_string(env, chunk->ast, "no"); + if (chunk_t->tag == TextType) + chunk_code = compile(env, chunk->ast); + else + chunk_code = compile_string(env, chunk->ast, "no"); } else { code_err(chunk->ast, "I don't know how to convert %T to %T", chunk_t, text_t); } diff --git a/types.c b/types.c index c943eb8..358a729 100644 --- a/types.c +++ b/types.c @@ -338,6 +338,10 @@ PUREFUNC bool can_promote(type_t *actual, type_t *needed) if (needed->tag == EnumType) return (enum_single_value_tag(needed, actual) != NULL); + // Lang to Text: + if (actual->tag == TextType && needed->tag == TextType && streq(Match(needed, TextType)->lang, "Text")) + return true; + // Text to C String if (actual->tag == TextType && !Match(actual, TextType)->lang && needed->tag == CStringType) return true;