Skip to content

Commit

Permalink
chore: Switch to using an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Aug 6, 2023
1 parent f76ab1c commit 4ceecb2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
3 changes: 3 additions & 0 deletions stdlib/number.gr
Expand Up @@ -30,6 +30,9 @@ include "runtime/atof/parse" as Atof
include "runtime/unsafe/tags"
include "runtime/exception"

from Atoi use { type ParseIntError }

provide { type ParseIntError }
/**
* Pi represented as a Number value.
*
Expand Down
25 changes: 21 additions & 4 deletions stdlib/number.md
Expand Up @@ -13,6 +13,22 @@ 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 @@ -709,7 +725,8 @@ No other changes yet.
</details>

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

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

|type|description|
|----|-----------|
|`Result<Number, Exception>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
|`Result<Number, Atoi.ParseIntError>`|`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 +784,7 @@ No other changes yet.
</details>

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

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

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

### Number.**sin**

Expand Down
11 changes: 8 additions & 3 deletions stdlib/runtime/atoi/parse.gr
Expand Up @@ -20,9 +20,14 @@ include "runtime/bigint" as BI
include "runtime/numbers"
from Numbers use { reducedInteger }

provide exception EmptyString
provide exception InvalidDigit
provide exception InvalidRadix
/**
* Represents an error that can occur when parsing ints.
*/
provide enum ParseIntError {
EmptyString,
InvalidDigit,
InvalidRadix,
}

primitive (&&) = "@and"
primitive (||) = "@or"
Expand Down
18 changes: 17 additions & 1 deletion stdlib/runtime/atoi/parse.md
Expand Up @@ -2,13 +2,29 @@
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, Exception>
parseInt : (string: String, radix: Number) => Result<Number, ParseIntError>
```

0 comments on commit 4ceecb2

Please sign in to comment.