forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblang.ml
45 lines (40 loc) · 1.04 KB
/
blang.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
44
45
open! Stdune
module Op = struct
type t =
| Eq
| Gt
| Gte
| Lte
| Lt
| Neq
let eval t (x : Ordering.t) =
match t, x with
| (Eq | Gte | Lte) , Eq
| (Neq | Lt | Lte) , Lt
| (Neq | Gt | Gte) , Gt -> true
| _, _ -> false
end
type t =
| Const of bool
| Expr of String_with_vars.t
| And of t list
| Or of t list
| Compare of Op.t * String_with_vars.t * String_with_vars.t
let true_ = Const true
let rec eval t ~dir ~f =
match t with
| Const x -> x
| Expr sw ->
begin match String_with_vars.expand sw ~mode:Single ~dir ~f with
| String "true" -> true
| String "false" -> false
| _ ->
let loc = String_with_vars.loc sw in
Errors.fail loc "This value must be either true or false"
end
| And xs -> List.for_all ~f:(eval ~f ~dir) xs
| Or xs -> List.exists ~f:(eval ~f ~dir) xs
| Compare (op, x, y) ->
let x = String_with_vars.expand x ~mode:Many ~dir ~f
and y = String_with_vars.expand y ~mode:Many ~dir ~f in
Op.eval op (Value.L.compare_vals ~dir x y)