From a878f3bb778513c0cc8bbeb1a8ff61664e43de29 Mon Sep 17 00:00:00 2001 From: Chouser Date: Sun, 25 Jan 2015 23:30:37 -0500 Subject: Ocaml: Use a real map type T.Map is now a real OCaml binary-tree map, and supports arbitrary mal value types for both keys and values. Metadata support is provided in the data objects, but not yet in the printer, reader, or core library. --- ocaml/printer.ml | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'ocaml/printer.ml') diff --git a/ocaml/printer.ml b/ocaml/printer.ml index 1257a69..3e09019 100644 --- a/ocaml/printer.ml +++ b/ocaml/printer.ml @@ -1,30 +1,27 @@ +module T = Types.Types + let join sep xs = List.fold_left (fun a x -> if a = "" then x else a ^ sep ^ x) "" xs -let rec pr_pairs xs str print_readably = match xs with - | k :: v :: more -> pr_pairs more ((if str = "" then str else (str ^ ", ")) - ^ (pr_str k print_readably) - ^ " " - ^ (pr_str v print_readably)) - print_readably - | _ :: [] -> raise (Invalid_argument "Partition requires even number of items") - | [] -> str - -and pr_str mal_obj print_readably = - match mal_obj with - | Types.Int i -> string_of_int i - | Types.Symbol s -> s - | Types.Keyword s -> ":" ^ s - | Types.Nil -> "nil" - | Types.Bool true -> "true" - | Types.Bool false -> "false" - | Types.String s -> - if print_readably - then "\"" ^ (Str.global_replace (Str.regexp "\\([\"\\]\\)") "\\\\\\1" s) ^ "\"" - else s - | Types.List xs -> - "(" ^ (join " " (List.map (fun s -> pr_str s print_readably) xs)) ^ ")" - | Types.Vector xs -> - "[" ^ (join " " (List.map (fun s -> pr_str s print_readably) xs)) ^ "]" - | Types.Map xs -> "{" ^ pr_pairs xs "" print_readably ^ "}" - | Types.Fn f -> "#" +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 -> "#" -- cgit v1.2.3