aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-13 02:20:48 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-13 02:20:48 -0400
commitaa74180ed1a32da1c0191d8e202922a5dc03f3ce (patch)
tree01a7c9faf81d9a69aced60881d7db2c15dba20bd /compile.c
parent158a1c6ad2d9b3f2a598e6d1d8debcc824ad73d0 (diff)
Fixing up integers
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index 2377ec25..2766143c 100644
--- a/compile.c
+++ b/compile.c
@@ -2331,6 +2331,17 @@ CORD compile(env_t *env, ast_t *ast)
// Struct constructor:
fn_t = Type(FunctionType, .args=Match(t, StructType)->fields, .ret=t);
return CORD_all("((", compile_type(t), "){", compile_arguments(env, ast, Match(fn_t, FunctionType)->args, call->args), "})");
+ } else if (t->tag == IntType && Match(t, IntType)->bits == 0) {
+ // Int/Num constructor:
+ if (!call->args || call->args->next)
+ code_err(call->fn, "This constructor takes exactly 1 argument");
+ type_t *actual = get_type(env, call->args->value);
+ if (actual->tag == IntType)
+ return CORD_all("I((Int64_t)(", compile(env, call->args->value), "))");
+ else if (actual->tag == NumType)
+ return CORD_all("Int$from_num((double)(", compile(env, call->args->value), "))");
+ else
+ code_err(call->args->value, "This %T value cannot be converted to a %T", actual, t);
} else if (t->tag == IntType || t->tag == NumType) {
// Int/Num constructor:
if (!call->args || call->args->next)