diff options
Diffstat (limited to 'ocaml/printer.ml')
| -rw-r--r-- | ocaml/printer.ml | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/ocaml/printer.ml b/ocaml/printer.ml index e87b3eb..fc9e47b 100644 --- a/ocaml/printer.ml +++ b/ocaml/printer.ml @@ -1,7 +1,7 @@ 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 = +let rec pr_str mal_obj print_readably = match mal_obj with | Types.Int i -> string_of_int i | Types.Symbol s -> s @@ -9,8 +9,10 @@ let rec pr_str mal_obj = | 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)) ^ ")" - | Types.Fn f -> "<fn>" + | Types.String s -> + if print_readably + then "\"" ^ (Str.global_replace (Str.regexp "\\([\"\\]\\)") "\\\\\\1" s) ^ "\"" + else s + | Types.MalList xs -> + "(" ^ (join " " (List.map (fun s -> pr_str s print_readably) xs)) ^ ")" + | Types.Fn f -> "#<fn>" |
