Skip to content

Commit

Permalink
chore: Switch back to an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Jan 31, 2024
1 parent 2b69ca9 commit 71f0002
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 55 deletions.
12 changes: 12 additions & 0 deletions compiler/test/stdlib/number.test.gr
Expand Up @@ -600,6 +600,18 @@ assert Result.isErr(Number.parseInt("zzzzz", 37))
assert Result.isErr(Number.parseInt("zzzzz", 9223372036854775807))
assert Result.isErr(Number.parseInt("10", 1.23))
assert Result.isErr(Number.parseInt("10", 2/3))
assert match (Number.parseInt("zzzzz", 10)) {
Err(Number.PraseIntInvalidDigit) => true,
_ => false,
}
assert match (Number.parseInt("", 10)) {
Err(Number.PraseIntEmptyString) => true,
_ => false,
}
assert match (Number.parseInt("10", 2/3)) {
Err(Number.ParseIntInvalidRadix) => true,
_ => false,
}

// Number.parse
// These tests primarily focus on rational parsing
Expand Down
15 changes: 12 additions & 3 deletions stdlib/number.gr
Expand Up @@ -30,9 +30,17 @@ include "runtime/atof/parse" as Atof
include "runtime/unsafe/tags"
include "runtime/exception"

from Atoi use { type ParseIntError }
from Atoi use {
exception PraseIntEmptyString,
exception PraseIntInvalidDigit,
exception ParseIntInvalidRadix,
}

provide { type ParseIntError }
provide {
exception PraseIntEmptyString,
exception PraseIntInvalidDigit,
exception ParseIntInvalidRadix,
}
/**
* Pi represented as a Number value.
*
Expand Down Expand Up @@ -423,9 +431,10 @@ provide let isInfinite = (x: Number) => {
*
* @param string: The string to parse
* @param radix: The number system base to use when parsing the input string
* @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise
* @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing a parseInt exception
*
* @since v0.4.5
* @history v0.6.0: Switched from a string based error message to a custom exception
*/
provide let parseInt = Atoi.parseInt

Expand Down
38 changes: 14 additions & 24 deletions stdlib/number.md
Expand Up @@ -13,22 +13,6 @@ No other changes yet.
include "number"
```

## Types

Type declarations included in the Number module.

### Number.**ParseIntError**

```grain
enum ParseIntError {
EmptyString,
InvalidDigit,
InvalidRadix,
}
```

Represents an error that can occur when parsing ints.

## Values

Functions and constants included in the Number module.
Expand Down Expand Up @@ -719,14 +703,20 @@ Returns:

### Number.**parseInt**

<details disabled>
<summary tabindex="-1">Added in <code>0.4.5</code></summary>
No other changes yet.
<details>
<summary>Added in <code>0.4.5</code></summary>
<table>
<thead>
<tr><th>version</th><th>changes</th></tr>
</thead>
<tbody>
<tr><td><code>next</code></td><td>Switched from a string based error message to a custom exception</td></tr>
</tbody>
</table>
</details>

```grain
parseInt :
(string: String, radix: Number) => Result<Number, Atoi.ParseIntError>
parseInt : (string: String, radix: Number) => Result<Number, Exception>
```

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

|type|description|
|----|-----------|
|`Result<Number, Atoi.ParseIntError>`|`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 a parseInt exception|

### Number.**parseFloat**

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

```grain
parse : (input: String) => Result<Number, Atoi.ParseIntError>
parse : (input: String) => Result<Number, Exception>
```

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

|type|description|
|----|-----------|
|`Result<Number, Atoi.ParseIntError>`|`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
32 changes: 21 additions & 11 deletions stdlib/runtime/atoi/parse.gr
Expand Up @@ -21,13 +21,23 @@ include "runtime/numbers"
from Numbers use { reducedInteger }

/**
* Represents an error that can occur when parsing ints.
* Represents an error caused by trying to parse an empty string.
*
* @since v0.6.0
*/
provide enum ParseIntError {
EmptyString,
InvalidDigit,
InvalidRadix,
}
provide exception PraseIntEmptyString
/**
* Represents an error caused by trying to parse a string with an invalid character.
*
* @since v0.6.0
*/
provide exception PraseIntInvalidDigit
/**
* Represents an error caused by trying to parse with an invalid radix.
*
* @since v0.6.0
*/
provide exception ParseIntInvalidRadix

primitive (&&) = "@and"
primitive (||) = "@or"
Expand Down Expand Up @@ -67,11 +77,11 @@ provide let parseInt = (string: String, radix: Number) => {
radix < WasmI32.fromGrain(2) ||
radix > WasmI32.fromGrain(36)
) {
return Err(InvalidRadix)
return Err(ParseIntInvalidRadix)
}

if (WasmI32.eqz(strLen)) {
return Err(EmptyString)
return Err(PraseIntEmptyString)
}

let mut char = WasmI32.load8U(offset, 0n)
Expand Down Expand Up @@ -132,12 +142,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(InvalidDigit)
return Err(PraseIntInvalidDigit)
},
}

if (digit >= WasmI32.wrapI64(radix)) {
return Err(InvalidDigit)
return Err(PraseIntInvalidDigit)
}

let digit = WasmI64.extendI32U(digit)
Expand Down Expand Up @@ -183,7 +193,7 @@ provide let parseInt = (string: String, radix: Number) => {
}
from WasmI64 use { (*) }
// TODO: Verify this is suitable for handling "_"
if (WasmI32.eqz(sawDigit)) return Err(InvalidDigit)
if (WasmI32.eqz(sawDigit)) return Err(PraseIntInvalidDigit)

if (WasmI32.eqz(isBigInt)) {
let value = if (negative) value else value * -1N
Expand Down
18 changes: 1 addition & 17 deletions stdlib/runtime/atoi/parse.md
Expand Up @@ -2,29 +2,13 @@
title: Parse
---

## Types

Type declarations included in the Parse module.

### Parse.**ParseIntError**

```grain
enum ParseIntError {
EmptyString,
InvalidDigit,
InvalidRadix,
}
```

Represents an error that can occur when parsing ints.

## Values

Functions and constants included in the Parse module.

### Parse.**parseInt**

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

0 comments on commit 71f0002

Please sign in to comment.