-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathactions.ml
79 lines (60 loc) · 2.58 KB
/
actions.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Sebastien Hinderer, projet Gallium, INRIA Paris *)
(* *)
(* Copyright 2016 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Definition of actions, basic blocks for tests *)
type code = out_channel -> Environments.t -> Result.t * Environments.t
type t = {
name : string;
action_description : string;
body : code;
mutable hook : code option
}
let name a = a.name
let description a = a.action_description
let action_name = Variables.make ("action_name", "Name of the current action")
let make ~name ~description body =
{ name; body; action_description = description; hook = None }
let update action code = { action with body = code }
let compare a1 a2 = String.compare a1.name a2.name
let (actions : (string, t) Hashtbl.t) = Hashtbl.create 10
let register action =
Hashtbl.add actions action.name action
let get_registered_actions () =
let f _name action acc = action::acc in
let unsorted_actions = Hashtbl.fold f actions [] in
List.sort compare unsorted_actions
let lookup name =
try Some (Hashtbl.find actions name)
with Not_found -> None
let set_hook name hook =
let action = (Hashtbl.find actions name) in
action.hook <- Some hook
let clear_hook name =
let action = (Hashtbl.find actions name) in
action.hook <- None
let clear_all_hooks () =
let f _name action = action.hook <- None in
Hashtbl.iter f actions
let run log env action =
let code = match action.hook with
| None -> action.body
| Some code -> code in
let env = Environments.add action_name action.name env in
code log env
module ActionSet = Set.Make
(struct
type nonrec t = t
let compare = compare
end)
let _ = Variables.register_variable action_name