Remove useless prefix from pp_expression_aux and pp_expression_list functions in src/ast_to_c.ml

This commit is contained in:
Benjamin Loison 2022-12-11 20:07:28 +01:00
parent cbddd63927
commit 014110791d

View File

@ -83,67 +83,65 @@ let rec pp_asnprevarlist node_name fmt : t_varlist -> unit = function
| _ -> raise (MyTypeError "This exception should not have beed be raised.") | _ -> raise (MyTypeError "This exception should not have beed be raised.")
let pp_expression node_name = let pp_expression node_name =
let rec pp_expression_aux prefix fmt expression = let rec pp_expression_aux fmt expression =
let rec pp_expression_list prefix fmt exprs = let rec pp_expression_list fmt exprs =
match exprs with match exprs with
| ETuple([], []) -> () | ETuple([], []) -> ()
| ETuple (_ :: tt, expr :: exprs) -> | ETuple (_ :: tt, expr :: exprs) ->
Format.fprintf fmt "%a%s%a" Format.fprintf fmt "%a%s%a"
(pp_expression_aux prefix) expr pp_expression_aux expr
(if (List.length tt > 0) then ", " else "") (if (List.length tt > 0) then ", " else "")
(pp_expression_list prefix) (ETuple (tt, exprs)) pp_expression_list (ETuple (tt, exprs))
| _ -> raise (MyTypeError "This exception should not have been raised.") | _ -> raise (MyTypeError "This exception should not have been raised.")
in in
match expression with match expression with
| EWhen (_, e1, e2) -> | EWhen (_, e1, e2) ->
begin begin
(* as don't use a variable assigned when the condition holds, can define it even if the condition doesn't hold *) (* as don't use a variable assigned when the condition holds, can define it even if the condition doesn't hold *)
Format.fprintf fmt "%s%a" Format.fprintf fmt "%a"
prefix pp_expression_aux e1
(pp_expression_aux prefix) e1
end end
(* TODO: *) (* TODO: *)
| EReset (_, e1, e2) -> | EReset (_, e1, e2) ->
begin begin
Format.fprintf fmt "\t\t\t%sRESET\n%a\t\t\tRESET\n%a" Format.fprintf fmt "\t\t\tRESET\n%a\t\t\tRESET\n%a"
prefix pp_expression_aux e1
(pp_expression_aux prefix) e1 pp_expression_aux e2
(pp_expression_aux prefix) e2
end end
| EConst (_, c) -> | EConst (_, c) ->
begin match c with begin match c with
| CBool b -> Format.fprintf fmt "%s%s" prefix (Bool.to_string b) | CBool b -> Format.fprintf fmt "%s" (Bool.to_string b)
| CInt i -> Format.fprintf fmt "%s%i" prefix i | CInt i -> Format.fprintf fmt "%i" i
| CReal r -> Format.fprintf fmt "%s%f" prefix r | CReal r -> Format.fprintf fmt "%f" r
end end
| EVar (_, IVar v) | EVar (_, BVar v) | EVar (_, RVar v) -> Format.fprintf fmt "%s%s" prefix v | EVar (_, IVar v) | EVar (_, BVar v) | EVar (_, RVar v) -> Format.fprintf fmt "%s" v
| EMonOp (_, mop, arg) -> | EMonOp (_, mop, arg) ->
begin match mop with begin match mop with
| MOp_not -> | MOp_not ->
Format.fprintf fmt "!%s%a" prefix Format.fprintf fmt "!%a"
(pp_expression_aux prefix) arg pp_expression_aux arg
| MOp_minus -> | MOp_minus ->
Format.fprintf fmt "-%s%a" prefix Format.fprintf fmt "-%a"
(pp_expression_aux prefix) arg pp_expression_aux arg
| MOp_pre -> | MOp_pre ->
Format.fprintf fmt "pre_%s_%s%a" node_name prefix Format.fprintf fmt "pre_%s_%a" node_name
(pp_expression_aux prefix) arg pp_expression_aux arg
end end
| EBinOp (_, BOp_arrow, arg, arg') -> | EBinOp (_, BOp_arrow, arg, arg') ->
Format.fprintf fmt "%sinit_%s ? %a : %a" prefix Format.fprintf fmt "init_%s ? %a : %a"
node_name node_name
(pp_expression_aux prefix) arg pp_expression_aux arg
(pp_expression_aux prefix) arg' pp_expression_aux arg'
| EBinOp (_, bop, arg, arg') -> | EBinOp (_, bop, arg, arg') ->
begin begin
let s = match bop with let s = match bop with
| BOp_add -> " + " | BOp_sub -> " - " | BOp_add -> " + " | BOp_sub -> " - "
| BOp_mul -> " * " | BOp_div -> " / " | BOp_mod -> " % " | BOp_mul -> " * " | BOp_div -> " / " | BOp_mod -> " % "
| BOp_and -> " && " | BOp_or -> " || " | _ -> "" (* `ocamlc` doesn't detect that `BOp_arrow` can't match here *) in | BOp_and -> " && " | BOp_or -> " || " | _ -> "" (* `ocamlc` doesn't detect that `BOp_arrow` can't match here *) in
Format.fprintf fmt "%s%a%s%a" prefix Format.fprintf fmt "%a%s%a"
(pp_expression_aux prefix) arg pp_expression_aux arg
s s
(pp_expression_aux prefix) arg' pp_expression_aux arg'
end end
| EComp (_, cop, arg, arg') -> | EComp (_, cop, arg, arg') ->
begin begin
@ -152,29 +150,28 @@ let pp_expression node_name =
| COp_neq -> " != " | COp_neq -> " != "
| COp_le -> " <= " | COp_lt -> " < " | COp_le -> " <= " | COp_lt -> " < "
| COp_ge -> " >= " | COp_gt -> " > " in | COp_ge -> " >= " | COp_gt -> " > " in
Format.fprintf fmt "%s%a%s%a" prefix Format.fprintf fmt "%a%s%a"
(pp_expression_aux prefix) arg pp_expression_aux arg
s s
(pp_expression_aux prefix) arg' pp_expression_aux arg'
end end
| ETriOp (_, top, arg, arg', arg'') -> | ETriOp (_, top, arg, arg', arg'') ->
begin match top with begin match top with
| TOp_if | TOp_merge -> | TOp_if | TOp_merge ->
Format.fprintf fmt "%s%a ? %a : %a" Format.fprintf fmt "%a ? %a : %a"
prefix pp_expression_aux arg
(pp_expression_aux prefix) arg pp_expression_aux arg'
(pp_expression_aux prefix) arg' pp_expression_aux arg''
(pp_expression_aux prefix) arg''
end end
| EApp (_, f, args) -> | EApp (_, f, args) ->
Format.fprintf fmt "%s%s(%a)" Format.fprintf fmt "%s(%a)"
prefix f.n_name f.n_name
(pp_expression_list prefix) args pp_expression_list args
| ETuple _ -> | ETuple _ ->
Format.fprintf fmt "%s%a" prefix Format.fprintf fmt "%a"
(pp_expression_list prefix) expression; pp_expression_list expression;
in in
pp_expression_aux "" pp_expression_aux
let rec pp_equations node_name fmt: t_eqlist -> unit = function let rec pp_equations node_name fmt: t_eqlist -> unit = function
| [] -> () | [] -> ()