diff options
| author | Chouser <chouser@n01se.net> | 2015-01-23 20:05:03 -0500 |
|---|---|---|
| committer | Chouser <chouser@n01se.net> | 2015-01-30 12:54:42 -0500 |
| commit | de04357cd5f2954e2d682abb97ca2b3b90ea75d1 (patch) | |
| tree | f52fd11b6b3d21880409dc74b53c324d31fec7b5 /ocaml/printer.ml | |
| parent | 9115534dc73fe18a12b3b2ecf436051b76bdd8a4 (diff) | |
| download | mal-de04357cd5f2954e2d682abb97ca2b3b90ea75d1.tar.gz mal-de04357cd5f2954e2d682abb97ca2b3b90ea75d1.zip | |
Ocaml: Add string functions
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>" |
