-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.ml
43 lines (39 loc) · 854 Bytes
/
value.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
type t =
| List of t list
| Symbol of string
| Number of int * int
| String of string
| Function of (t list -> t)
| Macro of (t list -> t)
| True
| False
| Nil
| Quote of t
let rec to_string v =
match v with
| List items ->
"'(" ^ (items |> List.map to_string |> String.concat " ") ^ ")"
| Symbol name -> "'" ^ name
| Number (n, d) -> Printf.sprintf "%d/%d" n d
| String s -> s
| Function _ -> "Function"
| Macro _ -> "Macro"
| False -> "False"
| True -> "True"
| Nil -> "Nil"
| Quote v -> "'" ^ (to_string v)
let of_boolean v =
if v then True else False
let rec to_boolean v =
match v with
| List [] -> false
| Number (0, _) -> false
| String "" -> false
| False -> false
| Nil -> false
| Quote v -> to_boolean v
| _ -> true
let to_number v =
match v with
| Number (a, b) -> Some (a / b)
| _ -> None