From eaafc5db050b0ad7fc3ad20c740e176c4c32ee57 Mon Sep 17 00:00:00 2001 From: Adrien Guatto Date: Thu, 1 Jul 2010 18:56:18 +0200 Subject: [PATCH] C backend: better array printing --- compiler/minils/sequential/cmain.ml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/compiler/minils/sequential/cmain.ml b/compiler/minils/sequential/cmain.ml index 97a5ac9..c2d163b 100644 --- a/compiler/minils/sequential/cmain.ml +++ b/compiler/minils/sequential/cmain.ml @@ -90,6 +90,8 @@ let main_def_of_class_def cd = | Tint | Tfloat | Tbool -> None | Tid (Name sid | Modname { id = sid; }) -> Some sid in + let cprint_string s = Csexpr (Cfun_call ("printf", [Cconst (Cstrlit s)])) in + (** Generates scanf statements. *) let rec read_lhs_of_ty lhs ty = match ty with | Tarray (ty, n) -> @@ -132,7 +134,8 @@ let main_def_of_class_def cd = let iter_var = Ident.name (Ident.fresh "i") in let lhs = Carray (lhs, Clhs (Cvar iter_var)) in let (reads, bufs) = write_lhs_of_ty lhs ty in - (Cfor (iter_var, 0, n, [reads]), bufs) + ([cprint_string "[ "; Cfor (iter_var, 0, n, reads); cprint_string "]"], + bufs) | _ -> let varn = Ident.name (Ident.fresh "buf") in let format_s = format_for_type ty in @@ -142,9 +145,9 @@ let main_def_of_class_def cd = | Some sid -> [Cfun_call ("string_of_" ^ sid, [Clhs lhs; Clhs (Cvar varn)])] in - (Csexpr (Cfun_call ("printf", - Cconst (Cstrlit ("=> " ^format_s ^ "\\t")) - :: ep)), + ([Csexpr (Cfun_call ("printf", + Cconst (Cstrlit (format_s ^ " ")) + :: ep))], match nbuf_opt with | None -> [] | Some id -> [(varn, Cty_arr (20, Cty_char))]) in @@ -156,8 +159,11 @@ let main_def_of_class_def cd = let (printf_calls, printf_decls) = let write_lhs_of_ty_for_vd vd = - write_lhs_of_ty (Cfield (Cvar "res", name vd.v_ident)) vd.v_type in + let (stm, vars) = + write_lhs_of_ty (Cfield (Cvar "res", name vd.v_ident)) vd.v_type in + (cprint_string "=> " :: stm, vars) in split (map write_lhs_of_ty_for_vd cd.step.out) in + let printf_calls = List.concat printf_calls in let cinp = cvarlist_of_ovarlist cd.step.inp in let cout = ["res", (Cty_id (cd.cl_id ^ "_out"))] in