Skip to content

Commit

Permalink
Add unwrap* methods for Option and some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
forell committed Dec 13, 2024
1 parent 18b0324 commit 01ac2d7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/Base/Int.fram
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub method sub = (extern dbl_subInt : Int -> Int -> Int) self
pub method mul = (extern dbl_mulInt : Int -> Int -> Int) self

pub method div (n : Int) =
assert {msg="Division by zero"} (n.neq 0);
assert {msg="Division by zero"} (n != 0);
(extern dbl_divInt : Int -> Int -> Int) self n

pub method mod (n : Int) =
assert {msg="Division by zero"} (n.neq 0);
assert {msg="Division by zero"} (n != 0);
(extern dbl_modInt : Int -> Int -> Int) self n

pub method neg {self : Int} = 0 - self
Expand Down
6 changes: 2 additions & 4 deletions lib/Base/Int64.fram
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ pub method toString {self : Int64} =
(extern dbl_int64ToString : Int64 -> String) self
pub method toIntErr { ~onError, self : Int64 } =
if self <= 0x3fffffffffffffffL && self >= 0xc000000000000000L then
(extern dbl_intToInt64 : Int64 -> Int) self
(extern dbl_int64ToInt : Int64 -> Int) self
else ~onError ()
pub method toIntOpt {self : Int64} =
pub method toInt {self : Int64} =
if self <= 0x3fffffffffffffffL && self >= 0x7fffffffffffffffL then
Some ((extern dbl_int64ToInt : Int64 -> Int) self)
else None
pub method toInt {self : Int64} =
self.toIntErr { ~onError = fn () => runtimeError "Conversion overflow" }

pub method abs {self : Int64} =
if self < 0L then self.neg else self
Expand Down
25 changes: 25 additions & 0 deletions lib/Base/Option.fram
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{# This file is part of DBL, released under MIT license.
# See LICENSE for details.
#}

import open /Base/Types
import open /Base/Assert

{## For `Some x` returns `x`, otherwise the provided argument is returned.
#}
pub method unwrapOr default =
match self with
| None => default
| Some x => x
end

{## This method should only be called on `Some x` values, in which case it
returns `x`. When applied to `None` the entire program crashes irrecoverably
with a runtime error. The resulting error message can be optionally specified
with `?msg`.
#}
pub method unwrap {?msg} =
match self with
| None => runtimeError (msg.unwrapOr "Called `unwrap` on `None`")
| Some x => x
end
1 change: 1 addition & 0 deletions lib/Prelude.fram
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import /Base/Types
import /Base/Operators
import /Base/Assert
import /Base/Bool
import /Base/Option
import /Base/Int
import /Base/Int64
import /Base/Char
Expand Down
2 changes: 1 addition & 1 deletion src/Eval/External.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let int_cmpop op = int_fun2 (fun x y -> of_bool (op x y))
let int64_fun f = VFn (fun v cont ->
match v with
| VNum64 n -> cont (f n)
| _ -> failwith "Not a 64-bit integer")
| _ -> runtime_error "Not a 64-bit integer")

let int64_fun2 f = int64_fun (fun x -> int64_fun (f x))

Expand Down

0 comments on commit 01ac2d7

Please sign in to comment.