aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-11-25 16:03:13 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-11-25 16:03:13 -0500
commit971b72fa2587abac32162a008918d3ed25ccce72 (patch)
treecc21b9d6c88cb82a0d7abe300df887f5a3deabd2 /compile.c
parent922b5857005be0609ecc59149748947a5c8b4b10 (diff)
Fix for empty constructors
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/compile.c b/compile.c
index 949d7adb..2d928cad 100644
--- a/compile.c
+++ b/compile.c
@@ -3069,16 +3069,8 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all(fn, "(", compile_arguments(env, ast, Match(fn_t, FunctionType)->args, call->args), ")");
} else if (fn_t->tag == TypeInfoType) {
type_t *t = Match(fn_t, TypeInfoType)->type;
- if (!call->args)
- code_err(ast, "This constructor requires an argument!");
-
- type_t *actual = get_type(env, call->args->value);
- if (type_eq(actual, t)) {
- if (call->args->next)
- code_err(ast, "This is too many arguments!");
- return compile(env, call->args->value);
- }
+ type_t *actual = call->args ? get_type(env, call->args->value) : NULL;
if (t->tag == StructType) {
// Struct constructor:
fn_t = Type(FunctionType, .args=Match(t, StructType)->fields, .ret=t);
@@ -3093,6 +3085,15 @@ CORD compile(env_t *env, ast_t *ast)
return compile_num_to_type(call->args->value, t);
} else if (t->tag == NumType || t->tag == BigIntType) {
if (!call->args) code_err(ast, "This constructor needs a value");
+ if (!call->args)
+ code_err(ast, "This constructor requires an argument!");
+
+ if (type_eq(actual, t)) {
+ if (call->args->next)
+ code_err(ast, "This is too many arguments!");
+ return compile(env, call->args->value);
+ }
+
arg_t *args = new(arg_t, .name="i", .type=actual); // No truncation argument
CORD arg_code = compile_arguments(env, ast, args, call->args);
if (is_numeric_type(actual)) {
@@ -3107,6 +3108,15 @@ CORD compile(env_t *env, ast_t *ast)
code_err(ast, "You cannot convert a %T to a %T this way.", actual, t);
}
} else if (t->tag == IntType || t->tag == ByteType) {
+ if (!call->args)
+ code_err(ast, "This constructor requires an argument!");
+
+ if (type_eq(actual, t)) {
+ if (call->args->next)
+ code_err(ast, "This is too many arguments!");
+ return compile(env, call->args->value);
+ }
+
if (is_numeric_type(actual)) {
arg_t *args = new(arg_t, .name="i", .type=actual, .next=new(arg_t, .name="truncate", .type=Type(BoolType),
.default_val=FakeAST(Bool, false)));