aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builtins/integers.c12
-rw-r--r--builtins/integers.h2
-rw-r--r--compile.c23
-rw-r--r--environment.c8
-rw-r--r--parse.c2
-rw-r--r--repl.c2
-rw-r--r--typecheck.c2
7 files changed, 32 insertions, 19 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index 87d1f0ad..4a7b5c3a 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -315,18 +315,20 @@ public Range_t Int$to(Int_t from, Int_t to) {
return (Range_t){from, to, Int$compare(&to, &from, &$Int) >= 0 ? (Int_t){.small=(1<<2)|1} : (Int_t){.small=(-1>>2)|1}};
}
-public Int_t Int$from_text(CORD text) {
+public Int_t Int$from_text(CORD text, bool *success) {
const char *str = CORD_to_const_char_star(text);
mpz_t i;
+ int result;
if (strncmp(str, "0x", 2) == 0) {
- mpz_init_set_str(i, str + 2, 16);
+ result = mpz_init_set_str(i, str + 2, 16);
} else if (strncmp(str, "0o", 2) == 0) {
- mpz_init_set_str(i, str + 2, 8);
+ result = mpz_init_set_str(i, str + 2, 8);
} else if (strncmp(str, "0b", 2) == 0) {
- mpz_init_set_str(i, str + 2, 2);
+ result = mpz_init_set_str(i, str + 2, 2);
} else {
- mpz_init_set_str(i, str, 10);
+ result = mpz_init_set_str(i, str, 10);
}
+ if (success) *success = (result == 0);
return Int$from_mpz(i);
}
diff --git a/builtins/integers.h b/builtins/integers.h
index b4cb2113..e5a662cc 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -82,7 +82,7 @@ CORD Int$octal(Int_t i, Int_t digits, bool prefix);
void Int$init_random(long seed);
Int_t Int$random(Int_t min, Int_t max);
Range_t Int$to(Int_t from, Int_t to);
-Int_t Int$from_text(CORD text);
+Int_t Int$from_text(CORD text, bool *success);
Int_t Int$abs(Int_t x);
Int_t Int$power(Int_t base, Int_t exponent);
Int_t Int$sqrt(Int_t i);
diff --git a/compile.c b/compile.c
index 219173b7..15045d6d 100644
--- a/compile.c
+++ b/compile.c
@@ -1314,7 +1314,7 @@ CORD compile_int_to_type(env_t *env, ast_t *ast, type_t *target)
}
int64_t target_bits = (int64_t)Match(target, IntType)->bits;
- Int_t int_val = Int$from_text(Match(ast, Int)->str);
+ Int_t int_val = Int$from_text(Match(ast, Int)->str, NULL);
mpz_t i;
mpz_init_set_int(i, int_val);
@@ -1354,7 +1354,7 @@ CORD compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t
if (spec_arg->type->tag == IntType && call_arg->value->tag == Int) {
value = compile_int_to_type(env, call_arg->value, spec_arg->type);
} else if (spec_arg->type->tag == NumType && call_arg->value->tag == Int) {
- Int_t int_val = Int$from_text(Match(call_arg->value, Int)->str);
+ Int_t int_val = Int$from_text(Match(call_arg->value, Int)->str, NULL);
double n = Int_to_Num(int_val);
value = CORD_asprintf(Match(spec_arg->type, NumType)->bits == TYPE_NBITS64
? "N64(%.20g)" : "N32(%.10g)", n);
@@ -1382,7 +1382,7 @@ CORD compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t
if (spec_arg->type->tag == IntType && call_arg->value->tag == Int) {
value = compile_int_to_type(env, call_arg->value, spec_arg->type);
} else if (spec_arg->type->tag == NumType && call_arg->value->tag == Int) {
- Int_t int_val = Int$from_text(Match(call_arg->value, Int)->str);
+ Int_t int_val = Int$from_text(Match(call_arg->value, Int)->str, NULL);
double n = Int_to_Num(int_val);
value = CORD_asprintf(Match(spec_arg->type, NumType)->bits == TYPE_NBITS64
? "N64(%.20g)" : "N32(%.10g)", n);
@@ -1513,7 +1513,7 @@ CORD compile(env_t *env, ast_t *ast)
}
case Int: {
const char *str = Match(ast, Int)->str;
- Int_t int_val = Int$from_text(str);
+ Int_t int_val = Int$from_text(str, NULL);
mpz_t i;
mpz_init_set_int(i, int_val);
@@ -1524,7 +1524,7 @@ CORD compile(env_t *env, ast_t *ast)
} else if (mpz_cmp_si(i, INT64_MAX) <= 0 && mpz_cmp_si(i, INT64_MIN) >= 0) {
return CORD_asprintf("Int64_to_Int(%s)", str);
} else {
- return CORD_asprintf("Int$from_text(\"%s\")", str);
+ return CORD_asprintf("Int$from_text(\"%s\", NULL)", str);
}
case IBITS64:
if ((mpz_cmp_si(i, INT64_MAX) < 0) && (mpz_cmp_si(i, INT64_MIN) > 0))
@@ -3037,7 +3037,18 @@ CORD compile_cli_arg_call(env_t *env, CORD fn_name, type_t *fn_type)
"}\n");
break;
}
- case IntType: case BigIntType: case NumType: {
+ case BigIntType: {
+ CORD type_name = type_to_cord(t);
+ code = CORD_all(code, "else if (pop_flag(argv, &i, \"", flag, "\", &flag)) {\n",
+ "if (flag == CORD_EMPTY)\n"
+ "USAGE_ERR(\"No value provided for '--", flag, "'\\n\", usage);\n"
+ "$", arg->name, " = ", type_name, "$from_text(flag, &", arg->name, "$is_set);\n"
+ "if (!", arg->name, "$is_set)\n"
+ "USAGE_ERR(\"Invalid value provided for '--", flag, "'\\n\", usage);\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",
"if (flag == CORD_EMPTY)\n"
diff --git a/environment.c b/environment.c
index 248588fd..f1d2a042 100644
--- a/environment.c
+++ b/environment.c
@@ -95,7 +95,7 @@ env_t *new_compilation_unit(CORD *libname)
{"clamped", "Int$clamped", "func(x:Int,low:Int,high:Int)->Int"},
{"divided_by", "Int$divided_by", "func(x:Int,y:Int)->Int"},
{"format", "Int$format", "func(i:Int, digits=0)->Text"},
- {"from_text", "Int$from_text", "func(text:Text, the_rest=!&Text)->Int"},
+ {"from_text", "Int$from_text", "func(text:Text, success=!&Bool)->Int"},
{"hex", "Int$hex", "func(i:Int, digits=0, uppercase=yes, prefix=yes)->Text"},
{"is_prime", "Int$is_prime", "func(x:Int,reps=50)->Bool"},
{"left_shifted", "Int$left_shifted", "func(x:Int,y:Int)->Int"},
@@ -137,7 +137,7 @@ env_t *new_compilation_unit(CORD *libname)
{"clamped", "Int32$clamped", "func(x:Int32,low:Int32,high:Int32)->Int32"},
{"divided_by", "Int32$divided_by", "func(x:Int32,y:Int32)->Int32"},
{"format", "Int32$format", "func(i:Int32, digits=0)->Text"},
- {"from_text", "Int$from_text", "func(text:Text, the_rest=!&Text)->Int32"},
+ {"from_text", "Int32$from_text", "func(text:Text, the_rest=!&Text)->Int32"},
{"hex", "Int32$hex", "func(i:Int32, digits=0, uppercase=yes, prefix=yes)->Text"},
{"max", "Int32$max", "Int32"},
{"min", "Int32$min", "Int32"},
@@ -153,7 +153,7 @@ env_t *new_compilation_unit(CORD *libname)
{"clamped", "Int16$clamped", "func(x:Int16,low:Int16,high:Int16)->Int16"},
{"divided_by", "Int16$divided_by", "func(x:Int16,y:Int16)->Int16"},
{"format", "Int16$format", "func(i:Int16, digits=0)->Text"},
- {"from_text", "Int$from_text", "func(text:Text, the_rest=!&Text)->Int16"},
+ {"from_text", "Int16$from_text", "func(text:Text, the_rest=!&Text)->Int16"},
{"hex", "Int16$hex", "func(i:Int16, digits=0, uppercase=yes, prefix=yes)->Text"},
{"max", "Int16$max", "Int16"},
{"min", "Int16$min", "Int16"},
@@ -169,7 +169,7 @@ env_t *new_compilation_unit(CORD *libname)
{"clamped", "Int8$clamped", "func(x:Int8,low:Int8,high:Int8)->Int8"},
{"divided_by", "Int8$divided_by", "func(x:Int8,y:Int8)->Int8"},
{"format", "Int8$format", "func(i:Int8, digits=0)->Text"},
- {"from_text", "Int$from_text", "func(text:Text, the_rest=!&Text)->Int8"},
+ {"from_text", "Int8$from_text", "func(text:Text, the_rest=!&Text)->Int8"},
{"hex", "Int8$hex", "func(i:Int8, digits=0, uppercase=yes, prefix=yes)->Text"},
{"max", "Int8$max", "Int8"},
{"min", "Int8$min", "Int8"},
diff --git a/parse.c b/parse.c
index 9d80cccb..95fc09ef 100644
--- a/parse.c
+++ b/parse.c
@@ -1894,7 +1894,7 @@ ast_t *parse_enum_def(parse_ctx_t *ctx, const char *pos) {
spaces(&pos);
if (match(&pos, "=")) {
ast_t *val = expect(ctx, tag_start, &pos, parse_int, "I expected an integer literal after this '='");
- Int_t i = Int$from_text(Match(val, Int)->str);
+ Int_t i = Int$from_text(Match(val, Int)->str, NULL);
// TODO check for overflow
next_value = (i.small >> 2);
}
diff --git a/repl.c b/repl.c
index 8ac4c7d5..a2b77a81 100644
--- a/repl.c
+++ b/repl.c
@@ -353,7 +353,7 @@ void eval(env_t *env, ast_t *ast, void *dest)
case Int: {
if (!dest) return;
switch (Match(ast, Int)->bits) {
- case 0: *(Int_t*)dest = Int$from_text(Match(ast, Int)->str); break;
+ case 0: *(Int_t*)dest = Int$from_text(Match(ast, Int)->str, NULL); break;
case 64: *(int64_t*)dest = Int64$from_text(Match(ast, Int)->str, NULL); break;
case 32: *(int32_t*)dest = Int32$from_text(Match(ast, Int)->str, NULL); break;
case 16: *(int16_t*)dest = Int16$from_text(Match(ast, Int)->str, NULL); break;
diff --git a/typecheck.c b/typecheck.c
index d8e0f6e1..bb40666f 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -1367,7 +1367,7 @@ bool is_constant(env_t *env, ast_t *ast)
case Int: {
auto info = Match(ast, Int);
if (info->bits == IBITS_UNSPECIFIED) {
- Int_t int_val = Int$from_text(info->str);
+ Int_t int_val = Int$from_text(info->str, NULL);
mpz_t i;
mpz_init_set_int(i, int_val);
return (mpz_cmpabs_ui(i, BIGGEST_SMALL_INT) <= 0);