package forester

  1. Overview
  2. Docs
A tool for tending mathematical forests

Install

Dune Dependency

Authors

Maintainers

Sources

4.3.0.tar.gz
md5=4cf7ec6a0a36810572f8b3a2af58631c
sha512=818f859dac00a18807563fd79663ec54232c8df196a8dc2de5e269a1d0c71085b1b490297ea0ecf3ef9cd32e40fb05ad2e10c241b03958c15e25c2942271eede

doc/src/forester.core/Tape_effect.ml.html

Source file Tape_effect.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module type S = sig
  val run : tape:Syn.t -> (unit -> 'a) -> 'a
  val pop_node_opt : unit -> Syn.node Range.located option

  val pop_arg_opt : unit -> Syn.t Range.located option
  val pop_arg : loc:Range.t option -> Syn.t Range.located
  val pop_args : unit -> Syn.t Range.located list
end

module Make () = struct
  open Bwd

  module T = Algaeff.State.Make (struct type t = Syn.t end)

  let pop_node_opt () =
    match T.get () with
    | node :: nodes ->
      T.set nodes;
      Some node
    | [] -> None

  let push_node node =
    T.modify @@ fun nodes -> node :: nodes

  let pop_arg_opt () =
    match T.get () with
    | Range.{value = Syn.Group (Braces, arg); _} as node :: nodes ->
      T.set nodes;
      Some ({node with value = arg})
    | Range.{value = (Syn.Sym _ | Syn.Verbatim _ | Syn.Var _); _} as node :: nodes ->
      T.set nodes;
      Some ({node with value = [node]})
    | _ -> None

  let pop_arg ~loc =
    match pop_arg_opt () with
    | Some arg -> arg
    | None -> Reporter.fatalf ?loc Type_error "Expected argument"

  let pop_args () =
    let rec loop acc =
      match pop_arg_opt () with
      | Some arg -> loop @@ Bwd.Snoc (acc, arg)
      | None -> Bwd.prepend acc []
    in
    loop Bwd.Emp

  let run ~tape =
    T.run ~init:tape

end
OCaml

Innovation. Community. Security.