Skip to content

Commit

Permalink
add lazy implementation of handle_default_opt in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Alain committed Dec 1, 2021
1 parent fd8ff75 commit 5ae1fe8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 20 additions & 1 deletion compiler/runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type source_position = {
law_headings : string list;
}

type 'a eoption = ENone of unit | ESome of 'a
type 'a eeoption = ENone of unit | ESome of 'a

exception EmptyError

Expand Down Expand Up @@ -215,6 +215,25 @@ let handle_default : 'a. (unit -> 'a) array -> (unit -> bool) -> (unit -> 'a) ->
in
match except with Some x -> x | None -> if just () then cons () else raise EmptyError

let handle_default_opt: 'a. 'a eoption array -> (unit -> bool eoption) -> (unit -> 'a eoption) -> 'a eoption =
fun exceptions just cons ->
let except =
Array.fold_left
(fun acc except ->
match acc, except with
| ENone, _ -> except
| ESome _, ENone -> acc
| ESome _, ESome _ -> raise ConflictError)
None exceptions
in
match except with
| ESome _ -> except
| ENone -> begin
match just () with
| ESome b -> if b then cons () else ENone
| ENone -> ENone
end

let no_input : unit -> 'a = fun _ -> raise EmptyError

let ( *$ ) (i1 : money) (i2 : decimal) : money =
Expand Down
3 changes: 3 additions & 0 deletions compiler/runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ val handle_default : (unit -> 'a) array -> (unit -> bool) -> (unit -> 'a) -> 'a
(** @raise EmptyError
@raise ConflictError *)

val handle_default_opt: 'a option array -> (unit -> bool option) -> (unit -> 'a option) -> 'a option
(** @raise ConflictError *)

val no_input : unit -> 'a

(**{1 Operators} *)
Expand Down

0 comments on commit 5ae1fe8

Please sign in to comment.