Auto promote to C String from Text

This commit is contained in:
Bruce Hill 2024-09-02 19:53:09 -04:00
parent 6d7a359f8f
commit 337aa911a9
3 changed files with 11 additions and 1 deletions

View File

@ -48,6 +48,12 @@ static bool promote(env_t *env, CORD *code, type_t *actual, type_t *needed)
if (actual->tag == IntType || actual->tag == NumType)
return true;
// Text to C String
if (actual->tag == TextType && !Match(actual, TextType)->lang && needed->tag == CStringType) {
*code = CORD_all("Text$as_c_string(", *code, ")");
return true;
}
// Automatic dereferencing:
if (actual->tag == PointerType && !Match(actual, PointerType)->is_optional
&& can_promote(Match(actual, PointerType)->pointed, needed)) {

View File

@ -33,7 +33,7 @@ env_t *new_compilation_unit(CORD *libname)
} global_vars[] = {
{"say", {.code="say", .type=Type(FunctionType, .args=new(arg_t, .name="text", .type=TEXT_TYPE,
.next=new(arg_t, .name="newline", .type=Type(BoolType), .default_val=FakeAST(Bool, true))), .ret=Type(VoidType))}},
{"fail", {.code="fail", .type=Type(FunctionType, .args=new(arg_t, .name="message", .type=TEXT_TYPE), .ret=Type(AbortType))}},
{"fail", {.code="fail", .type=Type(FunctionType, .args=new(arg_t, .name="message", .type=Type(CStringType)), .ret=Type(AbortType))}},
{"USE_COLOR", {.code="USE_COLOR", .type=Type(BoolType)}},
};

View File

@ -289,6 +289,10 @@ bool can_promote(type_t *actual, type_t *needed)
return cmp == NUM_PRECISION_EQUAL || cmp == NUM_PRECISION_LESS;
}
// Text to C String
if (actual->tag == TextType && !Match(actual, TextType)->lang && needed->tag == CStringType)
return true;
// Automatic dereferencing:
if (actual->tag == PointerType && !Match(actual, PointerType)->is_optional
&& can_promote(Match(actual, PointerType)->pointed, needed))