Skip to content

Commit

Permalink
chore(stdlib)!: Make parseInt use an exception in the err case
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Mar 17, 2023
1 parent d015a97 commit 2c2d161
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
8 changes: 4 additions & 4 deletions compiler/test/stdlib/number.test.gr
Expand Up @@ -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)
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions stdlib/number.md
Expand Up @@ -709,7 +709,7 @@ No other changes yet.
</details>

```grain
parseInt : (string: String, radix: Number) -> Result<Number, String>
parseInt : (string: String, radix: Number) -> Result<Number, Exception>
```

Parses a string representation of an integer into a `Number` using the
Expand All @@ -731,7 +731,7 @@ Returns:

|type|description|
|----|-----------|
|`Result<Number, String>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
|`Result<Number, Exception>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|

### Number.**parseFloat**

Expand Down Expand Up @@ -767,7 +767,7 @@ No other changes yet.
</details>

```grain
parse : (input: String) -> Result<Number, String>
parse : (input: String) -> Result<Number, Exception>
```

Parses a string representation of an integer, float, or rational into a `Number`.
Expand All @@ -783,7 +783,7 @@ Returns:

|type|description|
|----|-----------|
|`Result<Number, String>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
|`Result<Number, Exception>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|

### Number.**sin**

Expand Down
14 changes: 9 additions & 5 deletions stdlib/runtime/atoi/parse.gr
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/runtime/atoi/parse.md
Expand Up @@ -9,6 +9,6 @@ Functions and constants included in the Parse module.
### Parse.**parseInt**

```grain
parseInt : (string: String, radix: Number) -> Result<Number, String>
parseInt : (string: String, radix: Number) -> Result<Number, Exception>
```

0 comments on commit 2c2d161

Please sign in to comment.