From 7a4f2e73addf6dfcde2a6b17b62b961608e556a0 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 9 Nov 2024 15:11:11 -0500 Subject: Rename `from_text()` to `parse()` --- compile.c | 2 +- docs/booleans.md | 10 +++++----- docs/integers.md | 12 ++++++------ docs/nums.md | 8 ++++---- docs/text.md | 2 +- environment.c | 16 ++++++++-------- examples/base64/base64.tm | 4 ++-- repl.c | 2 +- stdlib/bools.c | 2 +- stdlib/bools.h | 2 +- stdlib/integers.c | 6 +++--- stdlib/integers.h | 4 ++-- stdlib/nums.c | 4 ++-- stdlib/nums.h | 4 ++-- stdlib/stdlib.c | 14 +++++++------- typecheck.c | 2 +- 16 files changed, 47 insertions(+), 47 deletions(-) diff --git a/compile.c b/compile.c index 372cd91d..081e5d69 100644 --- a/compile.c +++ b/compile.c @@ -875,7 +875,7 @@ CORD compile_statement(env_t *env, ast_t *ast) "}\n"); env->code->funcs = CORD_cat(env->code->funcs, wrapper); } else if (fndef->cache && fndef->cache->tag == Int) { - OptionalInt64_t cache_size = Int64$from_text(Text$from_str(Match(fndef->cache, Int)->str)); + OptionalInt64_t cache_size = Int64$parse(Text$from_str(Match(fndef->cache, Int)->str)); const char *arg_type_name = heap_strf("%s$args", Match(fndef->name, Var)->name); ast_t *args_def = FakeAST(StructDef, .name=arg_type_name, .fields=fndef->args); prebind_statement(env, args_def); diff --git a/docs/booleans.md b/docs/booleans.md index 4f44931a..80de6fa6 100644 --- a/docs/booleans.md +++ b/docs/booleans.md @@ -7,7 +7,7 @@ Boolean values have the type `Bool` and can be either `yes` ("true") or `no` This documentation provides details on boolean functions available in the API. -## `from_text` +## `parse` **Description:** Converts a string representation of a boolean value into a boolean. Acceptable @@ -16,7 +16,7 @@ boolean values are case-insensitive variations of `yes`/`no`, `y`/`n`, **Signature:** ```tomo -func from_text(text: Text -> Bool?) +func parse(text: Text -> Bool?) ``` **Parameters:** @@ -28,10 +28,10 @@ func from_text(text: Text -> Bool?) **Example:** ```tomo ->> Bool.from_text("yes") +>> Bool.parse("yes") = yes? ->> Bool.from_text("no") +>> Bool.parse("no") = no? ->> Bool.from_text("???") +>> Bool.parse("???") = !Bool ``` diff --git a/docs/integers.md b/docs/integers.md index 16728c44..f5e6838b 100644 --- a/docs/integers.md +++ b/docs/integers.md @@ -154,14 +154,14 @@ The octal string representation of the integer. --- -## `from_text` +## `parse` **Description:** Converts a text representation of an integer into an integer. **Signature:** ```tomo -func from_text(text: Text -> Int?) +func parse(text: Text -> Int?) ``` **Parameters:** @@ -175,17 +175,17 @@ a null value will be returned. **Example:** ```tomo ->> Int.from_text("123") +>> Int.parse("123") = 123? ->> Int.from_text("0xFF") +>> Int.parse("0xFF") = 255? # Can't parse: ->> Int.from_text("asdf") +>> Int.parse("asdf") = !Int # Outside valid range: ->> Int8.from_text("9999999") +>> Int8.parse("9999999") = !Int ``` diff --git a/docs/nums.md b/docs/nums.md index 63fae27f..c7171f37 100644 --- a/docs/nums.md +++ b/docs/nums.md @@ -568,14 +568,14 @@ A text representation of the number with the specified precision. --- -### `from_text` +### `parse` **Description:** Converts a text representation of a number into a floating-point number. **Signature:** ```tomo -func from_text(text: Text -> Num?) +func parse(text: Text -> Num?) ``` **Parameters:** @@ -587,9 +587,9 @@ The number represented by the text or a null value if the entire text can't be p **Example:** ```tomo ->> Num.from_text("3.14") +>> Num.parse("3.14") = 3.14 ->> Num.from_text("1e3") +>> Num.parse("1e3") = 1000 ``` diff --git a/docs/text.md b/docs/text.md index eab07a9e..b0d704b6 100644 --- a/docs/text.md +++ b/docs/text.md @@ -886,7 +886,7 @@ function to each. ```tomo >> "hello world":map($/world/, Text.upper) = "hello WORLD" ->> "Some nums: 1 2 3 4":map($/{int}/, func(i:Text): "$(Int.from_text(i) + 10)") +>> "Some nums: 1 2 3 4":map($/{int}/, func(i:Text): "$(Int.parse(i)! + 10)") = "Some nums: 11 12 13 14" ``` diff --git a/environment.c b/environment.c index b7b33f04..64b8a35b 100644 --- a/environment.c +++ b/environment.c @@ -97,7 +97,7 @@ env_t *new_compilation_unit(CORD libname) {"Void", Type(VoidType), "Void_t", "Void$info", {}}, {"Memory", Type(MemoryType), "Memory_t", "Memory$info", {}}, {"Bool", Type(BoolType), "Bool_t", "Bool$info", TypedArray(ns_entry_t, - {"from_text", "Bool$from_text", "func(text:Text -> Bool?)"}, + {"parse", "Bool$parse", "func(text:Text -> Bool?)"}, )}, {"Byte", Type(ByteType), "Byte_t", "Byte$info", TypedArray(ns_entry_t, {"max", "Byte$max", "Byte"}, @@ -112,7 +112,7 @@ env_t *new_compilation_unit(CORD libname) {"clamped", "Int$clamped", "func(x,low,high:Int -> Int)"}, {"divided_by", "Int$divided_by", "func(x,y:Int -> Int)"}, {"format", "Int$format", "func(i:Int, digits=0 -> Text)"}, - {"from_text", "Int$from_text", "func(text:Text -> Int?)"}, + {"parse", "Int$parse", "func(text:Text -> 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,y:Int -> Int)"}, @@ -137,7 +137,7 @@ env_t *new_compilation_unit(CORD libname) {"clamped", "Int64$clamped", "func(x,low,high:Int64 -> Int64)"}, {"divided_by", "Int64$divided_by", "func(x,y:Int64 -> Int64)"}, {"format", "Int64$format", "func(i:Int64, digits=0 -> Text)"}, - {"from_text", "Int64$from_text", "func(text:Text -> Int64?)"}, + {"parse", "Int64$parse", "func(text:Text -> Int64?)"}, {"hex", "Int64$hex", "func(i:Int64, digits=0, uppercase=yes, prefix=yes -> Text)"}, {"max", "Int64$max", "Int64"}, {"min", "Int64$min", "Int64"}, @@ -156,7 +156,7 @@ env_t *new_compilation_unit(CORD libname) {"clamped", "Int32$clamped", "func(x,low,high:Int32 -> Int32)"}, {"divided_by", "Int32$divided_by", "func(x,y:Int32 -> Int32)"}, {"format", "Int32$format", "func(i:Int32, digits=0 -> Text)"}, - {"from_text", "Int32$from_text", "func(text:Text -> Int32?)"}, + {"parse", "Int32$parse", "func(text:Text -> Int32?)"}, {"hex", "Int32$hex", "func(i:Int32, digits=0, uppercase=yes, prefix=yes -> Text)"}, {"max", "Int32$max", "Int32"}, {"min", "Int32$min", "Int32"}, @@ -175,7 +175,7 @@ env_t *new_compilation_unit(CORD libname) {"clamped", "Int16$clamped", "func(x,low,high:Int16 -> Int16)"}, {"divided_by", "Int16$divided_by", "func(x,y:Int16 -> Int16)"}, {"format", "Int16$format", "func(i:Int16, digits=0 -> Text)"}, - {"from_text", "Int16$from_text", "func(text:Text -> Int16?)"}, + {"parse", "Int16$parse", "func(text:Text -> Int16?)"}, {"hex", "Int16$hex", "func(i:Int16, digits=0, uppercase=yes, prefix=yes -> Text)"}, {"max", "Int16$max", "Int16"}, {"min", "Int16$min", "Int16"}, @@ -194,7 +194,7 @@ env_t *new_compilation_unit(CORD libname) {"clamped", "Int8$clamped", "func(x,low,high:Int8 -> Int8)"}, {"divided_by", "Int8$divided_by", "func(x,y:Int8 -> Int8)"}, {"format", "Int8$format", "func(i:Int8, digits=0 -> Text)"}, - {"from_text", "Int8$from_text", "func(text:Text -> Int8?)"}, + {"parse", "Int8$parse", "func(text:Text -> Int8?)"}, {"hex", "Int8$hex", "func(i:Int8, digits=0, uppercase=yes, prefix=yes -> Text)"}, {"max", "Int8$max", "Int8"}, {"min", "Int8$min", "Int8"}, @@ -224,7 +224,7 @@ env_t *new_compilation_unit(CORD libname) {"INF", "(Num_t)(INFINITY)", "Num"}, {"TAU", "(Num_t)(2.*M_PI)", "Num"}, {"mix", "Num$mix", "func(amount,x,y:Num -> Num)"}, - {"from_text", "Num$from_text", "func(text:Text -> Num?)"}, + {"parse", "Num$parse", "func(text:Text -> Num?)"}, {"abs", "fabs", "func(n:Num -> Num)"}, F(acos), F(acosh), F(asin), F(asinh), F(atan), F(atanh), F(cbrt), F(ceil), F(cos), F(cosh), F(erf), F(erfc), F(exp), F(exp2), F(expm1), F(floor), F(j0), F(j1), F(log), F(log10), F(log1p), F(log2), F(logb), @@ -252,7 +252,7 @@ env_t *new_compilation_unit(CORD libname) {"INF", "(Num32_t)(INFINITY)", "Num32"}, {"TAU", "(Num32_t)(2.f*M_PI)", "Num32"}, {"mix", "Num32$mix", "func(amount,x,y:Num32 -> Num32)"}, - {"from_text", "Num32$from_text", "func(text:Text -> Num32?)"}, + {"parse", "Num32$parse", "func(text:Text -> Num32?)"}, {"abs", "fabsf", "func(n:Num32 -> Num32)"}, F(acos), F(acosh), F(asin), F(asinh), F(atan), F(atanh), F(cbrt), F(ceil), F(cos), F(cosh), F(erf), F(erfc), F(exp), F(exp2), F(expm1), F(floor), F(j0), F(j1), F(log), F(log10), F(log1p), F(log2), F(logb), diff --git a/examples/base64/base64.tm b/examples/base64/base64.tm index 698c6bce..2568a1d1 100644 --- a/examples/base64/base64.tm +++ b/examples/base64/base64.tm @@ -25,7 +25,7 @@ _dec := [ ] lang Base64: - func from_text(text:Text -> Base64?): + func parse(text:Text -> Base64?): return Base64.from_bytes(text:utf8_bytes()) func from_bytes(bytes:[Byte] -> Base64?): @@ -94,4 +94,4 @@ func main(input=(/dev/stdin), decode=no): say(b:decode_text()!) else: text := input:read()! - say(Base64.from_text(text)!.text_content) + say(Base64.parse(text)!.text_content) diff --git a/repl.c b/repl.c index 869512f1..4a5d6644 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; - *(Int_t*)dest = Int$from_text(Text$from_str(Match(ast, Int)->str)); break; + *(Int_t*)dest = Int$parse(Text$from_str(Match(ast, Int)->str)); break; break; } case Num: { diff --git a/stdlib/bools.c b/stdlib/bools.c index bc1a6445..9a2c8cd6 100644 --- a/stdlib/bools.c +++ b/stdlib/bools.c @@ -22,7 +22,7 @@ PUREFUNC public Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo return *b ? Text("yes") : Text("no"); } -PUREFUNC public OptionalBool_t Bool$from_text(Text_t text) +PUREFUNC public OptionalBool_t Bool$parse(Text_t text) { if (Text$equal_ignoring_case(text, Text("yes")) || Text$equal_ignoring_case(text, Text("on")) diff --git a/stdlib/bools.h b/stdlib/bools.h index fbc40f8b..7dec5711 100644 --- a/stdlib/bools.h +++ b/stdlib/bools.h @@ -14,7 +14,7 @@ #define no (Bool_t)false PUREFUNC Text_t Bool$as_text(const bool *b, bool colorize, const TypeInfo_t *type); -OptionalBool_t Bool$from_text(Text_t text); +OptionalBool_t Bool$parse(Text_t text); extern const TypeInfo_t Bool$info; diff --git a/stdlib/integers.c b/stdlib/integers.c index 9bde1890..d0cc6617 100644 --- a/stdlib/integers.c +++ b/stdlib/integers.c @@ -330,7 +330,7 @@ public Int_t Int$from_str(const char *str) { return Int$from_mpz(i); } -public OptionalInt_t Int$from_text(Text_t text) { +public OptionalInt_t Int$parse(Text_t text) { return Int$from_str(Text$as_c_string(text)); } @@ -411,8 +411,8 @@ public const TypeInfo_t Int$info = { public to_attr Range_t KindOfInt ## $to(c_type from, c_type to) { \ return (Range_t){Int64_to_Int(from), Int64_to_Int(to), to >= from ? (Int_t){.small=(1<<2)&1} : (Int_t){.small=(1<<2)&1}}; \ } \ - public PUREFUNC Optional ## KindOfInt ## _t KindOfInt ## $from_text(Text_t text) { \ - OptionalInt_t full_int = Int$from_text(text); \ + public PUREFUNC Optional ## KindOfInt ## _t KindOfInt ## $parse(Text_t text) { \ + OptionalInt_t full_int = Int$parse(text); \ if (full_int.small == 0) return (Optional ## KindOfInt ## _t){.is_null=true}; \ if (Int$compare_value(full_int, I(min_val)) < 0) { \ return (Optional ## KindOfInt ## _t){.is_null=true}; \ diff --git a/stdlib/integers.h b/stdlib/integers.h index b441e2be..430a3eac 100644 --- a/stdlib/integers.h +++ b/stdlib/integers.h @@ -35,7 +35,7 @@ Text_t type_name ## $octal(c_type i, Int_t digits, bool prefix); \ Array_t type_name ## $bits(c_type x); \ to_attr Range_t type_name ## $to(c_type from, c_type to); \ - PUREFUNC Optional ## type_name ## _t type_name ## $from_text(Text_t text); \ + PUREFUNC Optional ## type_name ## _t type_name ## $parse(Text_t text); \ MACROLIKE PUREFUNC c_type type_name ## $clamped(c_type x, c_type min, c_type max) { \ return x < min ? min : (x > max ? max : x); \ } \ @@ -103,7 +103,7 @@ Text_t Int$hex(Int_t i, Int_t digits, bool uppercase, bool prefix); Text_t Int$octal(Int_t i, Int_t digits, bool prefix); PUREFUNC Range_t Int$to(Int_t from, Int_t to); OptionalInt_t Int$from_str(const char *str); -OptionalInt_t Int$from_text(Text_t text); +OptionalInt_t Int$parse(Text_t text); 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/stdlib/nums.c b/stdlib/nums.c index 30973fbc..8a210d7e 100644 --- a/stdlib/nums.c +++ b/stdlib/nums.c @@ -61,7 +61,7 @@ public CONSTFUNC double Num$mix(double amount, double x, double y) { return (1.0-amount)*x + amount*y; } -public OptionalNum_t Num$from_text(Text_t text) { +public OptionalNum_t Num$parse(Text_t text) { const char *str = Text$as_c_string(text); char *end = NULL; double d = strtod(str, &end); @@ -138,7 +138,7 @@ public CONSTFUNC float Num32$mix(float amount, float x, float y) { return (1.0f-amount)*x + amount*y; } -public OptionalNum32_t Num32$from_text(Text_t text) { +public OptionalNum32_t Num32$parse(Text_t text) { const char *str = Text$as_c_string(text); char *end = NULL; double d = strtod(str, &end); diff --git a/stdlib/nums.h b/stdlib/nums.h index a07a2710..dc7f9ff8 100644 --- a/stdlib/nums.h +++ b/stdlib/nums.h @@ -28,7 +28,7 @@ CONSTFUNC bool Num$finite(double n); CONSTFUNC bool Num$isnan(double n); double Num$nan(Text_t tag); CONSTFUNC double Num$mix(double amount, double x, double y); -OptionalNum_t Num$from_text(Text_t text); +OptionalNum_t Num$parse(Text_t text); MACROLIKE CONSTFUNC double Num$clamped(double x, double low, double high) { return (x <= low) ? low : (x >= high ? high : x); } @@ -45,7 +45,7 @@ CONSTFUNC bool Num32$isinf(float n); CONSTFUNC bool Num32$finite(float n); CONSTFUNC bool Num32$isnan(float n); CONSTFUNC float Num32$mix(float amount, float x, float y); -OptionalNum32_t Num32$from_text(Text_t text); +OptionalNum32_t Num32$parse(Text_t text); float Num32$nan(Text_t tag); MACROLIKE CONSTFUNC float Num32$clamped(float x, float low, float high) { return (x <= low) ? low : (x >= high ? high : x); diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c index f2c6897c..eb285316 100644 --- a/stdlib/stdlib.c +++ b/stdlib/stdlib.c @@ -57,37 +57,37 @@ static bool parse_single_arg(const TypeInfo_t *info, char *arg, void *dest) *(OptionalInt_t*)dest = parsed; return parsed.small != 0; } else if (info == &Int64$info) { - OptionalInt64_t parsed = Int64$from_text(Text$from_str(arg)); + OptionalInt64_t parsed = Int64$parse(Text$from_str(arg)); if (!parsed.is_null) *(OptionalInt64_t*)dest = parsed; return !parsed.is_null; } else if (info == &Int32$info) { - OptionalInt32_t parsed = Int32$from_text(Text$from_str(arg)); + OptionalInt32_t parsed = Int32$parse(Text$from_str(arg)); if (!parsed.is_null) *(OptionalInt32_t*)dest = parsed; return !parsed.is_null; } else if (info == &Int16$info) { - OptionalInt16_t parsed = Int16$from_text(Text$from_str(arg)); + OptionalInt16_t parsed = Int16$parse(Text$from_str(arg)); if (!parsed.is_null) *(OptionalInt16_t*)dest = parsed; return !parsed.is_null; } else if (info == &Int8$info) { - OptionalInt8_t parsed = Int8$from_text(Text$from_str(arg)); + OptionalInt8_t parsed = Int8$parse(Text$from_str(arg)); if (!parsed.is_null) *(OptionalInt8_t*)dest = parsed; return !parsed.is_null; } else if (info == &Bool$info) { - OptionalBool_t parsed = Bool$from_text(Text$from_str(arg)); + OptionalBool_t parsed = Bool$parse(Text$from_str(arg)); if (parsed != NULL_BOOL) *(OptionalBool_t*)dest = parsed; return parsed != NULL_BOOL; } else if (info == &Num$info) { - OptionalNum_t parsed = Num$from_text(Text$from_str(arg)); + OptionalNum_t parsed = Num$parse(Text$from_str(arg)); if (!isnan(parsed)) *(OptionalNum_t*)dest = parsed; return !isnan(parsed); } else if (info == &Num32$info) { - OptionalNum32_t parsed = Num32$from_text(Text$from_str(arg)); + OptionalNum32_t parsed = Num32$parse(Text$from_str(arg)); if (!isnan(parsed)) *(OptionalNum32_t*)dest = parsed; return !isnan(parsed); diff --git a/typecheck.c b/typecheck.c index a000393e..f844d30d 100644 --- a/typecheck.c +++ b/typecheck.c @@ -1275,7 +1275,7 @@ PUREFUNC bool is_constant(env_t *env, ast_t *ast) case Bool: case Num: case Null: return true; case Int: { auto info = Match(ast, Int); - Int_t int_val = Int$from_text(Text$from_str(info->str)); + Int_t int_val = Int$parse(Text$from_str(info->str)); if (int_val.small == 0) return false; // Failed to parse return (Int$compare_value(int_val, I(BIGGEST_SMALL_INT)) <= 0); } -- cgit v1.2.3