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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

open Syntax

module Text = struct
  let until_text_validate ~unsafe m =
    if unsafe then Ok m else Text_validate.modul m

  let until_group ~unsafe m =
    let* m = until_text_validate ~unsafe m in
    Grouped.of_symbolic m

  let until_assign ~unsafe m =
    let* m = until_group ~unsafe m in
    Assigned.of_grouped m

  let until_binary ~unsafe ~rac ~srac m =
    let* m = until_assign ~unsafe m in
    let* m = Rewrite.modul m in
    if srac then Code_generator.generate true m
    else if rac then Code_generator.generate false m
    else Ok m

  let until_binary_validate ~unsafe ~rac ~srac m =
    let* m = until_binary ~unsafe ~rac ~srac m in
    if unsafe then Ok m
    else
      let+ () = Binary_validate.modul m in
      m

  let until_optimize ~unsafe ~rac ~srac ~optimize m =
    let+ m = until_binary_validate ~unsafe ~rac ~srac m in
    if optimize then Optimize.modul m else m

  let until_link ~unsafe ~rac ~srac ~optimize ~name link_state m =
    let* m = until_optimize ~unsafe ~rac ~srac ~optimize m in
    Link.modul link_state ~name m

  let until_interpret ~unsafe ~rac ~srac ~optimize ~name link_state m =
    let* m, link_state =
      until_link ~unsafe ~rac ~srac ~optimize ~name link_state m
    in
    let+ () = Interpret.Concrete.modul link_state.envs m in
    link_state
end

module Binary = struct
  let until_binary_validate ~unsafe m =
    if unsafe then Ok m
    else
      let+ () = Binary_validate.modul m in
      m

  let until_optimize ~unsafe ~optimize m =
    let+ m = until_binary_validate ~unsafe m in
    if optimize then Optimize.modul m else m

  let until_link ~unsafe ~optimize ~name link_state m =
    let* m = until_optimize ~unsafe ~optimize m in
    Link.modul link_state ~name m

  let until_interpret ~unsafe ~optimize ~name link_state m =
    let* m, link_state = until_link ~unsafe ~optimize ~name link_state m in
    let+ () = Interpret.Concrete.modul link_state.envs m in
    link_state
end

module Any = struct
  let until_binary_validate ~unsafe ~rac ~srac = function
    | Kind.Wat m -> Text.until_binary_validate ~unsafe ~rac ~srac m
    | Wasm m -> Binary.until_binary_validate ~unsafe m
    | Wast _ | Ocaml _ -> assert false

  let until_optimize ~unsafe ~rac ~srac ~optimize = function
    | Kind.Wat m -> Text.until_optimize ~unsafe ~rac ~srac ~optimize m
    | Wasm m -> Binary.until_optimize ~unsafe ~optimize m
    | Wast _ | Ocaml _ -> assert false

  let until_link ~unsafe ~rac ~srac ~optimize ~name link_state = function
    | Kind.Wat m ->
      Text.until_link ~unsafe ~rac ~srac ~optimize ~name link_state m
    | Wasm m -> Binary.until_link ~unsafe ~optimize ~name link_state m
    | Wast _ | Ocaml _ -> assert false

  let until_interpret ~unsafe ~rac ~srac ~optimize ~name link_state = function
    | Kind.Wat m ->
      Text.until_interpret ~unsafe ~rac ~srac ~optimize ~name link_state m
    | Wasm m -> Binary.until_interpret ~unsafe ~optimize ~name link_state m
    | Wast _ | Ocaml _ -> assert false
end

module File = struct
  let until_binary_validate ~unsafe ~rac ~srac filename =
    let* m = Parse.guess_from_file filename in
    match m with
    | Kind.Wat m -> Text.until_binary_validate ~unsafe ~rac ~srac m
    | Wasm m -> Binary.until_binary_validate ~unsafe m
    | Wast _ | Ocaml _ -> assert false

  let until_optimize ~unsafe ~rac ~srac ~optimize filename =
    let* m = Parse.guess_from_file filename in
    match m with
    | Kind.Wat m -> Text.until_optimize ~unsafe ~rac ~srac ~optimize m
    | Wasm m -> Binary.until_optimize ~unsafe ~optimize m
    | Wast _ | Ocaml _ -> assert false

  let until_link ~unsafe ~rac ~srac ~optimize ~name link_state filename =
    let* m = Parse.guess_from_file filename in
    match m with
    | Kind.Wat m ->
      Text.until_link ~unsafe ~rac ~srac ~optimize ~name link_state m
    | Wasm m -> Binary.until_link ~unsafe ~optimize ~name link_state m
    | Wast _ | Ocaml _ -> assert false

  let until_interpret ~unsafe ~rac ~srac ~optimize ~name link_state filename =
    let* m = Parse.guess_from_file filename in
    match m with
    | Kind.Wat m ->
      Text.until_interpret ~unsafe ~rac ~srac ~optimize ~name link_state m
    | Wasm m -> Binary.until_interpret ~unsafe ~optimize ~name link_state m
    | Wast _ | Ocaml _ -> assert false
end