diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-03-10 12:55:01 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-03-10 12:55:01 -0400 |
| commit | eb8b501b95e4d8704245375eb5f532303cc8e2e7 (patch) | |
| tree | 0b942ed50e368e3813bdcdb150fed82fe700e630 | |
| parent | 793cda6013ad723018c3ff6c7654d9aa4eb3ada5 (diff) | |
For langs, do promotion to Text and text interpolation automatically and without adding quoting.
| -rw-r--r-- | compile.c | 11 | ||||
| -rw-r--r-- | types.c | 4 |
2 files changed, 13 insertions, 2 deletions
@@ -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); } @@ -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; |
