1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(* SPDX-License-Identifier: AGPL-3.0-or-later *)
(* Copyright © 2021-2024 OCamlPro *)
(* Written by the Owi programmers *)

open Syntax

let get_printer filename =
  let ext = Fpath.get_ext filename in
  match ext with
  | ".wat" ->
    let+ v = Parse.Text.Module.from_file filename in
    fun fmt () -> Text.pp_modul fmt v
  | ".wast" ->
    let+ v = Parse.Text.Script.from_file filename in
    fun fmt () -> Text.pp_script fmt v
  | ext -> Error (`Unsupported_file_extension ext)

let cmd_one inplace file =
  let* pp = get_printer file in
  if inplace then Bos.OS.File.writef file "%a@\n" pp ()
  else Ok (Fmt.pr "%a@\n" pp ())

let cmd ~inplace ~files = list_iter (cmd_one inplace) files