From 233b385608e09696b19495e39b54d35897899479 Mon Sep 17 00:00:00 2001 From: dsac Date: Sat, 17 Dec 2022 23:46:39 +0100 Subject: [PATCH] missing 'state->' added + print +,-,*,... --- src/ast_to_c.ml | 4 ++-- src/cprint.ml | 23 +++++++++++++++++------ src/test.node | 4 +++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/ast_to_c.ml b/src/ast_to_c.ml index be8b01b..71be88e 100644 --- a/src/ast_to_c.ml +++ b/src/ast_to_c.ml @@ -214,11 +214,11 @@ let cp_prevars fmt (node, h) = match Hashtbl.find_opt node_st.nt_map (v, false) with | Some (src_array, src_idx) -> let (dst_array, dst_idx) = Hashtbl.find node_st.nt_map (v, true) in - Format.fprintf fmt "\t%s[%d] = %s[%d];\n" + Format.fprintf fmt "\tstate->%s[%d] = state->%s[%d];\n" dst_array dst_idx src_array src_idx | None -> let (dst_array, dst_idx) = Hashtbl.find node_st.nt_map (v, true) in - Format.fprintf fmt "\t%s[%d] = %s;\n" + Format.fprintf fmt "\tstate->%s[%d] = %s;\n" dst_array dst_idx v ) (List.map Utils.name_of_var l) diff --git a/src/cprint.ml b/src/cprint.ml index 40f02e7..afd619c 100644 --- a/src/cprint.ml +++ b/src/cprint.ml @@ -80,9 +80,19 @@ let rec cp_prototypes fmt ((nodes, h): i_nodelist * node_states) = let rec cp_value fmt (value, (hloc: (ident * bool, string * int) Hashtbl.t)) = + let string_of_binop = function + | BOp_add -> "+" + | BOp_sub -> "-" + | BOp_mul -> "*" + | BOp_div -> "/" + | BOp_mod -> "%" + | BOp_and -> "&&" + | BOp_or -> "||" + | BOp_arrow -> failwith "[cprint.ml] string_of_binop undefined on (->)" + in match value with | CVariable (CVInput s) -> Format.fprintf fmt "%s" s - | CVariable (CVStored (arr, idx)) -> Format.fprintf fmt "%s[%d]" arr idx + | CVariable (CVStored (arr, idx)) -> Format.fprintf fmt "state->%s[%d]" arr idx | CConst (CInt i) -> Format.fprintf fmt "%d" i | CConst (CBool true) -> Format.fprintf fmt "true" | CConst (CBool false) -> Format.fprintf fmt "false" @@ -99,10 +109,11 @@ let rec cp_value fmt (value, (hloc: (ident * bool, string * int) Hashtbl.t)) = end | CVInput n -> n) in let (arr, idx) = Hashtbl.find hloc (varname, true) in - Format.fprintf fmt "%s[%d]" arr idx - | CBinOp (BOp_add, v, v') -> - Format.fprintf fmt "(%a) + (%a)" - cp_value (v, hloc) cp_value (v', hloc) + Format.fprintf fmt "state->%s[%d]" arr idx + | CBinOp (BOp_arrow, v, v') -> failwith "[cprint.ml] (->) TODO!" + | CBinOp (op, v, v') -> + Format.fprintf fmt "(%a) %s (%a)" + cp_value (v, hloc) (string_of_binop op) cp_value (v', hloc) (**| CComp of compop * c_value * c_value*) | _ -> failwith "[cprint.ml] TODO!" @@ -115,7 +126,7 @@ let cp_expression fmt (expr, hloc) = match expr with | CAssign (CVStored (arr, idx), value) -> begin - Format.fprintf fmt "%s%s[%d] = %a;\n" + Format.fprintf fmt "%sstate->%s[%d] = %a;\n" prefix arr idx cp_value (value, hloc) end | CAssign (CVInput _, _) -> failwith "should not happend." diff --git a/src/test.node b/src/test.node index 4e3d799..324533a 100644 --- a/src/test.node +++ b/src/test.node @@ -1,6 +1,8 @@ node n (i: int) returns (o: int); -var v, t: int; +var v, t: int; a, b: bool; let + a = true; + b = a and not (pre a); o = 1; v = pre o; t = o + pre i;