aboutsummaryrefslogtreecommitdiff
path: root/src/compile/optionals.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-12-07 18:44:58 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-12-07 18:44:58 -0500
commit85d1507d8b7e0139135e040b7b4b23c02097a155 (patch)
tree8eb7268aa29dd37846f16edbeb32c093efe1d63e /src/compile/optionals.c
parent43b0a91664fc0b9a5222805c68c3505fd9634689 (diff)
Consolidate logic for enums with and without tags with fields.
Diffstat (limited to 'src/compile/optionals.c')
-rw-r--r--src/compile/optionals.c30
1 files changed, 12 insertions, 18 deletions
diff --git a/src/compile/optionals.c b/src/compile/optionals.c
index 5ad67602..b9a3742e 100644
--- a/src/compile/optionals.c
+++ b/src/compile/optionals.c
@@ -103,10 +103,7 @@ Text_t check_none(type_t *t, Text_t value) {
else if (t->tag == BoolType) return Texts("((", value, ") == NONE_BOOL)");
else if (t->tag == TextType) return Texts("((", value, ").tag == TEXT_NONE)");
else if (t->tag == IntType || t->tag == ByteType || t->tag == StructType) return Texts("!(", value, ").has_value");
- else if (t->tag == EnumType) {
- if (enum_has_fields(t)) return Texts("((", value, ").$tag == 0)");
- else return Texts("((", value, ") == 0)");
- }
+ else if (t->tag == EnumType) return Texts("((", value, ").$tag == 0)");
print_err("Optional check not implemented for: ", type_to_text(t));
return EMPTY_TEXT;
}
@@ -139,20 +136,17 @@ Text_t compile_non_optional(env_t *env, ast_t *ast) {
for (tag_t *tag = e->tags; tag; tag = tag->next) {
if (streq(f->field, tag->name)) {
Text_t tag_name = namespace_name(e->env, e->env->namespace, Texts("tag$", tag->name));
- return Texts("({ ", compile_declaration(enum_t, Text("_test_enum")), " = ",
- compile_to_pointer_depth(env, f->fielded, 0, true), ";",
- "if unlikely (_test_enum.$tag != ", tag_name, ") {\n", "#line ", line, "\n",
- "fail_source(", quoted_str(f->fielded->file->filename), ", ",
- (int64_t)(f->fielded->start - f->fielded->file->text), ", ",
- (int64_t)(f->fielded->end - f->fielded->file->text), ", ", "\"This was expected to be ",
- tag->name, ", but it was: \", ", expr_as_text(Text("_test_enum"), enum_t, Text("false")),
- ", \"\\n\");\n}\n",
- Match(tag->type, StructType)->fields
- ? compile_maybe_incref(
- env, WrapLiteralCode(value, Texts("_test_enum.", tag->name), .type = tag->type),
- tag->type)
- : Text("EMPTY_STRUCT"),
- "; })");
+ return Texts(
+ "({ ", compile_declaration(enum_t, Text("_test_enum")), " = ",
+ compile_to_pointer_depth(env, f->fielded, 0, true), ";",
+ "if unlikely (_test_enum.$tag != ", tag_name, ") {\n", "#line ", line, "\n", "fail_source(",
+ quoted_str(f->fielded->file->filename), ", ", (int64_t)(f->fielded->start - f->fielded->file->text),
+ ", ", (int64_t)(f->fielded->end - f->fielded->file->text), ", ", "\"This was expected to be ",
+ tag->name, ", but it was: \", ", expr_as_text(Text("_test_enum"), enum_t, Text("false")),
+ ", \"\\n\");\n}\n",
+ compile_maybe_incref(
+ env, WrapLiteralCode(value, Texts("_test_enum.", tag->name), .type = tag->type), tag->type),
+ "; })");
}
}
code_err(ast, "The field '", f->field, "' is not a valid tag name of ", type_to_text(enum_t));