From 2c2d161790228e739860805d0272336e093427db Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Fri, 17 Mar 2023 00:34:10 -0400 Subject: [PATCH] chore(stdlib)!: Make `parseInt` use an exception in the err case --- compiler/test/stdlib/number.test.gr | 8 ++++---- stdlib/number.md | 8 ++++---- stdlib/runtime/atoi/parse.gr | 14 +++++++++----- stdlib/runtime/atoi/parse.md | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/compiler/test/stdlib/number.test.gr b/compiler/test/stdlib/number.test.gr index fa2700d7f1..b974e1c85f 100644 --- a/compiler/test/stdlib/number.test.gr +++ b/compiler/test/stdlib/number.test.gr @@ -602,7 +602,7 @@ assert Result.isErr(Number.parseInt("10", 2/3)) // Number.parse // These tests primarily focus on rational parsing -assert Number.parse("") == Err("Invalid input") +assert Result.isErr(Number.parse("")) assert Number.parse("42") == Ok(42) assert Number.parse("123.45") == Ok(123.45) assert Number.parse("1/1") == Ok(1) @@ -614,9 +614,9 @@ assert Number.parse("-3/-9") == Ok(1/3) assert Number.parse("0x3/-9") == Ok(-1/3) assert Number.parse("3/-0x9") == Ok(-1/3) assert Number.parse("9223372036854775808/27_670_116_110_564_327_424") == Ok(1/3) -assert Number.parse("1/2/") == Err("Invalid digit in input") -assert Number.parse("1/") == Err("Invalid input") -assert Number.parse("1//") == Err("Invalid digit in input") +assert Result.isErr(Number.parse("1/2/")) +assert Result.isErr(Number.parse("1/")) +assert Result.isErr(Number.parse("1//")) // Number.sign assert Number.sign(-10000) == -1 diff --git a/stdlib/number.md b/stdlib/number.md index d96e63b26b..6902cf4d7d 100644 --- a/stdlib/number.md +++ b/stdlib/number.md @@ -709,7 +709,7 @@ No other changes yet. ```grain -parseInt : (string: String, radix: Number) -> Result +parseInt : (string: String, radix: Number) -> Result ``` Parses a string representation of an integer into a `Number` using the @@ -731,7 +731,7 @@ Returns: |type|description| |----|-----------| -|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise| +|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise| ### Number.**parseFloat** @@ -767,7 +767,7 @@ No other changes yet. ```grain -parse : (input: String) -> Result +parse : (input: String) -> Result ``` Parses a string representation of an integer, float, or rational into a `Number`. @@ -783,7 +783,7 @@ Returns: |type|description| |----|-----------| -|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise| +|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise| ### Number.**sin** diff --git a/stdlib/runtime/atoi/parse.gr b/stdlib/runtime/atoi/parse.gr index 886d64a3fa..47a884271b 100644 --- a/stdlib/runtime/atoi/parse.gr +++ b/stdlib/runtime/atoi/parse.gr @@ -20,6 +20,10 @@ include "runtime/bigint" as BI include "runtime/numbers" from Numbers use { reducedInteger } +provide exception EmptyString +provide exception InvalidDigit +provide exception InvalidRadix + primitive (&&) = "@and" primitive (||) = "@or" @@ -57,11 +61,11 @@ provide let parseInt = (string: String, radix: Number) => { radix < WasmI32.fromGrain(2) || radix > WasmI32.fromGrain(36) ) { - return Err("Radix must be an integer between 2 and 36") + return Err(InvalidRadix) } if (WasmI32.eqz(strLen)) { - return Err("Invalid input") + return Err(EmptyString) } let mut char = WasmI32.load8U(offset, 0n) @@ -122,12 +126,12 @@ provide let parseInt = (string: String, radix: Number) => { c when c - _CHAR_A < 26n => digit = char - _CHAR_A + 10n, c when c - _CHAR_a < 26n => digit = char - _CHAR_a + 10n, _ => { - return Err("Invalid digit in input") + return Err(InvalidDigit) }, } if (digit >= WasmI32.wrapI64(radix)) { - return Err("Invalid digit in input") + return Err(InvalidDigit) } let digit = WasmI64.extendI32U(digit) @@ -170,7 +174,7 @@ provide let parseInt = (string: String, radix: Number) => { } // TODO: Verify this is suitable for handling "_" - if (WasmI32.eqz(sawDigit)) return Err("Invalid digit in input") + if (WasmI32.eqz(sawDigit)) return Err(InvalidDigit) if (WasmI32.eqz(isBigInt)) { let value = if (negative) value else WasmI64.mul(value, -1N) diff --git a/stdlib/runtime/atoi/parse.md b/stdlib/runtime/atoi/parse.md index 9b3ca6c507..e86185b2e3 100644 --- a/stdlib/runtime/atoi/parse.md +++ b/stdlib/runtime/atoi/parse.md @@ -9,6 +9,6 @@ Functions and constants included in the Parse module. ### Parse.**parseInt** ```grain -parseInt : (string: String, radix: Number) -> Result +parseInt : (string: String, radix: Number) -> Result ```