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

16 lines
526 B
OCaml

let join sep xs =
List.fold_left (fun a x -> if a = "" then x else a ^ sep ^ x) "" xs
let rec pr_str mal_obj =
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 -> "\""
^ (Str.global_replace (Str.regexp "\"") "\\\"" s)
^ "\""
| Types.MalList xs -> "(" ^ (join " " (List.map pr_str xs)) ^ ")"