aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-04 16:08:34 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-04 16:08:34 -0400
commit3513b94fc7505e48208814436a8d4bd6edcdd10c (patch)
tree7c7632c58d8a975173369d84d3f9b02f7e2f7501 /compile.c
parent973b1c55c209a8f91e8cfacb3ab4a053ef82939e (diff)
Unify parsing code to correctly handle parsing integers and numbers with
a &success boolean. Check for overflow as well.
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c24
1 files changed, 2 insertions, 22 deletions
diff --git a/compile.c b/compile.c
index 5c934d8d..caa59e3d 100644
--- a/compile.c
+++ b/compile.c
@@ -3078,7 +3078,7 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type)
"}\n");
break;
}
- case BigIntType: {
+ case BigIntType: case IntType: case NumType: {
CORD type_name = type_to_cord(t);
code = CORD_all(code, "else if (pop_flag(argv, &i, \"", flag, "\", &flag)) {\n",
"if (flag.length == 0)\n"
@@ -3089,19 +3089,6 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type)
"}\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",
- "if (flag.length == 0)\n"
- "USAGE_ERR(\"No value provided for '--", flag, "'\");\n"
- "Text_t invalid = Text(\"\");\n",
- "$", arg->name, " = ", type_name, "$from_text(flag, &invalid);\n"
- "if (invalid.length != 0)\n"
- "USAGE_ERR(\"Invalid value provided for '--", flag, "'\");\n",
- arg->name, "$is_set = yes;\n"
- "}\n");
- break;
- }
default:
compiler_err(NULL, NULL, NULL, "Main function has unsupported argument type: %T", t);
}
@@ -3138,20 +3125,13 @@ 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 == BoolType || t->tag == BigIntType) {
+ } else {
code = CORD_all(
code,
"bool success = false;\n",
"$", arg->name, " = ", type_to_cord(t), "$from_text(Text$from_str(argv[i]), &success)", ";\n"
"if (!success)\n"
"USAGE_ERR(\"Unable to parse this argument as a ", type_to_cord(t), ": %s\", argv[i]);\n");
- } else {
- code = CORD_all(
- code,
- "Text_t invalid = Text(\"\");\n",
- "$", arg->name, " = ", type_to_cord(t), "$from_text(Text$from_str(argv[i]), &invalid)", ";\n"
- "if (invalid.length != 0)\n"
- "USAGE_ERR(\"Unable to parse this argument as a ", type_to_cord(t), ": %s\", argv[i]);\n");
}
code = CORD_all(
code,