Automatically interpolate ints for pattern/shell/path

This commit is contained in:
Bruce Hill 2024-09-12 01:24:26 -04:00
parent 4e4e397e4c
commit bd111dbe2e
3 changed files with 18 additions and 12 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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"},
)},