aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2023-11-05 03:13:18 +0000
committerJJ2023-11-05 03:13:18 +0000
commit7f8c5a1de3ec14d3f517bcb7d082f0ed0bb02eab (patch)
tree02eac6bbf5d67fb52186f3f5c5827472c2c4b0cd
parent89955d6c0f78fe2f092eba2b402c0758efe21ace (diff)
std: minor updates, consistently use Error and Err
-rw-r--r--README.md14
-rw-r--r--std/default/format.pk4
-rw-r--r--std/default/options.pk6
-rw-r--r--std/default/results.pk16
4 files changed, 27 insertions, 13 deletions
diff --git a/README.md b/README.md
index e6b5cb1..44e7e3d 100644
--- a/README.md
+++ b/README.md
@@ -8,9 +8,9 @@ It aims to be clean and succinct while performant: inspired by the syntax and me
```nim
import std/tables
-mod Ast:
+mod ast:
pub type Value = interface
- func repr(self: Self): string
+ repr(Self): string
pub type Ident = string
pub type Expr = ref union
Literal: ref Value
@@ -20,19 +20,19 @@ mod Ast:
Conditional: struct
cond, then_branch, else_branch: Expr
- pub func eval(expr: Expr, context: var HashTable[Ident, Value]): Result[Value]
+ pub func eval(expr: Expr, context: var HashTable[Ident, Value]): Result[Value, ref Err]
match expr
of Literal(value):
Okay(value)
of Variable(ident):
context.get(ident)
- of Application{body, arg}:
- if body of Abstraction{param, body as inner_body}:
+ of Application(body, arg):
+ if body of Abstraction(param, body as inner_body):
context.set(param, eval(arg))
inner_body.eval(context)
else:
Error(InvalidExpr)
- of Conditional{cond, then_branch, else_branch}:
+ of Conditional(cond, then_branch, else_branch):
if eval(cond, context):
then_case.eval(context)
else:
@@ -40,7 +40,7 @@ mod Ast:
of _:
Error(InvalidExpr)
-Ast.eval("(λx.x) 413".parse(), HashTable.init())
+ast.eval("(λx.x) 413".parse(), HashTable.init())
```
## Why Puck?
diff --git a/std/default/format.pk b/std/default/format.pk
index ec573ef..ee8997d 100644
--- a/std/default/format.pk
+++ b/std/default/format.pk
@@ -7,13 +7,13 @@ pub type Display = interface
str(Self): str
dbg(Self): str
-## The Debug interface. Broadly implemented for every type with magic,
+## The Debug interface. Broadly implemented for every type with compiler magic,
## types can (and should) override the generic implementations.
pub type Debug = interface
dbg(Self): str
## Prints all of its arguments to the command line.
-pub proc print(params: varargs[Display]) =
+pub func print(params: varargs[Display]) =
stdout.write(params.map(x => x.str).join(" "), "\n")
## Prints all of its arguments to the command line, in Debug form.
diff --git a/std/default/options.pk b/std/default/options.pk
index bbe0d9c..397a8f3 100644
--- a/std/default/options.pk
+++ b/std/default/options.pk
@@ -3,14 +3,18 @@
import std/format
+## The `Option` type.
+## A type that represents either the presence or absence of a value.
pub type Option[T] = union
Some: T
None
+## Checks if a type is present within an `Option` type.
pub func is_some[T](self: Option[T]): bool =
self of Some(_)
+## Checks if a type is not present within an `Option` type.
pub func is_none[T](self: Option[T]): bool =
- not self.is_some
+ self of None
## Converts an `Option[T]` to a `Result[T, E]` given a user-provided error.
pub func err[T, E](self: Option[T], error: E): Result[T, E] =
diff --git a/std/default/results.pk b/std/default/results.pk
index 4e4d27a..8faf0c0 100644
--- a/std/default/results.pk
+++ b/std/default/results.pk
@@ -3,20 +3,30 @@
import std/[options, format]
+## The Result type.
+## A type that represents either success or failure.
pub type Result[T, E] = union
Okay: T
Error: E
-pub type Error = interface
+## The Err interface. Useful for dynamically dispatching errors.
+pub type Err = interface
str(Self): str
dbg(Self): str
-pub type Result[T] = Result[T, ref Error]
+## A `Result` type that uses dynamically dispatched errors.
+## The `Error` may be any type implementing `Err`.
+pub type Result[T] = Result[T, ref Err]
+## A `Result` type that only checks for success.
+## Does not contain a value.
+pub type Success[E] = Result[Unit, E]
+## Checks if a `Result` type was successful.
pub func is_ok[T, E](self: Result[T, E]): bool =
self of Okay(_)
+## Checks if a `Result` type was not successful.
pub func is_err[T, E](self: Result[T, E]): bool =
- not self.is_ok
+ self of Error(_)
## Converts from a `Result[T, E]` to an `Option[T]`.
pub func ok[T, E](self: Result[T, E]): Option[T] =