1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 01:57:09 +03:00
mal/ocaml/printer.ml

36 lines
1.2 KiB
OCaml

module T = Types.Types
let join sep xs =
List.fold_left (fun a x -> if a = "" then x else a ^ sep ^ x) "" xs
let meta obj =
match obj with
| T.List { T.meta = meta } -> meta
| T.Map { T.meta = meta } -> meta
| T.Vector { T.meta = meta } -> meta
| T.Symbol { T.meta = meta } -> meta
| _ -> T.Nil
let rec pr_str mal_obj print_readably =
let r = print_readably in
match mal_obj with
| T.Int i -> string_of_int i
| T.Symbol { T.value = s } -> s
| T.Keyword s -> ":" ^ s
| T.Nil -> "nil"
| T.Bool true -> "true"
| T.Bool false -> "false"
| T.String s ->
if r
then "\"" ^ (Str.global_replace (Str.regexp "\\([\"\\]\\)") "\\\\\\1" s) ^ "\""
else s
| T.List { T.value = xs } ->
"(" ^ (join " " (List.map (fun s -> pr_str s r) xs)) ^ ")"
| T.Vector { T.value = xs } ->
"[" ^ (join " " (List.map (fun s -> pr_str s r) xs)) ^ "]"
| T.Map { T.value = xs } ->
(Types.MalMap.fold (fun k v s -> s ^ (if s = "" then "{" else ", ") ^ (pr_str k r)
^ " " ^ (pr_str v r)) xs "")
^ "}"
| T.Fn f -> "#<fn>"