Skip to content

Commit

Permalink
Cleanup of counters_compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
gikiam committed Nov 10, 2023
1 parent 2bfc3ce commit 959ca43
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 201 deletions.
40 changes: 20 additions & 20 deletions core/grammar/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ type counter = {
}

type site = Port of port | Counter of counter
type agent_mod = Erase | Create
type agent_mod = NoMod | Erase | Create

type agent =
| Present of string Locality.annoted * site list * agent_mod option
| Present of string Locality.annoted * site list * agent_mod
| Absent of Locality.t

type mixture = agent list list
Expand Down Expand Up @@ -447,23 +447,24 @@ let site_to_json filenames = function
let print_agent_mod f = function
| Create -> Format.pp_print_string f "+"
| Erase -> Format.pp_print_string f "-"
| NoMod -> Format.pp_print_string f ""

let print_ast_agent f = function
| Absent _ -> Format.pp_print_string f "."
| Present ((agent_name, _), l, m) ->
Format.fprintf f "%s(%a)%a" agent_name
(Pp.list (fun f -> Format.fprintf f " ") print_ast_site)
l
(Pp.option ~with_space:false print_agent_mod)
m
l print_agent_mod m

let agent_mod_to_yojson = function
| Create -> `String "created"
| Erase -> `String "erase"
| NoMod -> `String "no_mod"

let agent_mod_of_yojson = function
| `String "created" -> Create
| `String "erase" -> Erase
| `String "no_mod" -> NoMod
| x ->
raise (Yojson.Basic.Util.Type_error ("Incorrect agent modification", x))

Expand All @@ -474,7 +475,7 @@ let agent_to_json filenames = function
[
"name", Locality.yojson_of_annoted ~filenames JsonUtil.of_string na;
"sig", JsonUtil.of_list (site_to_json filenames) l;
"mod", (JsonUtil.of_option agent_mod_to_yojson) m;
"mod", agent_mod_to_yojson m;
]

let agent_of_json filenames = function
Expand All @@ -490,28 +491,28 @@ let agent_of_json filenames = function
(JsonUtil.to_string ?error_msg:None)
n,
JsonUtil.to_list (site_of_json filenames) s,
(JsonUtil.to_option agent_mod_of_yojson) m )
agent_mod_of_yojson m )
| `Assoc [ ("name", n); ("mod", m) ] | `Assoc [ ("mod", m); ("name", n) ] ->
Present
( Locality.annoted_of_yojson ~filenames
(JsonUtil.to_string ?error_msg:None)
n,
[],
(JsonUtil.to_option agent_mod_of_yojson) m )
agent_mod_of_yojson m )
| `Assoc [ ("name", n); ("sig", s) ] | `Assoc [ ("sig", s); ("name", n) ] ->
Present
( Locality.annoted_of_yojson ~filenames
(JsonUtil.to_string ?error_msg:None)
n,
JsonUtil.to_list (site_of_json filenames) s,
None )
NoMod )
| `Assoc [ ("name", n) ] ->
Present
( Locality.annoted_of_yojson ~filenames
(JsonUtil.to_string ?error_msg:None)
n,
[],
None )
NoMod )
| x -> raise (Yojson.Basic.Util.Type_error ("Not an AST agent", x))

let print_ast_mix =
Expand All @@ -521,13 +522,13 @@ let to_erased_mixture =
List.map
(List.map (function
| Absent pos -> Absent pos
| Present (n, s, _) -> Present (n, s, Some Erase)))
| Present (n, s, _) -> Present (n, s, Erase)))

let to_created_mixture =
List.map
(List.map (function
| Absent pos -> Absent pos
| Present (n, s, _) -> Present (n, s, Some Create)))
| Present (n, s, _) -> Present (n, s, Create)))

let to_dummy_user_link = function
| [] | [ (LKappa.LNK_ANY, _) ] -> User_graph.WHATEVER
Expand Down Expand Up @@ -1224,10 +1225,10 @@ let merge_agents =
| Port p -> Port { p with port_link = [] }
| Counter _ as x -> x)
s,
None );
NoMod );
]
| Present ((na', _), s', _) :: t when String.compare na na' = 0 ->
Present (x, merge_sites s' s, None) :: t
Present (x, merge_sites s' s, NoMod) :: t
| ((Present _ | Absent _) as h) :: t -> h :: aux t
in
aux acc))
Expand Down Expand Up @@ -1291,10 +1292,9 @@ let split_mixture m =
| Absent _ -> pack
| Present (((_, pos) as na), intf, modif) ->
(match modif with
| Some Create ->
Absent pos :: lhs, Present (na, intf, None) :: rhs
| Some Erase -> Present (na, intf, None) :: lhs, Absent pos :: rhs
| None ->
| Create -> Absent pos :: lhs, Present (na, intf, NoMod) :: rhs
| Erase -> Present (na, intf, NoMod) :: lhs, Absent pos :: rhs
| NoMod ->
let intfl, intfr =
List.fold_left
(fun (l, r) -> function
Expand Down Expand Up @@ -1338,8 +1338,8 @@ let split_mixture m =
Counter { c with counter_test = None } :: r ))
([], []) intf
in
( Present (na, intfl, None) :: lhs,
Present (na, intfr, None) :: rhs )))
( Present (na, intfl, NoMod) :: lhs,
Present (na, intfr, NoMod) :: rhs )))
l ([], [])
in
ll :: lhs, rr :: rhs)
Expand Down
4 changes: 2 additions & 2 deletions core/grammar/ast.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type counter = {
}

type site = Port of port | Counter of counter
type agent_mod = Erase | Create
type agent_mod = NoMod | Erase | Create

type agent =
| Present of string Locality.annoted * site list * agent_mod option
| Present of string Locality.annoted * site list * agent_mod
| Absent of Locality.t

type mixture = agent list list
Expand Down
Loading

0 comments on commit 959ca43

Please sign in to comment.