aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-04 16:21:27 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-04 16:21:27 -0400
commit453c3610f9f17c48f4c62d7b04939275ea7c5406 (patch)
tree5ecbd1c393387fb7ed129ef3542ce77deba7ac87 /compile.c
parent3513b94fc7505e48208814436a8d4bd6edcdd10c (diff)
Support parsing enums (without members)
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index caa59e3d..e475486c 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,