aboutsummaryrefslogtreecommitdiff
path: root/ocaml/printer.ml
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-01-30 12:06:45 -0600
committerJoel Martin <github@martintribe.org>2015-01-30 12:06:45 -0600
commit644e5ff95e186094054221cf2f13048630f81fa0 (patch)
tree14f965eee633f4ea185f640c6130e54063056d6b /ocaml/printer.ml
parentbf518367d0706b2fa727acc5326230ef8d3c812b (diff)
parentbc6448bce925d18ecb893e8e8ee10d2b2178b31a (diff)
downloadmal-644e5ff95e186094054221cf2f13048630f81fa0.tar.gz
mal-644e5ff95e186094054221cf2f13048630f81fa0.zip
Merge pull request #3 from Chouser/ocaml
Ocaml
Diffstat (limited to 'ocaml/printer.ml')
-rw-r--r--ocaml/printer.ml38
1 files changed, 38 insertions, 0 deletions
diff --git a/ocaml/printer.ml b/ocaml/printer.ml
new file mode 100644
index 0000000..135c3ce
--- /dev/null
+++ b/ocaml/printer.ml
@@ -0,0 +1,38 @@
+module T = Types.Types
+
+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.Fn { 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 "\"" ^ (Reader.gsub (Str.regexp "\\([\"\\\n]\\)")
+ (function
+ | "\n" -> "\\n"
+ | x -> "\\" ^ x)
+ s) ^ "\""
+ else s
+ | T.List { T.value = xs } ->
+ "(" ^ (String.concat " " (List.map (fun s -> pr_str s r) xs)) ^ ")"
+ | T.Vector { T.value = xs } ->
+ "[" ^ (String.concat " " (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>"
+ | T.Atom x -> "(atom " ^ (pr_str !x r) ^ ")"