Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125open Sast
let space = Fmt.any " "
let separator v ppf () = match v with | Some Ampersand -> Fmt.string ppf "&" | Some Semicolon -> Fmt.string ppf ";" | None -> Fmt.string ppf ""
let and_or v ppf () = match v with | And -> Fmt.string ppf "&&" | Or -> Fmt.string ppf "||" | Noand_or -> Fmt.string ppf ""
let rec complete_commands : complete_commands Fmt.t = fun ppf cs -> Fmt.(list complete_command) ppf cs
and complete_command : complete_command Fmt.t = fun ppf (c, sep) -> Fmt.pf ppf "%a%a" clist c (separator sep) ()
and clist : clist Fmt.t = fun ppf c -> separator_list (and_or_list pipeline) ppf c
and and_or_list : 'a Fmt.t -> 'a and_or_list Fmt.t = fun pv ppf v -> let ls = Import.Nlist.to_list v in let pp ppf (p, s) = Fmt.pf ppf "%a%a" pv p (and_or s) () in Fmt.(list ~sep:Fmt.nop pp) ppf ls
and separator_list : 'a. 'a Fmt.t -> 'a separator_list Fmt.t = fun pv ppf v -> let ls = Import.Nlist.to_list v in let pp ppf (p, s) = Fmt.pf ppf "%a%a" pv p (separator (Some s)) () in Fmt.(list ~sep:Fmt.nop pp) ppf ls
and pipeline : pipeline Fmt.t = fun ppf -> function | Pipeline seq -> pipe_sequence ppf seq | Pipeline_Bang seq -> Fmt.pf ppf "! %a" pipe_sequence seq
and pipe_sequence : pipe_sequence Fmt.t = fun ppf c -> Fmt.(list ~sep:(Fmt.any " | ") command) ppf c
and command : command Fmt.t = fun ppf -> function | SimpleCommand s -> simple_command ppf s | CompoundCommand (c, _) -> compound_command ppf c | _ -> ()
and compound_command : compound_command Fmt.t = fun ppf -> function | Subshell (c, _) -> Fmt.pf ppf "$(%a)" clist c | BraceGroup (s, _) -> Fmt.pf ppf "%a" clist s | _ -> Fmt.pf ppf "TODO"
and simple_command : simple_command Fmt.t = fun ppf -> function | Prefixed (p, w, s) -> Fmt.pf ppf "%a %a %a" cmd_prefix p Fmt.(option word_cst) w Fmt.(option cmd_suffix) s | Named (n, s) -> Fmt.pf ppf "%a %a" word_cst n Fmt.(option cmd_suffix) s
and cmd_prefix : cmd_prefix Fmt.t = fun ppf -> Fmt.(list ~sep:space cmd_prefix_item) ppf
and cmd_suffix : cmd_suffix Fmt.t = fun ppf -> Fmt.(list ~sep:space cmd_suffix_item) ppf
and cmd_prefix_item : cmd_prefix_item Fmt.t = fun ppf -> function | Prefix_assignment assign -> assignment_word ppf assign | _ -> ()
and cmd_suffix_item : cmd_suffix_item Fmt.t = fun ppf -> function | Suffix_word w -> word_cst ppf w | Suffix_redirect ir -> io_redirect ppf ir
and io_redirect : io_redirect Fmt.t = fun ppf -> function | IoRedirect_IoFile (n, (op, filename)) -> Fmt.pf ppf "%i%a%a" n io_op op word_cst filename | IoRedirect_IoHere (n, _here) -> Fmt.pf ppf "%i << %s" n "TODO"
and io_op : io_op Fmt.t = fun ppf -> function | Io_op_andgreat -> Fmt.string ppf "&>" | Io_op_less -> Fmt.string ppf "<" | Io_op_lessand -> Fmt.string ppf "<&" | Io_op_great -> Fmt.string ppf ">" | Io_op_greatand -> Fmt.string ppf ">&" | Io_op_dgreat -> Fmt.string ppf ">>" | Io_op_lessgreat -> Fmt.string ppf "<>" | Io_op_clobber -> Fmt.string ppf ">|"
and assignment_word : assignment_word Fmt.t = fun ppf (Name name, word) -> Fmt.pf ppf "%s=%a" name word_cst word
and word_cst : word_cst Fmt.t = fun ppf -> Fmt.(list ~sep:(Fmt.any "") word_component) ppf
and word_component : word_component Fmt.t = fun ppf -> function | WordName s -> Fmt.string ppf s | WordAssignmentWord a -> assignment_word ppf a | WordDoubleQuoted s -> Fmt.pf ppf "\"%a\"" word_cst s | WordSingleQuoted s -> Fmt.pf ppf "'%a'" word_cst s | WordLiteral lit -> Fmt.string ppf lit | WordVariable v -> Fmt.pf ppf "$%a" variable v | WordEmpty -> () | WordGlobAll -> Fmt.string ppf "*" | WordGlobAny -> Fmt.string ppf "?" | WordSubshell s -> Fmt.pf ppf "$(%a)" complete_commands s | WordTildePrefix _ -> Fmt.pf ppf "~" | WordArithmeticExpression s -> Fmt.pf ppf "((%a))" Arith.pp s | WordReBracketExpression _ -> ()
and variable : variable Fmt.t = fun ppf (VariableAtom (v, _)) -> Fmt.string ppf v