diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-11-23 13:28:48 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-11-23 13:28:48 -0500 |
| commit | 0fa9a52090eb5d9ce88220c0134a8d2af6eb8d94 (patch) | |
| tree | 87a3181238da384a94077f401b0ba49eb8eb1e7e /src/compile/enums.c | |
| parent | 1c77d596b28ee45e0234cfa7c5f5679492f2178e (diff) | |
Accessing enum fields now gives an optional value instead of a boolean
Diffstat (limited to 'src/compile/enums.c')
| -rw-r--r-- | src/compile/enums.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/compile/enums.c b/src/compile/enums.c index ec7a1755..31af96ad 100644 --- a/src/compile/enums.c +++ b/src/compile/enums.c @@ -156,15 +156,20 @@ Text_t compile_enum_field_access(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)); - if (fielded_t->tag == PointerType) { + if (tag->type != NULL && Match(tag->type, StructType)->fields) { + return Texts("({ ", compile_declaration(value_t, Text("_e")), " = ", + compile_to_pointer_depth(env, f->fielded, 0, false), "; ", "_e.$tag == ", tag_name, " ? ", + promote_to_optional(tag->type, Texts("_e.", tag->name)), " : ", compile_none(tag->type), + "; })"); + } else if (fielded_t->tag == PointerType) { Text_t fielded = compile_to_pointer_depth(env, f->fielded, 1, false); - return Texts("((", fielded, ")->$tag == ", tag_name, ")"); + return Texts("((", fielded, ")->$tag == ", tag_name, " ? OPTIONAL_EMPTY_STRUCT : NONE_EMPTY_STRUCT)"); } else if (enum_has_fields(value_t)) { Text_t fielded = compile(env, f->fielded); - return Texts("((", fielded, ").$tag == ", tag_name, ")"); + return Texts("((", fielded, ").$tag == ", tag_name, " ? OPTIONAL_EMPTY_STRUCT : NONE_EMPTY_STRUCT)"); } else { Text_t fielded = compile(env, f->fielded); - return Texts("((", fielded, ") == ", tag_name, ")"); + return Texts("((", fielded, ") == ", tag_name, " ? OPTIONAL_EMPTY_STRUCT : NONE_EMPTY_STRUCT)"); } } } |
