type character = rule list and rule = Rule of state_number list * statement and statement = If of condition * statement * elseif list * statement | Decision of new_state * string | Case of string * arm list * statement and elseif = Elseif of condition * statement and arm = Arm of int list * statement and condition = Equals of string * int | And of condition list | Or of condition list and new_state = int option and state_number = int open Ast let rec condition_from_ast = function Cequals(s, n) -> Equals(s, n) | Cand cl -> And(List.map condition_from_ast cl) | Cor cl -> Or(List.map condition_from_ast cl) let rec statement_from_ast = function Eif(cond, ifso, ifnot) -> If(condition_from_ast cond, statement_from_ast ifso, [], statement_from_ast ifnot) | Edecision {next = NSfixed n; utterance = u} -> Decision(Some n, u) | Edecision {next = _; utterance = u} -> Decision(None, u) | Ecase(v, arms, default) -> Case(v, arms_from_ast arms, statement_from_ast default) and arms_from_ast arms = List.map (fun (lbls, stmt) -> Arm(lbls, statement_from_ast stmt)) arms let from_ast ch = List.map (fun (lbls, stmt) -> Rule(lbls, statement_from_ast stmt)) ch let rec condition_to_ast = function Equals(s, n) -> Cequals(s, n) | And cl -> Cand(List.map condition_to_ast cl) | Or cl -> Cor(List.map condition_to_ast cl) let rec statement_to_ast = function If(cond, ifso, elseif, default) -> Eif(condition_to_ast cond, statement_to_ast ifso, iftail_to_ast elseif default) | Decision(Some n, u) -> Edecision {next = NSfixed n; utterance = u} | Decision(None, u) -> Edecision {next = NSdefault; utterance = u} | Case(v, arms, default) -> Ecase(v, arms_to_ast arms, statement_to_ast default) and iftail_to_ast elseif default = match elseif with [] -> statement_to_ast default | Elseif(cond, stmt) :: rem -> Eif(condition_to_ast cond, statement_to_ast stmt, iftail_to_ast rem default) and arms_to_ast arms = List.map (fun (Arm(lbls, stmt)) -> (lbls, statement_to_ast stmt)) arms let to_ast ch = List.map (fun (Rule(lbls, stmt)) -> (lbls, statement_to_ast stmt)) ch