aboutsummaryrefslogtreecommitdiff
path: root/ocaml/printer.ml
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2015-01-25 23:30:37 -0500
committerChouser <chouser@n01se.net>2015-01-30 12:54:42 -0500
commita878f3bb778513c0cc8bbeb1a8ff61664e43de29 (patch)
tree9024de3b223d78abef619c4ee36316c24f4bee19 /ocaml/printer.ml
parentb7ffcab96166f15d6203551ffbc487da5076f92e (diff)
downloadmal-a878f3bb778513c0cc8bbeb1a8ff61664e43de29.tar.gz
mal-a878f3bb778513c0cc8bbeb1a8ff61664e43de29.zip
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.
Diffstat (limited to 'ocaml/printer.ml')
-rw-r--r--ocaml/printer.ml51
1 files changed, 24 insertions, 27 deletions
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 -> "#<fn>"
+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>"