[ast2C] printer: ok.

This commit is contained in:
dsac
2022-12-18 13:38:40 +01:00
parent ce686f6c9a
commit 1491e279f7
4 changed files with 58 additions and 24 deletions

View File

@@ -151,12 +151,13 @@ let prefix_ = ref "\t"
(** The following function prints one transformed equation of the program into a
* set of instruction ending in assignments. *)
let rec cp_expression fmt (expr, hloc) =
let rec cp_block fmt (b, hloc) =
match b with
| [] -> ()
| e :: b ->
Format.fprintf fmt "%a%a" cp_expression (e, hloc) cp_block (b, hloc)
and cp_expression fmt (expr, hloc) =
let prefix = !prefix_ in
let rec cp_block fmt = function
| [] -> ()
| e :: b -> Format.fprintf fmt "%a%a" cp_expression (e, hloc) cp_block b
in
match expr with
| CAssign (CVStored (arr, idx), value) ->
begin
@@ -195,15 +196,18 @@ let rec cp_expression fmt (expr, hloc) =
Format.fprintf fmt "%sif (%a) {\n%a%s}\n"
p
cp_value (v, hloc)
cp_block b1
cp_block (b1, hloc)
p;
prefix_ := p
| CIf (v, b1, b2) ->
let p = prefix in
prefix_ := prefix^"\t";
Format.fprintf fmt "%sif (%a) {\n%a%s} else {\n%a%s}\n"
prefix
p
cp_value (v, hloc)
cp_block b1
prefix
cp_block b2
prefix
cp_block (b1, hloc)
p
cp_block (b2, hloc)
p;
prefix_ := p