aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2015-01-24 00:44:51 -0500
committerChouser <chouser@n01se.net>2015-01-30 12:54:42 -0500
commitb7ffcab96166f15d6203551ffbc487da5076f92e (patch)
treedc2aa07a80dbf3f03bfa2c331bb140bd530ab21f
parentf2f11f6279e1b242ba75136cd037fabdd176118a (diff)
downloadmal-b7ffcab96166f15d6203551ffbc487da5076f92e.tar.gz
mal-b7ffcab96166f15d6203551ffbc487da5076f92e.zip
Ocaml: Add read, print for vector, map
-rw-r--r--ocaml/printer.ml14
-rw-r--r--ocaml/reader.ml10
-rw-r--r--ocaml/types.ml2
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