From bd111dbe2ea1718dd541f347ce9c0b08068cc544 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 12 Sep 2024 01:24:26 -0400 Subject: [PATCH] Automatically interpolate ints for pattern/shell/path --- builtins/integers.c | 26 ++++++++++++++------------ builtins/integers.h | 1 + environment.c | 3 +++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/builtins/integers.c b/builtins/integers.c index 9160bfc..cbdf6f5 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -17,24 +17,27 @@ static gmp_randstate_t Int_rng = {}; -public void Int$init_random(long seed) -{ +public void Int$init_random(long seed) { gmp_randinit_default(Int_rng); gmp_randseed_ui(Int_rng, (unsigned long)seed); } +public Text_t Int$value_as_text(Int_t i) { + if (__builtin_expect(i.small & 1, 1)) { + return Text$format("%ld", (i.small)>>2); + } else { + char *str = mpz_get_str(NULL, 10, *i.big); + return Text$from_str(str); + } +} + public Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type) { (void)type; if (!i) return Text("Int"); - if (__builtin_expect(i->small & 1, 1)) { - return Text$format(colorize ? "\x1b[35m%ld\x1b[m" : "%ld", (i->small)>>2); - } else { - char *str = mpz_get_str(NULL, 10, *i->big); - Text_t text = Text$from_str(str); - if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m")); - return text; - } + Text_t text = Int$value_as_text(*i); + if (colorize) text = Text$concat(Text("\x1b[35m"), text, Text("\x1b[m")); + return text; } public PUREFUNC int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type) { @@ -70,8 +73,7 @@ public PUREFUNC uint64_t Int$hash(const Int_t *x, const TypeInfo *type) { } } -public Text_t Int$format(Int_t i, Int_t digits_int) -{ +public Text_t Int$format(Int_t i, Int_t digits_int) { int64_t digits = Int_to_Int64(digits_int, false); if (__builtin_expect(i.small & 1, 1)) { return Text$format("%0.*ld", digits, (i.small)>>2); diff --git a/builtins/integers.h b/builtins/integers.h index 4a6416c..641d4e2 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -82,6 +82,7 @@ DEFINE_INT_TYPE(int8_t, Int8) #define OptionalInt_t Int_t Text_t Int$as_text(const Int_t *i, bool colorize, const TypeInfo *type); +Text_t Int$value_as_text(Int_t i); PUREFUNC uint64_t Int$hash(const Int_t *x, const TypeInfo *type); PUREFUNC int32_t Int$compare(const Int_t *x, const Int_t *y, const TypeInfo *type); PUREFUNC int32_t Int$compare_value(const Int_t x, const Int_t y); diff --git a/environment.c b/environment.c index 324021b..b262a8e 100644 --- a/environment.c +++ b/environment.c @@ -257,6 +257,7 @@ env_t *new_compilation_unit(CORD *libname) )}, {"NextLine", next_line_type, "NextLine_t", "NextLine", {}}, {"Pattern", Type(TextType, .lang="Pattern", .env=namespace_env(env, "Pattern")), "Pattern_t", "Pattern$info", TypedArray(ns_entry_t, + {"escape_int", "Int$value_as_text", "func(i:Int)->Pattern"}, {"escape_text", "Pattern$escape_text", "func(text:Text)->Pattern"}, )}, {"Path", Type(TextType, .lang="Path", .env=namespace_env(env, "Path")), "Text_t", "Text$info", TypedArray(ns_entry_t, @@ -265,6 +266,7 @@ env_t *new_compilation_unit(CORD *libname) {"by_line", "Path$by_line", "func(path:Path)->func()->NextLine"}, {"children", "Path$children", "func(path:Path, include_hidden=no)->[Path]"}, {"create_directory", "Path$create_directory", "func(path:Path, permissions=0o644_i32)"}, + {"escape_int", "Int$value_as_text", "func(i:Int)->Path"}, {"escape_path", "Path$escape_path", "func(path:Path)->Path"}, {"escape_text", "Path$escape_text", "func(text:Text)->Path"}, {"exists", "Path$exists", "func(path:Path)->Bool"}, @@ -286,6 +288,7 @@ env_t *new_compilation_unit(CORD *libname) {"write_unique", "Path$write_unique", "func(path:Path, text:Text)->Path"}, )}, {"Shell", Type(TextType, .lang="Shell", .env=namespace_env(env, "Shell")), "Shell_t", "Shell$info", TypedArray(ns_entry_t, + {"escape_int", "Int$value_as_text", "func(i:Int)->Shell"}, {"escape_text", "Shell$escape_text", "func(text:Text)->Shell"}, {"run", "Shell$run", "func(command:Shell, status=!&Int32)->Text"}, )},