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 Mar 3, 2024
1 parent 11bfa4d commit 1b9c1ef
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
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,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
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ from "runtime/atof/parse" include Parse as Atof
from "runtime/unsafe/tags" include Tags
from "runtime/exception" include 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 @@ -458,9 +466,10 @@ provide let isClose = (a, b, relativeTolerance=1e-9, absoluteTolerance=0.0) => {
*
* @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
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ No other changes yet.
from "number" 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 @@ -749,14 +733,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 @@ -778,7 +768,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 @@ -814,7 +804,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 @@ -830,7 +820,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.**asin**

Expand Down
32 changes: 21 additions & 11 deletions stdlib/runtime/atoi/parse.gr
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ from "runtime/numbers" include Numbers
use Numbers.{ 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) => {
}
use WasmI64.{ (*) }
// 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
Original file line number Diff line number Diff line change
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 1b9c1ef

Please sign in to comment.