From 80a09e6dba7042271cba5372e31c2e5e86e58215 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 2 Sep 2024 19:29:43 -0400 Subject: Fix up some integer and print statement stuff --- builtins/integers.c | 10 ++++++---- builtins/integers.h | 1 + compile.c | 14 +++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/builtins/integers.c b/builtins/integers.c index 45db160d..44ecaf91 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -345,8 +345,7 @@ 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(Text_t text, bool *success) { - const char *str = Text$as_c_string(text); +public Int_t Int$from_str(const char *str, bool *success) { mpz_t i; int result; if (strncmp(str, "0x", 2) == 0) { @@ -362,6 +361,10 @@ public Int_t Int$from_text(Text_t text, bool *success) { return Int$from_mpz(i); } +public Int_t Int$from_text(Text_t text, bool *success) { + return Int$from_str(Text$as_c_string(text), success); +} + public bool Int$is_prime(Int_t x, Int_t reps) { mpz_t p; @@ -406,8 +409,7 @@ public const TypeInfo $Int = { public Text_t KindOfInt ## $as_text(const c_type *i, bool colorize, const TypeInfo *type) { \ (void)type; \ if (!i) return Text$from_str(#KindOfInt); \ - Int_t as_int = KindOfInt##_to_Int(*i); \ - return Int$as_text(&as_int, colorize, type); \ + return Text$format(colorize ? "\x1b[35m%" fmt "\x1b[m" : "%" fmt, *i); \ } \ public int32_t KindOfInt ## $compare(const c_type *x, const c_type *y, const TypeInfo *type) { \ (void)type; \ diff --git a/builtins/integers.h b/builtins/integers.h index 359b1d57..6e2a1fe6 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -82,6 +82,7 @@ Text_t 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_str(const char *str, bool *success); Int_t Int$from_text(Text_t text, bool *success); Int_t Int$abs(Int_t x); Int_t Int$power(Int_t base, Int_t exponent); diff --git a/compile.c b/compile.c index 19a8b4c6..10a345ce 100644 --- a/compile.c +++ b/compile.c @@ -823,7 +823,7 @@ CORD compile_statement(env_t *env, ast_t *ast) if (!to_print) return CORD_EMPTY; - CORD code = "say(CORD_all("; + CORD code = "say(Text$concat("; for (ast_list_t *chunk = to_print; chunk; chunk = chunk->next) { if (chunk->ast->tag == TextLiteral) { code = CORD_cat(code, compile(env, chunk->ast)); @@ -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(Text$from_str(Match(ast, Int)->str), NULL); + Int_t int_val = Int$from_str(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(Text$from_str(Match(call_arg->value, Int)->str), NULL); + Int_t int_val = Int$from_str(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(Text$from_str(Match(call_arg->value, Int)->str), NULL); + Int_t int_val = Int$from_str(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(Text$from_str(str), NULL); + Int_t int_val = Int$from_str(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\", NULL)", str); + return CORD_asprintf("Int$from_str(\"%s\", NULL)", str); } case IBITS64: if ((mpz_cmp_si(i, INT64_MAX) < 0) && (mpz_cmp_si(i, INT64_MIN) > 0)) @@ -2629,7 +2629,7 @@ CORD compile(env_t *env, ast_t *ast) } else { empty = FakeAST( InlineCCode, - CORD_asprintf("fail_source(%s, %ld, %ld, \"This collection was empty!\");\n", + CORD_asprintf("fail_source(%r, %ld, %ld, \"This collection was empty!\");\n", CORD_quoted(ast->file->filename), (long)(reduction->iter->start - reduction->iter->file->text), (long)(reduction->iter->end - reduction->iter->file->text))); -- cgit v1.2.3