diff options
| author | Chouser <chouser@n01se.net> | 2015-01-25 23:30:37 -0500 |
|---|---|---|
| committer | Chouser <chouser@n01se.net> | 2015-01-30 12:54:42 -0500 |
| commit | a878f3bb778513c0cc8bbeb1a8ff61664e43de29 (patch) | |
| tree | 9024de3b223d78abef619c4ee36316c24f4bee19 /ocaml/types.ml | |
| parent | b7ffcab96166f15d6203551ffbc487da5076f92e (diff) | |
| download | mal-a878f3bb778513c0cc8bbeb1a8ff61664e43de29.tar.gz mal-a878f3bb778513c0cc8bbeb1a8ff61664e43de29.zip | |
Ocaml: Use a real map type
T.Map is now a real OCaml binary-tree map, and supports arbitrary mal value
types for both keys and values. Metadata support is provided in the data
objects, but not yet in the printer, reader, or core library.
Diffstat (limited to 'ocaml/types.ml')
| -rw-r--r-- | ocaml/types.ml | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/ocaml/types.ml b/ocaml/types.ml index 6440580..287cc88 100644 --- a/ocaml/types.ml +++ b/ocaml/types.ml @@ -1,15 +1,46 @@ -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 - | Nil - | Bool of bool - | String of string - | Fn of (mal_type list -> mal_type) +module rec Types + : sig + type 'a with_meta = { value : 'a; meta : t } + and t = + | List of t list with_meta + | Vector of t list with_meta + | Map of t MalMap.t with_meta + | Int of int + | Symbol of string with_meta + | Keyword of string + | Nil + | Bool of bool + | String of string + | Fn of (t list -> t) + end = Types + +and MalValue + : sig + type t = Types.t + val compare : t -> t -> int + end + = struct + type t = Types.t + let compare = Pervasives.compare + end + +and MalMap + : Map.S with type key = MalValue.t + = Map.Make(MalValue) let to_bool x = match x with - | Nil | Bool false -> false + | Types.Nil | Types.Bool false -> false | _ -> true + +type mal_type = MalValue.t + +let list x = Types.List { Types.value = x; meta = Types.Nil } +let map x = Types.Map { Types.value = x; meta = Types.Nil } +let vector x = Types.Vector { Types.value = x; meta = Types.Nil } +let symbol x = Types.Symbol { Types.value = x; meta = Types.Nil } + +let rec list_into_map target source = + match source with + | k :: v :: more -> list_into_map (MalMap.add k v target) more + | [] -> map target + | _ :: [] -> raise (Invalid_argument "Literal maps must contain an even number of forms") |
