···8989}
9090[@@deriving show]
91919292-module Tape = Tape_effect.Make ()
9393-9492type eval_env = {
9593 mode: eval_mode;
9694 config: Config.t;
···9997 jobs: Job.job Range.located Stack.t;
10098 emitted_trees: T.content T.article Stack.t;
10199 heap: Value.obj Symbol_table.t;
102102- mutable frontmatter: T.content T.frontmatter;
100100+ frontmatter: T.content T.frontmatter ref;
101101+ tape: Syn.t ref;
103102}
104103104104+let pop_node_opt ~env =
105105+ match env.tape.contents with
106106+ | node :: nodes ->
107107+ env.tape := nodes;
108108+ Some node
109109+ | [] -> None
110110+111111+let pop_arg_opt ~env =
112112+ match env.tape.contents with
113113+ | (Range.{value = Syn.Group (Braces, arg); _} as node) :: nodes ->
114114+ env.tape := nodes;
115115+ Some {node with value = arg}
116116+ | (Range.
117117+ {
118118+ value =
119119+ ( Syn.Sym _ | Syn.Verbatim _ | Syn.Var _ | Syn.Dx_sequent _
120120+ | Syn.Dx_query _ );
121121+ _;
122122+ } as node)
123123+ :: nodes ->
124124+ env.tape := nodes;
125125+ Some {node with value = [node]}
126126+ | _ -> None
127127+128128+let pop_arg ~env ~loc =
129129+ match pop_arg_opt ~env with
130130+ | Some arg -> arg
131131+ | None -> Reporter.fatal ?loc (Type_error {got = None; expected = [Argument]})
132132+105133let initial_eval_env config frontmatter : eval_env =
106134 {
107135 mode = Text_mode;
···112140 emitted_trees = Stack.create ();
113141 heap = Symbol_table.create 100;
114142 frontmatter;
143143+ tape = ref [];
115144 }
116145117146let get_current_uri ~env ~loc =
118118- match env.frontmatter.uri with
147147+ match env.frontmatter.contents.uri with
119148 | Some uri -> uri
120149 | None ->
121150 Reporter.fatal ?loc Internal_error
···191220 | TeX_cs.Word x -> Format.fprintf fmt "\\%s " x
192221193222let rec process_tape ~env =
194194- match Tape.pop_node_opt () with
223223+ match pop_node_opt ~env with
195224 | None -> Value.Content (T.Content [])
196225 | Some node -> eval_node ~env node
197226198198-and eval_tape ~env tape = Tape.run ~tape (fun () -> process_tape ~env)
227227+and eval_tape ~env tape = process_tape ~env:{env with tape = ref tape}
199228200200-and eval_pop_arg ~env ~loc = Tape.pop_arg ~loc |> Range.map (eval_tape ~env)
229229+and eval_pop_arg ~env ~loc = pop_arg ~env ~loc |> Range.map (eval_tape ~env)
201230202231and pop_content_arg ~env ~loc = eval_pop_arg ~env ~loc |> extract_content
203232···317346 {
318347 subtree with
319348 frontmatter =
320320- {subtree.frontmatter with uri; designated_parent = frontmatter.uri};
349349+ {subtree.frontmatter with uri; designated_parent = !frontmatter.uri};
321350 }
322351 in
323352 begin match uri with
···508537 | Verbatim str -> emit_content_node ~env ~loc @@ CDATA str
509538 | Title ->
510539 let title = pop_content_arg ~env ~loc in
511511- env.frontmatter <- {env.frontmatter with title = Some title};
540540+ env.frontmatter := {env.frontmatter.contents with title = Some title};
512541 process_tape ~env
513542 | Parent ->
514543 let parent_arg = eval_pop_arg ~env ~loc in
···520549 ~extra_remarks:
521550 [Asai.Diagnostic.loctext "Expected valid URI in parent declaration"]
522551 in
523523- env.frontmatter <- {env.frontmatter with designated_parent = Some parent};
552552+ env.frontmatter :=
553553+ {env.frontmatter.contents with designated_parent = Some parent};
524554 process_tape ~env
525555 | Meta ->
526556 let k = pop_text_arg ~env ~loc in
527557 let v = pop_content_arg ~env ~loc in
528528- env.frontmatter <-
529529- {env.frontmatter with metas = env.frontmatter.metas @ [(k, v)]};
558558+ env.frontmatter :=
559559+ {
560560+ env.frontmatter.contents with
561561+ metas = env.frontmatter.contents.metas @ [(k, v)];
562562+ };
530563 process_tape ~env
531564 | Attribution (role, type_) ->
532565 let arg = eval_pop_arg ~env ~loc in
···550583 T.Content_vertex (extract_content arg)
551584 in
552585 let attribution = T.{role; vertex} in
553553- env.frontmatter <-
586586+ env.frontmatter :=
554587 {
555555- env.frontmatter with
556556- attributions = env.frontmatter.attributions @ [attribution];
588588+ env.frontmatter.contents with
589589+ attributions = env.frontmatter.contents.attributions @ [attribution];
557590 };
558591 process_tape ~env
559592 | Tag type_ ->
···573606 ];
574607 T.Content_vertex (extract_content arg)
575608 in
576576- env.frontmatter <-
577577- {env.frontmatter with tags = env.frontmatter.tags @ [vertex]};
609609+ env.frontmatter :=
610610+ {
611611+ env.frontmatter.contents with
612612+ tags = env.frontmatter.contents.tags @ [vertex];
613613+ };
578614 process_tape ~env
579615 | Date ->
580616 let date_str = pop_text_arg ~env ~loc in
···584620 ~extra_remarks:
585621 [Asai.Diagnostic.loctextf "Invalid date string `%s`" date_str]
586622 | Some date ->
587587- env.frontmatter <-
588588- {env.frontmatter with dates = env.frontmatter.dates @ [date]};
623623+ env.frontmatter :=
624624+ {
625625+ env.frontmatter.contents with
626626+ dates = env.frontmatter.contents.dates @ [date];
627627+ };
589628 process_tape ~env
590629 end
591630 | Number ->
592631 let num = pop_text_arg ~env ~loc in
593593- env.frontmatter <- {env.frontmatter with number = Some num};
632632+ env.frontmatter := {env.frontmatter.contents with number = Some num};
594633 process_tape ~env
595634 | Taxon ->
596635 let taxon = Some (pop_content_arg ~env ~loc) in
597597- env.frontmatter <- {env.frontmatter with taxon};
636636+ env.frontmatter := {env.frontmatter.contents with taxon};
598637 process_tape ~env
599638 | Sym sym -> focus ~env ?loc:node.loc @@ Value.Sym sym
600639 | Dx_prop (rel, args) ->
···680719 match xs with
681720 | [] -> focus ~env ?loc @@ eval_tape ~env:{env with lex_env = rho} body
682721 | (info, y) :: ys -> (
683683- match Tape.pop_arg_opt () with
722722+ match pop_arg_opt ~env with
684723 | Some arg ->
685724 let yval =
686725 match info with
···714753 let attribution_is_author attr =
715754 match T.(attr.role) with T.Author -> true | _ -> false
716755 in
717717- let outer_frontmatter = env.frontmatter in
756756+ let outer_frontmatter = env.frontmatter.contents in
718757 let attributions =
719758 List.filter attribution_is_author outer_frontmatter.attributions
720759 in
···723762 ?source_path:outer_frontmatter.source_path ~dates:outer_frontmatter.dates
724763 ()
725764 in
726726- let env = {env with frontmatter} in
765765+ let env = {env with frontmatter = ref frontmatter} in
727766 let mainmatter =
728767 {value = eval_tape ~env syn; loc = None} |> extract_content
729768 in
730730- let frontmatter = env.frontmatter in
769769+ let frontmatter = env.frontmatter.contents in
731770 let backmatter =
732771 match uri with Some uri -> default_backmatter ~uri | None -> Content []
733772 in
···747786 ~emit:push
748787 @@ fun () ->
749788 let fm = T.default_frontmatter ~uri ?source_path () in
750750- let env = initial_eval_env config fm in
789789+ let env = initial_eval_env config (ref fm) in
751790 let main = eval_tree_inner ~env ~uri tree in
752791 let side = env.emitted_trees |> Stack.to_seq |> List.of_seq in
753792 let jobs = env.jobs |> Stack.to_seq |> List.of_seq in
-63
lib/compiler/Tape_effect.ml
···11-(*
22- * SPDX-FileCopyrightText: 2024 The Forester Project Contributors
33- *
44- * SPDX-License-Identifier: GPL-3.0-or-later
55- *)
66-77-open Forester_core
88-99-module type S = sig
1010- val run : tape:Syn.t -> (unit -> 'a) -> 'a
1111- val pop_node_opt : unit -> Syn.node Range.located option
1212- val pop_arg_opt : unit -> Syn.t Range.located option
1313- val pop_arg : loc:Range.t option -> Syn.t Range.located
1414- val pop_args : unit -> Syn.t Range.located list
1515-end
1616-1717-module Make () = struct
1818- open Bwd
1919-2020- module T = Algaeff.State.Make (struct
2121- type t = Syn.t
2222- end)
2323-2424- let pop_node_opt () =
2525- match T.get () with
2626- | node :: nodes ->
2727- T.set nodes;
2828- Some node
2929- | [] -> None
3030-3131- let pop_arg_opt () =
3232- match T.get () with
3333- | (Range.{value = Syn.Group (Braces, arg); _} as node) :: nodes ->
3434- T.set nodes;
3535- Some {node with value = arg}
3636- | (Range.
3737- {
3838- value =
3939- ( Syn.Sym _ | Syn.Verbatim _ | Syn.Var _ | Syn.Dx_sequent _
4040- | Syn.Dx_query _ );
4141- _;
4242- } as node)
4343- :: nodes ->
4444- T.set nodes;
4545- Some {node with value = [node]}
4646- | _ -> None
4747-4848- let pop_arg ~loc =
4949- match pop_arg_opt () with
5050- | Some arg -> arg
5151- | None ->
5252- Reporter.fatal ?loc (Type_error {got = None; expected = [Argument]})
5353-5454- let pop_args () =
5555- let rec loop acc =
5656- match pop_arg_opt () with
5757- | Some arg -> loop @@ Bwd.Snoc (acc, arg)
5858- | None -> Bwd.prepend acc []
5959- in
6060- loop Bwd.Emp
6161-6262- let run ~tape = T.run ~init:tape
6363-end
-17
lib/compiler/Tape_effect.mli
···11-(*
22- * SPDX-FileCopyrightText: 2024 The Forester Project Contributors
33- *
44- * SPDX-License-Identifier: GPL-3.0-or-later
55- *)
66-77-open Forester_core
88-99-module type S = sig
1010- val run : tape:Syn.t -> (unit -> 'a) -> 'a
1111- val pop_node_opt : unit -> Syn.node Range.located option
1212- val pop_arg_opt : unit -> Syn.t Range.located option
1313- val pop_arg : loc:Range.t option -> Syn.t Range.located
1414- val pop_args : unit -> Syn.t Range.located list
1515-end
1616-1717-module Make () : S