From 453c3610f9f17c48f4c62d7b04939275ea7c5406 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 4 Sep 2024 16:21:27 -0400 Subject: [PATCH] Support parsing enums (without members) --- compile.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/compile.c b/compile.c index caa59e3..e475486 100644 --- a/compile.c +++ b/compile.c @@ -3089,6 +3089,28 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type) "}\n"); break; } + case EnumType: { + env_t *enum_env = Match(t, EnumType)->env; + code = CORD_all(code, "else if (pop_flag(argv, &i, \"", flag, "\", &flag)) {\n", + "if (flag.length == 0)\n" + "USAGE_ERR(\"No value provided for '--", flag, "'\");\n"); + CORD all_tags = CORD_EMPTY; + for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) { + if (tag->type && Match(tag->type, StructType)->fields) + compiler_err(NULL, NULL, NULL, + "The type %T has enum fields with member values, which is not yet supported for command line arguments."); + all_tags = CORD_all(all_tags, " ", tag->name); + binding_t *b = get_binding(enum_env, tag->name); + code = CORD_all(code, + "if (Text$equal_ignoring_case(flag, Text(\"", tag->name, "\"))) {\n" + "$", arg->name, " = ", b->code, ";\n", + arg->name, "$is_set = yes;\n" + "} else "); + } + code = CORD_all(code, "USAGE_ERR(\"Invalid value provided for '--", flag, "'; valid values are:", all_tags, "\");\n", + "}\n"); + break; + } default: compiler_err(NULL, NULL, NULL, "Main function has unsupported argument type: %T", t); } @@ -3125,6 +3147,22 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type) "if (i < argc) {"); if (t->tag == TextType) { code = CORD_all(code, "$", arg->name, " = Text$from_str(argv[i]);\n"); + } else if (t->tag == EnumType) { + env_t *enum_env = Match(t, EnumType)->env; + CORD all_tags = CORD_EMPTY; + for (tag_t *tag = Match(t, EnumType)->tags; tag; tag = tag->next) { + if (tag->type && Match(tag->type, StructType)->fields) + compiler_err(NULL, NULL, NULL, + "The type %T has enum fields with member values, which is not yet supported for command line arguments."); + all_tags = CORD_all(all_tags, " ", tag->name); + binding_t *b = get_binding(enum_env, tag->name); + code = CORD_all(code, + "if (strcasecmp(argv[i], \"", tag->name, "\") == 0) {\n" + "$", arg->name, " = ", b->code, ";\n", + arg->name, "$is_set = yes;\n" + "} else "); + } + code = CORD_all(code, "USAGE_ERR(\"Invalid value provided for '--", arg->name, "'; valid values are:", all_tags, "\");\n"); } else { code = CORD_all( code,