1module Cst = Morbig.CST
2
3let redirect_to_string = function
4 | Cst.IoRedirect_IoFile { value = io_file; _ } -> (
5 match io_file with
6 | Cst.IoFile_Great_FileName
7 { value = Cst.Filename_Word { value = Cst.Word (w, _); _ }; _ } ->
8 Fmt.str "> %s" w
9 | Cst.IoFile_DGreat_FileName
10 { value = Cst.Filename_Word { value = Cst.Word (w, _); _ }; _ } ->
11 Fmt.str ">> %s" w
12 | _ -> failwith "Redirect Unsupported")
13 | _ -> failwith "IO Redirect Unsupported"
14
15let cmd_suffix_to_list s =
16 let rec loop = function
17 | Cst.CmdSuffix_Word { value = Cst.Word (s, _); _ } -> [ s ]
18 | Cst.CmdSuffix_CmdSuffix_Word (suff, { value = Cst.Word (s, _); _ }) ->
19 s :: loop suff.value
20 | Cst.CmdSuffix_CmdSuffix_IoRedirect (suff, { value = redirect; _ }) ->
21 let sf = loop suff.value in
22 redirect_to_string redirect :: sf
23 | _ -> failwith "Unsupported!"
24 in
25 loop s |> List.rev |> String.concat " "
26
27let of_cmd (c : Cst.command) =
28 match c with
29 | Cst.Command_SimpleCommand simple -> (
30 match simple.value with
31 | Cst.SimpleCommand_CmdName
32 { value = Cst.CmdName_Word { value = Cst.Word (w, _); _ }; _ } ->
33 w
34 | Cst.SimpleCommand_CmdName_CmdSuffix
35 ( { value = Cst.CmdName_Word { value = Cst.Word (w, _); _ }; _ },
36 { value = suff; _ } ) ->
37 let s = cmd_suffix_to_list suff in
38 w ^ " " ^ s
39 | _ -> failwith "Unsupported")
40 | _ -> failwith "Unsupported"
41
42let cmds_to_strings =
43 let v =
44 object
45 inherit [_] Morbig.CSTVisitors.reduce
46 method zero = []
47 method plus = List.append
48 method! visit_command acc c = of_cmd c :: acc
49 end
50 in
51 v#visit_program []
52
53let to_commands file =
54 let contents = Eio.Path.load file in
55 let name = Eio.Path.native_exn file |> Filename.basename in
56 let ast = Morbig.parse_string name contents in
57 cmds_to_strings ast