aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-19 12:39:45 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-19 12:39:45 -0400
commit8c4210ba01d7ef2b754593f38a296af004343133 (patch)
tree1e701a6a39bef3a450f901b0db0b64c933349bf5
parent752be14eed4c56186b0a814980445b279ea88661 (diff)
Support demoting int literals
-rw-r--r--compile.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/compile.c b/compile.c
index 03e0a077..873cae32 100644
--- a/compile.c
+++ b/compile.c
@@ -838,11 +838,16 @@ CORD compile_statement(env_t *env, ast_t *ast)
code_err(ast, "This function is not supposed to return any values, according to its type signature");
env = with_enum_scope(env, env->fn_ctx->return_type);
- type_t *ret_t = get_type(env, ret);
- CORD value = compile(env, ret);
- if (!promote(env, &value, ret_t, env->fn_ctx->return_type))
- code_err(ast, "This function expects a return value of type %T, but this return has type %T",
- env->fn_ctx->return_type, ret_t);
+ CORD value;
+ if (env->fn_ctx->return_type->tag == IntType && ret->tag == Int) {
+ value = compile_int_to_type(env, ret, env->fn_ctx->return_type);
+ } else {
+ type_t *ret_value_t = get_type(env, ret);
+ value = compile(env, ret);
+ if (!promote(env, &value, ret_value_t, env->fn_ctx->return_type))
+ code_err(ast, "This function expects a return value of type %T, but this return has type %T",
+ env->fn_ctx->return_type, ret_value_t);
+ }
return CORD_all(code, "return ", value, ";");
} else {
if (env->fn_ctx->return_type->tag != VoidType)