diff options
author | Son Ho | 2021-11-29 18:10:45 +0100 |
---|---|---|
committer | Son Ho | 2021-11-29 18:10:45 +0100 |
commit | 1991b6b9d1d1a5e00ac8b54a66458aa696e7baab (patch) | |
tree | b6ee5219af976db550e6856279dae65c2b63ed51 /src | |
parent | a16b9148a063fac9b0d9268d24926bcc374c5cb2 (diff) |
Implement expression_to_string
Diffstat (limited to '')
-rw-r--r-- | src/Print.ml | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Print.ml b/src/Print.ml index 24e038f9..441e76e6 100644 --- a/src/Print.ml +++ b/src/Print.ml @@ -668,4 +668,52 @@ module CfimAst = struct | A.Break i -> "break " ^ string_of_int i | A.Continue i -> "continue " ^ string_of_int i | A.Nop -> "nop" + + let rec expression_to_string (fmt : ast_formatter) (indent : string) + (indent_incr : string) (e : A.expression) : string = + match e with + | A.Statement st -> indent ^ statement_to_string fmt st ^ ";" + | A.Sequence (e1, e2) -> + expression_to_string fmt indent indent_incr e1 + ^ "\n" + ^ expression_to_string fmt indent indent_incr e2 + | A.Switch (op, tgts) -> ( + let op = operand_to_string fmt op in + match tgts with + | A.If (true_e, false_e) -> + let inner_indent = indent ^ indent_incr in + let inner_to_string = + expression_to_string fmt inner_indent indent_incr + in + let true_e = inner_to_string true_e in + let false_e = inner_to_string false_e in + indent ^ "if (" ^ op ^ ") {\n" ^ true_e ^ "\n" ^ indent ^ "}\n" + ^ indent ^ "else {\n" ^ false_e ^ "\n" ^ indent ^ "}" + | A.SwitchInt (_ty, branches, otherwise) -> + let indent1 = indent ^ indent_incr in + let indent2 = indent1 ^ indent_incr in + let inner_to_string1 = + expression_to_string fmt indent1 indent_incr + in + let inner_to_string2 = + expression_to_string fmt indent2 indent_incr + in + let branches = + List.map + (fun (sv, be) -> + indent1 + ^ PV.scalar_value_to_string sv + ^ " => {\n" ^ inner_to_string2 be ^ "\n" ^ indent1 ^ "}") + branches + in + let branches = String.concat "\n" branches in + let branches = + branches ^ "\n" ^ indent1 ^ "_ => {\n" + ^ inner_to_string2 otherwise ^ "\n" ^ indent1 ^ "}" + in + indent ^ "switch (" ^ op ^ ") {\n" ^ branches ^ "\n" ^ indent ^ "}") + | A.Loop loop_e -> + indent ^ "loop {\n" + ^ expression_to_string fmt (indent ^ indent_incr) indent_incr loop_e + ^ "\n" ^ indent ^ "}" end |