diff options
| -rw-r--r-- | ocaml/printer.ml | 14 | ||||
| -rw-r--r-- | ocaml/reader.ml | 10 | ||||
| -rw-r--r-- | ocaml/types.ml | 2 |
3 files changed, 24 insertions, 2 deletions
diff --git a/ocaml/printer.ml b/ocaml/printer.ml index 59c025d..1257a69 100644 --- a/ocaml/printer.ml +++ b/ocaml/printer.ml @@ -1,7 +1,16 @@ 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 print_readably = +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 @@ -15,4 +24,7 @@ let rec pr_str mal_obj print_readably = 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>" diff --git a/ocaml/reader.ml b/ocaml/reader.ml index 58e72fe..c452e05 100644 --- a/ocaml/reader.ml +++ b/ocaml/reader.ml @@ -54,10 +54,18 @@ and read_form all_tokens = | "`" -> read_quote "quasiquote" tokens | "~" -> read_quote "unquote" tokens | "~@" -> read_quote "splice-unquote" tokens - | "[" | "(" | "{" -> let list_reader = + | "(" -> let list_reader = read_list {list_form = []; tokens = tokens} in {form = Types.List list_reader.list_form; tokens = list_reader.tokens} + | "{" -> let list_reader = + read_list {list_form = []; tokens = tokens} in + {form = Types.Map list_reader.list_form; + tokens = list_reader.tokens} + | "[" -> let list_reader = + read_list {list_form = []; tokens = tokens} in + {form = Types.Vector list_reader.list_form; + tokens = list_reader.tokens} | _ -> {form = read_atom token; tokens = tokens} let read_str str = (read_form (List.filter ((<>) "") (find_re token_re str))).form diff --git a/ocaml/types.ml b/ocaml/types.ml index 34dba05..6440580 100644 --- a/ocaml/types.ml +++ b/ocaml/types.ml @@ -1,5 +1,7 @@ type mal_type = | List of mal_type list + | Vector of mal_type list + | Map of mal_type list | Int of int | Symbol of string | Keyword of string |
