-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* removed [@@deriving enum] because something dirty happen in theirs ppx world * removed tests * added unit tests * renaming * refactored, added mli file * refactored * rewritten
- Loading branch information
Showing
11 changed files
with
257 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ PKG bap | |
PKG bap-traces | ||
PKG uri | ||
PKG cmdliner | ||
PKG ppx_jane | ||
|
||
B _build | ||
B _build/lib | ||
B _build/plugin | ||
S lib/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
REC | ||
PKG ppx_deriving.std | ||
PKG ppx_jane | ||
PKG piqirun | ||
PKG bap | ||
|
||
B ../_build/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
open Core_kernel.Std | ||
|
||
module type Enumerated = sig | ||
type t | ||
val rank : t -> int | ||
val all : t list | ||
end | ||
|
||
module type Enumerable = sig | ||
type t | ||
val to_enum : t -> int | ||
val of_enum : int -> t option | ||
val max : int | ||
val min : int | ||
end | ||
|
||
let make_values rank xs = | ||
List.fold ~init:Int.Map.empty | ||
~f:(fun vals x -> Map.add vals ~key:(rank x) ~data:x) xs | ||
|
||
module type Substitution = sig | ||
include Enumerated | ||
val subs : (t * int) list | ||
end | ||
|
||
module Substitute(S : Substitution) : Enumerated with type t = S.t = struct | ||
include S | ||
|
||
let new_rank = | ||
let values = make_values rank all in | ||
let xs = Map.to_alist values in | ||
let subs = List.map ~f:(fun (x, ind) -> rank x, ind) subs in | ||
let values, _ = | ||
List.fold xs ~init:(Int.Map.empty,0) ~f:(fun (vals,ind') (ind, x) -> | ||
match List.find ~f:(fun (old_ind, new_ind) -> old_ind = ind) subs with | ||
| None -> | ||
Map.add vals ~key:ind ~data:(ind', x), ind' + 1 | ||
| Some (_, new_ind) -> | ||
Map.add vals ~key:ind ~data:(new_ind, x), new_ind + 1) in | ||
fun x -> fst @@ Map.find_exn values (rank x) | ||
|
||
let rank = new_rank | ||
end | ||
|
||
module Make(E : Enumerated) : Enumerable with type t := E.t = struct | ||
include E | ||
|
||
let values = make_values rank all | ||
let of_enum i = Map.find values i | ||
let to_enum x = rank x | ||
let max = Option.value_map ~default:0 ~f:fst (Map.max_elt values) | ||
let min = Option.value_map ~default:0 ~f:fst (Map.min_elt values) | ||
end | ||
|
||
module Make_substitute(S : Substitution) : Enumerable with type t := S.t = struct | ||
module E = Substitute(S) | ||
include Make(E) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
open Core_kernel.Std | ||
|
||
module type Enumerated = sig | ||
type t | ||
val rank : t -> int | ||
val all : t list | ||
end | ||
|
||
(** Replaces [@@deriving enum] interface from ppx_deriving, that | ||
treats variants with argument-less constructors as | ||
enumerations with an integer value assigned to every constructor. *) | ||
module type Enumerable = sig | ||
type t | ||
|
||
val to_enum : t -> int | ||
val of_enum : int -> t option | ||
val max : int | ||
val min : int | ||
end | ||
|
||
module type Substitution = sig | ||
include Enumerated | ||
|
||
(** [subs] is a list of substitions [ (t, ind); ... ], where | ||
an explicit index [ind] is set to a particular variant [t]. *) | ||
val subs : (t * int) list | ||
end | ||
|
||
module Make(A : Enumerated) : Enumerable with type t := A.t | ||
module Make_substitute(S : Substitution) : Enumerable with type t := S.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
REC | ||
S . | ||
B ../_build/test | ||
|
||
PKG oUnit | ||
PKG bap-frames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
open OUnit2 | ||
|
||
let suite () = | ||
"Bap-frames" >::: [ | ||
Test_enum.suite (); | ||
] | ||
|
||
let () = run_test_tt_main (suite ()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
open Core_kernel.Std | ||
open OUnit2 | ||
open Frame_enum | ||
|
||
type t = A | B | C | D | E [@@deriving enumerate, variants] | ||
|
||
module E = struct | ||
include Make(struct | ||
type nonrec t = t | ||
let rank = Variants.to_rank | ||
let all = all | ||
end) | ||
end | ||
|
||
module S = struct | ||
include Make_substitute(struct | ||
type nonrec t = t | ||
let subs = [B, 42; D, 56;] | ||
let rank = Variants.to_rank | ||
let all = all | ||
end) | ||
end | ||
|
||
let check x ctxt = assert_bool "test_enum failed" x | ||
|
||
let suite () = | ||
"Frame_enum" >::: [ | ||
"of_enum 0" >:: check (E.of_enum 0 = Some A); | ||
"of_enum 1" >:: check (E.of_enum 1 = Some B); | ||
"of_enum 2" >:: check (E.of_enum 2 = Some C); | ||
"of_enum 3" >:: check (E.of_enum 3 = Some D); | ||
"of_enum 4" >:: check (E.of_enum 4 = Some E); | ||
"of_enum 5" >:: check (E.of_enum 5 = None); | ||
"max" >:: check (E.max = 4); | ||
"min" >:: check (E.min = 0); | ||
"to_enum A" >:: check (E.to_enum A = 0); | ||
"to_enum B" >:: check (E.to_enum B = 1); | ||
"to_enum C" >:: check (E.to_enum C = 2); | ||
"to_enum D" >:: check (E.to_enum D = 3); | ||
"to_enum E" >:: check (E.to_enum E = 4); | ||
"substitute.of_enum 0" >:: check (S.of_enum 0 = Some A); | ||
"substitute.of_enum 42" >:: check (S.of_enum 42 = Some B); | ||
"substitute.of_enum 43" >:: check (S.of_enum 43 = Some C); | ||
"substitute.of_enum 56" >:: check (S.of_enum 56 = Some D); | ||
"substitute.of_enum 57" >:: check (S.of_enum 57 = Some E); | ||
"substitute.of_enum 3" >:: check (S.of_enum 1 = None); | ||
"substitute.max" >:: check (S.max = 57); | ||
"substitute.min" >:: check (S.min = 0); | ||
"substitute.to_enum A" >:: check (S.to_enum A = 0); | ||
"substitute.to_enum B" >:: check (S.to_enum B = 42); | ||
"substitute.to_enum C" >:: check (S.to_enum C = 43); | ||
"substitute.to_enum D" >:: check (S.to_enum D = 56); | ||
"substitute.to_enum E" >:: check (S.to_enum E = 57); | ||
] |