aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-04-12 13:43:23 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-04-12 13:43:23 -0400
commit48d272c3fa8af67c095aafe25a565f142a1a8ebf (patch)
treee2ca7648fc5a90ca78ff01f27e00d582f4c38ec5
parent6c01eef851439549018267fdc439e4884af0c624 (diff)
Support Text arrays as main function arguments
-rw-r--r--builtins/array.c2
-rw-r--r--compile.c26
2 files changed, 25 insertions, 3 deletions
diff --git a/builtins/array.c b/builtins/array.c
index 333a2c50..77a67667 100644
--- a/builtins/array.c
+++ b/builtins/array.c
@@ -11,9 +11,9 @@
#include <sys/param.h>
#include "array.h"
-#include "types.h"
#include "functions.h"
#include "halfsiphash.h"
+#include "types.h"
#include "util.h"
static inline size_t get_item_size(const TypeInfo *info)
diff --git a/compile.c b/compile.c
index 58ba82ce..4dcfeb4a 100644
--- a/compile.c
+++ b/compile.c
@@ -1898,6 +1898,8 @@ static CORD compile_main_arg_parser(env_t *env, const char *module_name, type_t
} else {
if (t->tag == BoolType)
usage = CORD_all(usage, "[--", flag, "|--no-", flag, "]");
+ else if (t->tag == ArrayType)
+ usage = CORD_all(usage, "<", flag, "...>");
else
usage = CORD_all(usage, "<", flag, ">");
}
@@ -1947,6 +1949,15 @@ static CORD compile_main_arg_parser(env_t *env, const char *module_name, type_t
"}\n");
break;
}
+ case ArrayType: {
+ if (Match(t, ArrayType)->item_type->tag != TextType)
+ compiler_err(NULL, NULL, NULL, "Main function has unsupported argument type: %T (only arrays of Text are supported)", t);
+ code = CORD_all(code, "else if (pop_flag(argv, &i, \"", flag, "\", &$flag)) {\n",
+ arg->name, " = Text$split(CORD_to_const_char_star($flag), \",\");\n",
+ arg->name, "$is_set = yes;\n"
+ "}\n");
+ break;
+ }
case IntType: case NumType: {
CORD type_name = type_to_cord(t);
code = CORD_all(code, "else if (pop_flag(argv, &i, \"", flag, "\", &$flag)) {\n",
@@ -1975,9 +1986,20 @@ static CORD compile_main_arg_parser(env_t *env, const char *module_name, type_t
"++i;\n");
for (arg_t *arg = fn_info->args; arg; arg = arg->next) {
- code = CORD_all(code, "if (!", arg->name, "$is_set) {\n");
type_t *t = get_arg_type(env, arg);
- if (arg->default_val) {
+ code = CORD_all(code, "if (!", arg->name, "$is_set) {\n");
+ if (t->tag == ArrayType) {
+ code = CORD_all(
+ code, arg->name, " = (array_t){};\n"
+ "for (; i < argc; i++) {\n"
+ "if (argv[i]) {\n"
+ "CORD $arg = CORD_from_char_star(argv[i]);\n"
+ "Array$insert(&", arg->name, ", &$arg, 0, $ArrayInfo(&$Text));\n"
+ "argv[i] = NULL;\n"
+ "}\n"
+ "}\n",
+ arg->name, "$is_set = yes;\n");
+ } else if (arg->default_val) {
code = CORD_all(code, arg->name, " = ", compile(env, arg->default_val), ";\n");
} else {
code = CORD_all(