fsharp: step 4: Added list and comparison functions.
[jackhill/mal.git] / ocaml / types.ml
CommitLineData
a878f3bb
C
1module rec Types
2 : sig
2b8e0ea4 3 type 'a with_meta = { value : 'a; meta : t }
a878f3bb
C
4 and t =
5 | List of t list with_meta
6 | Vector of t list with_meta
7 | Map of t MalMap.t with_meta
8 | Int of int
9 | Symbol of string with_meta
10 | Keyword of string
11 | Nil
12 | Bool of bool
13 | String of string
ecd3b6d8
C
14 | Fn of (t list -> t) with_meta
15 | Atom of t ref
a878f3bb
C
16 end = Types
17
18and MalValue
19 : sig
20 type t = Types.t
21 val compare : t -> t -> int
22 end
23 = struct
24 type t = Types.t
25 let compare = Pervasives.compare
26 end
27
28and MalMap
29 : Map.S with type key = MalValue.t
30 = Map.Make(MalValue)
de04357c 31
ecd3b6d8
C
32exception MalExn of Types.t
33
de04357c 34let to_bool x = match x with
a878f3bb 35 | Types.Nil | Types.Bool false -> false
de04357c 36 | _ -> true
a878f3bb
C
37
38type mal_type = MalValue.t
39
2b8e0ea4
C
40let list x = Types.List { Types.value = x; meta = Types.Nil }
41let map x = Types.Map { Types.value = x; meta = Types.Nil }
42let vector x = Types.Vector { Types.value = x; meta = Types.Nil }
43let symbol x = Types.Symbol { Types.value = x; meta = Types.Nil }
44let fn f = Types.Fn { Types.value = f; meta = Types.Nil }
a878f3bb
C
45
46let rec list_into_map target source =
47 match source with
48 | k :: v :: more -> list_into_map (MalMap.add k v target) more
49 | [] -> map target
50 | _ :: [] -> raise (Invalid_argument "Literal maps must contain an even number of forms")