Add -> support in C

This commit is contained in:
Benjamin Loison 2022-12-10 21:17:32 +01:00
parent 38f58f7558
commit da823ac3c8
2 changed files with 18 additions and 12 deletions

View File

@ -14,7 +14,7 @@ type var_list_delim =
let rec pp_varlist var_list_delim fmt : t_varlist -> unit = function let rec pp_varlist var_list_delim fmt : t_varlist -> unit = function
| ([], []) -> () | ([], []) -> ()
| ([TInt] , IVar h :: []) -> let s = "" in Format.fprintf fmt ( | ([TInt] , IVar h :: []) -> Format.fprintf fmt (
match var_list_delim with match var_list_delim with
| Base -> "%s" | Base -> "%s"
| Arg -> "int %s" | Arg -> "int %s"
@ -62,7 +62,7 @@ let rec pp_retvarlist fmt : t_varlist -> unit = function
Format.fprintf fmt "bool, %a" pp_retvarlist (tl, h' :: l) Format.fprintf fmt "bool, %a" pp_retvarlist (tl, h' :: l)
| _ -> raise (MyTypeError "This exception should not have beed be raised.") | _ -> raise (MyTypeError "This exception should not have beed be raised.")
let pp_expression = let pp_expression node_name =
let rec pp_expression_aux prefix fmt expression = let rec pp_expression_aux prefix fmt expression =
let rec pp_expression_list prefix fmt exprs = let rec pp_expression_list prefix fmt exprs =
match exprs with match exprs with
@ -113,13 +113,17 @@ let pp_expression =
Format.fprintf fmt "pre %s%a" prefix Format.fprintf fmt "pre %s%a" prefix
(pp_expression_aux prefix) arg (pp_expression_aux prefix) arg
end end
| EBinOp (_, BOp_arrow, arg, arg') ->
Format.fprintf fmt "%sinit_%s ? %a : %a" prefix
node_name
(pp_expression_aux prefix) arg
(pp_expression_aux prefix) 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 -> " % "
(* TODO: -> *) | BOp_and -> " && " | BOp_or -> " || " | _ -> "" (* `ocamlc` doesn't detect that `BOp_arrow` can't match here *) in
| BOp_and -> " && " | BOp_or -> " || " | BOp_arrow -> " -> " in
Format.fprintf fmt "%s%a%s%a" prefix Format.fprintf fmt "%s%a%s%a" prefix
(pp_expression_aux prefix) arg (pp_expression_aux prefix) arg
s s
@ -156,24 +160,27 @@ let pp_expression =
in in
pp_expression_aux "" pp_expression_aux ""
let rec pp_equations fmt: t_eqlist -> unit = function let rec pp_equations node_name fmt: t_eqlist -> unit = function
| [] -> () | [] -> ()
| (patt, expr) :: eqs -> | (patt, expr) :: eqs ->
Format.fprintf fmt "\t%a = %a;\n%a" Format.fprintf fmt "\t%a = %a;\n%a"
(pp_varlist Base) patt (pp_varlist Base) patt
pp_expression expr (pp_expression node_name) expr
pp_equations eqs (pp_equations node_name) eqs
(* TODO: manage general outputs *) (* TODO: manage general outputs *)
let pp_node fmt node = let pp_node fmt node =
Format.fprintf fmt "%a %s(%a)\n{\n\t%a\n\n\t%a\n\n%a\n\treturn %a;\n}\n" (* undefined behavior if the initial code uses a variable with name `init_{NODE_NAME}` *)
Format.fprintf fmt "bool init_%s = true;\n%a %s(%a)\n{\n\t%a\n\n\t%a\n\n%a\n\tinit_%s = false;\n\t\n\treturn %a;\n}\n"
node.n_name
pp_retvarlist (node.n_outputs) pp_retvarlist (node.n_outputs)
node.n_name node.n_name
(* could avoid newlines if they aren't used to seperate statements *) (* could avoid newlines if they aren't used to seperate statements *)
(pp_varlist Arg) node.n_inputs (pp_varlist Arg) node.n_inputs
(pp_varlist Dec) node.n_local_vars (pp_varlist Dec) node.n_local_vars
(pp_varlist Dec) node.n_outputs (pp_varlist Dec) node.n_outputs
pp_equations node.n_equations (pp_equations node.n_name) node.n_equations
node.n_name
(pp_varlist Base) node.n_outputs (pp_varlist Base) node.n_outputs
let rec pp_nodes fmt nodes = let rec pp_nodes fmt nodes =
@ -184,7 +191,7 @@ let rec pp_nodes fmt nodes =
let ast_to_c fmt prog = let ast_to_c fmt prog =
Format.fprintf fmt Format.fprintf fmt
(* could verify that uses a boolean in the ast before including `<stdbool.h>` *) (* could verify that uses, possibly indirectly (cf `->` implementation), a boolean in the ast before including `<stdbool.h>` *)
"#include <stdbool.h>\n\n%a" "#include <stdbool.h>\n\n%a"
pp_nodes prog pp_nodes prog

View File

@ -55,8 +55,7 @@ let pp_expression =
end end
| EConst (_, c) -> | EConst (_, c) ->
begin match c with begin match c with
| CBool true -> Format.fprintf fmt "\t\t\t%s<true : bool>\n" prefix | CBool b -> Format.fprintf fmt "\t\t\t%s<%s : bool>\n" prefix (Bool.to_string b)
| CBool false -> Format.fprintf fmt "\t\t\t%s<false : bool>\n" prefix
| CInt i -> Format.fprintf fmt "\t\t\t%s<%5d: int>\n" prefix i | CInt i -> Format.fprintf fmt "\t\t\t%s<%5d: int>\n" prefix i
| CReal r -> Format.fprintf fmt "\t\t\t%s<%5f: float>\n" prefix r | CReal r -> Format.fprintf fmt "\t\t\t%s<%5f: float>\n" prefix r
end end