Commit | Line | Data |
---|---|---|
a878f3bb C |
1 | module 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 | ||
18 | and 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 | ||
28 | and MalMap | |
29 | : Map.S with type key = MalValue.t | |
30 | = Map.Make(MalValue) | |
de04357c | 31 | |
ecd3b6d8 C |
32 | exception MalExn of Types.t |
33 | ||
de04357c | 34 | let to_bool x = match x with |
a878f3bb | 35 | | Types.Nil | Types.Bool false -> false |
de04357c | 36 | | _ -> true |
a878f3bb C |
37 | |
38 | type mal_type = MalValue.t | |
39 | ||
2b8e0ea4 C |
40 | let list x = Types.List { Types.value = x; meta = Types.Nil } |
41 | let map x = Types.Map { Types.value = x; meta = Types.Nil } | |
42 | let vector x = Types.Vector { Types.value = x; meta = Types.Nil } | |
43 | let symbol x = Types.Symbol { Types.value = x; meta = Types.Nil } | |
44 | let fn f = Types.Fn { Types.value = f; meta = Types.Nil } | |
a878f3bb C |
45 | |
46 | let 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") |